Skip to content

Commit c811c06

Browse files
committed
Workaround for mruby/c's allocator issue
1 parent 6fbdd00 commit c811c06

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

include/prism_xallocator.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,31 @@
2222
#else
2323
#define xmalloc(size) mrbc_raw_alloc(size)
2424
#define xcalloc(nmemb,size) mrbc_raw_calloc(nmemb, size)
25-
#define xrealloc(ptr,size) mrbc_raw_realloc(ptr, size)
26-
#define xfree(ptr) mrbc_raw_free(ptr)
25+
#define xrealloc(nmemb,size) mrc_raw_realloc(nmemb, size)
26+
#define xfree(ptr) mrc_raw_free(ptr)
2727

2828
#define mrc_malloc(c,size) mrbc_raw_alloc(size)
2929
#define mrc_calloc(c,nmemb,size) mrbc_raw_calloc(nmemb, size)
30-
#define mrc_realloc(c,ptr,size) mrbc_raw_realloc(ptr, size)
31-
#define mrc_free(c,ptr) mrbc_raw_free(ptr)
30+
#define mrc_realloc(c,ptr,size) mrc_raw_realloc(ptr, size)
31+
#define mrc_free(c,ptr) mrc_raw_free(ptr)
32+
33+
static inline void mrc_raw_free(void *ptr)
34+
{
35+
/* mrbc_raw_free() warns when ptr=NULL but it should be allowed in C99 */
36+
if (ptr == NULL) return;
37+
mrbc_raw_free(ptr);
38+
}
39+
40+
static inline void*
41+
mrc_raw_realloc(void *ptr, unsigned int size)
42+
{
43+
/* mrbc_raw_realloc() fails when ptr=NULL but it should be allowed in C99 */
44+
if (ptr == NULL) {
45+
return mrbc_raw_alloc(size);
46+
} else {
47+
return mrbc_raw_realloc(ptr, size);
48+
}
49+
}
3250
#endif
3351
#else
3452

0 commit comments

Comments
 (0)