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 "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<char *> g_defModules;
#ifdef HAVE_CLIPSHAPE_FEATURE
extern char **g_clipMapFiles;
extern int32_t g_clipMapFilesNum;
extern GrowArray<char *> g_clipMapFiles;
#endif
#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))
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();

View file

@ -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;

View file

@ -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<char *> g_defModules;
#ifdef HAVE_CLIPSHAPE_FEATURE
char **g_clipMapFiles = NULL;
int32_t g_clipMapFilesNum = 0;
GrowArray<char *> 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

View file

@ -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;

View file

@ -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);

View file

@ -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

View file

@ -937,8 +937,7 @@ void G_CleanupSearchPaths(void)
struct strllist *CommandPaths, *CommandGrps;
char **g_scriptModules = NULL;
int32_t g_scriptModulesNum = 0;
GrowArray<char *> 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));
}
//////////

View file

@ -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<char *> g_scriptModules;
extern void G_AddCon(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)
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)

View file

@ -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;

View file

@ -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();