hackpar / redis

Automatically exported from code.google.com/p/redis
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Is it a little waste of memory in Mac OSX? #452

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
redis-2.0.4  in file zmalloc.c, function:

void *zmalloc(size_t size) {
    void *ptr = malloc(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
}

When in the Mac OSX having the malloc_size. every malloc waste at least 
PREFIX_SIZE bytes? or is it for some particular reasons?

Maybe it is better like this :)

void *zmalloc(size_t size) {
#ifdef HAVE_MALLOC_SIZE
    void *ptr = malloc(size);
    if (!ptr) zmalloc_oom(size);
    increment_used_memory(redis_malloc_size(ptr));
    return ptr;
#else
    void *ptr = malloc(size+PREFIX_SIZE);
    if (!ptr) zmalloc_oom(size);
    *((size_t*)ptr) = size;
    increment_used_memory(size+PREFIX_SIZE);
    return (char*)ptr+PREFIX_SIZE;
#endif
}

And in function zrealloc I noticed when defined the macro HAVE_MALLOC_SIZE.

    oldsize = redis_malloc_size(ptr);
    newptr = realloc(ptr,size);

here, realloc not add the extra PREFIX_SIZE bytes.

Original issue reported on code.google.com by InsFocus on 12 Feb 2011 at 6:49

GoogleCodeExporter commented 8 years ago
Yes, this was an error causing a little more memory to be used on OSX. This has 
been fixed in 2.2, more specifically: 
https://github.com/antirez/redis/commit/7cdc98b6#L1R38 . Here, PREFIX_SIZE is 
set to 0 when malloc_size() is available (either when compiled on OSX or when 
tcmalloc is used).

Original comment by pcnoordh...@gmail.com on 13 Feb 2011 at 12:06