summaryrefslogtreecommitdiff
path: root/src/zmalloc.c
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@gmail.com>2010-07-24 23:20:00 +0200
committerBenjamin Kramer <benny.kra@gmail.com>2010-07-25 00:11:20 +0200
commit399f2f401c6fc3d489e2e40ffc78638425e3a09e (patch)
treef42d4b332351e17c4ccede5a767a5346329b4b63 /src/zmalloc.c
parentd9dd352b3693e1ad8ab2e0993b4f0a275d7fd512 (diff)
downloadredis-399f2f401c6fc3d489e2e40ffc78638425e3a09e.tar.gz
Add zcalloc and use it where appropriate
calloc is more effecient than malloc+memset when the system uses mmap to allocate memory. mmap always returns zeroed memory so the memset can be avoided. The threshold to use mmap is 16k in osx libc and 128k in bsd libc and glibc. The kernel can lazily allocate the pages, this reduces memory usage when we have a page table or hash table that is mostly empty. This change is most visible when you start a new redis instance with vm enabled. You'll see no increased memory usage no matter how big your page table is.
Diffstat (limited to 'src/zmalloc.c')
-rw-r--r--src/zmalloc.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/zmalloc.c b/src/zmalloc.c
index 8658376a3..5c1b5e9aa 100644
--- a/src/zmalloc.c
+++ b/src/zmalloc.c
@@ -89,6 +89,20 @@ void *zmalloc(size_t size) {
#endif
}
+void *zcalloc(size_t size) {
+ void *ptr = calloc(1, size+PREFIX_SIZE);
+
+ if (!ptr) zmalloc_oom(size);
+#ifdef HAVE_MALLOC_SIZE
+ increment_used_memory(redis_malloc_size(ptr));
+ return ptr;
+#else
+ *((size_t*)ptr) = size;
+ increment_used_memory(size+PREFIX_SIZE);
+ return (char*)ptr+PREFIX_SIZE;
+#endif
+}
+
void *zrealloc(void *ptr, size_t size) {
#ifndef HAVE_MALLOC_SIZE
void *realptr;