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:
Cory McWilliams 2022-06-04 17:04:51 +00:00
parent cf61e68713
commit 9c90b2bc1d
24 changed files with 404 additions and 343 deletions

View File

@ -432,9 +432,10 @@ function receive(message) {
rpc_in: {group: 'rpc', name: 'in'},
rpc_out: {group: 'rpc', name: 'out'},
memory_percent: {group: 'memory', name: 'total'},
js_malloc_percent: {group: 'memory', name: 'js'},
memory_percent: {group: 'memory', name: 'total'},
sqlite3_memory_percent: {group: 'memory', name: 'sql'},
tf_malloc_percent: {group: 'memory', name: 'tf'},
tls_malloc_percent: {group: 'memory', name: 'tls'},
uv_malloc_percent: {group: 'memory', name: 'uv'},
@ -444,7 +445,7 @@ function receive(message) {
import_count: {group: 'functions', name: 'imports'},
export_count: {group: 'functions', name: 'exports'},
};
const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff'];
const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00'];
let graph_key = k_groups[key]?.group || key;
let graph = gGraphs[graph_key];
if (!graph) {

View File

@ -1,7 +1,8 @@
#include "database.js.h"
#include "mem.h"
#include <assert.h>
#include <malloc.h>
#include <stdbool.h>
#include <string.h>
#include <sqlite3.h>
@ -62,7 +63,7 @@ static JSValue _database_create(JSContext* context, JSValueConst this_val, int a
JS_ToInt64(context, &value, data[0]);
sqlite3* db = (sqlite3*)(intptr_t)value;
database_t* database = malloc(sizeof(database_t));
database_t* database = tf_malloc(sizeof(database_t));
*database = (database_t)
{
.task = JS_GetContextOpaque(context),
@ -71,7 +72,7 @@ static JSValue _database_create(JSContext* context, JSValueConst this_val, int a
.db = db,
};
const char* id = JS_ToCString(context, argv[0]);
database->id = strdup(id);
database->id = tf_strdup(id);
JS_FreeCString(context, id);
JS_SetOpaque(object, database);
@ -90,8 +91,8 @@ static void _database_finalizer(JSRuntime *runtime, JSValue value)
database_t* database = JS_GetOpaque(value, _database_class_id);
if (database)
{
free((void*)database->id);
free(database);
tf_free((void*)database->id);
tf_free(database);
}
--_database_count;
}

View File

@ -1,9 +1,9 @@
#include "file.js.h"
#include "mem.h"
#include "task.h"
#include "util.js.h"
#include <malloc.h>
#include <stdbool.h>
#include <string.h>
#include <uv.h>
@ -60,7 +60,7 @@ static const int k_file_read_max = 8 * 1024 * 1024;
static void _file_async_close_callback(uv_fs_t* req)
{
uv_fs_req_cleanup(req);
free(req);
tf_free(req);
}
static void _file_read_read_callback(uv_fs_t* req)
@ -90,7 +90,7 @@ static void _file_read_read_callback(uv_fs_t* req)
if (result < 0)
{
uv_fs_req_cleanup(req);
free(fsreq);
tf_free(fsreq);
}
}
@ -113,7 +113,7 @@ static void _file_read_open_callback(uv_fs_t* req)
if (result < 0)
{
uv_fs_req_cleanup(req);
free(fsreq);
tf_free(fsreq);
}
}
}
@ -121,7 +121,7 @@ static void _file_read_open_callback(uv_fs_t* req)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
uv_fs_req_cleanup(req);
free(req);
tf_free(req);
}
}
@ -132,7 +132,7 @@ static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int ar
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
fs_req_t* req = malloc(sizeof(fs_req_t) + k_file_read_max);
fs_req_t* req = tf_malloc(sizeof(fs_req_t) + k_file_read_max);
*req = (fs_req_t)
{
.fs =
@ -146,7 +146,7 @@ static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int ar
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
uv_fs_req_cleanup(&req->fs);
free(req);
tf_free(req);
}
JS_FreeCString(context, file_name);
return promise_value;
@ -171,7 +171,7 @@ static void _file_write_write_callback(uv_fs_t* req)
if (result < 0)
{
uv_fs_req_cleanup(req);
free(fsreq);
tf_free(fsreq);
}
}
@ -194,7 +194,7 @@ static void _file_write_open_callback(uv_fs_t* req)
if (result < 0)
{
uv_fs_req_cleanup(req);
free(fsreq);
tf_free(fsreq);
}
}
}
@ -202,7 +202,7 @@ static void _file_write_open_callback(uv_fs_t* req)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
uv_fs_req_cleanup(req);
free(req);
tf_free(req);
}
}
@ -225,7 +225,7 @@ static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int a
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
fs_req_t* req = malloc(sizeof(fs_req_t) + size);
fs_req_t* req = tf_malloc(sizeof(fs_req_t) + size);
*req = (fs_req_t)
{
.fs =
@ -263,7 +263,7 @@ static void _file_async_callback(uv_fs_t* req)
{
tf_task_reject_promise(task, promise, JS_NewInt32(context, req->result));
}
free(req);
tf_free(req);
}
static JSValue _file_rename_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
@ -273,7 +273,7 @@ static JSValue _file_rename_file(JSContext* context, JSValueConst this_val, int
const char* new_name = JS_ToCString(context, argv[1]);
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
uv_fs_t* req = malloc(sizeof(uv_fs_t));
uv_fs_t* req = tf_malloc(sizeof(uv_fs_t));
*req = (uv_fs_t)
{
.data = (void*)(intptr_t)promise,
@ -294,7 +294,7 @@ static JSValue _file_unlink_file(JSContext* context, JSValueConst this_val, int
const char* file_name = JS_ToCString(context, argv[0]);
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
uv_fs_t* req = malloc(sizeof(uv_fs_t));
uv_fs_t* req = tf_malloc(sizeof(uv_fs_t));
*req = (uv_fs_t)
{
.data = (void*)(intptr_t)promise,
@ -315,7 +315,7 @@ JSValue _file_make_directory(JSContext* context, JSValueConst this_val, int argc
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
uv_fs_t* req = malloc(sizeof(uv_fs_t));
uv_fs_t* req = tf_malloc(sizeof(uv_fs_t));
*req = (uv_fs_t)
{
.data = (void*)(intptr_t)promise,
@ -336,7 +336,7 @@ JSValue _file_remove_directory(JSContext* context, JSValueConst this_val, int ar
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
uv_fs_t* req = malloc(sizeof(uv_fs_t));
uv_fs_t* req = tf_malloc(sizeof(uv_fs_t));
*req = (uv_fs_t)
{
.data = (void*)(intptr_t)promise,
@ -357,7 +357,7 @@ JSValue _file_stat(JSContext* context, JSValueConst this_val, int argc, JSValueC
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
file_stat_t* data = malloc(sizeof(file_stat_t));
file_stat_t* data = tf_malloc(sizeof(file_stat_t));
data->_task = task;
data->_promise = promise;
data->_request.data = data;
@ -368,7 +368,7 @@ JSValue _file_stat(JSContext* context, JSValueConst this_val, int argc, JSValueC
{
tf_task_reject_promise(task, promise, JS_NewInt32(context, result));
uv_fs_req_cleanup(&data->_request);
free(data);
tf_free(data);
}
JS_FreeCString(context, path);
return promise_value;
@ -399,5 +399,5 @@ static void _file_on_stat_complete(uv_fs_t* request)
JS_FreeValue(context, result);
}
uv_fs_req_cleanup(request);
free(data);
tf_free(data);
}

View File

@ -1,3 +1,4 @@
#include "mem.h"
#include "ssb.h"
#include "ssb.db.h"
#include "ssb.import.h"
@ -431,8 +432,8 @@ static int _tf_command_run(const char* file, int argc, char* argv[])
}
if (args.count > 1)
{
uv_thread_t* threads = malloc(sizeof(uv_thread_t) * args.count);
tf_run_thread_data_t* data = malloc(sizeof(tf_run_thread_data_t) * args.count);
uv_thread_t* threads = tf_malloc(sizeof(uv_thread_t) * args.count);
tf_run_thread_data_t* data = tf_malloc(sizeof(tf_run_thread_data_t) * args.count);
for (int i = 0 ; i < args.count; i++)
{
data[i] = (tf_run_thread_data_t)
@ -450,8 +451,8 @@ static int _tf_command_run(const char* file, int argc, char* argv[])
result = data[i].result;
}
}
free(data);
free(threads);
tf_free(data);
tf_free(threads);
}
return result;
@ -628,8 +629,8 @@ int main(int argc, char* argv[])
#if !defined(_WIN32)
prctl(PR_SET_PDEATHSIG, SIGKILL);
#endif
tf_util_replace_uv_allocator();
tf_util_replace_tls_allocator();
tf_mem_replace_uv_allocator();
tf_mem_replace_tls_allocator();
uv_setup_args(argc, argv);
tf_taskstub_startup();

154
src/mem.c Normal file
View File

@ -0,0 +1,154 @@
#include "mem.h"
#include <uv.h>
#include <openssl/crypto.h>
#include <string.h>
static int64_t s_tf_malloc_size;
static int64_t s_uv_malloc_size;
static int64_t s_tls_malloc_size;
static void* _tf_alloc(int64_t* total, size_t size)
{
void* ptr = malloc(size + sizeof(size_t));
if (ptr)
{
__atomic_add_fetch(total, size, __ATOMIC_RELAXED);
memcpy(ptr, &size, sizeof(size_t));
return (void*)((intptr_t)ptr + sizeof(size_t));
}
else
{
return NULL;
}
}
static void* _tf_realloc(int64_t* total, 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(total, (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(total, old_size, __ATOMIC_RELAXED);
return NULL;
}
}
static void _tf_free(int64_t* total, 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(total, size, __ATOMIC_RELAXED);
free(old_ptr);
}
}
static void* _tf_uv_alloc(size_t size)
{
return _tf_alloc(&s_uv_malloc_size, size);
}
static void* _tf_uv_realloc(void* ptr, size_t size)
{
return _tf_realloc(&s_uv_malloc_size, ptr, size);
}
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)
{
_tf_free(&s_uv_malloc_size, ptr);
}
void tf_mem_replace_uv_allocator()
{
uv_replace_allocator(_tf_uv_alloc, _tf_uv_realloc, _tf_uv_calloc, _tf_uv_free);
}
size_t tf_mem_get_uv_malloc_size()
{
return s_uv_malloc_size;
}
void* _tf_tls_alloc(size_t size, const char* file, int line)
{
return _tf_alloc(&s_tls_malloc_size, size);
}
void* _tf_tls_realloc(void* ptr, size_t size, const char* file, int line)
{
return _tf_realloc(&s_tls_malloc_size, ptr, size);
}
void _tf_tls_free(void* ptr, const char* file, int line)
{
_tf_free(&s_tls_malloc_size, ptr);
}
void tf_mem_replace_tls_allocator()
{
CRYPTO_set_mem_functions(_tf_tls_alloc, _tf_tls_realloc, _tf_tls_free);
}
size_t tf_mem_get_tls_malloc_size()
{
return s_tls_malloc_size;
}
void* tf_malloc(size_t size)
{
return _tf_alloc(&s_tf_malloc_size, size);
}
void* tf_realloc(void* ptr, size_t size)
{
return _tf_realloc(&s_tf_malloc_size, ptr, size);
}
void tf_free(void* ptr)
{
_tf_free(&s_tf_malloc_size, ptr);
}
char* tf_strdup(const char* string)
{
size_t len = strlen(string);
char* buffer = tf_malloc(len + 1);
memcpy(buffer, string, len + 1);
return buffer;
}
size_t tf_mem_get_tf_malloc_size()
{
return s_tf_malloc_size;
}

16
src/mem.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <stddef.h>
void tf_mem_replace_uv_allocator();
size_t tf_mem_get_uv_malloc_size();
void tf_mem_replace_tls_allocator();
size_t tf_mem_get_tls_malloc_size();
size_t tf_mem_get_tf_malloc_size();
void* tf_malloc(size_t size);
void* tf_realloc(void* ptr, size_t size);
void tf_free(void* ptr);
char* tf_strdup(const char* string);

View File

@ -1,8 +1,9 @@
#include "packetstream.h"
#include "mem.h"
#include <uv.h>
#include <malloc.h>
#include <stdbool.h>
#include <string.h>
@ -18,7 +19,7 @@ typedef struct _tf_packetstream_t {
tf_packetstream_t* tf_packetstream_create()
{
tf_packetstream_t* impl = malloc(sizeof(tf_packetstream_t));
tf_packetstream_t* impl = tf_malloc(sizeof(tf_packetstream_t));
*impl = (tf_packetstream_t) { 0 };
return impl;
}
@ -30,7 +31,7 @@ void tf_packetstream_destroy(tf_packetstream_t* stream)
stream->destroyed = true;
if (stream->buffer)
{
free(stream->buffer);
tf_free(stream->buffer);
stream->buffer = NULL;
}
if (stream->stream.data)
@ -39,13 +40,13 @@ void tf_packetstream_destroy(tf_packetstream_t* stream)
}
else
{
free(stream);
tf_free(stream);
}
}
static void _packetstream_allocate(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buffer)
{
buffer->base = malloc(suggested_size);
buffer->base = tf_malloc(suggested_size);
buffer->len = suggested_size;
}
@ -93,7 +94,7 @@ static void _packetstream_on_read(uv_stream_t* handle, ssize_t count, const uv_b
{
stream->buffer_capacity *= 2;
}
write_buffer = realloc(write_buffer, stream->buffer_capacity);
write_buffer = tf_realloc(write_buffer, stream->buffer_capacity);
}
if (write_buffer)
{
@ -108,7 +109,7 @@ static void _packetstream_on_read(uv_stream_t* handle, ssize_t count, const uv_b
{
tf_packetstream_close(stream);
}
free(buffer->base);
tf_free(buffer->base);
}
void tf_packetstream_start(tf_packetstream_t* stream)
@ -119,13 +120,13 @@ void tf_packetstream_start(tf_packetstream_t* stream)
static void _packetstream_on_write(uv_write_t* request, int status)
{
free(request);
tf_free(request);
}
void tf_packetstream_send(tf_packetstream_t* stream, int packet_type, const char* begin, size_t length)
{
size_t buffer_length = sizeof(uv_write_t) + sizeof(packet_type) + sizeof(length) + length;
uv_write_t* request = malloc(buffer_length);
uv_write_t* request = tf_malloc(buffer_length);
memset(request, 0, sizeof(uv_write_t));
char* buffer = (char*)(request + 1);
memcpy(buffer, &packet_type, sizeof(packet_type));
@ -152,12 +153,12 @@ static void _tf_packetstream_handle_closed(uv_handle_t* handle)
handle->data = NULL;
if (packetstream->buffer)
{
free(packetstream->buffer);
tf_free(packetstream->buffer);
packetstream->buffer = NULL;
}
if (packetstream->destroyed)
{
free(packetstream);
tf_free(packetstream);
}
}

View File

@ -1,4 +1,6 @@
#include "serialize.h"
#include "mem.h"
#include "task.h"
#include "taskstub.js.h"
#include "util.js.h"
@ -53,7 +55,7 @@ void tf_serialize_store(tf_task_t* task, tf_taskstub_t* to, void** out_buffer, s
{
buffer_t tmp = { 0 };
_serialize_store(task, to, &tmp, value);
tmp.data = realloc(tmp.data, tmp.size);
tmp.data = tf_realloc(tmp.data, tmp.size);
*out_buffer = tmp.data;
*out_size = tmp.size;
}
@ -68,7 +70,7 @@ static void _buffer_append(buffer_t* buffer, const void* data, size_t size)
if (buffer->capacity < buffer->size + size)
{
size_t new_capacity = (size + buffer->capacity) * 2;
buffer->data = realloc(buffer->data, new_capacity);
buffer->data = tf_realloc(buffer->data, new_capacity);
buffer->capacity = new_capacity;
}
memcpy((char*)buffer->data + buffer->size, data, size);

View File

@ -1,4 +1,6 @@
#include "socket.js.h"
#include "mem.h"
#include "task.h"
#include "tls.h"
#include "tlscontext.js.h"
@ -150,7 +152,7 @@ typedef struct _socket_resolve_data_t {
socket_t* _socket_create_internal(JSContext* context)
{
socket_t* socket = malloc(sizeof(socket_t));
socket_t* socket = tf_malloc(sizeof(socket_t));
memset(socket, 0, sizeof(*socket));
socket->_closePromise = -1;
@ -228,7 +230,7 @@ void _socket_close_internal(socket_t* socket)
JS_IsUndefined(socket->_object))
{
--_count;
free(socket);
tf_free(socket);
}
}
@ -345,7 +347,7 @@ bool _socket_processSomeOutgoingTls(socket_t* socket, promiseid_t promise, uv_wr
int result = tf_tls_session_read_encrypted(socket->_tls, buffer, sizeof(buffer));
if (result > 0)
{
char* request_buffer = malloc(sizeof(uv_write_t) + result);
char* request_buffer = tf_malloc(sizeof(uv_write_t) + result);
uv_write_t* request = (uv_write_t*)request_buffer;
memset(request, 0, sizeof(*request));
request->data = (void*)(intptr_t)(promise);
@ -361,7 +363,7 @@ bool _socket_processSomeOutgoingTls(socket_t* socket, promiseid_t promise, uv_wr
int writeResult = uv_write(request, (uv_stream_t*)&socket->_socket, &writeBuffer, 1, callback);
if (writeResult != 0)
{
free(request_buffer);
tf_free(request_buffer);
char error[256];
snprintf(error, sizeof(error), "uv_write: %s", uv_strerror(writeResult));
_socket_reportError(socket, error);
@ -387,7 +389,7 @@ JSValue _socket_bind(JSContext* context, JSValueConst this_val, int argc, JSValu
const char* node = JS_ToCString(tf_task_get_context(socket->_task), argv[0]);
const char* port = JS_ToCString(tf_task_get_context(socket->_task), argv[1]);
socket_resolve_data_t* data = malloc(sizeof(socket_resolve_data_t));
socket_resolve_data_t* data = tf_malloc(sizeof(socket_resolve_data_t));
memset(data, 0, sizeof(*data));
struct addrinfo hints = {
.ai_family = PF_INET,
@ -404,7 +406,7 @@ JSValue _socket_bind(JSContext* context, JSValueConst this_val, int argc, JSValu
char error[256];
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result));
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), error));
free(data);
tf_free(data);
}
return promise;
}
@ -432,7 +434,7 @@ void _socket_onResolvedForBind(uv_getaddrinfo_t* resolver, int status, struct ad
tf_task_resolve_promise(data->socket->_task, data->promise, JS_UNDEFINED);
}
}
free(data);
tf_free(data);
}
JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
@ -444,7 +446,7 @@ JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSV
strncpy(socket->_peerName, node, sizeof(socket->_peerName) - 1);
socket_resolve_data_t* data = malloc(sizeof(socket_resolve_data_t));
socket_resolve_data_t* data = tf_malloc(sizeof(socket_resolve_data_t));
memset(data, 0, sizeof(*data));
struct addrinfo hints = {
.ai_family = PF_INET,
@ -460,7 +462,7 @@ JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSV
char error[256];
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result));
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(context, "%s", error));
free(data);
tf_free(data);
}
JS_FreeCString(context, node);
@ -479,7 +481,7 @@ void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct
}
else
{
uv_connect_t* request = malloc(sizeof(uv_connect_t));
uv_connect_t* request = tf_malloc(sizeof(uv_connect_t));
memset(request, 0, sizeof(*request));
request->data = (void*)(intptr_t)data->promise;
int connectResult = uv_tcp_connect(request, &data->socket->_socket, result->ai_addr, _socket_onConnect);
@ -491,7 +493,7 @@ void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct
}
}
uv_freeaddrinfo(result);
free(data);
tf_free(data);
}
void _socket_onConnect(uv_connect_t* request, int status)
@ -512,7 +514,7 @@ void _socket_onConnect(uv_connect_t* request, int status)
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error));
}
}
free(request);
tf_free(request);
}
JSValue _socket_listen(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
@ -607,7 +609,7 @@ JSValue _socket_shutdown(JSContext* context, JSValueConst this_val, int argc, JS
void _socket_shutdownInternal(socket_t* socket, promiseid_t promise)
{
uv_shutdown_t* request = malloc(sizeof(uv_shutdown_t));
uv_shutdown_t* request = tf_malloc(sizeof(uv_shutdown_t));
memset(request, 0, sizeof(*request));
request->data = (void*)(intptr_t)promise;
int result = uv_shutdown(request, (uv_stream_t*)&socket->_socket, _socket_onShutdown);
@ -616,7 +618,7 @@ void _socket_shutdownInternal(socket_t* socket, promiseid_t promise)
char error[256];
snprintf(error, sizeof(error), "uv_shutdown: %s", uv_strerror(result));
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error));
free(request);
tf_free(request);
}
}
@ -641,7 +643,7 @@ void _socket_onTlsShutdown(uv_write_t* request, int status)
socket_t* socket = request->handle->data;
promiseid_t promise = (intptr_t)request->data;
_socket_processTlsShutdown(socket, promise);
free(request);
tf_free(request);
}
JSValue _socket_onError(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
@ -684,7 +686,7 @@ JSValue _socket_read(JSContext* context, JSValueConst this_val, int argc, JSValu
void _socket_allocateBuffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf)
{
*buf = uv_buf_init(malloc(suggestedSize), suggestedSize);
*buf = uv_buf_init(tf_malloc(suggestedSize), suggestedSize);
}
void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffer)
@ -777,7 +779,7 @@ void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffe
_socket_notifyDataRead(socket, buffer->base, readSize);
}
}
free(buffer->base);
tf_free(buffer->base);
JS_FreeValue(context, ref);
}
@ -851,7 +853,7 @@ int _socket_writeBytes(socket_t* socket, promiseid_t promise, int (*callback)(so
int _socket_writeInternal(socket_t* socket, promiseid_t promise, const char* data, size_t length)
{
char* rawBuffer = malloc(sizeof(uv_write_t) + length);
char* rawBuffer = tf_malloc(sizeof(uv_write_t) + length);
uv_write_t* request = (uv_write_t*)rawBuffer;
memcpy(rawBuffer + sizeof(uv_write_t), data, length);
@ -933,7 +935,7 @@ void _socket_onWrite(uv_write_t* request, int status)
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), error));
}
}
free(request);
tf_free(request);
}
JSValue _socket_isConnected(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
@ -978,7 +980,7 @@ void _socket_onShutdown(uv_shutdown_t* request, int status)
snprintf(error, sizeof(error), "uv_shutdown: %s", uv_strerror(status));
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error));
}
free(request);
tf_free(request);
}
JSValue _socket_getPeerName(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)

106
src/ssb.c
View File

@ -1,14 +1,12 @@
#include "ssb.h"
#include "mem.h"
#include "ssb.connections.h"
#include "ssb.db.h"
#include "trace.h"
#include <assert.h>
#include <malloc.h>
#include <base64c.h>
//#include <openssl/evp.h>
//#include <openssl/rand.h>
#include <quickjs.h>
#include <sodium/crypto_auth.h>
#include <sodium/crypto_box.h>
@ -264,7 +262,7 @@ static void _tf_ssb_connection_on_tcp_alloc(uv_handle_t* handle, size_t suggeste
{
tf_ssb_connection_t* connection = handle->data;
size_t malloc_size = sizeof(connection->recv_buffer) - connection->recv_size;
buf->base = malloc_size ? malloc(malloc_size) : NULL;
buf->base = malloc_size ? tf_malloc(malloc_size) : NULL;
buf->len = malloc_size;
}
@ -275,19 +273,19 @@ static void _tf_ssb_connection_on_write(uv_write_t* req, int status)
tf_ssb_connection_t* connection = req->data;
_tf_ssb_connection_close(connection, "write failed asynchronously");
}
free(req);
tf_free(req);
}
static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t size)
{
uv_write_t* write = malloc(sizeof(uv_write_t) + size);
uv_write_t* write = tf_malloc(sizeof(uv_write_t) + size);
*write = (uv_write_t) { .data = connection };
memcpy(write + 1, data, size);
int result = uv_write(write, (uv_stream_t*)&connection->tcp, &(uv_buf_t) { .base = (char*)(write + 1), .len = size }, 1, _tf_ssb_connection_on_write);
if (result)
{
_tf_ssb_connection_close(connection, "write failed");
free(write);
tf_free(write);
}
}
@ -372,7 +370,7 @@ static void _tf_ssb_nonce_inc(uint8_t* nonce)
static void _tf_ssb_connection_box_stream_send(tf_ssb_connection_t* connection, const uint8_t* message, size_t size)
{
uint8_t* message_enc = malloc(size + 34);
uint8_t* message_enc = tf_malloc(size + 34);
uint8_t nonce1[crypto_secretbox_NONCEBYTES];
memcpy(nonce1, connection->send_nonce, sizeof(nonce1));
@ -384,7 +382,7 @@ static void _tf_ssb_connection_box_stream_send(tf_ssb_connection_t* connection,
if (crypto_secretbox_easy(message_enc + 34 - 16, message, size, nonce2, connection->c_to_s_box_key) != 0)
{
_tf_ssb_connection_close(connection, "unable to secretbox message");
free(message_enc);
tf_free(message_enc);
return;
}
@ -394,12 +392,12 @@ static void _tf_ssb_connection_box_stream_send(tf_ssb_connection_t* connection,
if (crypto_secretbox_easy(message_enc, header, sizeof(header), nonce1, connection->c_to_s_box_key) != 0)
{
_tf_ssb_connection_close(connection, "unable to secretbox header");
free(message_enc);
tf_free(message_enc);
return;
}
_tf_ssb_write(connection, message_enc, size + 34);
free(message_enc);
tf_free(message_enc);
}
static bool _tf_ssb_connection_get_request_callback(tf_ssb_connection_t* connection, int32_t request_number, tf_ssb_rpc_callback_t** out_callback, void** out_user_data)
@ -431,7 +429,7 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ
/* TODO: This leaks the callback. */
return;
}
tf_ssb_request_t* request = malloc(sizeof(tf_ssb_request_t));
tf_ssb_request_t* request = tf_malloc(sizeof(tf_ssb_request_t));
*request = (tf_ssb_request_t)
{
.next = connection->requests,
@ -454,7 +452,7 @@ static void _tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, i
JS_FreeValue(tf_ssb_connection_get_context(connection), JS_MKPTR(JS_TAG_OBJECT, found->user_data));
}
*it = found->next;
free(found);
tf_free(found);
break;
}
}
@ -470,7 +468,7 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags,
{
tf_ssb_connection_add_request(connection, request_number, callback, user_data);
}
uint8_t* combined = malloc(9 + size);
uint8_t* combined = tf_malloc(9 + size);
*combined = flags;
uint32_t u32size = htonl((uint32_t)size);
memcpy(combined + 1, &u32size, sizeof(u32size));
@ -478,7 +476,7 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags,
memcpy(combined + 1 + sizeof(uint32_t), &rn, sizeof(rn));
memcpy(combined + 1 + 2 * sizeof(uint32_t), message, size);
_tf_ssb_connection_box_stream_send(connection, combined, 1 + 2 * sizeof(uint32_t) + size);
free(combined);
tf_free(combined);
printf("RPC SEND flags=%x RN=%d: %.*s\n", flags, request_number, (int)size, message);
connection->ssb->rpc_out++;
}
@ -543,7 +541,7 @@ void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_
size_t len = 0;
const char* messagestr = JS_ToCStringLen(context, &len, idval);
char* latin1 = strdup(messagestr);
char* latin1 = tf_strdup(messagestr);
uint8_t* write_pos = (uint8_t*)latin1;
const uint8_t* p = (const uint8_t*)messagestr;
while (p && *p)
@ -573,7 +571,7 @@ void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_
snprintf(out_id, out_id_size, "%%%s.sha256", id_base64);
free(latin1);
tf_free(latin1);
JS_FreeCString(context, messagestr);
JS_FreeValue(context, idval);
}
@ -1372,7 +1370,7 @@ void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* rea
!connection->tcp.data &&
!connection->connect.data)
{
free(connection);
tf_free(connection);
}
}
@ -1394,7 +1392,7 @@ static void _tf_ssb_connection_on_tcp_recv(uv_stream_t* stream, ssize_t nread, c
if (connection->recv_size + nread > sizeof(connection->recv_buffer))
{
_tf_ssb_connection_close(connection, "recv buffer overflow");
free(buf->base);
tf_free(buf->base);
return;
}
memcpy(connection->recv_buffer + connection->recv_size, buf->base, nread);
@ -1474,7 +1472,7 @@ static void _tf_ssb_connection_on_tcp_recv(uv_stream_t* stream, ssize_t nread, c
{
_tf_ssb_connection_close(connection, "read zero");
}
free(buf->base);
tf_free(buf->base);
}
static void _tf_ssb_connection_client_send_hello(uv_stream_t* stream)
@ -1539,7 +1537,7 @@ static bool _tf_ssb_load_keys(tf_ssb_t* ssb)
bool result = false;
char* json = NULL;
size_t path_size = strlen(home) + strlen(ssb->secrets_path) + 1;
char* path = malloc(path_size);
char* path = tf_malloc(path_size);
snprintf(path, path_size, "%s%s", home, ssb->secrets_path);
FILE* file = fopen(path, "rb");
@ -1562,7 +1560,7 @@ static bool _tf_ssb_load_keys(tf_ssb_t* ssb)
goto failed;
}
json = malloc(len + 1);
json = tf_malloc(len + 1);
if (fread(json, 1, len, file) != (size_t)len)
{
printf("Failed to read %s: %s\n.", path, strerror(errno));
@ -1599,7 +1597,7 @@ static bool _tf_ssb_load_keys(tf_ssb_t* ssb)
failed:
if (json)
{
free(json);
tf_free(json);
}
if (file)
{
@ -1607,7 +1605,7 @@ failed:
}
if (path)
{
free(path);
tf_free(path);
}
return result;
}
@ -1634,7 +1632,7 @@ static bool _tf_ssb_save_keys(tf_ssb_t* ssb)
}
size_t path_size = strlen(home) + strlen(ssb->secrets_path) + 1;
char* path = malloc(path_size);
char* path = tf_malloc(path_size);
snprintf(path, path_size, "%s%s", home, ssb->secrets_path);
JSContext* context = ssb->context;
@ -1659,7 +1657,7 @@ static bool _tf_ssb_save_keys(tf_ssb_t* ssb)
JS_FreeValue(context, jsonval);
JS_FreeValue(context, root);
free(path);
tf_free(path);
return result;
}
@ -1716,7 +1714,7 @@ void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats)
tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db, const char* secrets_path)
{
tf_ssb_t* ssb = malloc(sizeof(tf_ssb_t));
tf_ssb_t* ssb = tf_malloc(sizeof(tf_ssb_t));
memset(ssb, 0, sizeof(*ssb));
ssb->secrets_path = secrets_path ? secrets_path : k_secrets_path;
if (context)
@ -1886,7 +1884,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
node->cleanup(ssb, node->user_data);
node->cleanup = NULL;
}
free(node);
tf_free(node);
}
while (ssb->connections_changed)
{
@ -1897,7 +1895,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
while (ssb->message_added)
{
@ -1908,7 +1906,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
while (ssb->blob_want_added)
{
@ -1919,7 +1917,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
while (ssb->broadcasts_changed)
{
@ -1930,7 +1928,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
if (ssb->own_context)
{
@ -1946,9 +1944,9 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
tf_ssb_broadcast_t* broadcast = ssb->broadcasts;
ssb->broadcasts = broadcast->next;
ssb->broadcasts_count--;
free(broadcast);
tf_free(broadcast);
}
free(ssb);
tf_free(ssb);
}
void tf_ssb_run(tf_ssb_t* ssb)
@ -2034,7 +2032,7 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c
}
JSContext* context = ssb->context;
tf_ssb_connection_t* connection = malloc(sizeof(tf_ssb_connection_t));
tf_ssb_connection_t* connection = tf_malloc(sizeof(tf_ssb_connection_t));
memset(connection, 0, sizeof(*connection));
connection->ssb = ssb;
connection->tcp.data = connection;
@ -2097,13 +2095,13 @@ static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, s
{
printf("getaddrinfo => %s\n", uv_strerror(result));
}
free(connect);
tf_free(connect);
uv_freeaddrinfo(info);
}
void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key)
{
connect_t* connect = malloc(sizeof(connect_t));
connect_t* connect = tf_malloc(sizeof(connect_t));
*connect = (connect_t)
{
.ssb = ssb,
@ -2116,7 +2114,7 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke
if (r < 0)
{
printf("uv_getaddrinfo: %s\n", uv_strerror(r));
free(connect);
tf_free(connect);
}
}
@ -2129,7 +2127,7 @@ static void _tf_ssb_on_connection(uv_stream_t* stream, int status)
return;
}
tf_ssb_connection_t* connection = malloc(sizeof(tf_ssb_connection_t));
tf_ssb_connection_t* connection = tf_malloc(sizeof(tf_ssb_connection_t));
memset(connection, 0, sizeof(*connection));
connection->ssb = ssb;
connection->tcp.data = connection;
@ -2310,7 +2308,7 @@ void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address)
static void _tf_ssb_on_broadcast_listener_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
{
buf->base = malloc(suggested_size);
buf->base = tf_malloc(suggested_size);
buf->len = suggested_size;
}
@ -2353,7 +2351,7 @@ static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broad
}
printf("Received new broadcast: host=%s, pub=%s.\n", broadcast->host, key);
tf_ssb_broadcast_t* node = malloc(sizeof(tf_ssb_broadcast_t));
tf_ssb_broadcast_t* node = tf_malloc(sizeof(tf_ssb_broadcast_t));
*node = *broadcast;
node->next = ssb->broadcasts;
node->ctime = time(NULL);
@ -2368,7 +2366,7 @@ static void _tf_ssb_on_broadcast_listener_recv(uv_udp_t* handle, ssize_t nread,
{
if (nread <= 0)
{
free(buf->base);
tf_free(buf->base);
return;
}
@ -2387,7 +2385,7 @@ static void _tf_ssb_on_broadcast_listener_recv(uv_udp_t* handle, ssize_t nread,
}
entry = strtok_r(NULL, k_delim, &state);
}
free(buf->base);
tf_free(buf->base);
}
void tf_ssb_visit_broadcasts(tf_ssb_t* ssb, void (*callback)(const struct sockaddr_in* addr, const uint8_t* pub, void* user_data), void* user_data)
@ -2415,7 +2413,7 @@ static void _tf_ssb_broadcast_cleanup_timer(uv_timer_t* timer)
{
tf_ssb_broadcast_t* node = *it;
*it = node->next;
free(node);
tf_free(node);
ssb->broadcasts_count--;
modified++;
}
@ -2487,7 +2485,7 @@ const char** tf_ssb_get_connection_ids(tf_ssb_t* ssb)
}
}
char* buffer = malloc(sizeof(char*) * (count + 1) + k_id_base64_len * count);
char* buffer = tf_malloc(sizeof(char*) * (count + 1) + k_id_base64_len * count);
char** array = (char**)buffer;
char* strings = buffer + sizeof(char*) * (count + 1);
int i = 0;
@ -2518,7 +2516,7 @@ int tf_ssb_get_connections(tf_ssb_t* ssb, tf_ssb_connection_t** out_connections,
void tf_ssb_add_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_changed_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data)
{
tf_ssb_broadcasts_changed_callback_node_t* node = malloc(sizeof(tf_ssb_broadcasts_changed_callback_node_t));
tf_ssb_broadcasts_changed_callback_node_t* node = tf_malloc(sizeof(tf_ssb_broadcasts_changed_callback_node_t));
*node = (tf_ssb_broadcasts_changed_callback_node_t)
{
.callback = callback,
@ -2545,7 +2543,7 @@ void tf_ssb_remove_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
else
{
@ -2556,7 +2554,7 @@ void tf_ssb_remove_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_
void tf_ssb_add_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_connections_changed_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data)
{
tf_ssb_connections_changed_callback_node_t* node = malloc(sizeof(tf_ssb_connections_changed_callback_node_t));
tf_ssb_connections_changed_callback_node_t* node = tf_malloc(sizeof(tf_ssb_connections_changed_callback_node_t));
*node = (tf_ssb_connections_changed_callback_node_t)
{
.callback = callback,
@ -2583,7 +2581,7 @@ void tf_ssb_remove_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_connection
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
else
{
@ -2601,7 +2599,7 @@ void tf_ssb_add_rpc_callback(tf_ssb_t* ssb, const char** name, tf_ssb_rpc_callba
name_count++;
name_len += strlen(name[i]) + 1;
}
tf_ssb_rpc_callback_node_t* node = malloc(sizeof(tf_ssb_rpc_callback_node_t) + (name_count + 1) * sizeof(const char*) + name_len);
tf_ssb_rpc_callback_node_t* node = tf_malloc(sizeof(tf_ssb_rpc_callback_node_t) + (name_count + 1) * sizeof(const char*) + name_len);
*node = (tf_ssb_rpc_callback_node_t)
{
.name = (const char**)(node + 1),
@ -2650,7 +2648,7 @@ JSValue tf_ssb_connection_get_object(tf_ssb_connection_t* connection)
void tf_ssb_add_message_added_callback(tf_ssb_t* ssb, void (*callback)(tf_ssb_t* ssb, const char* id, void* user_data), void (*cleanup)(tf_ssb_t* ssb, void* user_data), void* user_data)
{
tf_ssb_message_added_callback_node_t* node = malloc(sizeof(tf_ssb_message_added_callback_node_t));
tf_ssb_message_added_callback_node_t* node = tf_malloc(sizeof(tf_ssb_message_added_callback_node_t));
*node = (tf_ssb_message_added_callback_node_t)
{
.callback = callback,
@ -2677,7 +2675,7 @@ void tf_ssb_remove_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_ca
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
else
{
@ -2699,7 +2697,7 @@ void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id)
void tf_ssb_add_blob_want_added_callback(tf_ssb_t* ssb, void (*callback)(tf_ssb_t* ssb, const char* id, void* user_data), void (*cleanup)(tf_ssb_t* ssb, void* user_data), void* user_data)
{
tf_ssb_blob_want_added_callback_node_t* node = malloc(sizeof(tf_ssb_blob_want_added_callback_node_t));
tf_ssb_blob_want_added_callback_node_t* node = tf_malloc(sizeof(tf_ssb_blob_want_added_callback_node_t));
*node = (tf_ssb_blob_want_added_callback_node_t)
{
.callback = callback,
@ -2726,7 +2724,7 @@ void tf_ssb_remove_blob_want_added_callback(tf_ssb_t* ssb, tf_ssb_blob_want_adde
{
node->cleanup(ssb, node->user_data);
}
free(node);
tf_free(node);
}
else
{

View File

@ -1,11 +1,11 @@
#include "ssb.connections.h"
#include "mem.h"
#include "ssb.h"
#include <uv.h>
#include <sqlite3.h>
#include <malloc.h>
#include <string.h>
#if !defined(_countof)
@ -97,7 +97,7 @@ static void _tf_ssb_connections_timer(uv_timer_t* timer)
tf_ssb_connections_t* tf_ssb_connections_create(tf_ssb_t* ssb)
{
tf_ssb_connections_t* connections = malloc(sizeof(tf_ssb_connections_t));
tf_ssb_connections_t* connections = tf_malloc(sizeof(tf_ssb_connections_t));
memset(connections, 0, sizeof(*connections));
connections->ssb = ssb;
connections->db = tf_ssb_get_db(ssb);
@ -117,7 +117,7 @@ static void _tf_ssb_connections_on_handle_close(uv_handle_t* handle)
{
tf_ssb_connections_t* connections = handle->data;
handle->data = NULL;
free(connections);
tf_free(connections);
}
void tf_ssb_connections_destroy(tf_ssb_connections_t* connections)

View File

@ -1,5 +1,6 @@
#include "ssb.db.h"
#include "mem.h"
#include "ssb.h"
#include "trace.h"
@ -248,7 +249,7 @@ bool tf_ssb_db_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_
int size = sqlite3_column_bytes(statement, 0);
if (out_blob)
{
*out_blob = malloc(size + 1);
*out_blob = tf_malloc(size + 1);
memcpy(*out_blob, blob, size);
(*out_blob)[size] = '\0';
}
@ -277,7 +278,7 @@ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_
int size = sqlite3_column_bytes(statement, 0);
if (out_blob)
{
*out_blob = malloc(size + 1);
*out_blob = tf_malloc(size + 1);
if (size)
{
memcpy(*out_blob, blob, size);
@ -376,7 +377,7 @@ bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* aut
}
if (out_content)
{
*out_content = strdup((const char*)sqlite3_column_text(statement, 2));
*out_content = tf_strdup((const char*)sqlite3_column_text(statement, 2));
}
found = true;
}

View File

@ -1,5 +1,6 @@
#include "ssb.export.h"
#include "mem.h"
#include "ssb.db.h"
#include "ssb.h"
@ -87,7 +88,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key)
_write_file(file_path, blob, size);
JSContext* context = tf_ssb_get_context(ssb);
JSValue app = JS_ParseJSON(context, (const char*)blob, size, NULL);
free(blob);
tf_free(blob);
JSValue files = JS_GetPropertyStr(context, app, "files");
JSPropertyEnum* ptab = NULL;
@ -109,7 +110,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key)
{
snprintf(file_path, sizeof(file_path), "apps/%s/%s/%s", user, path, file_name);
_write_file(file_path, file_blob, file_size);
free(file_blob);
tf_free(file_blob);
}
JS_FreeCString(context, file_name);

View File

@ -1,5 +1,6 @@
#include "ssb.import.h"
#include "mem.h"
#include "ssb.db.h"
#include "ssb.h"
@ -26,7 +27,7 @@ static void _tf_ssb_import_file_close(uv_fs_t* req)
tf_import_file_t* file = req->data;
(*file->work_left)--;
uv_fs_req_cleanup(req);
free(req->data);
tf_free(req->data);
}
static void _tf_ssb_import_add_app(tf_ssb_t* ssb, const char* user, const char* app)
@ -127,7 +128,7 @@ static void _tf_ssb_import_scandir(uv_fs_t* req)
while (uv_fs_scandir_next(req, &ent) == 0)
{
size_t len = strlen(import->parent) + strlen(ent.name) + 2;
char* path = malloc(len);
char* path = tf_malloc(len);
snprintf(path, len, "%s/%s", import->parent, ent.name);
if (ent.type == UV_DIRENT_DIR)
{
@ -136,7 +137,7 @@ static void _tf_ssb_import_scandir(uv_fs_t* req)
else
{
size_t size = sizeof(tf_import_file_t) + strlen(import->parent) +1 + strlen(ent.name) + 1;
tf_import_file_t* file = malloc(size);
tf_import_file_t* file = tf_malloc(size);
memset(file, 0, size);
file->ssb = import->ssb;
file->user = import->user;
@ -152,11 +153,11 @@ static void _tf_ssb_import_scandir(uv_fs_t* req)
if (r < 0)
{
printf("Failed to open %s: %s.\n", path, uv_strerror(r));
free(file);
tf_free(file);
import->work_left--;
}
}
free(path);
tf_free(path);
}
import->work_left--;
}

View File

@ -1,12 +1,12 @@
#include "ssb.js.h"
#include "database.js.h"
#include "mem.h"
#include "ssb.db.h"
#include "ssb.h"
#include "task.h"
#include "util.js.h"
#include <malloc.h>
#include <sodium/crypto_hash_sha256.h>
#include <sodium/crypto_sign.h>
#include <string.h>
@ -48,7 +48,7 @@ static JSValue _tf_ssb_getMessage(JSContext* context, JSValueConst this_val, int
result = JS_NewObject(context);
JS_SetPropertyStr(context, result, "timestamp", JS_NewFloat64(context, timestamp));
JS_SetPropertyStr(context, result, "content", JS_NewString(context, contents));
free(contents);
tf_free(contents);
}
JS_FreeCString(context, id);
}
@ -67,7 +67,7 @@ static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int ar
if (tf_ssb_db_blob_get(ssb, id, &blob, &size))
{
result = JS_NewArrayBufferCopy(context, blob, size);
free(blob);
tf_free(blob);
}
JS_FreeCString(context, id);
}
@ -133,7 +133,7 @@ static JSValue _tf_ssb_messageContentGet(JSContext* context, JSValueConst this_v
if (tf_ssb_db_message_content_get(ssb, id, &blob, &size))
{
result = JS_NewArrayBufferCopy(context, blob, size);
free(blob);
tf_free(blob);
}
JS_FreeCString(context, id);
}
@ -155,7 +155,7 @@ static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, in
{
JS_SetPropertyUint32(context, result, i, JS_NewString(context, *p));
}
free(connections);
tf_free(connections);
}
}
return result;
@ -618,7 +618,7 @@ void tf_ssb_run_file(JSContext* context, const char* file_name)
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
source = malloc(file_size + 1);
source = tf_malloc(file_size + 1);
fread(source, 1, file_size, file);
source[file_size] = '\0';
fclose(file);
@ -663,7 +663,7 @@ void tf_ssb_run_file(JSContext* context, const char* file_name)
}
JS_FreeValue(context, result);
free(source);
tf_free(source);
}
static JSValue _tf_ssb_add_event_listener(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)

View File

@ -1,5 +1,6 @@
#include "ssb.h"
#include "mem.h"
#include "ssb.db.h"
#include "ssb.js.h"
#include "tests.h"
@ -48,7 +49,7 @@ static void _ssb_test_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change,
{
count++;
}
free(c);
tf_free(c);
if (ssb == test->ssb0)
{
@ -294,7 +295,7 @@ void tf_ssb_test_following(const tf_test_options_t* options)
printf("* %s\n", *p); \
} \
printf("\n"); \
free(f); \
tf_free(f); \
} \
while (0)
#endif

View File

@ -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);
}

View File

@ -1,11 +1,11 @@
#include "taskstub.js.h"
#include "mem.h"
#include "packetstream.h"
#include "serialize.h"
#include "task.h"
#include "util.js.h"
#include <malloc.h>
#include <string.h>
#include <stdio.h>
@ -68,7 +68,7 @@ static void _taskstub_finalizer(JSRuntime *runtime, JSValue value);
static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_task_t* parent = tf_task_get(context);
tf_taskstub_t* stub = malloc(sizeof(tf_taskstub_t));
tf_taskstub_t* stub = tf_malloc(sizeof(tf_taskstub_t));
memset(stub, 0, sizeof(*stub));
stub->_stream = tf_packetstream_create();
@ -216,7 +216,7 @@ tf_task_t* tf_taskstub_get_owner(const tf_taskstub_t* stub)
tf_taskstub_t* tf_taskstub_create_parent(tf_task_t* task, uv_file file)
{
JSValue parentObject = JS_NewObject(tf_task_get_context(task));
tf_taskstub_t* parentStub = malloc(sizeof(tf_taskstub_t));
tf_taskstub_t* parentStub = tf_malloc(sizeof(tf_taskstub_t));
memset(parentStub, 0, sizeof(tf_taskstub_t));
parentStub->_stream = tf_packetstream_create();
parentStub->_on_exit = JS_UNDEFINED;
@ -249,7 +249,7 @@ static void _taskstub_cleanup(tf_taskstub_t* stub)
JS_IsUndefined(stub->_object) &&
stub->_finalized)
{
free(stub);
tf_free(stub);
}
}
@ -321,7 +321,7 @@ static JSValue _taskstub_setImports(JSContext* context, JSValueConst this_val, i
size_t size;
tf_serialize_store(tf_task_get(context), stub, &buffer, &size, argv[0]);
tf_packetstream_send(stub->_stream, kSetImports, (char*)buffer, size);
free(buffer);
tf_free(buffer);
return JS_UNDEFINED;
}
@ -332,7 +332,7 @@ static JSValue _taskstub_setRequires(JSContext* context, JSValueConst this_val,
size_t size;
tf_serialize_store(tf_task_get(context), stub, &buffer, &size, argv[0]);
tf_packetstream_send(stub->_stream, kSetRequires, (char*)buffer, size);
free(buffer);
tf_free(buffer);
return JS_UNDEFINED;
}
@ -343,7 +343,7 @@ static JSValue _taskstub_loadFile(JSContext* context, JSValueConst this_val, int
size_t size;
tf_serialize_store(tf_task_get(context), stub, &buffer, &size, argv[0]);
tf_packetstream_send(stub->_stream, kLoadFile, (char*)buffer, size);
free(buffer);
tf_free(buffer);
return JS_UNDEFINED;
}

View File

@ -1,5 +1,6 @@
#include "tests.h"
#include "mem.h"
#include "ssb.tests.h"
#include <assert.h>
@ -559,7 +560,7 @@ static void _tf_test_run(const tf_test_options_t* options, const char* name, voi
bool specified = false;
if (options->tests)
{
char* dup = strdup(options->tests);
char* dup = tf_strdup(options->tests);
char* state = NULL;
const char* t = NULL;
while ((t = strtok_r(t ? NULL : dup, ",", &state)) != NULL)
@ -570,7 +571,7 @@ static void _tf_test_run(const tf_test_options_t* options, const char* name, voi
break;
}
}
free(dup);
tf_free(dup);
}
if (!options->tests || specified)

View File

@ -1,8 +1,9 @@
#include "tls.h"
#include "mem.h"
#if defined(_WIN32)
#define TF_TLS_SCHANNEL
#include <malloc.h>
#elif defined(__MACH__)
#define TF_TLS_APPLE
#else
@ -1050,7 +1051,7 @@ TlsContext* TlsContext::create()
tf_tls_context_t* tf_tls_context_create()
{
tf_tls_context_t* context = malloc(sizeof(tf_tls_context_t));
tf_tls_context_t* context = tf_malloc(sizeof(tf_tls_context_t));
memset(context, 0, sizeof(*context));
#if defined(TF_TLS_OPENSSL)
SSL_library_init();
@ -1135,7 +1136,7 @@ bool tf_tls_context_add_trusted_certificate(tf_tls_context_t* context, const cha
tf_tls_session_t* tf_tls_context_create_session(tf_tls_context_t* context)
{
#if defined(TF_TLS_OPENSSL)
tf_tls_session_t* session = malloc(sizeof(tf_tls_session_t));
tf_tls_session_t* session = tf_malloc(sizeof(tf_tls_session_t));
memset(session, 0, sizeof(*session));
session->context = context;
session->bio_in = BIO_new(BIO_s_mem());
@ -1151,7 +1152,7 @@ void tf_tls_context_destroy(tf_tls_context_t* context)
{
#if defined(TF_TLS_OPENSSL)
SSL_CTX_free(context->context);
free(context);
tf_free(context);
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
#endif
@ -1166,9 +1167,9 @@ void tf_tls_session_destroy(tf_tls_session_t* session)
}
if (session->hostname)
{
free((void*)session->hostname);
tf_free((void*)session->hostname);
}
free(session);
tf_free(session);
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
#endif
@ -1179,12 +1180,12 @@ void tf_tls_session_set_hostname(tf_tls_session_t* session, const char* hostname
#if defined(TF_TLS_OPENSSL)
if (session->hostname)
{
free((void*)session->hostname);
tf_free((void*)session->hostname);
session->hostname = NULL;
}
if (hostname)
{
session->hostname = strdup(hostname);
session->hostname = tf_strdup(hostname);
}
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)

View File

@ -1,5 +1,6 @@
#include "tlscontext.js.h"
#include "mem.h"
#include "task.h"
#include "tls.h"
@ -78,7 +79,7 @@ int tf_tls_context_get_count()
JSValue _tls_context_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_tls_context_t* tls = malloc(sizeof(tf_tls_context_t));
tf_tls_context_t* tls = tf_malloc(sizeof(tf_tls_context_t));
memset(tls, 0, sizeof(*tls));
++_count;
@ -104,5 +105,5 @@ void _tls_context_finalizer(JSRuntime *runtime, JSValue value)
tls->context = NULL;
}
--_count;
free(tls);
tf_free(tls);
}

View File

@ -1,7 +1,8 @@
#include "trace.h"
#include "mem.h"
#include <assert.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
@ -56,7 +57,7 @@ static void _trace_append(tf_trace_t* trace, const char* buffer, size_t size, vo
tf_trace_t* tf_trace_create()
{
tf_trace_t* trace = malloc(sizeof(tf_trace_t));
tf_trace_t* trace = tf_malloc(sizeof(tf_trace_t));
memset(trace, 0, sizeof(*trace));
trace->callback = _trace_append;
return trace;
@ -68,9 +69,9 @@ void tf_trace_destroy(tf_trace_t* trace)
{
tf_trace_stack_t* stack = trace->stack;
trace->stack = stack->next;
free(stack);
tf_free(stack);
}
free(trace);
tf_free(trace);
}
void tf_trace_raw(tf_trace_t* trace, const char* buffer, size_t size)
@ -116,7 +117,7 @@ void tf_trace_begin(tf_trace_t* trace, const char* name)
if (!trace->stack || trace->stack->count + 1 > _countof(trace->stack->names))
{
tf_trace_stack_t* stack = malloc(sizeof(tf_trace_stack_t));
tf_trace_stack_t* stack = tf_malloc(sizeof(tf_trace_stack_t));
memset(stack, 0, sizeof(*stack));
stack->next = trace->stack;
trace->stack = stack;
@ -205,7 +206,7 @@ char* tf_trace_export(tf_trace_t* trace)
}
static const int k_extra_size = 1024;
char* buffer = malloc(k_buffer_size + k_extra_size);
char* buffer = tf_malloc(k_buffer_size + k_extra_size);
const char* newline = strchr(trace->buffer + trace->write_offset, '\n');
int begin = newline ? newline - trace->buffer : 0;
size_t size = 0;

View File

@ -1,5 +1,6 @@
#include "util.js.h"
#include "mem.h"
#include "task.h"
#include "trace.h"
@ -7,128 +8,8 @@
#include <uv.h>
#include <openssl/crypto.h>
#include <string.h>
static int64_t s_uv_malloc_size;
static int64_t s_tls_malloc_size;
static void* _tf_alloc(int64_t* total, size_t size)
{
void* ptr = malloc(size + sizeof(size_t));
if (ptr)
{
__atomic_add_fetch(total, size, __ATOMIC_RELAXED);
memcpy(ptr, &size, sizeof(size_t));
return (void*)((intptr_t)ptr + sizeof(size_t));
}
else
{
return NULL;
}
}
static void* _tf_realloc(int64_t* total, 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(total, (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(total, old_size, __ATOMIC_RELAXED);
return NULL;
}
}
static void _tf_free(int64_t* total, 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(total, size, __ATOMIC_RELAXED);
free(old_ptr);
}
}
static void* _tf_uv_alloc(size_t size)
{
return _tf_alloc(&s_uv_malloc_size, size);
}
static void* _tf_uv_realloc(void* ptr, size_t size)
{
return _tf_realloc(&s_uv_malloc_size, ptr, size);
}
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)
{
_tf_free(&s_uv_malloc_size, 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_get_uv_malloc_size()
{
return s_uv_malloc_size;
}
void* _tf_tls_alloc(size_t size, const char* file, int line)
{
return _tf_alloc(&s_tls_malloc_size, size);
}
void* _tf_tls_realloc(void* ptr, size_t size, const char* file, int line)
{
return _tf_realloc(&s_tls_malloc_size, ptr, size);
}
void _tf_tls_free(void* ptr, const char* file, int line)
{
_tf_free(&s_tls_malloc_size, ptr);
}
void tf_util_replace_tls_allocator()
{
CRYPTO_set_mem_functions(_tf_tls_alloc, _tf_tls_realloc, _tf_tls_free);
}
size_t tf_util_get_tls_malloc_size()
{
return s_tls_malloc_size;
}
static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
size_t length = 0;
@ -271,7 +152,7 @@ typedef struct _timeout_t {
static void _handle_closed(uv_handle_t* handle)
{
free(handle);
tf_free(handle);
}
static void _util_timeoutCallback(uv_timer_t* handle)
@ -288,7 +169,7 @@ static void _util_timeoutCallback(uv_timer_t* handle)
tf_util_report_error(context, result);
JS_FreeValue(context, result);
tf_trace_end(tf_task_get_trace(timeout->_task));
free(timeout);
tf_free(timeout);
uv_close((uv_handle_t*)handle, _handle_closed);
}
@ -296,14 +177,14 @@ static JSValue _util_setTimeout(JSContext* context, JSValueConst this_val, int a
{
tf_task_t* task = JS_GetContextOpaque(context);
timeout_t* timeout = malloc(sizeof(timeout_t));
timeout_t* timeout = tf_malloc(sizeof(timeout_t));
*timeout = (timeout_t)
{
._task = task,
._callback = JS_DupValue(context, argv[0]),
};
uv_timer_t* timer = malloc(sizeof(uv_timer_t));
uv_timer_t* timer = tf_malloc(sizeof(uv_timer_t));
memset(timer, 0, sizeof(uv_timer_t));
uv_timer_init(tf_task_get_loop(task), timer);
timer->data = timeout;

View File

@ -4,12 +4,6 @@
#include <stdbool.h>
void tf_util_replace_uv_allocator();
size_t tf_util_get_uv_malloc_size();
void tf_util_replace_tls_allocator();
size_t tf_util_get_tls_malloc_size();
void tf_util_register(JSContext* context);
JSValue tf_util_utf8_decode(JSContext* context, JSValue value);
uint8_t* tf_util_try_get_array_buffer(JSContext* context, size_t* psize, JSValueConst obj);