Skip to content

Commit c7b46a4

Browse files
committed
zmalloc.c converted to use atomicvar.h.
1 parent 7e5d690 commit c7b46a4

File tree

1 file changed

+5
-33
lines changed

1 file changed

+5
-33
lines changed

src/zmalloc.c

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void zlibc_free(void *ptr) {
4343
#include <pthread.h>
4444
#include "config.h"
4545
#include "zmalloc.h"
46+
#include "atomicvar.h"
4647

4748
#ifdef HAVE_MALLOC_SIZE
4849
#define PREFIX_SIZE (0)
@@ -67,32 +68,11 @@ void zlibc_free(void *ptr) {
6768
#define free(ptr) je_free(ptr)
6869
#endif
6970

70-
#if defined(__ATOMIC_RELAXED)
71-
#define update_zmalloc_stat_add(__n) __atomic_add_fetch(&used_memory, (__n), __ATOMIC_RELAXED)
72-
#define update_zmalloc_stat_sub(__n) __atomic_sub_fetch(&used_memory, (__n), __ATOMIC_RELAXED)
73-
#elif defined(HAVE_ATOMIC)
74-
#define update_zmalloc_stat_add(__n) __sync_add_and_fetch(&used_memory, (__n))
75-
#define update_zmalloc_stat_sub(__n) __sync_sub_and_fetch(&used_memory, (__n))
76-
#else
77-
#define update_zmalloc_stat_add(__n) do { \
78-
pthread_mutex_lock(&used_memory_mutex); \
79-
used_memory += (__n); \
80-
pthread_mutex_unlock(&used_memory_mutex); \
81-
} while(0)
82-
83-
#define update_zmalloc_stat_sub(__n) do { \
84-
pthread_mutex_lock(&used_memory_mutex); \
85-
used_memory -= (__n); \
86-
pthread_mutex_unlock(&used_memory_mutex); \
87-
} while(0)
88-
89-
#endif
90-
9171
#define update_zmalloc_stat_alloc(__n) do { \
9272
size_t _n = (__n); \
9373
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
9474
if (zmalloc_thread_safe) { \
95-
update_zmalloc_stat_add(_n); \
75+
atomicIncr(used_memory,__n,&used_memory_mutex); \
9676
} else { \
9777
used_memory += _n; \
9878
} \
@@ -102,7 +82,7 @@ void zlibc_free(void *ptr) {
10282
size_t _n = (__n); \
10383
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
10484
if (zmalloc_thread_safe) { \
105-
update_zmalloc_stat_sub(_n); \
85+
atomicDecr(used_memory,__n,&used_memory_mutex); \
10686
} else { \
10787
used_memory -= _n; \
10888
} \
@@ -222,18 +202,10 @@ size_t zmalloc_used_memory(void) {
222202
size_t um;
223203

224204
if (zmalloc_thread_safe) {
225-
#if defined(__ATOMIC_RELAXED) || defined(HAVE_ATOMIC)
226-
um = update_zmalloc_stat_add(0);
227-
#else
228-
pthread_mutex_lock(&used_memory_mutex);
229-
um = used_memory;
230-
pthread_mutex_unlock(&used_memory_mutex);
231-
#endif
232-
}
233-
else {
205+
atomicGet(used_memory,um,&used_memory_mutex);
206+
} else {
234207
um = used_memory;
235208
}
236-
237209
return um;
238210
}
239211

0 commit comments

Comments
 (0)