Skip to content

Commit c9ffca9

Browse files
committed
Add lovrStrdup macro;
1 parent caeac2a commit c9ffca9

File tree

7 files changed

+17
-61
lines changed

7 files changed

+17
-61
lines changed

src/api/l_thread.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ static char* threadRunner(Thread* thread, Blob* body, Variant* arguments, uint32
2828
}
2929

3030
// Error handling
31-
size_t length;
32-
const char* message = lua_tolstring(L, -1, &length);
33-
34-
if (message) {
35-
char* error = lovrMalloc(length + 1);
36-
memcpy(error, message, length + 1);
31+
if (lua_type(L, -1) == LUA_TSTRING) {
32+
const char* message = lua_tostring(L, -1);
33+
char* error = lovrStrdup(message);
3734
lua_close(L);
3835
return error;
3936
}

src/modules/data/blob.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ Blob* lovrBlobCreate(void* data, size_t size, const char* name) {
88
blob->ref = 1;
99
blob->data = data;
1010
blob->size = size;
11-
if (name) {
12-
size_t length = strlen(name);
13-
char* string = lovrMalloc(length + 1);
14-
memcpy(string, name, length + 1);
15-
blob->name = string;
16-
}
11+
blob->name = lovrStrdup(name);
1712
return blob;
1813
}
1914

src/modules/event/event.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,13 @@ void lovrEventPush(Event event) {
6666
#ifndef LOVR_DISABLE_THREAD
6767
if (event.type == EVENT_THREAD_ERROR) {
6868
lovrRetain(event.data.thread.thread);
69-
size_t length = strlen(event.data.thread.error);
70-
char* copy = lovrMalloc(length + 1);
71-
memcpy(copy, event.data.thread.error, length + 1);
72-
event.data.thread.error = copy;
69+
event.data.thread.error = lovrStrdup(event.data.thread.error);
7370
}
7471
#endif
7572

7673
if (event.type == EVENT_FILECHANGED) {
77-
size_t length = strlen(event.data.file.path);
78-
char* copy = lovrMalloc(length + 1);
79-
memcpy(copy, event.data.file.path, length + 1);
80-
event.data.file.path = copy;
81-
82-
if (event.data.file.oldpath) {
83-
length = strlen(event.data.file.oldpath);
84-
copy = lovrMalloc(length + 1);
85-
memcpy(copy, event.data.file.oldpath, length + 1);
86-
event.data.file.oldpath = copy;
87-
}
74+
event.data.file.path = lovrStrdup(event.data.file.path);
75+
event.data.file.oldpath = lovrStrdup(event.data.file.oldpath);
8876
}
8977

9078
mtx_lock(&state.lock);

src/modules/filesystem/filesystem.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,8 @@ const char* lovrFilesystemGetRequirePath(void) {
602602
}
603603

604604
void lovrFilesystemSetRequirePath(const char* requirePath) {
605-
size_t length = strlen(requirePath);
606605
lovrFree(state.requirePath);
607-
state.requirePath = lovrMalloc(length + 1);
608-
memcpy(state.requirePath, requirePath, length + 1);
606+
state.requirePath = lovrStrdup(requirePath);
609607
}
610608

611609
// Archive: dir
@@ -1126,8 +1124,7 @@ File* lovrFileCreate(const char* p, OpenMode mode) {
11261124
file->mode = mode;
11271125
file->handle = handle;
11281126
file->archive = archive;
1129-
file->path = lovrMalloc(length + 1);
1130-
memcpy(file->path, path, length + 1);
1127+
file->path = lovrStrdup(path);
11311128
lovrRetain(archive);
11321129
return file;
11331130
}

src/modules/graphics/graphics.c

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,9 +1127,7 @@ static void compilePipeline(void* arg) {
11271127
PipelineJob* job = arg;
11281128
if (!gpu_pipeline_init_graphics(job->pipeline, job->info, NULL)) {
11291129
const char* error = gpu_get_error();
1130-
size_t length = strlen(error);
1131-
job->error = lovrMalloc(length + 1);
1132-
memcpy(job->error, error, length + 1);
1130+
job->error = lovrStrdup(error);
11331131
}
11341132
}
11351133

@@ -2420,13 +2418,7 @@ Texture* lovrTextureCreate(const TextureInfo* info) {
24202418
texture->info.mipmaps = mipmaps;
24212419
texture->info.samples = samples;
24222420
texture->info.srgb = srgb;
2423-
2424-
if (info->label) {
2425-
size_t size = strlen(info->label) + 1;
2426-
char* label = lovrMalloc(size);
2427-
memcpy(label, info->label, size);
2428-
texture->info.label = label;
2429-
}
2421+
texture->info.label = lovrStrdup(info->label);
24302422

24312423
uint32_t levelCount = 0;
24322424
uint32_t levelOffsets[16];
@@ -2609,14 +2601,7 @@ Texture* lovrTextureCreateView(Texture* parent, const TextureViewInfo* info) {
26092601
texture->ref = 1;
26102602
texture->gpu = (gpu_texture*) (texture + 1);
26112603
texture->info = *base;
2612-
2613-
if (info->label) {
2614-
size_t size = strlen(info->label) + 1;
2615-
char* label = lovrMalloc(size);
2616-
memcpy(label, info->label, size);
2617-
texture->info.label = label;
2618-
}
2619-
2604+
texture->info.label = lovrStrdup(info->label);
26202605
texture->root = parent->root;
26212606
texture->baseLayer = parent->baseLayer + info->layerIndex;
26222607
texture->baseLevel = parent->baseLevel + info->levelIndex;
@@ -3346,13 +3331,7 @@ Shader* lovrShaderCreate(const ShaderInfo* info) {
33463331
shader->ref = 1;
33473332
shader->gpu = (gpu_shader*) (shader + 1);
33483333
shader->info = *info;
3349-
3350-
if (info->label) {
3351-
size_t size = strlen(info->label) + 1;
3352-
char* label = lovrMalloc(size);
3353-
memcpy(label, info->label, size);
3354-
shader->info.label = label;
3355-
}
3334+
shader->info.label = lovrStrdup(info->label);
33563335

33573336
// Validate stage combinations
33583337
for (uint32_t i = 0; i < info->stageCount; i++) {

src/modules/physics/physics.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,8 @@ World* lovrWorldCreate(WorldInfo* info) {
344344
world->tagCount = info->tagCount;
345345
world->staticTagMask = info->staticTagMask;
346346
for (uint32_t i = 0; i < world->tagCount; i++) {
347-
size_t length = strlen(info->tags[i]);
348-
world->tags[i] = lovrMalloc(length + 1);
349-
memcpy(world->tags[i], info->tags[i], length + 1);
350-
world->tagLookup[i] = (uint32_t) hash64(info->tags[i], length);
347+
world->tags[i] = lovrStrdup(info->tags[i]);
348+
world->tagLookup[i] = (uint32_t) hash64(info->tags[i], strlen(info->tags[i]));
351349
}
352350

353351
uint32_t broadPhaseLayerCount = world->staticTagMask ? 2 : 1;

src/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void* lovrCalloc(size_t size);
2727
void* lovrRealloc(void* data, size_t size);
2828
void lovrFree(void* data);
2929

30+
#define lovrStrdup(s) (s ? memcpy(lovrMalloc(strlen(s) + 1), s, strlen(s) + 1) : NULL)
31+
3032
// Refcounting (to be refcounted, a struct must have a uint32_t refcount as its first field)
3133
void lovrRetain(void* ref);
3234
void lovrRelease(void* ref, void (*destructor)(void*));

0 commit comments

Comments
 (0)