From 3ae7cb1de50822d4f447a387576b57826359dbcb Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Sat, 17 Feb 2018 22:30:39 +0000 Subject: [PATCH] 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 --- source/build/include/build.h | 7 ++-- source/build/include/collections.h | 67 ++++++++++++++++++++++++++++++ source/build/src/build.cpp | 14 +++---- source/build/src/clip.cpp | 3 +- source/build/src/common.cpp | 14 ++----- source/build/src/defs.cpp | 5 +-- source/duke3d/src/astub.cpp | 20 ++++----- source/duke3d/src/cmdline.cpp | 4 +- source/duke3d/src/common.cpp | 7 +--- source/duke3d/src/common_game.h | 3 +- source/duke3d/src/game.cpp | 18 ++++---- source/duke3d/src/gamedef.cpp | 9 ++-- source/sw/src/game.cpp | 7 ++-- 13 files changed, 111 insertions(+), 67 deletions(-) create mode 100644 source/build/include/collections.h diff --git a/source/build/include/build.h b/source/build/include/build.h index 65bae6334..c6929f0ff 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -23,6 +23,7 @@ #include "glad/glad.h" #include "glbuild.h" #include "palette.h" +#include "collections.h" #ifdef __cplusplus extern "C" { @@ -1349,12 +1350,10 @@ extern const char *G_DefaultDefFile(void); extern const char *G_DefFile(void); extern char *g_defNamePtr; -extern char **g_defModules; -extern int32_t g_defModulesNum; +extern GrowArray g_defModules; #ifdef HAVE_CLIPSHAPE_FEATURE -extern char **g_clipMapFiles; -extern int32_t g_clipMapFilesNum; +extern GrowArray g_clipMapFiles; #endif #ifdef USE_OPENGL diff --git a/source/build/include/collections.h b/source/build/include/collections.h new file mode 100644 index 000000000..be0c8876f --- /dev/null +++ b/source/build/include/collections.h @@ -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 ::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_ diff --git a/source/build/src/build.cpp b/source/build/src/build.cpp index 9d5b24ce8..441618e95 100644 --- a/source/build/src/build.cpp +++ b/source/build/src/build.cpp @@ -687,10 +687,9 @@ int app_main(int argc, char const * const * argv) if (!loaddefinitionsfile(defsfile)) initprintf("Definitions file \"%s\" loaded.\n",defsfile); - for (i=0; i < g_defModulesNum; ++i) - Bfree(g_defModules[i]); - DO_FREE_AND_NULL(g_defModules); - g_defModulesNum = 0; + for (char * m : g_defModules) + free(m); + g_defModules.clear(); if (E_PostInit()) M32_FatalEngineError(); @@ -738,10 +737,9 @@ int app_main(int argc, char const * const * argv) if (k>0) initprintf("There was an error loading the sprite clipping map (status %d).\n", k); - for (i=0; i < g_clipMapFilesNum; ++i) - Bfree(g_clipMapFiles[i]); - DO_FREE_AND_NULL(g_clipMapFiles); - g_clipMapFilesNum = 0; + for (char * f : g_clipMapFiles) + free(f); + g_clipMapFiles.clear(); #endif taglab_init(); diff --git a/source/build/src/clip.cpp b/source/build/src/clip.cpp index 9d2ae20e8..d1608a634 100644 --- a/source/build/src/clip.cpp +++ b/source/build/src/clip.cpp @@ -77,7 +77,8 @@ int32_t clipmapinfo_load(void) int32_t i, k, w; int32_t lwcp = 0; - int32_t fi; + size_t fi; + size_t const g_clipMapFilesNum = g_clipMapFiles.size(); int32_t *fisec = NULL; int32_t *fispr = NULL; diff --git a/source/build/src/common.cpp b/source/build/src/common.cpp index 82def804b..b2b93db25 100644 --- a/source/build/src/common.cpp +++ b/source/build/src/common.cpp @@ -68,12 +68,10 @@ void clearDefNamePtr(void) // g_defNamePtr assumed to be assigned to right after } -char **g_defModules = NULL; -int32_t g_defModulesNum = 0; +GrowArray g_defModules; #ifdef HAVE_CLIPSHAPE_FEATURE -char **g_clipMapFiles = NULL; -int32_t g_clipMapFilesNum = 0; +GrowArray g_clipMapFiles; #endif void G_AddDef(const char *buffer) @@ -85,17 +83,13 @@ void G_AddDef(const char *buffer) void G_AddDefModule(const char *buffer) { - g_defModules = (char **) Xrealloc (g_defModules, (g_defModulesNum+1) * sizeof(char *)); - g_defModules[g_defModulesNum] = Xstrdup(buffer); - ++g_defModulesNum; + g_defModules.append(Xstrdup(buffer)); } #ifdef HAVE_CLIPSHAPE_FEATURE void G_AddClipMap(const char *buffer) { - g_clipMapFiles = (char **) Xrealloc (g_clipMapFiles, (g_clipMapFilesNum+1) * sizeof(char *)); - g_clipMapFiles[g_clipMapFilesNum] = Xstrdup(buffer); - ++g_clipMapFilesNum; + g_clipMapFiles.append(Xstrdup(buffer)); } #endif diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index ef8e5c74f..00eea8e57 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -3637,7 +3637,6 @@ int32_t loaddefinitionsfile(const char *fn) { scriptfile *script; int32_t f = flushlogwindow; - int32_t i; script = scriptfile_fromfile(fn); @@ -3650,8 +3649,8 @@ int32_t loaddefinitionsfile(const char *fn) defsparser(script); } - for (i=0; i < g_defModulesNum; ++i) - defsparser_include(g_defModules[i], NULL, NULL); + for (char const * m : g_defModules) + defsparser_include(m, NULL, NULL); flushlogwindow = f; diff --git a/source/duke3d/src/astub.cpp b/source/duke3d/src/astub.cpp index 95cc30e3e..6f4ef111d 100644 --- a/source/duke3d/src/astub.cpp +++ b/source/duke3d/src/astub.cpp @@ -8074,9 +8074,7 @@ static void G_CheckCommandLine(int32_t argc, char const * const * argv) char clipshape[16] = "_clipshape0.map"; clipshape[10] = j; - g_clipMapFiles = (char **) Xrealloc (g_clipMapFiles, (g_clipMapFilesNum+1) * sizeof(char *)); - g_clipMapFiles[g_clipMapFilesNum] = Xstrdup(clipshape); - ++g_clipMapFilesNum; + g_clipMapFiles.append(Xstrdup(clipshape)); } #endif @@ -9350,7 +9348,6 @@ static int32_t parsegroupfiles(scriptfile *script) int loaddefinitions_game(const char *fn, int32_t preload) { scriptfile *script; - int32_t i; UNREFERENCED_PARAMETER(preload); @@ -9358,8 +9355,8 @@ int loaddefinitions_game(const char *fn, int32_t preload) if (script) parsegroupfiles(script); - for (i=0; i < g_defModulesNum; ++i) - parsegroupfiles_include(g_defModules[i], NULL, "null"); + for (char const * m : g_defModules) + parsegroupfiles_include(m, NULL, "null"); if (script) scriptfile_close(script); @@ -9901,7 +9898,7 @@ END: static int32_t loadconsounds(const char *fn) { scriptfile *script; - int32_t ret, i; + int32_t ret; initprintf("Loading sounds from \"%s\"\n",fn); @@ -9913,13 +9910,12 @@ static int32_t loadconsounds(const char *fn) } ret = parseconsounds(script); - for (i=0; i < g_scriptModulesNum; ++i) + for (char * m : g_scriptModules) { - parseconsounds_include(g_scriptModules[i], NULL, "null"); - Bfree(g_scriptModules[i]); + parseconsounds_include(m, NULL, "null"); + free(m); } - DO_FREE_AND_NULL(g_scriptModules); - g_scriptModulesNum = 0; + g_scriptModules.clear(); if (ret < 0) initprintf("There was an error parsing \"%s\".\n", fn); diff --git a/source/duke3d/src/cmdline.cpp b/source/duke3d/src/cmdline.cpp index 19f04d0ae..08e80b016 100644 --- a/source/duke3d/src/cmdline.cpp +++ b/source/duke3d/src/cmdline.cpp @@ -196,9 +196,7 @@ void G_CheckCommandLine(int32_t argc, char const * const * argv) char clipshape[16] = "_clipshape0.map"; clipshape[10] = j; - g_clipMapFiles = (char **) Xrealloc(g_clipMapFiles, (g_clipMapFilesNum+1) * sizeof(char *)); - g_clipMapFiles[g_clipMapFilesNum] = Xstrdup(clipshape); - ++g_clipMapFilesNum; + g_clipMapFiles.append(Xstrdup(clipshape)); } #endif diff --git a/source/duke3d/src/common.cpp b/source/duke3d/src/common.cpp index d9a3da1fd..7ea47dee7 100644 --- a/source/duke3d/src/common.cpp +++ b/source/duke3d/src/common.cpp @@ -937,8 +937,7 @@ void G_CleanupSearchPaths(void) struct strllist *CommandPaths, *CommandGrps; -char **g_scriptModules = NULL; -int32_t g_scriptModulesNum = 0; +GrowArray g_scriptModules; void G_AddGroup(const char *buffer) { @@ -987,9 +986,7 @@ void G_AddCon(const char *buffer) void G_AddConModule(const char *buffer) { - g_scriptModules = (char **) Xrealloc (g_scriptModules, (g_scriptModulesNum+1) * sizeof(char *)); - g_scriptModules[g_scriptModulesNum] = Xstrdup(buffer); - ++g_scriptModulesNum; + g_scriptModules.append(Xstrdup(buffer)); } ////////// diff --git a/source/duke3d/src/common_game.h b/source/duke3d/src/common_game.h index c4592019e..c619d7740 100644 --- a/source/duke3d/src/common_game.h +++ b/source/duke3d/src/common_game.h @@ -97,8 +97,7 @@ extern const char *G_GrpFile(void); extern const char *G_DefaultConFile(void); extern const char *G_ConFile(void); -extern char **g_scriptModules; -extern int32_t g_scriptModulesNum; +extern GrowArray g_scriptModules; extern void G_AddCon(const char *buffer); extern void G_AddConModule(const char *buffer); diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index b42ce5cf5..959361450 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -5500,8 +5500,8 @@ int loaddefinitions_game(const char *fileName, int32_t firstPass) if (pScript) parsedefinitions_game(pScript, firstPass); - for (bssize_t i=0; i < g_defModulesNum; ++i) - parsedefinitions_game_include(g_defModules[i], NULL, "null", firstPass); + for (char const * m : g_defModules) + parsedefinitions_game_include(m, NULL, "null", firstPass); if (pScript) scriptfile_close(pScript); @@ -6418,10 +6418,9 @@ int app_main(int argc, char const * const * argv) } loaddefinitions_game(defsfile, FALSE); - for (bssize_t i = 0; i < g_defModulesNum; ++i) Bfree(g_defModules[i]); - - DO_FREE_AND_NULL(g_defModules); - g_defModulesNum = 0; + for (char * m : g_defModules) + free(m); + g_defModules.clear(); if (E_PostInit()) G_FatalEngineError(); @@ -6493,10 +6492,9 @@ int app_main(int argc, char const * const * argv) if (clipMapError > 0) initprintf("There was an error loading the sprite clipping map (status %d).\n", clipMapError); - for (bssize_t i=0; i < g_clipMapFilesNum; ++i) - Bfree(g_clipMapFiles[i]); - DO_FREE_AND_NULL(g_clipMapFiles); - g_clipMapFilesNum = 0; + for (char * m : g_clipMapFiles) + free(m); + g_clipMapFiles.clear(); #endif // check if the minifont will support lowercase letters (3136-3161) diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index f6b6a90fe..849e5c1f4 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -6539,13 +6539,12 @@ void C_Compile(const char *fileName) C_ParseCommand(1); - for (int i=0; i < g_scriptModulesNum; ++i) + for (char * m : g_scriptModules) { - C_Include(g_scriptModules[i]); - Bfree(g_scriptModules[i]); + C_Include(m); + free(m); } - DO_FREE_AND_NULL(g_scriptModules); - g_scriptModulesNum = 0; + g_scriptModules.clear(); flushlogwindow = 1; diff --git a/source/sw/src/game.cpp b/source/sw/src/game.cpp index 02eb62644..9fc4982d0 100644 --- a/source/sw/src/game.cpp +++ b/source/sw/src/game.cpp @@ -1032,10 +1032,9 @@ InitGame(int32_t argc, char const * const * argv) if (!loaddefinitionsfile(G_DefFile())) buildputs("Definitions file loaded.\n"); - for (i=0; i < g_defModulesNum; ++i) - Bfree(g_defModules[i]); - DO_FREE_AND_NULL(g_defModules); - g_defModulesNum = 0; + for (char * m : g_defModules) + free(m); + g_defModules.clear(); if (E_PostInit()) SW_FatalEngineError();