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:
10
src/task.c
10
src/task.c
@ -15,6 +15,7 @@
|
||||
#include "util.js.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <stdio.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);
|
||||
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)
|
||||
{
|
||||
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, "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_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);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
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)
|
||||
{
|
||||
memmove(task->_imports + index + 1, task->_imports + index, sizeof(import_record_t*) * (task->_import_count - index));
|
||||
|
Reference in New Issue
Block a user