From 18b30961ee45ced30a97abad49f6765897067b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 5 Dec 2023 10:46:40 +0100 Subject: [PATCH] DRY malloc_usable_size --- cutils.h | 20 ++++++++++++++++++++ qjs.c | 41 ++++++----------------------------------- quickjs.c | 29 ++++------------------------- 3 files changed, 30 insertions(+), 60 deletions(-) diff --git a/cutils.h b/cutils.h index fa85a64..0a46ac1 100644 --- a/cutils.h +++ b/cutils.h @@ -38,6 +38,13 @@ #define alloca _alloca #define ssize_t ptrdiff_t #endif +#if defined(__APPLE__) +#include +#elif defined(__linux__) || defined(__CYGWIN__) +#include +#elif defined(__FreeBSD__) +#include +#endif #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) @@ -331,4 +338,17 @@ void rqsort(void *base, size_t nmemb, size_t size, int64_t js__gettimeofday_us(void); uint64_t js__hrtime_ns(void); +static inline size_t js__malloc_usable_size(const void *ptr) +{ +#if defined(__APPLE__) + return malloc_size(ptr); +#elif defined(_WIN32) + return _msize((void *)ptr); +#elif defined(__linux__) || defined(__FreeBSD__) + return malloc_usable_size((void *)ptr); +#else + return 0; +#endif +} + #endif /* CUTILS_H */ diff --git a/qjs.c b/qjs.c index 0530c9e..d7aa45c 100644 --- a/qjs.c +++ b/qjs.c @@ -34,13 +34,6 @@ #include #include #include -#if defined(__APPLE__) -#include -#elif defined(__linux__) || defined(__CYGWIN__) -#include -#elif defined(__FreeBSD__) -#include -#endif #include "cutils.h" #include "quickjs-libc.h" @@ -130,20 +123,6 @@ static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr, return ptr - dp->base; } -/* default memory allocation functions with memory limitation */ -static inline size_t js_trace_malloc_usable_size(void *ptr) -{ -#if defined(__APPLE__) - return malloc_size(ptr); -#elif defined(_WIN32) - return _msize(ptr); -#elif defined(__linux__) || defined(__FreeBSD__) - return malloc_usable_size(ptr); -#else - return 0; -#endif -} - static void #if defined(_WIN32) && !defined(__clang__) /* mingw printf is used */ @@ -167,7 +146,7 @@ __attribute__((format(printf, 2, 3))) } else { printf("H%+06lld.%zd", js_trace_malloc_ptr_offset(ptr, s->opaque), - js_trace_malloc_usable_size(ptr)); + js__malloc_usable_size(ptr)); } fmt++; continue; @@ -202,7 +181,7 @@ static void *js_trace_malloc(JSMallocState *s, size_t size) js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr); if (ptr) { s->malloc_count++; - s->malloc_size += js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD; + s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD; } return ptr; } @@ -214,7 +193,7 @@ static void js_trace_free(JSMallocState *s, void *ptr) js_trace_malloc_printf(s, "F %p\n", ptr); s->malloc_count--; - s->malloc_size -= js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD; + s->malloc_size -= js__malloc_usable_size(ptr) + MALLOC_OVERHEAD; free(ptr); } @@ -227,7 +206,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size) return NULL; return js_trace_malloc(s, size); } - old_size = js_trace_malloc_usable_size(ptr); + old_size = js__malloc_usable_size(ptr); if (size == 0) { js_trace_malloc_printf(s, "R %zd %p\n", size, ptr); s->malloc_count--; @@ -243,7 +222,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size) ptr = realloc(ptr, size); js_trace_malloc_printf(s, " -> %p\n", ptr); if (ptr) { - s->malloc_size += js_trace_malloc_usable_size(ptr) - old_size; + s->malloc_size += js__malloc_usable_size(ptr) - old_size; } return ptr; } @@ -252,15 +231,7 @@ static const JSMallocFunctions trace_mf = { js_trace_malloc, js_trace_free, js_trace_realloc, -#if defined(__APPLE__) - malloc_size, -#elif defined(_WIN32) - (size_t (*)(const void *))_msize, -#elif defined(__linux__) || defined(__CYGWIN__) - (size_t (*)(const void *))malloc_usable_size, -#else - NULL, -#endif + js__malloc_usable_size }; #define PROG_NAME "qjs" diff --git a/quickjs.c b/quickjs.c index 03c10aa..d624598 100644 --- a/quickjs.c +++ b/quickjs.c @@ -36,13 +36,6 @@ #include #include #include -#if defined(__APPLE__) -#include -#elif defined(__linux__) || defined(__CYGWIN__) -#include -#elif defined(__FreeBSD__) -#include -#endif #include "cutils.h" #include "list.h" @@ -1639,20 +1632,6 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque) rt->user_opaque = opaque; } -/* default memory allocation functions with memory limitation */ -static inline size_t js_def_malloc_usable_size(void *ptr) -{ -#if defined(__APPLE__) - return malloc_size(ptr); -#elif defined(_WIN32) - return _msize(ptr); -#elif defined(__linux__) || defined(__FreeBSD__) - return malloc_usable_size(ptr); -#else - return 0; -#endif -} - static void *js_def_malloc(JSMallocState *s, size_t size) { void *ptr; @@ -1668,7 +1647,7 @@ static void *js_def_malloc(JSMallocState *s, size_t size) return NULL; s->malloc_count++; - s->malloc_size += js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD; + s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD; return ptr; } @@ -1678,7 +1657,7 @@ static void js_def_free(JSMallocState *s, void *ptr) return; s->malloc_count--; - s->malloc_size -= js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD; + s->malloc_size -= js__malloc_usable_size(ptr) + MALLOC_OVERHEAD; free(ptr); } @@ -1691,7 +1670,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size) return NULL; return js_def_malloc(s, size); } - old_size = js_def_malloc_usable_size(ptr); + old_size = js__malloc_usable_size(ptr); if (size == 0) { s->malloc_count--; s->malloc_size -= old_size + MALLOC_OVERHEAD; @@ -1705,7 +1684,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size) if (!ptr) return NULL; - s->malloc_size += js_def_malloc_usable_size(ptr) - old_size; + s->malloc_size += js__malloc_usable_size(ptr) - old_size; return ptr; }