3434#include <errno.h>
3535#include <fcntl.h>
3636#include <time.h>
37- #if defined(__APPLE__ )
38- #include <malloc/malloc.h>
39- #elif defined(__linux__ ) || defined(__CYGWIN__ )
40- #include <malloc.h>
41- #elif defined(__FreeBSD__ )
42- #include <malloc_np.h>
43- #endif
4437
4538#include "cutils.h"
4639#include "quickjs-libc.h"
@@ -130,20 +123,6 @@ static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr,
130123 return ptr - dp -> base ;
131124}
132125
133- /* default memory allocation functions with memory limitation */
134- static inline size_t js_trace_malloc_usable_size (void * ptr )
135- {
136- #if defined(__APPLE__ )
137- return malloc_size (ptr );
138- #elif defined(_WIN32 )
139- return _msize (ptr );
140- #elif defined(__linux__ ) || defined(__FreeBSD__ )
141- return malloc_usable_size (ptr );
142- #else
143- return 0 ;
144- #endif
145- }
146-
147126static void
148127#if defined(_WIN32 ) && !defined(__clang__ )
149128/* mingw printf is used */
@@ -167,7 +146,7 @@ __attribute__((format(printf, 2, 3)))
167146 } else {
168147 printf ("H%+06lld.%zd" ,
169148 js_trace_malloc_ptr_offset (ptr , s -> opaque ),
170- js_trace_malloc_usable_size (ptr ));
149+ js__malloc_usable_size (ptr ));
171150 }
172151 fmt ++ ;
173152 continue ;
@@ -202,7 +181,7 @@ static void *js_trace_malloc(JSMallocState *s, size_t size)
202181 js_trace_malloc_printf (s , "A %zd -> %p\n" , size , ptr );
203182 if (ptr ) {
204183 s -> malloc_count ++ ;
205- s -> malloc_size += js_trace_malloc_usable_size (ptr ) + MALLOC_OVERHEAD ;
184+ s -> malloc_size += js__malloc_usable_size (ptr ) + MALLOC_OVERHEAD ;
206185 }
207186 return ptr ;
208187}
@@ -214,7 +193,7 @@ static void js_trace_free(JSMallocState *s, void *ptr)
214193
215194 js_trace_malloc_printf (s , "F %p\n" , ptr );
216195 s -> malloc_count -- ;
217- s -> malloc_size -= js_trace_malloc_usable_size (ptr ) + MALLOC_OVERHEAD ;
196+ s -> malloc_size -= js__malloc_usable_size (ptr ) + MALLOC_OVERHEAD ;
218197 free (ptr );
219198}
220199
@@ -227,7 +206,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
227206 return NULL ;
228207 return js_trace_malloc (s , size );
229208 }
230- old_size = js_trace_malloc_usable_size (ptr );
209+ old_size = js__malloc_usable_size (ptr );
231210 if (size == 0 ) {
232211 js_trace_malloc_printf (s , "R %zd %p\n" , size , ptr );
233212 s -> malloc_count -- ;
@@ -243,7 +222,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
243222 ptr = realloc (ptr , size );
244223 js_trace_malloc_printf (s , " -> %p\n" , ptr );
245224 if (ptr ) {
246- s -> malloc_size += js_trace_malloc_usable_size (ptr ) - old_size ;
225+ s -> malloc_size += js__malloc_usable_size (ptr ) - old_size ;
247226 }
248227 return ptr ;
249228}
@@ -252,15 +231,7 @@ static const JSMallocFunctions trace_mf = {
252231 js_trace_malloc ,
253232 js_trace_free ,
254233 js_trace_realloc ,
255- #if defined(__APPLE__ )
256- malloc_size ,
257- #elif defined(_WIN32 )
258- (size_t (* )(const void * ))_msize ,
259- #elif defined(__linux__ ) || defined (__CYGWIN__ )
260- (size_t (* )(const void * ))malloc_usable_size ,
261- #else
262- NULL ,
263- #endif
234+ js__malloc_usable_size
264235};
265236
266237#define PROG_NAME "qjs"
0 commit comments