mirror of
https://github.com/fluencelabs/redis
synced 2025-03-17 16:10:50 +00:00
zmalloc.c converted to use atomicvar.h.
This commit is contained in:
parent
7e5d69019e
commit
c7b46a4719
@ -43,6 +43,7 @@ void zlibc_free(void *ptr) {
|
||||
#include <pthread.h>
|
||||
#include "config.h"
|
||||
#include "zmalloc.h"
|
||||
#include "atomicvar.h"
|
||||
|
||||
#ifdef HAVE_MALLOC_SIZE
|
||||
#define PREFIX_SIZE (0)
|
||||
@ -67,32 +68,11 @@ void zlibc_free(void *ptr) {
|
||||
#define free(ptr) je_free(ptr)
|
||||
#endif
|
||||
|
||||
#if defined(__ATOMIC_RELAXED)
|
||||
#define update_zmalloc_stat_add(__n) __atomic_add_fetch(&used_memory, (__n), __ATOMIC_RELAXED)
|
||||
#define update_zmalloc_stat_sub(__n) __atomic_sub_fetch(&used_memory, (__n), __ATOMIC_RELAXED)
|
||||
#elif defined(HAVE_ATOMIC)
|
||||
#define update_zmalloc_stat_add(__n) __sync_add_and_fetch(&used_memory, (__n))
|
||||
#define update_zmalloc_stat_sub(__n) __sync_sub_and_fetch(&used_memory, (__n))
|
||||
#else
|
||||
#define update_zmalloc_stat_add(__n) do { \
|
||||
pthread_mutex_lock(&used_memory_mutex); \
|
||||
used_memory += (__n); \
|
||||
pthread_mutex_unlock(&used_memory_mutex); \
|
||||
} while(0)
|
||||
|
||||
#define update_zmalloc_stat_sub(__n) do { \
|
||||
pthread_mutex_lock(&used_memory_mutex); \
|
||||
used_memory -= (__n); \
|
||||
pthread_mutex_unlock(&used_memory_mutex); \
|
||||
} while(0)
|
||||
|
||||
#endif
|
||||
|
||||
#define update_zmalloc_stat_alloc(__n) do { \
|
||||
size_t _n = (__n); \
|
||||
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
|
||||
if (zmalloc_thread_safe) { \
|
||||
update_zmalloc_stat_add(_n); \
|
||||
atomicIncr(used_memory,__n,&used_memory_mutex); \
|
||||
} else { \
|
||||
used_memory += _n; \
|
||||
} \
|
||||
@ -102,7 +82,7 @@ void zlibc_free(void *ptr) {
|
||||
size_t _n = (__n); \
|
||||
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
|
||||
if (zmalloc_thread_safe) { \
|
||||
update_zmalloc_stat_sub(_n); \
|
||||
atomicDecr(used_memory,__n,&used_memory_mutex); \
|
||||
} else { \
|
||||
used_memory -= _n; \
|
||||
} \
|
||||
@ -222,18 +202,10 @@ size_t zmalloc_used_memory(void) {
|
||||
size_t um;
|
||||
|
||||
if (zmalloc_thread_safe) {
|
||||
#if defined(__ATOMIC_RELAXED) || defined(HAVE_ATOMIC)
|
||||
um = update_zmalloc_stat_add(0);
|
||||
#else
|
||||
pthread_mutex_lock(&used_memory_mutex);
|
||||
um = used_memory;
|
||||
pthread_mutex_unlock(&used_memory_mutex);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
atomicGet(used_memory,um,&used_memory_mutex);
|
||||
} else {
|
||||
um = used_memory;
|
||||
}
|
||||
|
||||
return um;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user