Replace the sqlite allocator, and use our own tracking for stats. Want to use this to collect callstacks for all allocations.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4185 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
62
src/mem.c
62
src/mem.c
@ -5,6 +5,7 @@
|
||||
#include <openssl/crypto.h>
|
||||
|
||||
#include <quickjs.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@ -12,6 +13,7 @@ static int64_t s_tf_malloc_size;
|
||||
static int64_t s_uv_malloc_size;
|
||||
static int64_t s_tls_malloc_size;
|
||||
static int64_t s_js_malloc_size;
|
||||
static int64_t s_sqlite_malloc_size;
|
||||
|
||||
static void* _tf_alloc(int64_t* total, size_t size)
|
||||
{
|
||||
@ -251,3 +253,63 @@ size_t tf_mem_get_js_malloc_size()
|
||||
{
|
||||
return s_js_malloc_size;
|
||||
}
|
||||
|
||||
static void* _tf_sqlite_malloc(int size)
|
||||
{
|
||||
return _tf_alloc(&s_sqlite_malloc_size, size);
|
||||
}
|
||||
|
||||
static void _tf_sqlite_free(void* ptr)
|
||||
{
|
||||
_tf_free(&s_sqlite_malloc_size, ptr);
|
||||
}
|
||||
|
||||
static void* _tf_sqlite_realloc(void* ptr, int size)
|
||||
{
|
||||
return _tf_realloc(&s_sqlite_malloc_size, ptr, size);
|
||||
}
|
||||
|
||||
static int _tf_sqlite_size(void* ptr)
|
||||
{
|
||||
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));
|
||||
}
|
||||
return (int)old_size;
|
||||
}
|
||||
|
||||
static int _tf_sqlite_roundup(int size)
|
||||
{
|
||||
return (size + 7) & ~7;
|
||||
}
|
||||
|
||||
static int _tf_sqlite_init(void* user_data)
|
||||
{
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static void _tf_sqlite_shutdown(void* user_data)
|
||||
{
|
||||
}
|
||||
|
||||
void tf_mem_replace_sqlite_allocator()
|
||||
{
|
||||
sqlite3_mem_methods methods =
|
||||
{
|
||||
.xMalloc = _tf_sqlite_malloc,
|
||||
.xFree = _tf_sqlite_free,
|
||||
.xRealloc = _tf_sqlite_realloc,
|
||||
.xSize = _tf_sqlite_size,
|
||||
.xRoundup = _tf_sqlite_roundup,
|
||||
.xInit = _tf_sqlite_init,
|
||||
.xShutdown = _tf_sqlite_shutdown,
|
||||
};
|
||||
sqlite3_config(SQLITE_CONFIG_MALLOC, &methods);
|
||||
}
|
||||
|
||||
size_t tf_mem_get_sqlite_malloc_size()
|
||||
{
|
||||
return s_sqlite_malloc_size;
|
||||
}
|
||||
|
Reference in New Issue
Block a user