pull/1/head
Jeff Becker 6 years ago
parent c0ff64868d
commit 91a9108300
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -10,7 +10,7 @@ find_package(sodium)
set(EXE llarpd) set(EXE llarpd)
set(EXE_SRC daemon/main.c) set(EXE_SRC daemon/main.c)
set(LIBS ${sodium_LIBRARY_RELEASE} pthread stdc++fs) set(LIBS ${sodium_LIBRARY_RELEASE} pthread stdc++fs jemalloc)
set(LIB llarp) set(LIB llarp)

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
struct llarp_main { struct llarp_main {
struct llarp_alloc mem;
struct llarp_router *router; struct llarp_router *router;
struct llarp_threadpool *worker; struct llarp_threadpool *worker;
struct llarp_logic *logic; struct llarp_logic *logic;
@ -82,6 +83,7 @@ int shutdown_llarp(struct llarp_main *m) {
} }
struct llarp_main llarp = { struct llarp_main llarp = {
{0,0,0},
0, 0,
0, 0,
0, 0,
@ -95,7 +97,8 @@ struct llarp_main llarp = {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
const char *conffname = "daemon.ini"; const char *conffname = "daemon.ini";
if (argc > 1) conffname = argv[1]; if (argc > 1) conffname = argv[1];
llarp_mem_stdlib(); llarp_mem_jemalloc(&llarp.mem);
struct llarp_alloc * mem = &llarp.mem;
llarp_new_config(&llarp.config); llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop); llarp_ev_loop_alloc(&llarp.mainloop);
printf("%s loading config file %s\n", LLARP_VERSION, conffname); printf("%s loading config file %s\n", LLARP_VERSION, conffname);
@ -105,8 +108,8 @@ int main(int argc, char *argv[]) {
iter.visit = iter_main_config; iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter); llarp_config_iter(llarp.config, &iter);
llarp.nodedb = llarp_nodedb_new(); llarp.nodedb = llarp_nodedb_new(mem);
if (llarp.nodedb_dir[0]) { if (llarp.nodedb_dir[0]) {
llarp.nodedb_dir[sizeof(llarp.nodedb_dir) - 1] = 0; llarp.nodedb_dir[sizeof(llarp.nodedb_dir) - 1] = 0;
char *dir = llarp.nodedb_dir; char *dir = llarp.nodedb_dir;
@ -114,7 +117,7 @@ int main(int argc, char *argv[]) {
// ensure worker thread pool // ensure worker thread pool
if (!llarp.worker) llarp.worker = llarp_init_threadpool(2); if (!llarp.worker) llarp.worker = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.worker, llarp.mainloop); llarp.router = llarp_init_router(mem, llarp.worker, llarp.mainloop);
if (llarp_configure_router(llarp.router, llarp.config)) { if (llarp_configure_router(llarp.router, llarp.config)) {

@ -24,7 +24,7 @@ bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t *buff);
struct llarp_ai_list; struct llarp_ai_list;
struct llarp_ai_list *llarp_ai_list_new(); struct llarp_ai_list *llarp_ai_list_new(struct llarp_alloc * mem);
void llarp_ai_list_free(struct llarp_ai_list **l); void llarp_ai_list_free(struct llarp_ai_list **l);
bool llarp_ai_list_bencode(struct llarp_ai_list *l, llarp_buffer_t *buff); bool llarp_ai_list_bencode(struct llarp_ai_list *l, llarp_buffer_t *buff);

@ -10,10 +10,13 @@ extern "C" {
struct llarp_async_dh; struct llarp_async_dh;
struct llarp_async_dh *llarp_async_dh_new(llarp_seckey_t ourkey, struct llarp_async_dh *llarp_async_dh_new(
struct llarp_crypto *crypto, struct llarp_alloc * mem,
struct llarp_threadpool *handler, llarp_seckey_t ourkey,
struct llarp_threadpool *worker); struct llarp_crypto *crypto,
struct llarp_threadpool *handler,
struct llarp_threadpool *worker);
void llarp_async_dh_free(struct llarp_async_dh **dh); void llarp_async_dh_free(struct llarp_async_dh **dh);
struct llarp_dh_result; struct llarp_dh_result;
@ -46,6 +49,7 @@ struct llarp_cipher_result {
}; };
struct llarp_async_cipher *llarp_async_cipher_new( struct llarp_async_cipher *llarp_async_cipher_new(
struct llarp_alloc * mem,
llarp_sharedkey_t key, struct llarp_crypto *crypto, llarp_sharedkey_t key, struct llarp_crypto *crypto,
struct llarp_threadpool *result, struct llarp_threadpool *worker); struct llarp_threadpool *result, struct llarp_threadpool *worker);

@ -1,6 +1,7 @@
#ifndef LLARP_DTLS_H_ #ifndef LLARP_DTLS_H_
#define LLARP_DTLS_H_ #define LLARP_DTLS_H_
#include <llarp/mem.h>
#include <llarp/link.h> #include <llarp/link.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -8,6 +9,7 @@ extern "C" {
#endif #endif
struct llarp_dtls_args { struct llarp_dtls_args {
struct llarp_alloc * mem;
char key_file[255]; char key_file[255];
char cert_file[255]; char cert_file[255];
}; };

@ -1,26 +1,26 @@
#ifndef LLARP_MEM_H_ #ifndef LLARP_MEM_H_
#define LLARP_MEM_H_ #define LLARP_MEM_H_
#include <stdlib.h>
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <stdlib.h>
struct llarp_alloc { struct llarp_alloc {
void *(*alloc)(size_t sz, size_t align); void * impl;
void (*free)(void *ptr); void *(*alloc)(struct llarp_alloc * mem, size_t sz, size_t align);
void (*free)(struct llarp_alloc * mem, void *ptr);
}; };
void llarp_mem_stdlib(struct llarp_alloc * mem);
void llarp_mem_jemalloc(struct llarp_alloc * mem);
void llarp_mem_dmalloc(struct llarp_alloc * mem);
void llarp_mem_slab(struct llarp_alloc * mem, uint32_t * buf, size_t sz);
/** global memory allocator */
extern struct llarp_alloc llarp_g_mem;
/** init llarp_g_mem with stdlib malloc */
void llarp_mem_stdlib();
/** init llarp_g_mem with jemalloc */
void llarp_mem_jemalloc();
/** init llarp_g_mem with dmalloc */
void llarp_mem_dmalloc();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -9,7 +9,7 @@ extern "C" {
struct llarp_nodedb; struct llarp_nodedb;
/** create an empty nodedb */ /** create an empty nodedb */
struct llarp_nodedb *llarp_nodedb_new(); struct llarp_nodedb *llarp_nodedb_new(struct llarp_alloc * mem);
/** free a nodedb and all loaded rc */ /** free a nodedb and all loaded rc */
void llarp_nodedb_free(struct llarp_nodedb **n); void llarp_nodedb_free(struct llarp_nodedb **n);

@ -13,7 +13,7 @@ extern "C" {
struct llarp_router; struct llarp_router;
struct llarp_router *llarp_init_router(struct llarp_threadpool *worker, struct llarp_ev_loop * netloop); struct llarp_router *llarp_init_router(struct llarp_alloc * mem, struct llarp_threadpool *worker, struct llarp_ev_loop * netloop);
void llarp_free_router(struct llarp_router **router); void llarp_free_router(struct llarp_router **router);
bool llarp_configure_router(struct llarp_router *router, bool llarp_configure_router(struct llarp_router *router,

@ -14,7 +14,7 @@ struct llarp_rc {
llarp_sig_t signature; llarp_sig_t signature;
}; };
bool llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buf); bool llarp_rc_bdecode(struct llarp_alloc * mem, struct llarp_rc *rc, llarp_buffer_t *buf);
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buf); bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buf);
void llarp_rc_free(struct llarp_rc *rc); void llarp_rc_free(struct llarp_rc *rc);
bool llarp_rc_verify_sig(struct llarp_rc *rc); bool llarp_rc_verify_sig(struct llarp_rc *rc);

@ -60,12 +60,14 @@ struct llarp_ai_list_node {
}; };
struct llarp_ai_list { struct llarp_ai_list {
struct llarp_alloc * mem;
struct llarp_ai_list_node *root; struct llarp_ai_list_node *root;
}; };
struct llarp_ai_list *llarp_ai_list_new() { struct llarp_ai_list *llarp_ai_list_new(struct llarp_alloc * mem) {
struct llarp_ai_list *l = llarp_g_mem.alloc(sizeof(struct llarp_ai_list), 8); struct llarp_ai_list *l = mem->alloc(mem, sizeof(struct llarp_ai_list), 8);
if (l) { if (l) {
l->mem = mem;
l->root = NULL; l->root = NULL;
} }
return l; return l;
@ -73,13 +75,14 @@ struct llarp_ai_list *llarp_ai_list_new() {
void llarp_ai_list_free(struct llarp_ai_list **l) { void llarp_ai_list_free(struct llarp_ai_list **l) {
if (*l) { if (*l) {
struct llarp_alloc * mem = (*l)->mem;
struct llarp_ai_list_node *cur = (*l)->root; struct llarp_ai_list_node *cur = (*l)->root;
while (cur) { while (cur) {
struct llarp_ai_list_node *tmp = cur->next; struct llarp_ai_list_node *tmp = cur->next;
llarp_g_mem.free(cur); mem->free(mem, cur);
cur = tmp; cur = tmp;
} }
llarp_g_mem.free(*l); mem->free(mem, *l);
*l = NULL; *l = NULL;
} }
} }

@ -40,7 +40,7 @@ bool llarp_buffer_readfile(llarp_buffer_t* buff, FILE* f, llarp_alloc* mem) {
len = ftell(f); len = ftell(f);
rewind(f); rewind(f);
if (len > 0) { if (len > 0) {
buff->base = static_cast<char*>(mem->alloc(len, 8)); buff->base = static_cast<char*>(mem->alloc(mem, len, 8));
buff->cur = buff->base; buff->cur = buff->base;
buff->sz = len; buff->sz = len;
ssize_t sz = fread(buff->base, len, 1, f); ssize_t sz = fread(buff->base, len, 1, f);

@ -8,6 +8,7 @@ struct llarp_async_dh {
struct llarp_threadpool *worker; struct llarp_threadpool *worker;
struct llarp_threadpool *result; struct llarp_threadpool *result;
llarp_seckey_t ourkey; llarp_seckey_t ourkey;
struct llarp_alloc * mem;
}; };
struct llarp_dh_internal { struct llarp_dh_internal {
@ -21,7 +22,7 @@ struct llarp_dh_internal {
static void llarp_crypto_dh_result(void *call) { static void llarp_crypto_dh_result(void *call) {
struct llarp_dh_internal *impl = (struct llarp_dh_internal *)call; struct llarp_dh_internal *impl = (struct llarp_dh_internal *)call;
impl->result.hook(&impl->result); impl->result.hook(&impl->result);
llarp_g_mem.free(impl); impl->parent->mem->free(impl->parent->mem, impl);
} }
static void llarp_crypto_dh_work(void *user) { static void llarp_crypto_dh_work(void *user) {
@ -37,7 +38,7 @@ static void llarp_async_dh_exec(struct llarp_async_dh *dh, llarp_dh_func func,
llarp_tunnel_nounce_t nounce, llarp_tunnel_nounce_t nounce,
llarp_dh_complete_hook result, void *user) { llarp_dh_complete_hook result, void *user) {
struct llarp_dh_internal *impl = struct llarp_dh_internal *impl =
llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32); dh->mem->alloc(dh->mem, sizeof(struct llarp_dh_internal), 32);
struct llarp_thread_job job = {.user = impl, .work = &llarp_crypto_dh_work}; struct llarp_thread_job job = {.user = impl, .work = &llarp_crypto_dh_work};
memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t)); memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t));
memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t)); memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t));
@ -60,12 +61,15 @@ void llarp_async_server_dh(struct llarp_async_dh *dh, llarp_pubkey_t theirkey,
llarp_async_dh_exec(dh, dh->server, theirkey, nounce, result, user); llarp_async_dh_exec(dh, dh->server, theirkey, nounce, result, user);
} }
struct llarp_async_dh *llarp_async_dh_new(llarp_seckey_t ourkey, struct llarp_async_dh *llarp_async_dh_new(
struct llarp_crypto *crypto, struct llarp_alloc * mem,
struct llarp_threadpool *result, llarp_seckey_t ourkey,
struct llarp_threadpool *worker) { struct llarp_crypto *crypto,
struct llarp_threadpool *result,
struct llarp_threadpool *worker) {
struct llarp_async_dh *dh = struct llarp_async_dh *dh =
llarp_g_mem.alloc(sizeof(struct llarp_async_dh), 16); mem->alloc(mem, sizeof(struct llarp_async_dh), 16);
dh->mem = mem;
dh->client = crypto->dh_client; dh->client = crypto->dh_client;
dh->server = crypto->dh_server; dh->server = crypto->dh_server;
memcpy(dh->ourkey, ourkey, sizeof(llarp_seckey_t)); memcpy(dh->ourkey, ourkey, sizeof(llarp_seckey_t));
@ -76,7 +80,8 @@ struct llarp_async_dh *llarp_async_dh_new(llarp_seckey_t ourkey,
void llarp_async_dh_free(struct llarp_async_dh **dh) { void llarp_async_dh_free(struct llarp_async_dh **dh) {
if (*dh) { if (*dh) {
llarp_g_mem.free(*dh); struct llarp_alloc * mem = (*dh)->mem;
mem->free(mem, *dh);
*dh = NULL; *dh = NULL;
} }
} }
@ -93,13 +98,15 @@ struct llarp_async_cipher {
struct llarp_threadpool *worker; struct llarp_threadpool *worker;
struct llarp_threadpool *result; struct llarp_threadpool *result;
llarp_sym_cipher_func func; llarp_sym_cipher_func func;
struct llarp_alloc * mem;
}; };
static void llarp_crypto_cipher_result(void *user) { static void llarp_crypto_cipher_result(void *user) {
struct llarp_async_cipher_internal *impl = struct llarp_async_cipher_internal *impl =
(struct llarp_async_cipher_internal *)user; (struct llarp_async_cipher_internal *)user;
struct llarp_alloc * mem = impl->parent->mem;
impl->result.hook(&impl->result); impl->result.hook(&impl->result);
llarp_g_mem.free(impl); mem->free(mem, impl);
} }
static void llarp_crypto_cipher_work(void *work) { static void llarp_crypto_cipher_work(void *work) {
@ -114,8 +121,9 @@ static void llarp_crypto_cipher_work(void *work) {
void llarp_async_cipher_queue_op(struct llarp_async_cipher *c, void llarp_async_cipher_queue_op(struct llarp_async_cipher *c,
llarp_buffer_t *buff, llarp_nounce_t n, llarp_buffer_t *buff, llarp_nounce_t n,
llarp_cipher_complete_hook h, void *user) { llarp_cipher_complete_hook h, void *user) {
struct llarp_alloc * mem = c->mem;
struct llarp_async_cipher_internal *impl = struct llarp_async_cipher_internal *impl =
llarp_g_mem.alloc(sizeof(struct llarp_async_cipher_internal), 16); mem->alloc(mem, sizeof(struct llarp_async_cipher_internal), 16);
impl->parent = c; impl->parent = c;
memcpy(impl->nounce, n, sizeof(llarp_nounce_t)); memcpy(impl->nounce, n, sizeof(llarp_nounce_t));
impl->result.user = user; impl->result.user = user;
@ -129,10 +137,13 @@ void llarp_async_cipher_queue_op(struct llarp_async_cipher *c,
} }
struct llarp_async_cipher *llarp_async_cipher_new( struct llarp_async_cipher *llarp_async_cipher_new(
llarp_sharedkey_t key, struct llarp_crypto *crypto, struct llarp_alloc * mem,
struct llarp_threadpool *result, struct llarp_threadpool *worker) { llarp_sharedkey_t key, struct llarp_crypto *crypto,
struct llarp_threadpool *result, struct llarp_threadpool *worker) {
struct llarp_async_cipher *cipher = struct llarp_async_cipher *cipher =
llarp_g_mem.alloc(sizeof(struct llarp_async_cipher), 16); mem->alloc(mem, sizeof(struct llarp_async_cipher), 16);
cipher->mem = mem;
cipher->func = crypto->xchacha20; cipher->func = crypto->xchacha20;
cipher->result = result; cipher->result = result;
cipher->worker = worker; cipher->worker = worker;
@ -142,7 +153,8 @@ struct llarp_async_cipher *llarp_async_cipher_new(
void llarp_async_cipher_free(struct llarp_async_cipher **c) { void llarp_async_cipher_free(struct llarp_async_cipher **c) {
if (*c) { if (*c) {
llarp_g_mem.free(*c); struct llarp_alloc * mem = (*c)->mem;
mem->free(mem, *c);
*c = NULL; *c = NULL;
} }
} }

@ -3,13 +3,16 @@
struct dtls_link struct dtls_link
{ {
struct llarp_alloc * mem;
struct llarp_logic * logic; struct llarp_logic * logic;
uint32_t timeout_job_id; uint32_t timeout_job_id;
}; };
static struct dtls_link * dtls_link_alloc(struct llarp_msg_muxer * muxer, char * keyfile, char * certfile) static struct dtls_link * dtls_link_alloc(struct llarp_alloc * mem, struct llarp_msg_muxer * muxer, char * keyfile, char * certfile)
{ {
struct dtls_link * link = llarp_g_mem.alloc(sizeof(struct dtls_link), 8); struct dtls_link * link = mem->alloc(mem, sizeof(struct dtls_link), 8);
if(link)
link->mem = mem;
return link; return link;
} }
@ -88,14 +91,16 @@ static void dtls_link_try_establish(struct llarp_link * link, struct llarp_link_
static void dtls_link_free(struct llarp_link *l) static void dtls_link_free(struct llarp_link *l)
{ {
llarp_g_mem.free(l->impl); struct dtls_link * link = l->impl;
struct llarp_alloc * mem = link->mem;
mem->free(mem, link);
} }
void dtls_link_init(struct llarp_link * link, struct llarp_dtls_args args, struct llarp_msg_muxer * muxer) void dtls_link_init(struct llarp_link * link, struct llarp_dtls_args args, struct llarp_msg_muxer * muxer)
{ {
link->impl = dtls_link_alloc(muxer, args.key_file, args.cert_file); link->impl = dtls_link_alloc(args.mem, muxer, args.key_file, args.cert_file);
link->name = dtls_link_name; link->name = dtls_link_name;
/* /*
link->register_listener = dtls_link_reg_listener; link->register_listener = dtls_link_reg_listener;

@ -10,12 +10,14 @@ struct llarp_xi_list_node {
}; };
struct llarp_xi_list { struct llarp_xi_list {
struct llarp_alloc * mem;
struct llarp_xi_list_node *root; struct llarp_xi_list_node *root;
}; };
struct llarp_xi_list *llarp_xi_list_new() { struct llarp_xi_list *llarp_xi_list_new(struct llarp_alloc * mem) {
struct llarp_xi_list *l = llarp_g_mem.alloc(sizeof(struct llarp_xi_list), 8); struct llarp_xi_list *l = mem->alloc(mem, sizeof(struct llarp_xi_list), 8);
if (l) { if (l) {
l->mem = mem;
l->root = NULL; l->root = NULL;
} }
return l; return l;
@ -23,13 +25,14 @@ struct llarp_xi_list *llarp_xi_list_new() {
void llarp_xi_list_free(struct llarp_xi_list **l) { void llarp_xi_list_free(struct llarp_xi_list **l) {
if (*l) { if (*l) {
struct llarp_alloc * mem = (*l)->mem;
struct llarp_xi_list_node *current = (*l)->root; struct llarp_xi_list_node *current = (*l)->root;
while (current) { while (current) {
struct llarp_xi_list_node *tmp = current->next; struct llarp_xi_list_node *tmp = current->next;
llarp_g_mem.free(current); mem->free(mem, current);
current = tmp; current = tmp;
} }
llarp_g_mem.free(*l); mem->free(mem, *l);
*l = NULL; *l = NULL;
} }
} }

@ -24,7 +24,7 @@ static bool iwp_link_configure(struct llarp_link * l, const char * ifname, int a
static struct iwp_link * iwp_link_alloc(struct iwp_configure_args * args) static struct iwp_link * iwp_link_alloc(struct iwp_configure_args * args)
{ {
struct iwp_link * l = args->mem->alloc(sizeof(struct iwp_link), 16); struct iwp_link * l = args->mem->alloc(args->mem, sizeof(struct iwp_link), 16);
l->alloc = args->mem; l->alloc = args->mem;
l->netloop = args->ev; l->netloop = args->ev;
l->keyfile = args->keyfile; l->keyfile = args->keyfile;

@ -2,13 +2,15 @@
#include <llarp/mem.h> #include <llarp/mem.h>
struct llarp_logic { struct llarp_logic {
struct llarp_alloc * mem;
struct llarp_threadpool* thread; struct llarp_threadpool* thread;
struct llarp_timer_context* timer; struct llarp_timer_context* timer;
}; };
struct llarp_logic* llarp_init_logic() { struct llarp_logic* llarp_init_logic(struct llarp_alloc * mem) {
struct llarp_logic* logic = llarp_g_mem.alloc(sizeof(struct llarp_logic), 8); struct llarp_logic* logic = mem->alloc(mem, sizeof(struct llarp_logic), 8);
if (logic) { if (logic) {
logic->mem = mem;
logic->thread = llarp_init_threadpool(1); logic->thread = llarp_init_threadpool(1);
logic->timer = llarp_init_timer(); logic->timer = llarp_init_timer();
} }
@ -17,9 +19,10 @@ struct llarp_logic* llarp_init_logic() {
void llarp_free_logic(struct llarp_logic** logic) { void llarp_free_logic(struct llarp_logic** logic) {
if (*logic) { if (*logic) {
struct llarp_alloc * mem = (*logic)->mem;
llarp_free_threadpool(&(*logic)->thread); llarp_free_threadpool(&(*logic)->thread);
llarp_free_timer(&(*logic)->timer); llarp_free_timer(&(*logic)->timer);
llarp_g_mem.free(*logic); mem->free(mem, *logic);
*logic = NULL; *logic = NULL;
} }
} }

@ -1,5 +1,11 @@
#include <llarp/mem.h> #include <llarp/mem.h>
extern "C" { extern "C" {
struct llarp_alloc llarp_g_mem = {.alloc = nullptr, .free = nullptr};
void llarp_mem_slab(struct llarp_alloc * mem, uint32_t * buf, size_t sz)
{
// not implemented
abort();
}
} }

@ -15,8 +15,8 @@ static constexpr size_t alignment() {
} }
template <typename T> template <typename T>
static T *Alloc(llarp_alloc *mem = &llarp_g_mem) { static T *Alloc(llarp_alloc *mem) {
return static_cast<T *>(mem->alloc(sizeof(T), alignment<T>())); return static_cast<T *>(mem->alloc(mem, sizeof(T), alignment<T>()));
} }
} // namespace llarp } // namespace llarp

@ -2,18 +2,20 @@
#include <llarp/mem.h> #include <llarp/mem.h>
namespace llarp { namespace llarp {
static void *jem_malloc(size_t sz, size_t align) { static void *jem_malloc(struct llarp_alloc * mem, size_t sz, size_t align) {
return mallocx(sz, MALLOCX_ALIGN(align)); (void) mem;
} return mallocx(sz, MALLOCX_ALIGN(align));
}
static void jem_free(void *ptr) { static void jem_free(struct llarp_alloc * mem, void *ptr) {
if (ptr) free(ptr); (void) mem;
} if (ptr) free(ptr);
}
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
void llarp_mem_jemalloc() { void llarp_mem_jemalloc(struct llarp_alloc * mem) {
llarp_g_mem.alloc = llarp::jem_malloc; mem->alloc = llarp::jem_malloc;
llarp_g_mem.free = llarp::jem_free; mem->free = llarp::jem_free;
} }
} }

@ -2,7 +2,8 @@
#include <cstring> #include <cstring>
namespace llarp { namespace llarp {
void *std_malloc(size_t sz, size_t align) { void *std_malloc(struct llarp_alloc *mem, size_t sz, size_t align) {
(void)mem;
(void)align; (void)align;
void *ptr = malloc(sz); void *ptr = malloc(sz);
if (ptr) { if (ptr) {
@ -12,15 +13,16 @@ void *std_malloc(size_t sz, size_t align) {
abort(); abort();
} }
void std_free(void *ptr) { void std_free(struct llarp_alloc * mem, void *ptr) {
(void) mem;
if (ptr) free(ptr); if (ptr) free(ptr);
} }
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
void llarp_mem_stdlib() { void llarp_mem_stdlib(struct llarp_alloc * mem) {
llarp_g_mem.alloc = llarp::std_malloc; mem->alloc = llarp::std_malloc;
llarp_g_mem.free = llarp::std_free; mem->free = llarp::std_free;
} }
} }

@ -3,12 +3,27 @@
#include <map> #include <map>
#include "crypto.hpp" #include "crypto.hpp"
#include "fs.hpp" #include "fs.hpp"
#include "mem.hpp"
static const char skiplist_subdirs[] = "0123456789ABCDEF"; static const char skiplist_subdirs[] = "0123456789ABCDEF";
struct llarp_nodedb { struct llarp_nodedb {
llarp_nodedb(struct llarp_alloc * m) : mem(m) {}
llarp_alloc * mem;
std::map<llarp::pubkey, llarp_rc *> entries; std::map<llarp::pubkey, llarp_rc *> entries;
void Clear()
{
auto itr = entries.begin();
while(itr != entries.end())
{
mem->free(mem, itr->second);
itr = entries.erase(itr);
}
}
ssize_t Load(const fs::path &path) { ssize_t Load(const fs::path &path) {
std::error_code ec; std::error_code ec;
if (!fs::exists(path, ec)) { if (!fs::exists(path, ec)) {
@ -27,16 +42,16 @@ struct llarp_nodedb {
} }
bool loadfile(const fs::path &fpath) { bool loadfile(const fs::path &fpath) {
llarp_rc *rc = new llarp_rc; llarp_rc *rc = llarp::Alloc<llarp_rc>(mem);
llarp_buffer_t buff; llarp_buffer_t buff;
FILE *f = fopen(fpath.c_str(), "rb"); FILE *f = fopen(fpath.c_str(), "rb");
if (!f) return false; if (!f) return false;
if (!llarp_buffer_readfile(&buff, f, &llarp_g_mem)) { if (!llarp_buffer_readfile(&buff, f, mem)) {
fclose(f); fclose(f);
return false; return false;
} }
fclose(f); fclose(f);
if (llarp_rc_bdecode(rc, &buff)) { if (llarp_rc_bdecode(mem, rc, &buff)) {
if (llarp_rc_verify_sig(rc)) { if (llarp_rc_verify_sig(rc)) {
llarp::pubkey pk; llarp::pubkey pk;
memcpy(pk.data(), rc->pubkey, pk.size()); memcpy(pk.data(), rc->pubkey, pk.size());
@ -45,7 +60,7 @@ struct llarp_nodedb {
} }
} }
llarp_rc_free(rc); llarp_rc_free(rc);
delete rc; mem->free(mem, rc);
return false; return false;
} }
@ -60,12 +75,20 @@ struct llarp_nodedb {
extern "C" { extern "C" {
struct llarp_nodedb *llarp_nodedb_new() { struct llarp_nodedb *llarp_nodedb_new(struct llarp_alloc * mem) {
return new llarp_nodedb; void * ptr = mem->alloc(mem, sizeof(llarp_nodedb), llarp::alignment<llarp_nodedb>());
if(!ptr) return nullptr;
return new (ptr) llarp_nodedb(mem);
} }
void llarp_nodedb_free(struct llarp_nodedb **n) { void llarp_nodedb_free(struct llarp_nodedb **n) {
if (*n) delete *n; if (*n)
{
struct llarp_alloc *mem = (*n)->mem;
(*n)->Clear();
(*n)->~llarp_nodedb();
mem->free(mem, *n);
}
*n = nullptr; *n = nullptr;
} }

@ -11,7 +11,7 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
const char *key, const char *val); const char *key, const char *val);
} // namespace llarp } // namespace llarp
llarp_router::llarp_router() : ready(false) { llarp_msg_muxer_init(&muxer); } llarp_router::llarp_router(struct llarp_alloc *m) : ready(false), mem(m) { llarp_msg_muxer_init(&muxer); }
llarp_router::~llarp_router() {} llarp_router::~llarp_router() {}
@ -20,7 +20,10 @@ void llarp_router::AddLink(struct llarp_link *link) {
while (head->next && head->link) head = head->next; while (head->next && head->link) head = head->next;
if (head->link) if (head->link)
head->next = new llarp::router_links{link, nullptr}; {
void * ptr = mem->alloc(mem, sizeof(llarp::router_links), 8);
head->next = new (ptr) llarp::router_links{link, nullptr};
}
else else
head->link = link; head->link = link;
@ -42,11 +45,16 @@ void llarp_router::Close() {
} }
extern "C" { extern "C" {
struct llarp_router *llarp_init_router(struct llarp_threadpool *tp, struct llarp_ev_loop * netloop) { struct llarp_router *llarp_init_router(struct llarp_alloc * mem, struct llarp_threadpool *tp, struct llarp_ev_loop * netloop) {
llarp_router *router = new llarp_router; void * ptr = mem->alloc(mem, sizeof(llarp_router), 16);
router->netloop = netloop; if(!ptr) return nullptr;
router->tp = tp; llarp_router *router = new (ptr) llarp_router(mem);
llarp_crypto_libsodium_init(&router->crypto); if(router)
{
router->netloop = netloop;
router->tp = tp;
llarp_crypto_libsodium_init(&router->crypto);
}
return router; return router;
} }
@ -73,8 +81,10 @@ void llarp_stop_router(struct llarp_router *router) {
void llarp_free_router(struct llarp_router **router) { void llarp_free_router(struct llarp_router **router) {
if (*router) { if (*router) {
(*router)->ForEachLink([](llarp_link *link) { link->free_impl(link); delete link; }); struct llarp_alloc * mem = (*router)->mem;
delete *router; (*router)->ForEachLink([mem](llarp_link *link) { link->free_impl(link); mem->free(mem, link); });
(*router)->~llarp_router();
mem->free(mem, *router);
} }
*router = nullptr; *router = nullptr;
} }
@ -87,13 +97,13 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
llarp_router *self = static_cast<llarp_router *>(iter->user); llarp_router *self = static_cast<llarp_router *>(iter->user);
if (StrEq(section, "links")) { if (StrEq(section, "links")) {
iwp_configure_args args = { iwp_configure_args args = {
.mem = &llarp_g_mem, .mem = self->mem,
.ev = self->netloop, .ev = self->netloop,
.crypto = &self->crypto, .crypto = &self->crypto,
.keyfile=self->transport_keyfile .keyfile=self->transport_keyfile
}; };
if (StrEq(val, "eth")) { if (StrEq(val, "eth")) {
struct llarp_link *link = llarp::Alloc<llarp_link>(); struct llarp_link *link = llarp::Alloc<llarp_link>(self->mem);
iwp_link_init(link, args, &self->muxer); iwp_link_init(link, args, &self->muxer);
if(llarp_link_initialized(link)) if(llarp_link_initialized(link))
{ {
@ -104,10 +114,10 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
return; return;
} }
} }
delete link; self->mem->free(self->mem, link);
printf("failed to configure ethernet link for %s\n", key); printf("failed to configure ethernet link for %s\n", key);
} else { } else {
struct llarp_link *link = llarp::Alloc<llarp_link>(); struct llarp_link *link = llarp::Alloc<llarp_link>(self->mem);
uint16_t port = std::atoi(val); uint16_t port = std::atoi(val);
iwp_link_init(link, args, &self->muxer); iwp_link_init(link, args, &self->muxer);
if(llarp_link_initialized(link)) if(llarp_link_initialized(link))
@ -119,7 +129,7 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
return; return;
} }
} }
delete link; self->mem->free(self->mem, link);
printf("failed to configure inet link for %s port %d\n", key, port); printf("failed to configure inet link for %s port %d\n", key, port);
} }
} }

@ -23,14 +23,9 @@ struct llarp_router {
llarp_crypto crypto; llarp_crypto crypto;
llarp_msg_muxer muxer; llarp_msg_muxer muxer;
llarp_path_context *paths; llarp_path_context *paths;
llarp_alloc * mem;
static void *operator new(size_t sz) { llarp_router(llarp_alloc * mem);
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>());
}
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
llarp_router();
~llarp_router(); ~llarp_router();
void AddLink(struct llarp_link *link); void AddLink(struct llarp_link *link);

@ -7,18 +7,25 @@ void llarp_rc_free(struct llarp_rc *rc) {
llarp_ai_list_free(&rc->addrs); llarp_ai_list_free(&rc->addrs);
} }
struct llarp_rc_decoder
{
struct llarp_rc * rc;
struct llarp_alloc * mem;
};
static bool llarp_rc_decode_dict(struct dict_reader * r, llarp_buffer_t * key) static bool llarp_rc_decode_dict(struct dict_reader * r, llarp_buffer_t * key)
{ {
int64_t v; int64_t v;
llarp_buffer_t strbuf; llarp_buffer_t strbuf;
struct llarp_rc * rc = r->user; struct llarp_rc_decoder * dec = r->user;
struct llarp_alloc * mem = dec->mem;
struct llarp_rc * rc = dec->rc;
if(!key) return true; if(!key) return true;
if(llarp_buffer_eq(*key, "a")) if(llarp_buffer_eq(*key, "a"))
{ {
rc->addrs = llarp_ai_list_new(); rc->addrs = llarp_ai_list_new(mem);
return llarp_ai_list_bdecode(rc->addrs, r->buffer); return llarp_ai_list_bdecode(rc->addrs, r->buffer);
} }
@ -41,7 +48,7 @@ static bool llarp_rc_decode_dict(struct dict_reader * r, llarp_buffer_t * key)
if(llarp_buffer_eq(*key, "x")) if(llarp_buffer_eq(*key, "x"))
{ {
rc->exits = llarp_xi_list_new(); rc->exits = llarp_xi_list_new(mem);
return llarp_xi_list_bdecode(rc->exits, r->buffer); return llarp_xi_list_bdecode(rc->exits, r->buffer);
} }
@ -58,9 +65,13 @@ static bool llarp_rc_decode_dict(struct dict_reader * r, llarp_buffer_t * key)
return false; return false;
} }
bool llarp_rc_bdecode(struct llarp_rc * rc, llarp_buffer_t *buff) { bool llarp_rc_bdecode(struct llarp_alloc * mem, struct llarp_rc * rc, llarp_buffer_t *buff) {
struct llarp_rc_decoder decode = {
.rc = rc,
.mem = mem
};
struct dict_reader r = { struct dict_reader r = {
.user = rc, .user = &decode,
.on_key = &llarp_rc_decode_dict .on_key = &llarp_rc_decode_dict
}; };
return bdecode_read_dict(buff, &r); return bdecode_read_dict(buff, &r);

Loading…
Cancel
Save