Skip to content

Commit e0c14d6

Browse files
committed
Minor tweaks allocations functions
Move some functions to be `inline`
1 parent d8dd78a commit e0c14d6

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

extlib.h

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -561,18 +561,9 @@ inline void *ext_allocator_realloc(Ext_Allocator *a, void *ptr, size_t old_sz, s
561561
inline void ext_allocator_free(Ext_Allocator *a, void *ptr, size_t size) {
562562
a->free(a, ptr, size);
563563
}
564-
char *ext_allocator_strdup(Ext_Allocator *a, const char *s);
565-
void *ext_allocator_memdup(Ext_Allocator *a, const void *mem, size_t size);
566-
// Backward compatibility: old _alloc suffix functions now use ext_allocator_* internally
567-
// Note: parameter order changed - allocator is now first parameter
568-
// DEPRECATED: Use ext_allocator_strdup and ext_allocator_memdup instead
569-
#define ext_strdup_alloc(s, a) ext_allocator_strdup(a, s)
570-
#define ext_memdup_alloc(mem, size, a) ext_allocator_memdup(a, mem, size)
571564

572565
// Allocation functions that use the current configured context to allocate, reallocate and free
573566
// memory.
574-
// It is reccomended to always use these functions instead of malloc/realloc/free when you need
575-
// memory to make the behaviour of your code configurable via the context.
576567
inline void *ext_alloc(size_t size) {
577568
return ext_allocator_alloc(ext_context->alloc, size);
578569
}
@@ -582,15 +573,37 @@ inline void *ext_realloc(void *ptr, size_t old_sz, size_t new_sz) {
582573
inline void ext_free(void *ptr, size_t size) {
583574
ext_allocator_free(ext_context->alloc, ptr, size);
584575
}
576+
577+
// Copies a cstring by using the provided allocator
578+
char *ext_allocator_strdup(Ext_Allocator *a, const char *s) {
579+
size_t len = strlen(s);
580+
char *res = a->alloc(a, len + 1);
581+
memcpy(res, s, len);
582+
res[len] = '\0';
583+
return res;
584+
}
585+
586+
// Copies a memory region of `size` bytes by using the provided allocator
587+
void *ext_allocator_memdup(Ext_Allocator *a, const void *mem, size_t size) {
588+
return memcpy(a->alloc(a, size), mem, size);
589+
}
590+
585591
// Copies a cstring by using the current context allocator
586592
inline char *ext_strdup(const char *s) {
587593
return ext_allocator_strdup(ext_context->alloc, s);
588594
}
595+
589596
// Copies a memory region of `size` bytes by using the current context allocator
590597
inline void *ext_memdup(const void *mem, size_t size) {
591598
return ext_allocator_memdup(ext_context->alloc, mem, size);
592599
}
593600

601+
// Backward compatibility: old _alloc suffix functions now use ext_allocator_* internally
602+
// Note: parameter order changed - allocator is now first parameter
603+
// DEPRECATED: Use ext_allocator_strdup and ext_allocator_memdup instead
604+
#define ext_strdup_alloc(s, a) ext_allocator_strdup(a, s)
605+
#define ext_memdup_alloc(mem, size, a) ext_allocator_memdup(a, mem, size)
606+
594607
// A default allocator that uses malloc/realloc/free.
595608
// It is the allocator configured in the context at program start.
596609
typedef struct Ext_DefaultAllocator {
@@ -1683,7 +1696,7 @@ typedef int EXT_SIPHASH_2_4_can_only_be_used_in_64_bit_builds[sizeof(size_t) ==
16831696
// do..while(0) and sizeof()==
16841697
#endif
16851698

1686-
static size_t ext_siphash_bytes_(const void *p, size_t len, size_t seed) {
1699+
static inline size_t ext_siphash_bytes_(const void *p, size_t len, size_t seed) {
16871700
unsigned char *d = (unsigned char *)p;
16881701
size_t i, j;
16891702
size_t v0, v1, v2, v3, data;
@@ -1905,21 +1918,12 @@ extern inline void *ext_alloc(size_t size);
19051918
extern inline void *ext_realloc(void *ptr, size_t old_sz, size_t new_sz);
19061919
extern inline void ext_free(void *ptr, size_t size);
19071920

1921+
extern inline char *ext_allocator_strdup(Ext_Allocator *a, const char *s);
1922+
extern inline void *ext_allocator_memdup(Ext_Allocator *a, const void *mem, size_t size);
1923+
19081924
extern inline char *ext_strdup(const char *s);
19091925
extern inline void *ext_memdup(const void *mem, size_t size);
19101926

1911-
char *ext_allocator_strdup(Ext_Allocator *a, const char *s) {
1912-
size_t len = strlen(s);
1913-
char *res = a->alloc(a, len + 1);
1914-
memcpy(res, s, len);
1915-
res[len] = '\0';
1916-
return res;
1917-
}
1918-
1919-
void *ext_allocator_memdup(Ext_Allocator *a, const void *mem, size_t size) {
1920-
return memcpy(a->alloc(a, size), mem, size);
1921-
}
1922-
19231927
#ifdef EXTLIB_WASM
19241928
extern char __heap_base[];
19251929
static void *ext_heap_start = (void *)__heap_base;

0 commit comments

Comments
 (0)