macro define in zmalloc.c:
#define update_zmalloc_stat_alloc(__n) do { \
size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
atomicIncr(used_memory,__n); \
} while(0)
I make a silly test for the above code:
int used_memory = 0;
#define update_zmalloc_stat_alloc(__n) do { \
size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
used_memory += __n; \
} while(0)
int main() {
update_zmalloc_stat_alloc(3);
printf("%d\n", used_memory);
update_zmalloc_stat_alloc(9);
printf("%d\n", used_memory);
}
the output is:
3
12
Further more, if I remove the two lines of codes which related to variable _n, I still got 3 12.
#define update_zmalloc_stat_alloc(__n) do { \
used_memory += __n; \
} while(0)
...so, why compute the variable _n ? Is it a Bug? Did I misunderstood anything ? (so confused)
also, the used_memory_mutex variable is not used.
Comment From: jonahharris
It's accounting for possible alignment padding of sizeof(long) within the underlying allocator. Also, used_memory_mutex is utilized by the atomicX macros in the case where CAS isn't available and pthreads is used (see atomicvar.h).
Suggest to close @antirez
Comment From: liuhui-hust
Thank you for your answer @jonahharris but I'm still wondering why the function atomicIncr doesn't use the variable _n?
Comment From: jonahharris
@liuhui-hust noted! I totally misread that. You're correct. Also, it appears to no longer be needed as zmalloc_size() is used to calculate the allocation size and takes padding into account in all cases where that would have been needed. Submitting a patch.
Comment From: oranagra
Thank you for reporting. indeed is just just some leftover dead code. trimmed in https://github.com/redis/redis/commit/50f5181488d9c3a5557a0b32249a4acdd70684ce