Attempting to learn about a slow memory leak.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3887 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
@ -9,6 +9,83 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static int64_t s_uv_malloc_size;
|
||||
|
||||
static void* _tf_uv_alloc(size_t size)
|
||||
{
|
||||
void* ptr = malloc(size + sizeof(size_t));
|
||||
if (ptr)
|
||||
{
|
||||
__atomic_add_fetch(&s_uv_malloc_size, size, __ATOMIC_RELAXED);
|
||||
memcpy(ptr, &size, sizeof(size_t));
|
||||
return (void*)((intptr_t)ptr + sizeof(size_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void* _tf_uv_realloc(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(&s_uv_malloc_size, (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(&s_uv_malloc_size, old_size, __ATOMIC_RELAXED);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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(&s_uv_malloc_size, size, __ATOMIC_RELAXED);
|
||||
free(old_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_uv_get_malloc_size()
|
||||
{
|
||||
return s_uv_malloc_size;
|
||||
}
|
||||
|
||||
static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
size_t length = 0;
|
||||
|
Reference in New Issue
Block a user