forked from cory/tildefriends
Add some helpers for resizing dynamic arrays to allow them to both not grow if they're at capacity or shrink if significantly below capacity.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3902 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
parent
4e3bf99327
commit
e1ca715c64
@ -432,8 +432,9 @@ function receive(message) {
|
|||||||
rpc_in: {group: 'rpc', name: 'in'},
|
rpc_in: {group: 'rpc', name: 'in'},
|
||||||
rpc_out: {group: 'rpc', name: 'out'},
|
rpc_out: {group: 'rpc', name: 'out'},
|
||||||
|
|
||||||
|
arena_percent: {group: 'memory', name: 'm'},
|
||||||
js_malloc_percent: {group: 'memory', name: 'js'},
|
js_malloc_percent: {group: 'memory', name: 'js'},
|
||||||
memory_percent: {group: 'memory', name: 'total'},
|
memory_percent: {group: 'memory', name: 'tot'},
|
||||||
sqlite3_memory_percent: {group: 'memory', name: 'sql'},
|
sqlite3_memory_percent: {group: 'memory', name: 'sql'},
|
||||||
tf_malloc_percent: {group: 'memory', name: 'tf'},
|
tf_malloc_percent: {group: 'memory', name: 'tf'},
|
||||||
tls_malloc_percent: {group: 'memory', name: 'tls'},
|
tls_malloc_percent: {group: 'memory', name: 'tls'},
|
||||||
@ -445,7 +446,7 @@ function receive(message) {
|
|||||||
import_count: {group: 'functions', name: 'imports'},
|
import_count: {group: 'functions', name: 'imports'},
|
||||||
export_count: {group: 'functions', name: 'exports'},
|
export_count: {group: 'functions', name: 'exports'},
|
||||||
};
|
};
|
||||||
const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00'];
|
const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00', '#888'];
|
||||||
let graph_key = k_groups[key]?.group || key;
|
let graph_key = k_groups[key]?.group || key;
|
||||||
let graph = gGraphs[graph_key];
|
let graph = gGraphs[graph_key];
|
||||||
if (!graph) {
|
if (!graph) {
|
||||||
|
26
src/mem.c
26
src/mem.c
@ -154,6 +154,32 @@ char* tf_strdup(const char* string)
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* tf_resize_vec(void* ptr, size_t size)
|
||||||
|
{
|
||||||
|
void* alloc_ptr = ptr ? (void*)((intptr_t)ptr - sizeof(size_t)) : NULL;
|
||||||
|
size_t alloc_size = 0;
|
||||||
|
if (alloc_ptr)
|
||||||
|
{
|
||||||
|
memcpy(&alloc_size, alloc_ptr, sizeof(size_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (alloc_size >= 16 * size + sizeof(size_t))
|
||||||
|
{
|
||||||
|
/* If we've dropped significantly in size, resize down. */
|
||||||
|
return tf_realloc(ptr, size);
|
||||||
|
}
|
||||||
|
else if (alloc_size >= size + sizeof(size_t))
|
||||||
|
{
|
||||||
|
/* Otherwise, if we're big enough, stay the same size. */
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* If we need to grow, overallocate 2x to give room to continue growing. */
|
||||||
|
return tf_realloc(ptr, size * 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t tf_mem_get_tf_malloc_size()
|
size_t tf_mem_get_tf_malloc_size()
|
||||||
{
|
{
|
||||||
return s_tf_malloc_size;
|
return s_tf_malloc_size;
|
||||||
|
@ -14,3 +14,5 @@ void* tf_malloc(size_t size);
|
|||||||
void* tf_realloc(void* ptr, size_t size);
|
void* tf_realloc(void* ptr, size_t size);
|
||||||
void tf_free(void* ptr);
|
void tf_free(void* ptr);
|
||||||
char* tf_strdup(const char* string);
|
char* tf_strdup(const char* string);
|
||||||
|
|
||||||
|
void* tf_resize_vec(void* ptr, size_t size);
|
||||||
|
10
src/task.c
10
src/task.c
@ -15,6 +15,7 @@
|
|||||||
#include "util.js.h"
|
#include "util.js.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <malloc.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -672,7 +673,7 @@ exportid_t tf_task_export_function(tf_task_t* task, tf_taskstub_t* to, JSValue f
|
|||||||
};
|
};
|
||||||
|
|
||||||
int index = _insert_index(&id, task->_exports, task->_export_count, sizeof(export_record_t*), _export_compare);
|
int index = _insert_index(&id, task->_exports, task->_export_count, sizeof(export_record_t*), _export_compare);
|
||||||
task->_exports = tf_realloc(task->_exports, sizeof(export_record_t*) * (task->_export_count + 1));
|
task->_exports = tf_resize_vec(task->_exports, sizeof(export_record_t*) * (task->_export_count + 1));
|
||||||
if (task->_export_count - index)
|
if (task->_export_count - index)
|
||||||
{
|
{
|
||||||
memmove(task->_exports + index + 1, task->_exports + index, sizeof(export_record_t*) * (task->_export_count - index));
|
memmove(task->_exports + index + 1, task->_exports + index, sizeof(export_record_t*) * (task->_export_count - index));
|
||||||
@ -744,6 +745,9 @@ static JSValue _tf_task_getStats(JSContext* context, JSValueConst this_val, int
|
|||||||
JS_SetPropertyStr(context, result, "tls_malloc_percent", JS_NewFloat64(context, 100.0f * tf_mem_get_tls_malloc_size() / total_memory));
|
JS_SetPropertyStr(context, result, "tls_malloc_percent", JS_NewFloat64(context, 100.0f * tf_mem_get_tls_malloc_size() / total_memory));
|
||||||
JS_SetPropertyStr(context, result, "tf_malloc_percent", JS_NewFloat64(context, 100.0f * tf_mem_get_tf_malloc_size() / total_memory));
|
JS_SetPropertyStr(context, result, "tf_malloc_percent", JS_NewFloat64(context, 100.0f * tf_mem_get_tf_malloc_size() / total_memory));
|
||||||
|
|
||||||
|
struct mallinfo mi = mallinfo();
|
||||||
|
JS_SetPropertyStr(context, result, "arena_percent", JS_NewFloat64(context, 100.0f * mi.arena / total_memory));
|
||||||
|
|
||||||
JS_SetPropertyStr(context, result, "socket_count", JS_NewInt32(context, tf_socket_get_count()));
|
JS_SetPropertyStr(context, result, "socket_count", JS_NewInt32(context, tf_socket_get_count()));
|
||||||
JS_SetPropertyStr(context, result, "socket_open_count", JS_NewInt32(context, tf_socket_get_open_count()));
|
JS_SetPropertyStr(context, result, "socket_open_count", JS_NewInt32(context, tf_socket_get_open_count()));
|
||||||
|
|
||||||
@ -1200,7 +1204,7 @@ JSValue tf_task_allocate_promise(tf_task_t* task, promiseid_t* out_promise)
|
|||||||
};
|
};
|
||||||
JSValue result = JS_NewPromiseCapability(task->_context, promise.values);
|
JSValue result = JS_NewPromiseCapability(task->_context, promise.values);
|
||||||
int index = _insert_index((void*)(intptr_t)promiseId, task->_promises, task->_promise_count, sizeof(promise_t), _promise_compare);
|
int index = _insert_index((void*)(intptr_t)promiseId, task->_promises, task->_promise_count, sizeof(promise_t), _promise_compare);
|
||||||
task->_promises = tf_realloc(task->_promises, sizeof(promise_t) * (task->_promise_count + 1));
|
task->_promises = tf_resize_vec(task->_promises, sizeof(promise_t) * (task->_promise_count + 1));
|
||||||
if (task->_promise_count - index)
|
if (task->_promise_count - index)
|
||||||
{
|
{
|
||||||
memmove(task->_promises + index + 1, task->_promises + index, sizeof(promise_t) * (task->_promise_count - index));
|
memmove(task->_promises + index + 1, task->_promises + index, sizeof(promise_t) * (task->_promise_count - index));
|
||||||
@ -1717,7 +1721,7 @@ JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_
|
|||||||
};
|
};
|
||||||
|
|
||||||
int index = _insert_index(import, task->_imports, task->_import_count, sizeof(import_record_t*), _import_compare);
|
int index = _insert_index(import, task->_imports, task->_import_count, sizeof(import_record_t*), _import_compare);
|
||||||
task->_imports = tf_realloc(task->_imports, sizeof(import_record_t*) * (task->_import_count + 1));
|
task->_imports = tf_resize_vec(task->_imports, sizeof(import_record_t*) * (task->_import_count + 1));
|
||||||
if (task->_import_count - index)
|
if (task->_import_count - index)
|
||||||
{
|
{
|
||||||
memmove(task->_imports + index + 1, task->_imports + index, sizeof(import_record_t*) * (task->_import_count - index));
|
memmove(task->_imports + index + 1, task->_imports + index, sizeof(import_record_t*) * (task->_import_count - index));
|
||||||
|
Loading…
Reference in New Issue
Block a user