Merge pull request #42 from ccawley2011/rwstrdup

Replace use of strdup with rwstrdup
This commit is contained in:
aap 2021-01-14 00:34:13 +01:00 committed by GitHub
commit c493613724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 21 deletions

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <assert.h> #include <assert.h>
#include <new> #include <new>
@ -156,6 +157,15 @@ void *mustrealloc_h(void *p, size_t sz, uint32 hint)
return nil; 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 = { MemoryFunctions defaultMemfuncs = {
malloc_h, malloc_h,
realloc_h, realloc_h,

View File

@ -33,7 +33,7 @@ registerUniform(const char *name)
assert(0 && "no space for uniform"); assert(0 && "no space for uniform");
return -1; return -1;
} }
uniformRegistry.uniformNames[uniformRegistry.numUniforms] = strdup(name); uniformRegistry.uniformNames[uniformRegistry.numUniforms] = rwStrdup(name, MEMDUR_EVENT);
return uniformRegistry.numUniforms++; return uniformRegistry.numUniforms++;
} }
@ -56,7 +56,7 @@ registerBlock(const char *name)
// TODO: print error // TODO: print error
if(uniformRegistry.numBlocks+1 >= MAX_BLOCKS) if(uniformRegistry.numBlocks+1 >= MAX_BLOCKS)
return -1; return -1;
uniformRegistry.blockNames[uniformRegistry.numBlocks] = strdup(name); uniformRegistry.blockNames[uniformRegistry.numBlocks] = rwStrdup(name, MEMDUR_EVENT);
return uniformRegistry.numBlocks++; return uniformRegistry.numBlocks++;
} }

View File

@ -894,17 +894,6 @@ Image::extractMask(void)
return img; 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 void
Image::setSearchPath(const char *path) Image::setSearchPath(const char *path)
{ {
@ -913,7 +902,7 @@ Image::setSearchPath(const char *path)
rwFree(g->searchPaths); rwFree(g->searchPaths);
g->numSearchPaths = 0; g->numSearchPaths = 0;
if(path) if(path)
g->searchPaths = p = rwstrdup(path); g->searchPaths = p = rwStrdup(path, MEMDUR_EVENT);
else{ else{
g->searchPaths = nil; g->searchPaths = nil;
return; return;
@ -946,7 +935,7 @@ Image::getFilename(const char *name)
char *s, *p = g->searchPaths; char *s, *p = g->searchPaths;
size_t len = strlen(name)+1; size_t len = strlen(name)+1;
if(g->numSearchPaths == 0){ if(g->numSearchPaths == 0){
s = rwstrdup(name); s = rwStrdup(name, MEMDUR_EVENT);
makePath(s); makePath(s);
f = fopen(s, "rb"); f = fopen(s, "rb");
if(f){ if(f){
@ -1038,7 +1027,7 @@ Image::registerFileFormat(const char *ext, fileRead read, fileWrite write)
ImageGlobals *g = PLUGINOFFSET(ImageGlobals, engine, imageModuleOffset); ImageGlobals *g = PLUGINOFFSET(ImageGlobals, engine, imageModuleOffset);
if(g->numFileFormats >= (int)nelem(g->fileFormats)) if(g->numFileFormats >= (int)nelem(g->fileFormats))
return 0; 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].read = read;
g->fileFormats[g->numFileFormats].write = write; g->fileFormats[g->numFileFormats].write = write;
g->numFileFormats++; g->numFileFormats++;

View File

@ -12,11 +12,6 @@
#include "lodepng/lodepng.h" #include "lodepng/lodepng.h"
#ifdef _WIN32
/* srsly? */
#define strdup _strdup
#endif
#define PLUGIN_ID 0 #define PLUGIN_ID 0
namespace rw { namespace rw {

View File

@ -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 *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); } 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 rwMalloc(s, h) rw::malloc_LOC(s,h,RWHERE)
#define rwMallocT(t, s, h) (t*)rw::malloc_LOC((s)*sizeof(t),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) #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 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 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 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 defaultMemfuncs;
extern MemoryFunctions managedMemfuncs; extern MemoryFunctions managedMemfuncs;