Merge branch 'master' into new-ssl-api

This commit is contained in:
michael-grunder 2020-05-30 09:30:01 -07:00
commit ffd6eaebd6
4 changed files with 60 additions and 29 deletions

View File

@ -500,6 +500,36 @@ if (redisInitiateSSLWithContext(c, ssl) != REDIS_OK) {
} }
``` ```
## 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 ## AUTHORS
Hiredis was written by Salvatore Sanfilippo (antirez at gmail) and Hiredis was written by Salvatore Sanfilippo (antirez at gmail) and

30
alloc.c
View File

@ -34,11 +34,11 @@
#include <stdlib.h> #include <stdlib.h>
hiredisAllocFuncs hiredisAllocFns = { hiredisAllocFuncs hiredisAllocFns = {
.malloc = malloc, .mallocFn = malloc,
.calloc = calloc, .callocFn = calloc,
.realloc = realloc, .reallocFn = realloc,
.strdup = strdup, .strdupFn = strdup,
.free = free, .freeFn = free,
}; };
/* Override hiredis' allocators with ones supplied by the user */ /* Override hiredis' allocators with ones supplied by the user */
@ -53,34 +53,34 @@ hiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *override) {
/* Reset allocators to use libc defaults */ /* Reset allocators to use libc defaults */
void hiredisResetAllocators(void) { void hiredisResetAllocators(void) {
hiredisAllocFns = (hiredisAllocFuncs) { hiredisAllocFns = (hiredisAllocFuncs) {
.malloc = malloc, .mallocFn = malloc,
.calloc = calloc, .callocFn = calloc,
.realloc = realloc, .reallocFn = realloc,
.strdup = strdup, .strdupFn = strdup,
.free = free, .freeFn = free,
}; };
} }
#ifdef _WIN32 #ifdef _WIN32
void *hi_malloc(size_t size) { void *hi_malloc(size_t size) {
return hiredisAllocFns.malloc(size); return hiredisAllocFns.mallocFn(size);
} }
void *hi_calloc(size_t nmemb, size_t 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) { void *hi_realloc(void *ptr, size_t size) {
return hiredisAllocFns.realloc(ptr, size); return hiredisAllocFns.reallocFn(ptr, size);
} }
char *hi_strdup(const char *str) { char *hi_strdup(const char *str) {
return hiredisAllocFns.strdup(str); return hiredisAllocFns.strdupFn(str);
} }
void hi_free(void *ptr) { void hi_free(void *ptr) {
hiredisAllocFns.free(ptr); hiredisAllocFns.freeFn(ptr);
} }
#endif #endif

20
alloc.h
View File

@ -39,11 +39,11 @@ extern "C" {
/* Structure pointing to our actually configured allocators */ /* Structure pointing to our actually configured allocators */
typedef struct hiredisAllocFuncs { typedef struct hiredisAllocFuncs {
void *(*malloc)(size_t); void *(*mallocFn)(size_t);
void *(*calloc)(size_t,size_t); void *(*callocFn)(size_t,size_t);
void *(*realloc)(void*,size_t); void *(*reallocFn)(void*,size_t);
char *(*strdup)(const char*); char *(*strdupFn)(const char*);
void (*free)(void*); void (*freeFn)(void*);
} hiredisAllocFuncs; } hiredisAllocFuncs;
hiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *ha); hiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *ha);
@ -55,23 +55,23 @@ void hiredisResetAllocators(void);
extern hiredisAllocFuncs hiredisAllocFns; extern hiredisAllocFuncs hiredisAllocFns;
static inline void *hi_malloc(size_t size) { 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) { 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) { 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) { static inline char *hi_strdup(const char *str) {
return hiredisAllocFns.strdup(str); return hiredisAllocFns.strdupFn(str);
} }
static inline void hi_free(void *ptr) { static inline void hi_free(void *ptr) {
hiredisAllocFns.free(ptr); hiredisAllocFns.freeFn(ptr);
} }
#else #else

9
test.c
View File

@ -550,10 +550,11 @@ static void *hi_realloc_fail(void *ptr, size_t size) {
static void test_allocator_injection(void) { static void test_allocator_injection(void) {
hiredisAllocFuncs ha = { hiredisAllocFuncs ha = {
.malloc = hi_malloc_fail, .mallocFn = hi_malloc_fail,
.calloc = hi_calloc_fail, .callocFn = hi_calloc_fail,
.realloc = hi_realloc_fail, .reallocFn = hi_realloc_fail,
.free = NULL, .strdupFn = strdup,
.freeFn = free,
}; };
// Override hiredis allocators // Override hiredis allocators