Skip to content

Commit 18b3096

Browse files
committed
DRY malloc_usable_size
1 parent 8128e66 commit 18b3096

File tree

3 files changed

+30
-60
lines changed

3 files changed

+30
-60
lines changed

cutils.h

+20
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
#define alloca _alloca
3939
#define ssize_t ptrdiff_t
4040
#endif
41+
#if defined(__APPLE__)
42+
#include <malloc/malloc.h>
43+
#elif defined(__linux__) || defined(__CYGWIN__)
44+
#include <malloc.h>
45+
#elif defined(__FreeBSD__)
46+
#include <malloc_np.h>
47+
#endif
4148

4249
#define likely(x) __builtin_expect(!!(x), 1)
4350
#define unlikely(x) __builtin_expect(!!(x), 0)
@@ -331,4 +338,17 @@ void rqsort(void *base, size_t nmemb, size_t size,
331338
int64_t js__gettimeofday_us(void);
332339
uint64_t js__hrtime_ns(void);
333340

341+
static inline size_t js__malloc_usable_size(const void *ptr)
342+
{
343+
#if defined(__APPLE__)
344+
return malloc_size(ptr);
345+
#elif defined(_WIN32)
346+
return _msize((void *)ptr);
347+
#elif defined(__linux__) || defined(__FreeBSD__)
348+
return malloc_usable_size((void *)ptr);
349+
#else
350+
return 0;
351+
#endif
352+
}
353+
334354
#endif /* CUTILS_H */

qjs.c

+6-35
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@
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-
147126
static 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"

quickjs.c

+4-25
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@
3636
#include <time.h>
3737
#include <fenv.h>
3838
#include <math.h>
39-
#if defined(__APPLE__)
40-
#include <malloc/malloc.h>
41-
#elif defined(__linux__) || defined(__CYGWIN__)
42-
#include <malloc.h>
43-
#elif defined(__FreeBSD__)
44-
#include <malloc_np.h>
45-
#endif
4639

4740
#include "cutils.h"
4841
#include "list.h"
@@ -1639,20 +1632,6 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque)
16391632
rt->user_opaque = opaque;
16401633
}
16411634

1642-
/* default memory allocation functions with memory limitation */
1643-
static inline size_t js_def_malloc_usable_size(void *ptr)
1644-
{
1645-
#if defined(__APPLE__)
1646-
return malloc_size(ptr);
1647-
#elif defined(_WIN32)
1648-
return _msize(ptr);
1649-
#elif defined(__linux__) || defined(__FreeBSD__)
1650-
return malloc_usable_size(ptr);
1651-
#else
1652-
return 0;
1653-
#endif
1654-
}
1655-
16561635
static void *js_def_malloc(JSMallocState *s, size_t size)
16571636
{
16581637
void *ptr;
@@ -1668,7 +1647,7 @@ static void *js_def_malloc(JSMallocState *s, size_t size)
16681647
return NULL;
16691648

16701649
s->malloc_count++;
1671-
s->malloc_size += js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
1650+
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
16721651
return ptr;
16731652
}
16741653

@@ -1678,7 +1657,7 @@ static void js_def_free(JSMallocState *s, void *ptr)
16781657
return;
16791658

16801659
s->malloc_count--;
1681-
s->malloc_size -= js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
1660+
s->malloc_size -= js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
16821661
free(ptr);
16831662
}
16841663

@@ -1691,7 +1670,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
16911670
return NULL;
16921671
return js_def_malloc(s, size);
16931672
}
1694-
old_size = js_def_malloc_usable_size(ptr);
1673+
old_size = js__malloc_usable_size(ptr);
16951674
if (size == 0) {
16961675
s->malloc_count--;
16971676
s->malloc_size -= old_size + MALLOC_OVERHEAD;
@@ -1705,7 +1684,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
17051684
if (!ptr)
17061685
return NULL;
17071686

1708-
s->malloc_size += js_def_malloc_usable_size(ptr) - old_size;
1687+
s->malloc_size += js__malloc_usable_size(ptr) - old_size;
17091688
return ptr;
17101689
}
17111690

0 commit comments

Comments
 (0)