Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
c7b46a47
Commit
c7b46a47
authored
Sep 25, 2015
by
antirez
Browse files
zmalloc.c converted to use atomicvar.h.
parent
7e5d6901
Changes
1
Show whitespace changes
Inline
Side-by-side
src/zmalloc.c
View file @
c7b46a47
...
@@ -43,6 +43,7 @@ void zlibc_free(void *ptr) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment