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:
96
src/task.c
96
src/task.c
@ -3,6 +3,7 @@
|
||||
#include "bcrypt.js.h"
|
||||
#include "database.js.h"
|
||||
#include "file.js.h"
|
||||
#include "mem.h"
|
||||
#include "packetstream.h"
|
||||
#include "serialize.h"
|
||||
#include "socket.js.h"
|
||||
@ -138,7 +139,7 @@ static bool _export_record_release(tf_task_t* task, export_record_t** export)
|
||||
{
|
||||
JS_FreeValue(task->_context, (*export)->_function);
|
||||
(*export)->_function = JS_UNDEFINED;
|
||||
free(*export);
|
||||
tf_free(*export);
|
||||
int index = export - task->_exports;
|
||||
if (task->_export_count - index)
|
||||
{
|
||||
@ -232,7 +233,7 @@ static bool _import_record_release(import_record_t** import)
|
||||
}
|
||||
task->_import_count--;
|
||||
|
||||
free(record);
|
||||
tf_free(record);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -288,7 +289,7 @@ void tf_task_send_error_to_parent(tf_task_t* task, JSValue error)
|
||||
size_t size = 0;
|
||||
tf_serialize_store(task, task->_parent, &buffer, &size, error);
|
||||
tf_packetstream_send(tf_taskstub_get_stream(task->_parent), kTaskError, buffer, size);
|
||||
free(buffer);
|
||||
tf_free(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@ -301,7 +302,7 @@ static const char* _task_loadFile(const char* fileName)
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
result = malloc(fileSize + 1);
|
||||
result = tf_malloc(fileSize + 1);
|
||||
fread(result, 1, fileSize, file);
|
||||
result[fileSize] = '\0';
|
||||
fclose(file);
|
||||
@ -331,18 +332,18 @@ int tf_task_execute(tf_task_t* task, const char* fileName)
|
||||
}
|
||||
if (!task->_path)
|
||||
{
|
||||
char* path = strdup(fileName);
|
||||
char* path = tf_strdup(fileName);
|
||||
char* slash = strrchr(path, '/');
|
||||
if (slash)
|
||||
{
|
||||
*slash = '\0';
|
||||
task->_path = strdup(path);
|
||||
task->_path = tf_strdup(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
task->_path = strdup("./");
|
||||
task->_path = tf_strdup("./");
|
||||
}
|
||||
free(path);
|
||||
tf_free(path);
|
||||
}
|
||||
if (source)
|
||||
{
|
||||
@ -353,7 +354,7 @@ int tf_task_execute(tf_task_t* task, const char* fileName)
|
||||
executed = true;
|
||||
}
|
||||
JS_FreeValue(task->_context, result);
|
||||
free((void*)source);
|
||||
tf_free((void*)source);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -579,12 +580,12 @@ void tf_task_send_promise_message(tf_task_t* from, tf_taskstub_t* to, tf_task_me
|
||||
size_t size;
|
||||
tf_serialize_store(from, to, &buffer, &size, payload);
|
||||
|
||||
char* copy = (char*)malloc(sizeof(promise) + size);
|
||||
char* copy = tf_malloc(sizeof(promise) + size);
|
||||
memcpy(copy, &promise, sizeof(promise));
|
||||
memcpy(copy + sizeof(promise), buffer, size);
|
||||
tf_packetstream_send(tf_taskstub_get_stream(to), type, copy, size + sizeof(promise));
|
||||
free(buffer);
|
||||
free(copy);
|
||||
tf_free(buffer);
|
||||
tf_free(copy);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -597,13 +598,13 @@ static void _tf_task_sendPromiseExportMessage(tf_task_t* from, tf_taskstub_t* to
|
||||
void* buffer;
|
||||
size_t size;
|
||||
tf_serialize_store(from, to, &buffer, &size, result);
|
||||
char* copy = (char*)malloc(sizeof(promise) + sizeof(exportId) + size);
|
||||
char* copy = tf_malloc(sizeof(promise) + sizeof(exportId) + size);
|
||||
memcpy(copy, &promise, sizeof(promise));
|
||||
memcpy(copy + sizeof(promise), &exportId, sizeof(exportId));
|
||||
memcpy(copy + sizeof(promise) + sizeof(exportId), buffer, size);
|
||||
tf_packetstream_send(tf_taskstub_get_stream(to), messageType, copy, sizeof(promise) + sizeof(exportId) + size);
|
||||
free(buffer);
|
||||
free(copy);
|
||||
tf_free(buffer);
|
||||
tf_free(copy);
|
||||
}
|
||||
|
||||
JSValue _tf_task_get_parent(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
@ -662,7 +663,7 @@ exportid_t tf_task_export_function(tf_task_t* task, tf_taskstub_t* to, JSValue f
|
||||
id = task->_nextExport++;
|
||||
} while (_task_get_export(task, id));
|
||||
|
||||
export = malloc(sizeof(export_record_t));
|
||||
export = tf_malloc(sizeof(export_record_t));
|
||||
*export = (export_record_t)
|
||||
{
|
||||
._export_id = id,
|
||||
@ -671,7 +672,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 = realloc(task->_exports, sizeof(export_record_t*) * (task->_export_count + 1));
|
||||
task->_exports = tf_realloc(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));
|
||||
@ -710,7 +711,7 @@ static JSValue _tf_task_trace(JSContext* context, JSValueConst this_val, int arg
|
||||
|
||||
char* trace = tf_trace_export(task->_trace);
|
||||
JSValue result = JS_NewString(context, trace);
|
||||
free(trace);
|
||||
tf_free(trace);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -739,8 +740,9 @@ static JSValue _tf_task_getStats(JSContext* context, JSValueConst this_val, int
|
||||
JS_ComputeMemoryUsage(runtime, &js);
|
||||
JS_SetPropertyStr(context, result, "js_malloc_percent", JS_NewFloat64(context, 100.0f * js.malloc_size / total_memory));
|
||||
|
||||
JS_SetPropertyStr(context, result, "uv_malloc_percent", JS_NewFloat64(context, 100.0f * tf_util_get_uv_malloc_size() / total_memory));
|
||||
JS_SetPropertyStr(context, result, "tls_malloc_percent", JS_NewFloat64(context, 100.0f * tf_util_get_tls_malloc_size() / total_memory));
|
||||
JS_SetPropertyStr(context, result, "uv_malloc_percent", JS_NewFloat64(context, 100.0f * tf_mem_get_uv_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, "socket_count", JS_NewInt32(context, tf_socket_get_count()));
|
||||
JS_SetPropertyStr(context, result, "socket_open_count", JS_NewInt32(context, tf_socket_get_open_count()));
|
||||
@ -988,7 +990,7 @@ static const char* _tf_task_resolveRequire(tf_task_t* task, const char* require)
|
||||
uv_fs_t request;
|
||||
if (uv_fs_access(&task->_loop, &request, test, R_OK, 0) == 0)
|
||||
{
|
||||
return strdup(test);
|
||||
return tf_strdup(test);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@ -1024,12 +1026,12 @@ JSValue _tf_task_require(JSContext* context, JSValueConst this_val, int argc, JS
|
||||
if (it)
|
||||
{
|
||||
result = JS_DupValue(task->_context, it->value);
|
||||
free((void*)path);
|
||||
tf_free((void*)path);
|
||||
}
|
||||
else
|
||||
{
|
||||
JSValue exports = JS_NewObject(task->_context);
|
||||
script_export_t* export = malloc(sizeof(script_export_t));
|
||||
script_export_t* export = tf_malloc(sizeof(script_export_t));
|
||||
*export = (script_export_t)
|
||||
{
|
||||
.name = path,
|
||||
@ -1058,7 +1060,7 @@ JSValue _tf_task_require(JSContext* context, JSValueConst this_val, int argc, JS
|
||||
JS_FreeValue(task->_context, eval);
|
||||
JS_SetPropertyStr(task->_context, global, "exports", oldExports);
|
||||
JS_FreeValue(task->_context, global);
|
||||
free((void*)source);
|
||||
tf_free((void*)source);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -1110,7 +1112,7 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int
|
||||
uint8_t* array = tf_util_try_get_array_buffer(context, &length, value);
|
||||
if (array)
|
||||
{
|
||||
char* source = malloc(length + 1);
|
||||
char* source = tf_malloc(length + 1);
|
||||
memcpy(source, array, length);
|
||||
source[length] = '\0';
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
@ -1120,7 +1122,7 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int
|
||||
tf_util_report_error(context, result);
|
||||
JS_SetPropertyStr(context, global, "exports", oldExports);
|
||||
JS_FreeValue(context, global);
|
||||
free(source);
|
||||
tf_free(source);
|
||||
return exports;
|
||||
}
|
||||
else if (JS_IsString(value))
|
||||
@ -1198,7 +1200,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 = realloc(task->_promises, sizeof(promise_t) * (task->_promise_count + 1));
|
||||
task->_promises = tf_realloc(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));
|
||||
@ -1260,7 +1262,7 @@ taskid_t tf_task_allocate_task_id(tf_task_t* task, tf_taskstub_t* stub)
|
||||
id = task->_nextTask++;
|
||||
} while (id == k_task_parent_id || _tf_task_get_stub(task, id));
|
||||
|
||||
task_child_node_t* node = malloc(sizeof(task_child_node_t));
|
||||
task_child_node_t* node = tf_malloc(sizeof(task_child_node_t));
|
||||
*node = (task_child_node_t)
|
||||
{
|
||||
.id = id,
|
||||
@ -1284,7 +1286,7 @@ void tf_task_remove_child(tf_task_t* task, tf_taskstub_t* child)
|
||||
{
|
||||
task_child_node_t* node = *it;
|
||||
*it = node->next;
|
||||
free(node);
|
||||
tf_free(node);
|
||||
task->_child_count--;
|
||||
break;
|
||||
}
|
||||
@ -1408,7 +1410,7 @@ JSModuleDef* _tf_task_module_loader(JSContext* context, const char* module_name,
|
||||
uint8_t* array = tf_util_try_get_array_buffer(context, &length, source_value);
|
||||
if (array)
|
||||
{
|
||||
source = malloc(length + 1);
|
||||
source = tf_malloc(length + 1);
|
||||
memcpy(source, array, length);
|
||||
source[length] = '\0';
|
||||
}
|
||||
@ -1421,7 +1423,7 @@ JSModuleDef* _tf_task_module_loader(JSContext* context, const char* module_name,
|
||||
}
|
||||
|
||||
JSValue result = JS_Eval(context, source, length, module_name, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
|
||||
free(source);
|
||||
tf_free(source);
|
||||
if (tf_util_report_error(task->_context, result))
|
||||
{
|
||||
return NULL;
|
||||
@ -1434,7 +1436,7 @@ JSModuleDef* _tf_task_module_loader(JSContext* context, const char* module_name,
|
||||
|
||||
tf_task_t* tf_task_create()
|
||||
{
|
||||
tf_task_t* task = malloc(sizeof(tf_task_t));
|
||||
tf_task_t* task = tf_malloc(sizeof(tf_task_t));
|
||||
*task = (tf_task_t) { 0 };
|
||||
++_count;
|
||||
task->_runtime = JS_NewRuntime();
|
||||
@ -1509,7 +1511,7 @@ void tf_task_activate(tf_task_t* task)
|
||||
if (task->_args)
|
||||
{
|
||||
char* saveptr = NULL;
|
||||
char* copy = strdup(task->_args);
|
||||
char* copy = tf_strdup(task->_args);
|
||||
char* start = copy;
|
||||
while (true)
|
||||
{
|
||||
@ -1531,7 +1533,7 @@ void tf_task_activate(tf_task_t* task)
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
free(copy);
|
||||
tf_free(copy);
|
||||
}
|
||||
JS_SetPropertyStr(context, global, "tildefriends", tildefriends);
|
||||
|
||||
@ -1606,18 +1608,18 @@ void tf_task_destroy(tf_task_t* task)
|
||||
for (int i = 0; i < task->_import_count; i++)
|
||||
{
|
||||
JS_FreeValue(task->_context, task->_imports[i]->_function);
|
||||
free(task->_imports[i]);
|
||||
tf_free(task->_imports[i]);
|
||||
}
|
||||
free(task->_imports);
|
||||
tf_free(task->_imports);
|
||||
task->_imports = NULL;
|
||||
task->_import_count = 0;
|
||||
|
||||
for (int i = 0; i < task->_export_count; i++)
|
||||
{
|
||||
JS_FreeValue(task->_context, task->_exports[i]->_function);
|
||||
free(task->_exports[i]);
|
||||
tf_free(task->_exports[i]);
|
||||
}
|
||||
free(task->_exports);
|
||||
tf_free(task->_exports);
|
||||
task->_exports = NULL;
|
||||
task->_export_count = 0;
|
||||
|
||||
@ -1626,7 +1628,7 @@ void tf_task_destroy(tf_task_t* task)
|
||||
task_child_node_t* node = task->_children;
|
||||
tf_taskstub_destroy(node->stub);
|
||||
task->_children = node->next;
|
||||
free(node);
|
||||
tf_free(node);
|
||||
}
|
||||
if (task->_parent)
|
||||
{
|
||||
@ -1636,7 +1638,7 @@ void tf_task_destroy(tf_task_t* task)
|
||||
{
|
||||
tf_task_reject_promise(task, task->_promises[task->_promise_count - 1].id, JS_NULL);
|
||||
}
|
||||
free(task->_promises);
|
||||
tf_free(task->_promises);
|
||||
task->_promises = NULL;
|
||||
JS_FreeValue(task->_context, task->_requires);
|
||||
JS_FreeValue(task->_context, task->_loadedFiles);
|
||||
@ -1645,8 +1647,8 @@ void tf_task_destroy(tf_task_t* task)
|
||||
script_export_t* export = task->_scriptExports;
|
||||
JS_FreeValue(task->_context, export->value);
|
||||
task->_scriptExports = export->next;
|
||||
free((void*)export->name);
|
||||
free(export);
|
||||
tf_free((void*)export->name);
|
||||
tf_free(export);
|
||||
}
|
||||
|
||||
if (task->_ssb)
|
||||
@ -1689,8 +1691,8 @@ void tf_task_destroy(tf_task_t* task)
|
||||
tf_trace_destroy(task->_trace);
|
||||
}
|
||||
--_count;
|
||||
free((void*)task->_path);
|
||||
free(task);
|
||||
tf_free((void*)task->_path);
|
||||
tf_free(task);
|
||||
}
|
||||
|
||||
JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_id)
|
||||
@ -1703,7 +1705,7 @@ JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_
|
||||
}
|
||||
|
||||
JSValue function = JS_NewObjectClass(task->_context, _import_class_id);
|
||||
import_record_t* import = malloc(sizeof(import_record_t));
|
||||
import_record_t* import = tf_malloc(sizeof(import_record_t));
|
||||
JS_SetOpaque(function, import);
|
||||
*import = (import_record_t)
|
||||
{
|
||||
@ -1715,7 +1717,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 = realloc(task->_imports, sizeof(import_record_t*) * (task->_import_count + 1));
|
||||
task->_imports = tf_realloc(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));
|
||||
@ -1739,7 +1741,7 @@ void tf_task_print(tf_task_t* task, int argc, JSValueConst* argv)
|
||||
size_t size;
|
||||
tf_serialize_store(task, task->_parent, &buffer, &size, array);
|
||||
tf_packetstream_send(tf_taskstub_get_stream(task->_parent), kPrint, buffer, size);
|
||||
free(buffer);
|
||||
tf_free(buffer);
|
||||
|
||||
JS_FreeValue(task->_context, array);
|
||||
}
|
||||
|
Reference in New Issue
Block a user