mirror of
https://github.com/fluencelabs/redis
synced 2025-04-01 15:21:03 +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 <pthread.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "zmalloc.h"
|
#include "zmalloc.h"
|
||||||
|
#include "atomicvar.h"
|
||||||
|
|
||||||
#ifdef HAVE_MALLOC_SIZE
|
#ifdef HAVE_MALLOC_SIZE
|
||||||
#define PREFIX_SIZE (0)
|
#define PREFIX_SIZE (0)
|
||||||
@ -67,32 +68,11 @@ void zlibc_free(void *ptr) {
|
|||||||
#define free(ptr) je_free(ptr)
|
#define free(ptr) je_free(ptr)
|
||||||
#endif
|
#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 { \
|
#define update_zmalloc_stat_alloc(__n) do { \
|
||||||
size_t _n = (__n); \
|
size_t _n = (__n); \
|
||||||
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
|
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
|
||||||
if (zmalloc_thread_safe) { \
|
if (zmalloc_thread_safe) { \
|
||||||
update_zmalloc_stat_add(_n); \
|
atomicIncr(used_memory,__n,&used_memory_mutex); \
|
||||||
} else { \
|
} else { \
|
||||||
used_memory += _n; \
|
used_memory += _n; \
|
||||||
} \
|
} \
|
||||||
@ -102,7 +82,7 @@ void zlibc_free(void *ptr) {
|
|||||||
size_t _n = (__n); \
|
size_t _n = (__n); \
|
||||||
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
|
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
|
||||||
if (zmalloc_thread_safe) { \
|
if (zmalloc_thread_safe) { \
|
||||||
update_zmalloc_stat_sub(_n); \
|
atomicDecr(used_memory,__n,&used_memory_mutex); \
|
||||||
} else { \
|
} else { \
|
||||||
used_memory -= _n; \
|
used_memory -= _n; \
|
||||||
} \
|
} \
|
||||||
@ -222,18 +202,10 @@ size_t zmalloc_used_memory(void) {
|
|||||||
size_t um;
|
size_t um;
|
||||||
|
|
||||||
if (zmalloc_thread_safe) {
|
if (zmalloc_thread_safe) {
|
||||||
#if defined(__ATOMIC_RELAXED) || defined(HAVE_ATOMIC)
|
atomicGet(used_memory,um,&used_memory_mutex);
|
||||||
um = update_zmalloc_stat_add(0);
|
} else {
|
||||||
#else
|
|
||||||
pthread_mutex_lock(&used_memory_mutex);
|
|
||||||
um = used_memory;
|
|
||||||
pthread_mutex_unlock(&used_memory_mutex);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
um = used_memory;
|
um = used_memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
return um;
|
return um;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user