Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
ffd6eaeb
Commit
ffd6eaeb
authored
May 30, 2020
by
michael-grunder
Browse files
Merge branch 'master' into new-ssl-api
parents
190bca88
e553e0f3
Changes
4
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
ffd6eaeb
...
...
@@ -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
Hiredis was written by Salvatore Sanfilippo (antirez at gmail) and
...
...
alloc.c
View file @
ffd6eaeb
...
...
@@ -34,11 +34,11 @@
#include <stdlib.h>
hiredisAllocFuncs
hiredisAllocFns
=
{
.
malloc
=
malloc
,
.
calloc
=
calloc
,
.
realloc
=
realloc
,
.
strdup
=
strdup
,
.
free
=
free
,
.
malloc
Fn
=
malloc
,
.
calloc
Fn
=
calloc
,
.
realloc
Fn
=
realloc
,
.
strdup
Fn
=
strdup
,
.
free
Fn
=
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
,
.
malloc
Fn
=
malloc
,
.
calloc
Fn
=
calloc
,
.
realloc
Fn
=
realloc
,
.
strdup
Fn
=
strdup
,
.
free
Fn
=
free
,
};
}
#ifdef _WIN32
void
*
hi_malloc
(
size_t
size
)
{
return
hiredisAllocFns
.
malloc
(
size
);
return
hiredisAllocFns
.
malloc
Fn
(
size
);
}
void
*
hi_calloc
(
size_t
nmemb
,
size_t
size
)
{
return
hiredisAllocFns
.
calloc
(
nmemb
,
size
);
return
hiredisAllocFns
.
calloc
Fn
(
nmemb
,
size
);
}
void
*
hi_realloc
(
void
*
ptr
,
size_t
size
)
{
return
hiredisAllocFns
.
realloc
(
ptr
,
size
);
return
hiredisAllocFns
.
realloc
Fn
(
ptr
,
size
);
}
char
*
hi_strdup
(
const
char
*
str
)
{
return
hiredisAllocFns
.
strdup
(
str
);
return
hiredisAllocFns
.
strdup
Fn
(
str
);
}
void
hi_free
(
void
*
ptr
)
{
hiredisAllocFns
.
free
(
ptr
);
hiredisAllocFns
.
free
Fn
(
ptr
);
}
#endif
alloc.h
View file @
ffd6eaeb
...
...
@@ -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
*
(
*
malloc
Fn
)(
size_t
);
void
*
(
*
calloc
Fn
)(
size_t
,
size_t
);
void
*
(
*
realloc
Fn
)(
void
*
,
size_t
);
char
*
(
*
strdup
Fn
)(
const
char
*
);
void
(
*
free
Fn
)(
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
.
malloc
Fn
(
size
);
}
static
inline
void
*
hi_calloc
(
size_t
nmemb
,
size_t
size
)
{
return
hiredisAllocFns
.
calloc
(
nmemb
,
size
);
return
hiredisAllocFns
.
calloc
Fn
(
nmemb
,
size
);
}
static
inline
void
*
hi_realloc
(
void
*
ptr
,
size_t
size
)
{
return
hiredisAllocFns
.
realloc
(
ptr
,
size
);
return
hiredisAllocFns
.
realloc
Fn
(
ptr
,
size
);
}
static
inline
char
*
hi_strdup
(
const
char
*
str
)
{
return
hiredisAllocFns
.
strdup
(
str
);
return
hiredisAllocFns
.
strdup
Fn
(
str
);
}
static
inline
void
hi_free
(
void
*
ptr
)
{
hiredisAllocFns
.
free
(
ptr
);
hiredisAllocFns
.
free
Fn
(
ptr
);
}
#else
...
...
test.c
View file @
ffd6eaeb
...
...
@@ -550,10 +550,11 @@ 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
,
.
strdupFn
=
strdup
,
.
freeFn
=
free
,
};
// Override hiredis allocators
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment