DRY malloc_usable_size
This commit is contained in:
parent
8128e66145
commit
18b30961ee
3 changed files with 30 additions and 60 deletions
20
cutils.h
20
cutils.h
|
@ -38,6 +38,13 @@
|
|||
#define alloca _alloca
|
||||
#define ssize_t ptrdiff_t
|
||||
#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 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 */
|
||||
|
|
41
qjs.c
41
qjs.c
|
@ -34,13 +34,6 @@
|
|||
#include <errno.h>
|
||||
#include <fcntl.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 "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"
|
||||
|
|
29
quickjs.c
29
quickjs.c
|
@ -36,13 +36,6 @@
|
|||
#include <time.h>
|
||||
#include <fenv.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 "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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue