DRY malloc_usable_size

This commit is contained in:
Saúl Ibarra Corretgé 2023-12-05 10:46:40 +01:00
parent 8128e66145
commit 18b30961ee
3 changed files with 30 additions and 60 deletions

View file

@ -38,6 +38,13 @@
#define alloca _alloca #define alloca _alloca
#define ssize_t ptrdiff_t #define ssize_t ptrdiff_t
#endif #endif
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__) || defined(__CYGWIN__)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#endif
#define likely(x) __builtin_expect(!!(x), 1) #define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0) #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); int64_t js__gettimeofday_us(void);
uint64_t js__hrtime_ns(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 */ #endif /* CUTILS_H */

41
qjs.c
View file

@ -34,13 +34,6 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h> #include <time.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__) || defined(__CYGWIN__)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#endif
#include "cutils.h" #include "cutils.h"
#include "quickjs-libc.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; 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 static void
#if defined(_WIN32) && !defined(__clang__) #if defined(_WIN32) && !defined(__clang__)
/* mingw printf is used */ /* mingw printf is used */
@ -167,7 +146,7 @@ __attribute__((format(printf, 2, 3)))
} else { } else {
printf("H%+06lld.%zd", printf("H%+06lld.%zd",
js_trace_malloc_ptr_offset(ptr, s->opaque), js_trace_malloc_ptr_offset(ptr, s->opaque),
js_trace_malloc_usable_size(ptr)); js__malloc_usable_size(ptr));
} }
fmt++; fmt++;
continue; 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); js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
if (ptr) { if (ptr) {
s->malloc_count++; 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; return ptr;
} }
@ -214,7 +193,7 @@ static void js_trace_free(JSMallocState *s, void *ptr)
js_trace_malloc_printf(s, "F %p\n", ptr); js_trace_malloc_printf(s, "F %p\n", ptr);
s->malloc_count--; 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); free(ptr);
} }
@ -227,7 +206,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
return NULL; return NULL;
return js_trace_malloc(s, size); return js_trace_malloc(s, size);
} }
old_size = js_trace_malloc_usable_size(ptr); old_size = js__malloc_usable_size(ptr);
if (size == 0) { if (size == 0) {
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr); js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
s->malloc_count--; s->malloc_count--;
@ -243,7 +222,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
ptr = realloc(ptr, size); ptr = realloc(ptr, size);
js_trace_malloc_printf(s, " -> %p\n", ptr); js_trace_malloc_printf(s, " -> %p\n", ptr);
if (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; return ptr;
} }
@ -252,15 +231,7 @@ static const JSMallocFunctions trace_mf = {
js_trace_malloc, js_trace_malloc,
js_trace_free, js_trace_free,
js_trace_realloc, js_trace_realloc,
#if defined(__APPLE__) js__malloc_usable_size
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
}; };
#define PROG_NAME "qjs" #define PROG_NAME "qjs"

View file

@ -36,13 +36,6 @@
#include <time.h> #include <time.h>
#include <fenv.h> #include <fenv.h>
#include <math.h> #include <math.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__) || defined(__CYGWIN__)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#endif
#include "cutils.h" #include "cutils.h"
#include "list.h" #include "list.h"
@ -1639,20 +1632,6 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque)
rt->user_opaque = 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) static void *js_def_malloc(JSMallocState *s, size_t size)
{ {
void *ptr; void *ptr;
@ -1668,7 +1647,7 @@ static void *js_def_malloc(JSMallocState *s, size_t size)
return NULL; return NULL;
s->malloc_count++; 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; return ptr;
} }
@ -1678,7 +1657,7 @@ static void js_def_free(JSMallocState *s, void *ptr)
return; return;
s->malloc_count--; 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); free(ptr);
} }
@ -1691,7 +1670,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
return NULL; return NULL;
return js_def_malloc(s, size); return js_def_malloc(s, size);
} }
old_size = js_def_malloc_usable_size(ptr); old_size = js__malloc_usable_size(ptr);
if (size == 0) { if (size == 0) {
s->malloc_count--; s->malloc_count--;
s->malloc_size -= old_size + MALLOC_OVERHEAD; 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) if (!ptr)
return NULL; 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; return ptr;
} }