Replace bespoke CON/def module and clipmap filename array reallocing with a new class, GrowArray.

git-svn-id: https://svn.eduke32.com/eduke32@6673 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
hendricks266 2018-02-17 22:30:39 +00:00
parent a5be1c2e17
commit 3ae7cb1de5
13 changed files with 111 additions and 67 deletions

View file

@ -23,6 +23,7 @@
#include "glad/glad.h" #include "glad/glad.h"
#include "glbuild.h" #include "glbuild.h"
#include "palette.h" #include "palette.h"
#include "collections.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -1349,12 +1350,10 @@ extern const char *G_DefaultDefFile(void);
extern const char *G_DefFile(void); extern const char *G_DefFile(void);
extern char *g_defNamePtr; extern char *g_defNamePtr;
extern char **g_defModules; extern GrowArray<char *> g_defModules;
extern int32_t g_defModulesNum;
#ifdef HAVE_CLIPSHAPE_FEATURE #ifdef HAVE_CLIPSHAPE_FEATURE
extern char **g_clipMapFiles; extern GrowArray<char *> g_clipMapFiles;
extern int32_t g_clipMapFilesNum;
#endif #endif
#ifdef USE_OPENGL #ifdef USE_OPENGL

View file

@ -0,0 +1,67 @@
#pragma once
#ifndef collections_h_
#define collections_h_
#if CXXSTD >= 2011 || EDUKE32_MSVC_PREREQ(1800)
// GrowArray - heap-allocated storage that can expand at runtime
// requirements: type must work properly with realloc -- otherwise, use std::vector
template <typename T, size_t increment_ = 1, typename = enable_if_t<std::is_pod<T>::value>, typename = enable_if_t<(increment_ > 0)>>
struct GrowArray
{
FORCE_INLINE T * begin() const { return data_; }
FORCE_INLINE T * end() const { return data_ + size_; }
FORCE_INLINE size_t size() const { return size_; }
FORCE_INLINE T& operator[](size_t index) { return data_[index]; }
FORCE_INLINE const T& operator[](size_t index) const { return data_[index]; }
FORCE_INLINE T& first() { return data_[0]; }
FORCE_INLINE const T& first() const { return data_[0]; }
FORCE_INLINE T& last() { return data_[size_-1]; }
FORCE_INLINE const T& last() const { return data_[size_-1]; }
void append(T item)
{
if (size_ == capacity_)
reallocate(capacity_ + increment_);
data_[size_++] = item;
}
void removeLast()
{
--size_;
}
void vacuum()
{
if (size_ < capacity_)
reallocate(size);
}
void clear()
{
size_ = 0;
capacity_ = 0;
free(data_);
data_ = nullptr;
}
protected:
void reallocate(size_t newcapacity)
{
data_ = (T *)Xrealloc(data_, newcapacity * sizeof(T));
capacity_ = newcapacity;
}
T * data_ = nullptr;
size_t size_ = 0, capacity_ = 0;
};
#endif
#endif // collections_h_

View file

@ -687,10 +687,9 @@ int app_main(int argc, char const * const * argv)
if (!loaddefinitionsfile(defsfile)) if (!loaddefinitionsfile(defsfile))
initprintf("Definitions file \"%s\" loaded.\n",defsfile); initprintf("Definitions file \"%s\" loaded.\n",defsfile);
for (i=0; i < g_defModulesNum; ++i) for (char * m : g_defModules)
Bfree(g_defModules[i]); free(m);
DO_FREE_AND_NULL(g_defModules); g_defModules.clear();
g_defModulesNum = 0;
if (E_PostInit()) if (E_PostInit())
M32_FatalEngineError(); M32_FatalEngineError();
@ -738,10 +737,9 @@ int app_main(int argc, char const * const * argv)
if (k>0) if (k>0)
initprintf("There was an error loading the sprite clipping map (status %d).\n", k); initprintf("There was an error loading the sprite clipping map (status %d).\n", k);
for (i=0; i < g_clipMapFilesNum; ++i) for (char * f : g_clipMapFiles)
Bfree(g_clipMapFiles[i]); free(f);
DO_FREE_AND_NULL(g_clipMapFiles); g_clipMapFiles.clear();
g_clipMapFilesNum = 0;
#endif #endif
taglab_init(); taglab_init();

View file

@ -77,7 +77,8 @@ int32_t clipmapinfo_load(void)
int32_t i, k, w; int32_t i, k, w;
int32_t lwcp = 0; int32_t lwcp = 0;
int32_t fi; size_t fi;
size_t const g_clipMapFilesNum = g_clipMapFiles.size();
int32_t *fisec = NULL; int32_t *fisec = NULL;
int32_t *fispr = NULL; int32_t *fispr = NULL;

View file

@ -68,12 +68,10 @@ void clearDefNamePtr(void)
// g_defNamePtr assumed to be assigned to right after // g_defNamePtr assumed to be assigned to right after
} }
char **g_defModules = NULL; GrowArray<char *> g_defModules;
int32_t g_defModulesNum = 0;
#ifdef HAVE_CLIPSHAPE_FEATURE #ifdef HAVE_CLIPSHAPE_FEATURE
char **g_clipMapFiles = NULL; GrowArray<char *> g_clipMapFiles;
int32_t g_clipMapFilesNum = 0;
#endif #endif
void G_AddDef(const char *buffer) void G_AddDef(const char *buffer)
@ -85,17 +83,13 @@ void G_AddDef(const char *buffer)
void G_AddDefModule(const char *buffer) void G_AddDefModule(const char *buffer)
{ {
g_defModules = (char **) Xrealloc (g_defModules, (g_defModulesNum+1) * sizeof(char *)); g_defModules.append(Xstrdup(buffer));
g_defModules[g_defModulesNum] = Xstrdup(buffer);
++g_defModulesNum;
} }
#ifdef HAVE_CLIPSHAPE_FEATURE #ifdef HAVE_CLIPSHAPE_FEATURE
void G_AddClipMap(const char *buffer) void G_AddClipMap(const char *buffer)
{ {
g_clipMapFiles = (char **) Xrealloc (g_clipMapFiles, (g_clipMapFilesNum+1) * sizeof(char *)); g_clipMapFiles.append(Xstrdup(buffer));
g_clipMapFiles[g_clipMapFilesNum] = Xstrdup(buffer);
++g_clipMapFilesNum;
} }
#endif #endif

View file

@ -3637,7 +3637,6 @@ int32_t loaddefinitionsfile(const char *fn)
{ {
scriptfile *script; scriptfile *script;
int32_t f = flushlogwindow; int32_t f = flushlogwindow;
int32_t i;
script = scriptfile_fromfile(fn); script = scriptfile_fromfile(fn);
@ -3650,8 +3649,8 @@ int32_t loaddefinitionsfile(const char *fn)
defsparser(script); defsparser(script);
} }
for (i=0; i < g_defModulesNum; ++i) for (char const * m : g_defModules)
defsparser_include(g_defModules[i], NULL, NULL); defsparser_include(m, NULL, NULL);
flushlogwindow = f; flushlogwindow = f;

View file

@ -8074,9 +8074,7 @@ static void G_CheckCommandLine(int32_t argc, char const * const * argv)
char clipshape[16] = "_clipshape0.map"; char clipshape[16] = "_clipshape0.map";
clipshape[10] = j; clipshape[10] = j;
g_clipMapFiles = (char **) Xrealloc (g_clipMapFiles, (g_clipMapFilesNum+1) * sizeof(char *)); g_clipMapFiles.append(Xstrdup(clipshape));
g_clipMapFiles[g_clipMapFilesNum] = Xstrdup(clipshape);
++g_clipMapFilesNum;
} }
#endif #endif
@ -9350,7 +9348,6 @@ static int32_t parsegroupfiles(scriptfile *script)
int loaddefinitions_game(const char *fn, int32_t preload) int loaddefinitions_game(const char *fn, int32_t preload)
{ {
scriptfile *script; scriptfile *script;
int32_t i;
UNREFERENCED_PARAMETER(preload); UNREFERENCED_PARAMETER(preload);
@ -9358,8 +9355,8 @@ int loaddefinitions_game(const char *fn, int32_t preload)
if (script) if (script)
parsegroupfiles(script); parsegroupfiles(script);
for (i=0; i < g_defModulesNum; ++i) for (char const * m : g_defModules)
parsegroupfiles_include(g_defModules[i], NULL, "null"); parsegroupfiles_include(m, NULL, "null");
if (script) if (script)
scriptfile_close(script); scriptfile_close(script);
@ -9901,7 +9898,7 @@ END:
static int32_t loadconsounds(const char *fn) static int32_t loadconsounds(const char *fn)
{ {
scriptfile *script; scriptfile *script;
int32_t ret, i; int32_t ret;
initprintf("Loading sounds from \"%s\"\n",fn); initprintf("Loading sounds from \"%s\"\n",fn);
@ -9913,13 +9910,12 @@ static int32_t loadconsounds(const char *fn)
} }
ret = parseconsounds(script); ret = parseconsounds(script);
for (i=0; i < g_scriptModulesNum; ++i) for (char * m : g_scriptModules)
{ {
parseconsounds_include(g_scriptModules[i], NULL, "null"); parseconsounds_include(m, NULL, "null");
Bfree(g_scriptModules[i]); free(m);
} }
DO_FREE_AND_NULL(g_scriptModules); g_scriptModules.clear();
g_scriptModulesNum = 0;
if (ret < 0) if (ret < 0)
initprintf("There was an error parsing \"%s\".\n", fn); initprintf("There was an error parsing \"%s\".\n", fn);

View file

@ -196,9 +196,7 @@ void G_CheckCommandLine(int32_t argc, char const * const * argv)
char clipshape[16] = "_clipshape0.map"; char clipshape[16] = "_clipshape0.map";
clipshape[10] = j; clipshape[10] = j;
g_clipMapFiles = (char **) Xrealloc(g_clipMapFiles, (g_clipMapFilesNum+1) * sizeof(char *)); g_clipMapFiles.append(Xstrdup(clipshape));
g_clipMapFiles[g_clipMapFilesNum] = Xstrdup(clipshape);
++g_clipMapFilesNum;
} }
#endif #endif

View file

@ -937,8 +937,7 @@ void G_CleanupSearchPaths(void)
struct strllist *CommandPaths, *CommandGrps; struct strllist *CommandPaths, *CommandGrps;
char **g_scriptModules = NULL; GrowArray<char *> g_scriptModules;
int32_t g_scriptModulesNum = 0;
void G_AddGroup(const char *buffer) void G_AddGroup(const char *buffer)
{ {
@ -987,9 +986,7 @@ void G_AddCon(const char *buffer)
void G_AddConModule(const char *buffer) void G_AddConModule(const char *buffer)
{ {
g_scriptModules = (char **) Xrealloc (g_scriptModules, (g_scriptModulesNum+1) * sizeof(char *)); g_scriptModules.append(Xstrdup(buffer));
g_scriptModules[g_scriptModulesNum] = Xstrdup(buffer);
++g_scriptModulesNum;
} }
////////// //////////

View file

@ -97,8 +97,7 @@ extern const char *G_GrpFile(void);
extern const char *G_DefaultConFile(void); extern const char *G_DefaultConFile(void);
extern const char *G_ConFile(void); extern const char *G_ConFile(void);
extern char **g_scriptModules; extern GrowArray<char *> g_scriptModules;
extern int32_t g_scriptModulesNum;
extern void G_AddCon(const char *buffer); extern void G_AddCon(const char *buffer);
extern void G_AddConModule(const char *buffer); extern void G_AddConModule(const char *buffer);

View file

@ -5500,8 +5500,8 @@ int loaddefinitions_game(const char *fileName, int32_t firstPass)
if (pScript) if (pScript)
parsedefinitions_game(pScript, firstPass); parsedefinitions_game(pScript, firstPass);
for (bssize_t i=0; i < g_defModulesNum; ++i) for (char const * m : g_defModules)
parsedefinitions_game_include(g_defModules[i], NULL, "null", firstPass); parsedefinitions_game_include(m, NULL, "null", firstPass);
if (pScript) if (pScript)
scriptfile_close(pScript); scriptfile_close(pScript);
@ -6418,10 +6418,9 @@ int app_main(int argc, char const * const * argv)
} }
loaddefinitions_game(defsfile, FALSE); loaddefinitions_game(defsfile, FALSE);
for (bssize_t i = 0; i < g_defModulesNum; ++i) Bfree(g_defModules[i]); for (char * m : g_defModules)
free(m);
DO_FREE_AND_NULL(g_defModules); g_defModules.clear();
g_defModulesNum = 0;
if (E_PostInit()) if (E_PostInit())
G_FatalEngineError(); G_FatalEngineError();
@ -6493,10 +6492,9 @@ int app_main(int argc, char const * const * argv)
if (clipMapError > 0) if (clipMapError > 0)
initprintf("There was an error loading the sprite clipping map (status %d).\n", clipMapError); initprintf("There was an error loading the sprite clipping map (status %d).\n", clipMapError);
for (bssize_t i=0; i < g_clipMapFilesNum; ++i) for (char * m : g_clipMapFiles)
Bfree(g_clipMapFiles[i]); free(m);
DO_FREE_AND_NULL(g_clipMapFiles); g_clipMapFiles.clear();
g_clipMapFilesNum = 0;
#endif #endif
// check if the minifont will support lowercase letters (3136-3161) // check if the minifont will support lowercase letters (3136-3161)

View file

@ -6539,13 +6539,12 @@ void C_Compile(const char *fileName)
C_ParseCommand(1); C_ParseCommand(1);
for (int i=0; i < g_scriptModulesNum; ++i) for (char * m : g_scriptModules)
{ {
C_Include(g_scriptModules[i]); C_Include(m);
Bfree(g_scriptModules[i]); free(m);
} }
DO_FREE_AND_NULL(g_scriptModules); g_scriptModules.clear();
g_scriptModulesNum = 0;
flushlogwindow = 1; flushlogwindow = 1;

View file

@ -1032,10 +1032,9 @@ InitGame(int32_t argc, char const * const * argv)
if (!loaddefinitionsfile(G_DefFile())) buildputs("Definitions file loaded.\n"); if (!loaddefinitionsfile(G_DefFile())) buildputs("Definitions file loaded.\n");
for (i=0; i < g_defModulesNum; ++i) for (char * m : g_defModules)
Bfree(g_defModules[i]); free(m);
DO_FREE_AND_NULL(g_defModules); g_defModules.clear();
g_defModulesNum = 0;
if (E_PostInit()) if (E_PostInit())
SW_FatalEngineError(); SW_FatalEngineError();