From 757d937cda76f6e02cb2005036920c2f107ae96c Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 22 Dec 2020 20:51:15 +0000 Subject: [PATCH] Replace use of strdup with rwstrdup --- src/engine.cpp | 10 ++++++++++ src/gl/gl3shader.cpp | 4 ++-- src/image.cpp | 17 +++-------------- src/png.cpp | 5 ----- src/rwengine.h | 3 +++ 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 58e3e1c..8790a2d 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -156,6 +157,15 @@ void *mustrealloc_h(void *p, size_t sz, uint32 hint) return nil; } +char *strdup_LOC(const char *s, uint32 hint, const char *here) { + char *t; + size_t sz = strlen(s)+1; + t = (char*)malloc_LOC(sz, hint, here); + if(t) + memcpy(t, s, sz); + return t; +} + MemoryFunctions defaultMemfuncs = { malloc_h, realloc_h, diff --git a/src/gl/gl3shader.cpp b/src/gl/gl3shader.cpp index 0dc4a57..11880d3 100644 --- a/src/gl/gl3shader.cpp +++ b/src/gl/gl3shader.cpp @@ -33,7 +33,7 @@ registerUniform(const char *name) assert(0 && "no space for uniform"); return -1; } - uniformRegistry.uniformNames[uniformRegistry.numUniforms] = strdup(name); + uniformRegistry.uniformNames[uniformRegistry.numUniforms] = rwStrdup(name, MEMDUR_EVENT); return uniformRegistry.numUniforms++; } @@ -56,7 +56,7 @@ registerBlock(const char *name) // TODO: print error if(uniformRegistry.numBlocks+1 >= MAX_BLOCKS) return -1; - uniformRegistry.blockNames[uniformRegistry.numBlocks] = strdup(name); + uniformRegistry.blockNames[uniformRegistry.numBlocks] = rwStrdup(name, MEMDUR_EVENT); return uniformRegistry.numBlocks++; } diff --git a/src/image.cpp b/src/image.cpp index 8132ddf..aa72875 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -894,17 +894,6 @@ Image::extractMask(void) return img; } -static char* -rwstrdup(const char *s) -{ - char *t; - size_t len = strlen(s)+1; - t = (char*)rwMalloc(len, MEMDUR_EVENT); - if(t) - memcpy(t, s, len); - return t; -} - void Image::setSearchPath(const char *path) { @@ -913,7 +902,7 @@ Image::setSearchPath(const char *path) rwFree(g->searchPaths); g->numSearchPaths = 0; if(path) - g->searchPaths = p = rwstrdup(path); + g->searchPaths = p = rwStrdup(path, MEMDUR_EVENT); else{ g->searchPaths = nil; return; @@ -946,7 +935,7 @@ Image::getFilename(const char *name) char *s, *p = g->searchPaths; size_t len = strlen(name)+1; if(g->numSearchPaths == 0){ - s = rwstrdup(name); + s = rwStrdup(name, MEMDUR_EVENT); makePath(s); f = fopen(s, "rb"); if(f){ @@ -1038,7 +1027,7 @@ Image::registerFileFormat(const char *ext, fileRead read, fileWrite write) ImageGlobals *g = PLUGINOFFSET(ImageGlobals, engine, imageModuleOffset); if(g->numFileFormats >= nelem(g->fileFormats)) return 0; - g->fileFormats[g->numFileFormats].extension = rwstrdup(ext); + g->fileFormats[g->numFileFormats].extension = rwStrdup(ext, MEMDUR_EVENT); g->fileFormats[g->numFileFormats].read = read; g->fileFormats[g->numFileFormats].write = write; g->numFileFormats++; diff --git a/src/png.cpp b/src/png.cpp index 5749a32..4dc077c 100644 --- a/src/png.cpp +++ b/src/png.cpp @@ -12,11 +12,6 @@ #include "lodepng/lodepng.h" -#ifdef _WIN32 -/* srsly? */ -#define strdup _strdup -#endif - #define PLUGIN_ID 0 namespace rw { diff --git a/src/rwengine.h b/src/rwengine.h index f49ae4e..a84aa3a 100644 --- a/src/rwengine.h +++ b/src/rwengine.h @@ -206,6 +206,8 @@ inline void *realloc_LOC(void *p, size_t sz, uint32 hint, const char *here) { al inline void *mustmalloc_LOC(size_t sz, uint32 hint, const char *here) { allocLocation = here; return rw::Engine::memfuncs.rwmustmalloc(sz,hint); } inline void *mustrealloc_LOC(void *p, size_t sz, uint32 hint, const char *here) { allocLocation = here; return rw::Engine::memfuncs.rwmustrealloc(p,sz,hint); } +char *strdup_LOC(const char *s, uint32 hint, const char *here); + #define rwMalloc(s, h) rw::malloc_LOC(s,h,RWHERE) #define rwMallocT(t, s, h) (t*)rw::malloc_LOC((s)*sizeof(t),h,RWHERE) #define rwRealloc(p, s, h) rw::realloc_LOC(p,s,h,RWHERE) @@ -215,6 +217,7 @@ inline void *mustrealloc_LOC(void *p, size_t sz, uint32 hint, const char *here) #define rwNewT(t, s, h) (t*)rw::mustmalloc_LOC((s)*sizeof(t),h,RWHERE) #define rwResize(p, s, h) rw::mustrealloc_LOC(p,s,h,RWHERE) #define rwResizeT(t, p, s, h) (t*)rw::mustrealloc_LOC(p,(s)*sizeof(t),h,RWHERE) +#define rwStrdup(s, h) rw::strdup_LOC(s,h,RWHERE) extern MemoryFunctions defaultMemfuncs; extern MemoryFunctions managedMemfuncs;