31
31
#include <stdlib.h>
32
32
#include <string.h>
33
33
34
+ #ifdef __APPLE__
35
+ #include <malloc/malloc.h>
36
+ #define HAVE_MALLOC_SIZE
37
+ #define redis_malloc_size (p ) malloc_size(p)
38
+ #endif
39
+
34
40
static size_t used_memory = 0 ;
35
41
36
42
void * zmalloc (size_t size ) {
37
43
void * ptr = malloc (size + sizeof (size_t ));
38
44
39
45
if (!ptr ) return NULL ;
46
+ #ifdef HAVE_MALLOC_SIZE
47
+ used_memory += redis_malloc_size (ptr );
48
+ return ptr ;
49
+ #else
40
50
* ((size_t * )ptr ) = size ;
41
51
used_memory += size + sizeof (size_t );
42
52
return (char * )ptr + sizeof (size_t );
53
+ #endif
43
54
}
44
55
45
56
void * zrealloc (void * ptr , size_t size ) {
57
+ #ifndef HAVE_MALLOC_SIZE
46
58
void * realptr ;
59
+ #endif
47
60
size_t oldsize ;
48
61
void * newptr ;
49
62
50
63
if (ptr == NULL ) return zmalloc (size );
64
+ #ifdef HAVE_MALLOC_SIZE
65
+ oldsize = redis_malloc_size (ptr );
66
+ newptr = realloc (ptr ,size );
67
+ if (!newptr ) return NULL ;
68
+
69
+ used_memory -= oldsize ;
70
+ used_memory += redis_malloc_size (newptr );
71
+ return newptr ;
72
+ #else
51
73
realptr = (char * )ptr - sizeof (size_t );
52
74
oldsize = * ((size_t * )realptr );
53
75
newptr = realloc (realptr ,size + sizeof (size_t ));
@@ -57,17 +79,25 @@ void *zrealloc(void *ptr, size_t size) {
57
79
used_memory -= oldsize ;
58
80
used_memory += size ;
59
81
return (char * )newptr + sizeof (size_t );
82
+ #endif
60
83
}
61
84
62
85
void zfree (void * ptr ) {
86
+ #ifndef HAVE_MALLOC_SIZE
63
87
void * realptr ;
64
88
size_t oldsize ;
89
+ #endif
65
90
66
91
if (ptr == NULL ) return ;
92
+ #ifdef HAVE_MALLOC_SIZE
93
+ used_memory -= redis_malloc_size (ptr );
94
+ free (ptr );
95
+ #else
67
96
realptr = (char * )ptr - sizeof (size_t );
68
97
oldsize = * ((size_t * )realptr );
69
98
used_memory -= oldsize + sizeof (size_t );
70
99
free (realptr );
100
+ #endif
71
101
}
72
102
73
103
char * zstrdup (const char * s ) {
0 commit comments