summaryrefslogtreecommitdiff
path: root/src/zmalloc.c
diff options
context:
space:
mode:
authorYossi Gottlieb <yossigo@gmail.com>2021-02-25 09:24:41 +0200
committerGitHub <noreply@github.com>2021-02-25 09:24:41 +0200
commit3ea4c43add4f6038830ae0a6e42821481f7ce9a0 (patch)
treeef03d4592465ced7da63445374e141ce017ba5b5 /src/zmalloc.c
parentc6f0ea2c81ffd5452dc6373d79786fdec8874b20 (diff)
downloadredis-3ea4c43add4f6038830ae0a6e42821481f7ce9a0.tar.gz
Cleanup usage of malloc_usable_size. (#8554)
* Add better control of malloc_usable_size() usage. * Use malloc_usable_size on alpine libc daily job. * Add no-malloc-usable-size daily jobs. * Fix zmalloc(0) when HAVE_MALLOC_SIZE is undefined. In order to align with the jemalloc behavior, this should never return NULL or OOM panic.
Diffstat (limited to 'src/zmalloc.c')
-rw-r--r--src/zmalloc.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/zmalloc.c b/src/zmalloc.c
index fbac09616..1dc662e57 100644
--- a/src/zmalloc.c
+++ b/src/zmalloc.c
@@ -60,6 +60,11 @@ void zlibc_free(void *ptr) {
#define ASSERT_NO_SIZE_OVERFLOW(sz) assert((sz) + PREFIX_SIZE > (sz))
#endif
+/* When using the libc allocator, use a minimum allocation size to match the
+ * jemalloc behavior that doesn't return NULL in this case.
+ */
+#define MALLOC_MIN_SIZE(x) ((x) > 0 ? (x) : sizeof(long))
+
/* Explicitly override malloc/free etc when using tcmalloc. */
#if defined(USE_TCMALLOC)
#define malloc(size) tc_malloc(size)
@@ -93,7 +98,7 @@ static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom;
* '*usable' is set to the usable size if non NULL. */
void *ztrymalloc_usable(size_t size, size_t *usable) {
ASSERT_NO_SIZE_OVERFLOW(size);
- void *ptr = malloc(size+PREFIX_SIZE);
+ void *ptr = malloc(MALLOC_MIN_SIZE(size)+PREFIX_SIZE);
if (!ptr) return NULL;
#ifdef HAVE_MALLOC_SIZE
@@ -153,7 +158,7 @@ void zfree_no_tcache(void *ptr) {
* '*usable' is set to the usable size if non NULL. */
void *ztrycalloc_usable(size_t size, size_t *usable) {
ASSERT_NO_SIZE_OVERFLOW(size);
- void *ptr = calloc(1, size+PREFIX_SIZE);
+ void *ptr = calloc(1, MALLOC_MIN_SIZE(size)+PREFIX_SIZE);
if (ptr == NULL) return NULL;
#ifdef HAVE_MALLOC_SIZE
@@ -675,6 +680,7 @@ int zmalloc_test(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);
+ printf("Malloc prefix size: %d\n", (int) PREFIX_SIZE);
printf("Initial used memory: %zu\n", zmalloc_used_memory());
ptr = zmalloc(123);
printf("Allocated 123 bytes; used: %zu\n", zmalloc_used_memory());