3636 * for instance to free results obtained by backtrace_symbols(). We need
3737 * to define this function before including zmalloc.h that may shadow the
3838 * free implementation if we use jemalloc or another non standard allocator. */
39- void zlibc_free (void * ptr ) {
39+ extern " C " void zlibc_free (void *ptr) {
4040 free (ptr);
4141}
4242
@@ -49,13 +49,16 @@ void zlibc_free(void *ptr) {
4949#ifdef HAVE_MALLOC_SIZE
5050#define PREFIX_SIZE (0 )
5151#else
52+ #define PREFIX_SIZE 16
5253#if defined(__sun) || defined(__sparc) || defined(__sparc__)
53- #define PREFIX_SIZE (sizeof(long long))
54+ static_assert ( PREFIX_SIZE >= (sizeof (long long )), " " );
5455#else
55- #define PREFIX_SIZE (sizeof(size_t))
56+ static_assert ( PREFIX_SIZE >= (sizeof (size_t )), " " );
5657#endif
5758#endif
5859
60+ static_assert ((PREFIX_SIZE % 16 ) == 0 , " Our prefix must be modulo 16-bytes or our pointers will not be aligned" );
61+
5962/* Explicitly override malloc/free etc when using tcmalloc. */
6063#if defined(USE_MEMKIND)
6164#define malloc (size, type ) salloc(size, type)
@@ -104,9 +107,9 @@ static void zmalloc_default_oom(size_t size) {
104107
105108static void (*zmalloc_oom_handler)(size_t ) = zmalloc_default_oom;
106109
107- void * zmalloc (size_t size , enum MALLOC_CLASS class ) {
108- (void )class ;
109- void * ptr = malloc (size + PREFIX_SIZE , class );
110+ void *zmalloc (size_t size, enum MALLOC_CLASS mclass ) {
111+ (void )mclass ;
112+ void *ptr = malloc (size+PREFIX_SIZE, mclass );
110113
111114 if (!ptr) zmalloc_oom_handler (size);
112115#ifdef HAVE_MALLOC_SIZE
@@ -137,9 +140,9 @@ void zfree_no_tcache(void *ptr) {
137140}
138141#endif
139142
140- void * zcalloc (size_t size , enum MALLOC_CLASS class ) {
141- (void )(class );
142- void * ptr = calloc (1 , size + PREFIX_SIZE , class );
143+ void *zcalloc (size_t size, enum MALLOC_CLASS mclass ) {
144+ (void )(mclass );
145+ void *ptr = calloc (1 , size+PREFIX_SIZE, mclass );
143146
144147 if (!ptr) zmalloc_oom_handler (size);
145148#ifdef HAVE_MALLOC_SIZE
@@ -152,7 +155,7 @@ void *zcalloc(size_t size, enum MALLOC_CLASS class) {
152155#endif
153156}
154157
155- void * zrealloc (void * ptr , size_t size , enum MALLOC_CLASS class ) {
158+ void *zrealloc (void *ptr, size_t size, enum MALLOC_CLASS mclass ) {
156159#ifndef HAVE_MALLOC_SIZE
157160 void *realptr;
158161#endif
@@ -163,10 +166,10 @@ void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) {
163166 zfree (ptr);
164167 return NULL ;
165168 }
166- if (ptr == NULL ) return zmalloc (size , class );
169+ if (ptr == NULL ) return zmalloc (size, mclass );
167170#ifdef HAVE_MALLOC_SIZE
168171 oldsize = zmalloc_size (ptr);
169- newptr = realloc (ptr ,size , class );
172+ newptr = realloc (ptr,size, mclass );
170173 if (!newptr) zmalloc_oom_handler (size);
171174
172175 update_zmalloc_stat_free (oldsize);
@@ -175,7 +178,7 @@ void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) {
175178#else
176179 realptr = (char *)ptr-PREFIX_SIZE;
177180 oldsize = *((size_t *)realptr);
178- newptr = realloc (realptr ,size + PREFIX_SIZE , class );
181+ newptr = realloc (realptr,size+PREFIX_SIZE, mclass );
179182 if (!newptr) zmalloc_oom_handler (size);
180183
181184 *((size_t *)newptr) = size;
@@ -222,7 +225,7 @@ void zfree(void *ptr) {
222225
223226char *zstrdup (const char *s) {
224227 size_t l = strlen (s)+1 ;
225- char * p = zmalloc (l , MALLOC_SHARED );
228+ char *p = ( char *) zmalloc (l, MALLOC_SHARED);
226229
227230 memcpy (p,s,l);
228231 return p;
0 commit comments