From f5d258504333d5598428853b7293e26814f65acf Mon Sep 17 00:00:00 2001 From: Michael Grunder Date: Mon, 25 May 2020 12:17:43 -0700 Subject: Use unique names for allocator struct members (#823) Using `strdup` as a struct member causes issues in older gcc --- alloc.c | 30 +++++++++++++++--------------- alloc.h | 20 ++++++++++---------- test.c | 8 ++++---- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/alloc.c b/alloc.c index 2b19c48..7fb6b35 100644 --- a/alloc.c +++ b/alloc.c @@ -34,11 +34,11 @@ #include hiredisAllocFuncs hiredisAllocFns = { - .malloc = malloc, - .calloc = calloc, - .realloc = realloc, - .strdup = strdup, - .free = free, + .mallocFn = malloc, + .callocFn = calloc, + .reallocFn = realloc, + .strdupFn = strdup, + .freeFn = free, }; /* Override hiredis' allocators with ones supplied by the user */ @@ -53,34 +53,34 @@ hiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *override) { /* Reset allocators to use libc defaults */ void hiredisResetAllocators(void) { hiredisAllocFns = (hiredisAllocFuncs) { - .malloc = malloc, - .calloc = calloc, - .realloc = realloc, - .strdup = strdup, - .free = free, + .mallocFn = malloc, + .callocFn = calloc, + .reallocFn = realloc, + .strdupFn = strdup, + .freeFn = free, }; } #ifdef _WIN32 void *hi_malloc(size_t size) { - return hiredisAllocFns.malloc(size); + return hiredisAllocFns.mallocFn(size); } void *hi_calloc(size_t nmemb, size_t size) { - return hiredisAllocFns.calloc(nmemb, size); + return hiredisAllocFns.callocFn(nmemb, size); } void *hi_realloc(void *ptr, size_t size) { - return hiredisAllocFns.realloc(ptr, size); + return hiredisAllocFns.reallocFn(ptr, size); } char *hi_strdup(const char *str) { - return hiredisAllocFns.strdup(str); + return hiredisAllocFns.strdupFn(str); } void hi_free(void *ptr) { - hiredisAllocFns.free(ptr); + hiredisAllocFns.freeFn(ptr); } #endif diff --git a/alloc.h b/alloc.h index b581e56..34a05f4 100644 --- a/alloc.h +++ b/alloc.h @@ -39,11 +39,11 @@ extern "C" { /* Structure pointing to our actually configured allocators */ typedef struct hiredisAllocFuncs { - void *(*malloc)(size_t); - void *(*calloc)(size_t,size_t); - void *(*realloc)(void*,size_t); - char *(*strdup)(const char*); - void (*free)(void*); + void *(*mallocFn)(size_t); + void *(*callocFn)(size_t,size_t); + void *(*reallocFn)(void*,size_t); + char *(*strdupFn)(const char*); + void (*freeFn)(void*); } hiredisAllocFuncs; hiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *ha); @@ -55,23 +55,23 @@ void hiredisResetAllocators(void); extern hiredisAllocFuncs hiredisAllocFns; static inline void *hi_malloc(size_t size) { - return hiredisAllocFns.malloc(size); + return hiredisAllocFns.mallocFn(size); } static inline void *hi_calloc(size_t nmemb, size_t size) { - return hiredisAllocFns.calloc(nmemb, size); + return hiredisAllocFns.callocFn(nmemb, size); } static inline void *hi_realloc(void *ptr, size_t size) { - return hiredisAllocFns.realloc(ptr, size); + return hiredisAllocFns.reallocFn(ptr, size); } static inline char *hi_strdup(const char *str) { - return hiredisAllocFns.strdup(str); + return hiredisAllocFns.strdupFn(str); } static inline void hi_free(void *ptr) { - hiredisAllocFns.free(ptr); + hiredisAllocFns.freeFn(ptr); } #else diff --git a/test.c b/test.c index 48d36d0..56ba9d6 100644 --- a/test.c +++ b/test.c @@ -547,10 +547,10 @@ static void *hi_realloc_fail(void *ptr, size_t size) { static void test_allocator_injection(void) { hiredisAllocFuncs ha = { - .malloc = hi_malloc_fail, - .calloc = hi_calloc_fail, - .realloc = hi_realloc_fail, - .free = NULL, + .mallocFn = hi_malloc_fail, + .callocFn = hi_calloc_fail, + .reallocFn = hi_realloc_fail, + .freeFn = NULL, }; // Override hiredis allocators -- cgit v1.2.3 From e553e0f382394ea055d0b6b7637ab2534cf38146 Mon Sep 17 00:00:00 2001 From: Michael Grunder Date: Tue, 26 May 2020 10:06:28 -0700 Subject: Document allocator injection and completeness fix in test.c (#824) --- README.md | 30 ++++++++++++++++++++++++++++++ test.c | 3 ++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4e7b228..cb40ba9 100644 --- a/README.md +++ b/README.md @@ -474,6 +474,36 @@ Instead, use `redisInitiateSSL()` which also provides greater control over the configuration of the SSL connection, as the caller is responsible to create a connection context using `SSL_new()` and configure it as required. +## Allocator injection + +Hiredis uses a pass-thru structure of function pointers defined in +[alloc.h](https://github.com/redis/hiredis/blob/f5d25850/alloc.h#L41) that conttain +the currently configured allocation and deallocation functions. By default they +just point to libc (`malloc`, `calloc`, `realloc`, etc). + +### Overriding + +One can override the allocators like so: + +```c +hiredisAllocFuncs myfuncs = { + .mallocFn = my_malloc, + .callocFn = my_calloc, + .reallocFn = my_realloc, + .strdupFn = my_strdup, + .freeFn = my_free, +}; + +// Override allocators (function returns current allocators if needed) +hiredisAllocFuncs orig = hiredisSetAllocators(&myfuncs); +``` + +To reset the allocators to their default libc function simply call: + +```c +hiredisResetAllocators(); +``` + ## AUTHORS Hiredis was written by Salvatore Sanfilippo (antirez at gmail) and diff --git a/test.c b/test.c index 56ba9d6..8cc30f3 100644 --- a/test.c +++ b/test.c @@ -550,7 +550,8 @@ static void test_allocator_injection(void) { .mallocFn = hi_malloc_fail, .callocFn = hi_calloc_fail, .reallocFn = hi_realloc_fail, - .freeFn = NULL, + .strdupFn = strdup, + .freeFn = free, }; // Override hiredis allocators -- cgit v1.2.3