Skip to content

Commit b5bb22a

Browse files
committedNov 7, 2019
SanderMertens#106 add strdup to OS API
1 parent 3094e58 commit b5bb22a

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed
 

‎include/flecs/util/os_api.h

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void* (*ecs_os_api_calloc_t)(
4747
size_t num,
4848
size_t size);
4949

50+
typedef
51+
char* (*ecs_os_api_strdup_t)(
52+
const char *str);
5053

5154
/* Threads */
5255
typedef
@@ -147,6 +150,7 @@ typedef struct ecs_os_api_t {
147150
ecs_os_api_realloc_t realloc;
148151
ecs_os_api_calloc_t calloc;
149152
ecs_os_api_free_t free;
153+
ecs_os_api_strdup_t strdup;
150154

151155
/* Threads */
152156
ecs_os_api_thread_new_t thread_new;
@@ -203,6 +207,7 @@ void ecs_os_set_api_defaults(void);
203207
#define ecs_os_free(ptr) ecs_os_api.free(ptr);
204208
#define ecs_os_realloc(ptr, size) ecs_os_api.realloc(ptr, size)
205209
#define ecs_os_calloc(num, size) ecs_os_api.calloc(num, size)
210+
#define ecs_os_strdup(str) ecs_os_api.strdup(str)
206211

207212
#if defined(_MSC_VER) || defined(__MINGW32__)
208213
#define ecs_os_alloca(type, count) _alloca(sizeof(type) * (count))

‎src/column_system.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ ecs_entity_t ecs_new_col_system(
756756
memset(system_data, 0, sizeof(EcsColSystem));
757757
system_data->base.action = action;
758758
system_data->base.enabled = true;
759-
system_data->base.signature = strdup(sig);
759+
system_data->base.signature = ecs_os_strdup(sig);
760760
system_data->base.time_spent = 0;
761761
system_data->base.columns = ecs_vector_new(&system_column_params, count);
762762
system_data->base.invoke_count = 0;

‎src/os_api.c

+9
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ void ecs_os_api_free(void *ptr) {
263263
free(ptr);
264264
}
265265

266+
static
267+
char* ecs_os_api_strdup(const char *str) {
268+
int len = strlen(str);
269+
char *result = ecs_os_api_malloc(len + 1);
270+
strcpy(result, str);
271+
return result;
272+
}
273+
266274
void ecs_os_set_api_defaults(void)
267275
{
268276
/* Don't overwrite if already initialized */
@@ -276,6 +284,7 @@ void ecs_os_set_api_defaults(void)
276284
ecs_os_api.free = ecs_os_api_free;
277285
ecs_os_api.realloc = ecs_os_api_realloc;
278286
ecs_os_api.calloc = ecs_os_api_calloc;
287+
ecs_os_api.strdup = ecs_os_api_strdup;
279288

280289
#ifdef __BAKE__
281290
ecs_os_api.thread_new = bake_thread_new;

‎src/system.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ ecs_entity_t new_row_system(
105105
EcsRowSystem *system_data = ecs_get_ptr(world, result, EcsRowSystem);
106106
memset(system_data, 0, sizeof(EcsRowSystem));
107107
system_data->base.action = action;
108-
system_data->base.signature = strdup(sig);
108+
system_data->base.signature = ecs_os_strdup(sig);
109109
system_data->base.enabled = true;
110110
system_data->base.invoke_count = 0;
111111
system_data->base.kind = kind;

0 commit comments

Comments
 (0)
Please sign in to comment.