summaryrefslogtreecommitdiff
path: root/src/zmalloc.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-09-25 15:34:16 +0200
committerantirez <antirez@gmail.com>2015-10-01 13:02:25 +0200
commitc7b46a471996e45a464a2b52d6cdc81487f19a05 (patch)
tree6619a866aeb9c1ebe3b966928e3ad8fd79708dee /src/zmalloc.c
parent7e5d69019e233d306accce19611f68c92ff45da8 (diff)
downloadredis-c7b46a471996e45a464a2b52d6cdc81487f19a05.tar.gz
zmalloc.c converted to use atomicvar.h.
Diffstat (limited to 'src/zmalloc.c')
-rw-r--r--src/zmalloc.c38
1 files changed, 5 insertions, 33 deletions
diff --git a/src/zmalloc.c b/src/zmalloc.c
index 640ee19e2..da62447e1 100644
--- a/src/zmalloc.c
+++ b/src/zmalloc.c
@@ -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;
}