Use a custom allocator for everything.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3892 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-06-04 17:04:51 +00:00
parent cf61e68713
commit 9c90b2bc1d
24 changed files with 404 additions and 343 deletions

View File

@ -1,5 +1,6 @@
#include "util.js.h"
#include "mem.h"
#include "task.h"
#include "trace.h"
@ -7,128 +8,8 @@
#include <uv.h>
#include <openssl/crypto.h>
#include <string.h>
static int64_t s_uv_malloc_size;
static int64_t s_tls_malloc_size;
static void* _tf_alloc(int64_t* total, size_t size)
{
void* ptr = malloc(size + sizeof(size_t));
if (ptr)
{
__atomic_add_fetch(total, size, __ATOMIC_RELAXED);
memcpy(ptr, &size, sizeof(size_t));
return (void*)((intptr_t)ptr + sizeof(size_t));
}
else
{
return NULL;
}
}
static void* _tf_realloc(int64_t* total, void* ptr, size_t size)
{
void* old_ptr = ptr ? (void*)((intptr_t)ptr - sizeof(size_t)) : NULL;
size_t old_size = 0;
if (old_ptr)
{
memcpy(&old_size, old_ptr, sizeof(size_t));
}
void* new_ptr = realloc(old_ptr, size + sizeof(size_t));
if (new_ptr)
{
__atomic_add_fetch(total, (int64_t)size - (int64_t)old_size, __ATOMIC_RELAXED);
memcpy(new_ptr, &size, sizeof(size_t));
return (void*)((intptr_t)new_ptr + sizeof(size_t));
}
else
{
__atomic_sub_fetch(total, old_size, __ATOMIC_RELAXED);
return NULL;
}
}
static void _tf_free(int64_t* total, void* ptr)
{
if (ptr)
{
void* old_ptr = (void*)((intptr_t)ptr - sizeof(size_t));
size_t size = 0;
memcpy(&size, old_ptr, sizeof(size_t));
__atomic_sub_fetch(total, size, __ATOMIC_RELAXED);
free(old_ptr);
}
}
static void* _tf_uv_alloc(size_t size)
{
return _tf_alloc(&s_uv_malloc_size, size);
}
static void* _tf_uv_realloc(void* ptr, size_t size)
{
return _tf_realloc(&s_uv_malloc_size, ptr, size);
}
static void* _tf_uv_calloc(size_t nmemb, size_t size)
{
void* ptr = calloc(1, nmemb * size + sizeof(size_t));
if (ptr)
{
size_t total_size = nmemb * size;
__atomic_add_fetch(&s_uv_malloc_size, total_size, __ATOMIC_RELAXED);
memcpy(ptr, &total_size, sizeof(size_t));
return (void*)((intptr_t)ptr + sizeof(size_t));
}
else
{
return NULL;
}
}
static void _tf_uv_free(void* ptr)
{
_tf_free(&s_uv_malloc_size, ptr);
}
void tf_util_replace_uv_allocator()
{
uv_replace_allocator(_tf_uv_alloc, _tf_uv_realloc, _tf_uv_calloc, _tf_uv_free);
}
size_t tf_util_get_uv_malloc_size()
{
return s_uv_malloc_size;
}
void* _tf_tls_alloc(size_t size, const char* file, int line)
{
return _tf_alloc(&s_tls_malloc_size, size);
}
void* _tf_tls_realloc(void* ptr, size_t size, const char* file, int line)
{
return _tf_realloc(&s_tls_malloc_size, ptr, size);
}
void _tf_tls_free(void* ptr, const char* file, int line)
{
_tf_free(&s_tls_malloc_size, ptr);
}
void tf_util_replace_tls_allocator()
{
CRYPTO_set_mem_functions(_tf_tls_alloc, _tf_tls_realloc, _tf_tls_free);
}
size_t tf_util_get_tls_malloc_size()
{
return s_tls_malloc_size;
}
static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
size_t length = 0;
@ -271,7 +152,7 @@ typedef struct _timeout_t {
static void _handle_closed(uv_handle_t* handle)
{
free(handle);
tf_free(handle);
}
static void _util_timeoutCallback(uv_timer_t* handle)
@ -288,7 +169,7 @@ static void _util_timeoutCallback(uv_timer_t* handle)
tf_util_report_error(context, result);
JS_FreeValue(context, result);
tf_trace_end(tf_task_get_trace(timeout->_task));
free(timeout);
tf_free(timeout);
uv_close((uv_handle_t*)handle, _handle_closed);
}
@ -296,14 +177,14 @@ static JSValue _util_setTimeout(JSContext* context, JSValueConst this_val, int a
{
tf_task_t* task = JS_GetContextOpaque(context);
timeout_t* timeout = malloc(sizeof(timeout_t));
timeout_t* timeout = tf_malloc(sizeof(timeout_t));
*timeout = (timeout_t)
{
._task = task,
._callback = JS_DupValue(context, argv[0]),
};
uv_timer_t* timer = malloc(sizeof(uv_timer_t));
uv_timer_t* timer = tf_malloc(sizeof(uv_timer_t));
memset(timer, 0, sizeof(uv_timer_t));
uv_timer_init(tf_task_get_loop(task), timer);
timer->data = timeout;