diff --git a/daemon/Makefile b/daemon/Makefile index 4711cd69e..bff803a03 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -93,6 +93,7 @@ SRCS += nftables.c endif LIBSRCS := loglib.c auxlib.c rtplib.c str.c socket.c streambuf.c ssllib.c dtmflib.c mix_buffer.c poller.c \ bufferpool.c bencode.c netfilter_api.c +LIBSRCS += arena.c ifeq ($(with_transcoding),yes) LIBSRCS += codeclib.c resample.c cchain.c LIBSRCS += rescale.c diff --git a/daemon/call.c b/daemon/call.c index ee6e22043..dba72c67f 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -5538,7 +5538,7 @@ static void __call_free(call_t *c) { bufferpool_unref(ps->stats_out); } - memory_arena_free(&c->buffer); + arena_free(&c->buffer); ice_fragments_cleanup(c->sdp_fragments, true); t_hash_table_destroy(c->sdp_fragments); rwlock_destroy(&c->master_lock); @@ -5551,7 +5551,7 @@ static call_t *call_create(const str *callid) { ilog(LOG_NOTICE, "Creating new call"); c = obj_alloc0(call_t, __call_free); - memory_arena_init(&c->buffer); + arena_init(&c->buffer, g_malloc, g_free); rwlock_init(&c->master_lock); c->tags = str_ml_ht_new(); c->viabranches = str_ml_ht_new(); @@ -5751,7 +5751,7 @@ static bool call_merge(call_t *call, call_t *call2) { call_memory_arena_set(call); // move buffers - bencode_buffer_merge(&call->buffer, &call2->buffer); + arena_merge(&call->buffer, &call2->buffer); // the ids below are about to be renumbered, and a snapshot is keyed on them call_checkpoint_free_all(call2); diff --git a/daemon/control_ng.c b/daemon/control_ng.c index 85dc355bc..632ee760e 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -205,7 +205,7 @@ static parser_arg __bencode_dictionary_add_dictionary(bencode_item_t *n, const c } static parser_arg __bencode_dictionary_add_dictionary_dup(bencode_item_t *n, const char *e) { size_t len = strlen(e) + 1; - char *s = bencode_buffer_alloc(n->buffer, len); + char *s = arena_alloc(n->buffer, len); memcpy(s, e, len); return (parser_arg) bencode_dictionary_add_dictionary(n, s); } @@ -214,7 +214,7 @@ static parser_arg __bencode_dictionary_add_list(bencode_item_t *n, const char *e } static parser_arg __bencode_dictionary_add_list_dup(bencode_item_t *n, const char *e) { size_t len = strlen(e) + 1; - char *s = bencode_buffer_alloc(n->buffer, len); + char *s = arena_alloc(n->buffer, len); memcpy(s, e, len); return (parser_arg) bencode_dictionary_add_list(n, s); } @@ -228,10 +228,10 @@ static str __bencode_collapse_str(ng_parser_ctx_t *ctx, bencode_item_t *a, void return bencode_collapse_str(a); } static const char *__bencode_strdup(ng_parser_ctx_t *ctx, const char *s) { - return bencode_strdup(ctx->buffer, s); + return arena_strdup(ctx->buffer, s); } -static void __bencode_ctx_init(ng_parser_ctx_t *ctx, bencode_buffer_t *buf) { - bencode_buffer_init(buf); +static void __bencode_ctx_init(ng_parser_ctx_t *ctx, arena_t *buf) { + arena_init(buf, g_malloc, g_free); *ctx = (ng_parser_ctx_t) { .parser = &ng_parser_native, .buffer = buf }; } @@ -499,7 +499,7 @@ static str json_collapse(ng_parser_ctx_t *ctx, JsonNode *a, void **to_free) { json_node_unref(a); return out; } -static void json_ctx_init(ng_parser_ctx_t *ctx, bencode_buffer_t *buf) { +static void json_ctx_init(ng_parser_ctx_t *ctx, arena_t *buf) { *ctx = (ng_parser_ctx_t) { .parser = &ng_parser_json }; } static str dummy_encode_len(char *out, const char *in, size_t in_len) { @@ -704,7 +704,7 @@ struct control_ng_stats* get_control_ng_stats(const sockaddr_t *addr) { } static void __ng_buffer_free(ng_buffer *ngbuf) { - bencode_buffer_free(&ngbuf->buffer); + arena_free(&ngbuf->buffer); if (ngbuf->ref) obj_put_o(ngbuf->ref); if (ngbuf->json) diff --git a/daemon/cookie_cache.c b/daemon/cookie_cache.c index 0af4fe955..850a5d21e 100644 --- a/daemon/cookie_cache.c +++ b/daemon/cookie_cache.c @@ -9,14 +9,14 @@ #include "str.h" INLINE void cookie_cache_state_init(struct cookie_cache_state *s) { - bencode_buffer_init(&s->buffer); + arena_init(&s->buffer, g_malloc, g_free); s->in_use = g_hash_table_new((GHashFunc) str_hash, (GEqualFunc) str_equal); s->cookies = g_hash_table_new((GHashFunc) str_hash, (GEqualFunc) str_equal); } INLINE void cookie_cache_state_cleanup(struct cookie_cache_state *s) { g_hash_table_destroy(s->cookies); g_hash_table_destroy(s->in_use); - bencode_buffer_free(&s->buffer); + arena_free(&s->buffer); } void cookie_cache_init(struct cookie_cache *c) { @@ -31,10 +31,10 @@ void cookie_cache_init(struct cookie_cache *c) { static void __cookie_cache_check_swap(struct cookie_cache *c) { if (rtpe_now - c->swap_time_us >= 30000000LL) { g_hash_table_remove_all(c->old.cookies); - bencode_buffer_free(&c->old.buffer); + arena_free(&c->old.buffer); swap_ptrs(&c->old.cookies, &c->current.cookies); c->old.buffer = c->current.buffer; - bencode_buffer_init(&c->current.buffer); + arena_init(&c->current.buffer, g_malloc, g_free); c->swap_time_us = rtpe_now; } } @@ -43,7 +43,7 @@ static cache_entry *__cache_entry_dup(struct cookie_cache_state *c, const cache_ if (!s) return NULL; cache_entry *r; - r = bencode_buffer_alloc(&c->buffer, sizeof(*r)); + r = arena_alloc(&c->buffer, sizeof(*r)); r->reply = bencode_str_strdup(&c->buffer, &s->reply); r->command = s->command; r->callid = bencode_str_strdup(&c->buffer, &s->callid); diff --git a/daemon/dtmf.c b/daemon/dtmf.c index b8fb876c1..5ba570e1b 100644 --- a/daemon/dtmf.c +++ b/daemon/dtmf.c @@ -106,11 +106,11 @@ static void dtmf_bencode_and_notify(struct call_media *media, unsigned int event call_t *call = media->call; struct call_monologue *ml = media->monologue; - bencode_buffer_t bencbuf; + arena_t bencbuf; bencode_item_t *notify, *data, *tags; str encoded_data; - int ret = bencode_buffer_init(&bencbuf); - assert(ret == 0); + bool ret = arena_init(&bencbuf, g_malloc, g_free); + assert(ret == true); notify = bencode_dictionary(&bencbuf); bencode_dictionary_add_string(notify, "notify", "onDTMF"); @@ -137,7 +137,7 @@ static void dtmf_bencode_and_notify(struct call_media *media, unsigned int event encoded_data = bencode_collapse_str(notify); notify_ng_tcp_clients(&encoded_data); - bencode_buffer_free(&bencbuf); + arena_free(&bencbuf); } static GString *dtmf_json_print(struct call_media *media, unsigned int event, unsigned int volume, diff --git a/daemon/janus.c b/daemon/janus.c index a82b02892..cc823296d 100644 --- a/daemon/janus.c +++ b/daemon/janus.c @@ -1634,7 +1634,7 @@ static const char *janus_trickle(JsonReader *reader, struct janus_session *sessi // top-level structures first, with auto cleanup g_auto(sdp_streams_q) streams = TYPED_GQUEUE_INIT; g_autoptr(ng_buffer) ngbuf = ng_buffer_new(NULL); - bencode_buffer_init(&ngbuf->buffer); + arena_init(&ngbuf->buffer, g_malloc, g_free); g_auto(sdp_ng_flags) flags; call_ng_flags_init(&flags, OP_OTHER); @@ -1646,7 +1646,7 @@ static const char *janus_trickle(JsonReader *reader, struct janus_session *sessi // allocate and parse candidate str cand_str; - cand_str = bencode_strdup_str(&ngbuf->buffer, candidate); + cand_str = arena_strdup_str(&ngbuf->buffer, candidate); str_shift_cmp(&cand_str, "candidate:"); // skip prefix if (!cand_str.len) // end of candidates return NULL; @@ -1663,12 +1663,12 @@ static const char *janus_trickle(JsonReader *reader, struct janus_session *sessi g_autoptr(char) handle_buf = NULL; handle_buf = g_strdup_printf("%" PRIu64, handle_id); - flags.from_tag = bencode_strdup_str(&ngbuf->buffer, handle_buf); - flags.call_id = bencode_strdup_str(&ngbuf->buffer, call_id); + flags.from_tag = arena_strdup_str(&ngbuf->buffer, handle_buf); + flags.call_id = arena_strdup_str(&ngbuf->buffer, call_id); // populate and allocate a=mid if (sdp_mid) - sp->media_id = bencode_strdup_str(&ngbuf->buffer, sdp_mid); + sp->media_id = arena_strdup_str(&ngbuf->buffer, sdp_mid); // check m= line index if (sdp_m_line >= 0) @@ -1677,7 +1677,7 @@ static const char *janus_trickle(JsonReader *reader, struct janus_session *sessi // ufrag can be given in-line or separately sp->ice_ufrag = cand->ufrag; if (!sp->ice_ufrag.len && ufrag) - sp->ice_ufrag = bencode_strdup_str(&ngbuf->buffer, ufrag); + sp->ice_ufrag = arena_strdup_str(&ngbuf->buffer, ufrag); // finally do the update trickle_ice_update(ngbuf, call, &flags, &streams); diff --git a/daemon/main.c b/daemon/main.c index c5e42b6c1..0cd8cab63 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1706,7 +1706,7 @@ RTPE_CONFIG_CHARPP_PARAMS static void early_init(void) { socket_init(); // needed for socktype_udp rtpe_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free); - memory_arena_init(&rtpe_arena); + arena_init(&rtpe_arena, g_malloc, g_free); memory_arena = &rtpe_arena; } @@ -2249,7 +2249,7 @@ int main(int argc, char **argv) { kernel_shutdown_table(); options_free(); bufferpool_cleanup(); - memory_arena_free(&rtpe_arena); + arena_free(&rtpe_arena); bufferpool_destroy(rtpe_bufferpool); return 0; diff --git a/daemon/media_player.c b/daemon/media_player.c index 1a563d869..c0d770cf9 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -757,7 +757,7 @@ static bool media_player_cache_get_entry(struct media_player *mp, call_memory_arena_release(); entry = mp->cache_entry = obj_alloc0(struct media_player_cache_entry, __media_player_cache_entry_free); - memory_arena_init(&entry->arena); + arena_init(&entry->arena, g_malloc, g_free); memory_arena = &entry->arena; struct media_player_cache_index *ins_key = &entry->index; @@ -2214,7 +2214,7 @@ static void __media_player_cache_entry_free(struct media_player_cache_entry *e) kernel_free_packet_stream(e->kernel_idx); g_free(e->index.index.file.s); payload_type_clear(&e->index.dst_pt); - memory_arena_free(&e->arena); + arena_free(&e->arena); } #endif diff --git a/daemon/ng_client.c b/daemon/ng_client.c index 63178acc3..3d1f48bf5 100644 --- a/daemon/ng_client.c +++ b/daemon/ng_client.c @@ -1,5 +1,6 @@ #include "ng_client.h" #include "media_socket.h" +#include "bencode.h" struct endpoint_sockets { endpoint_t dst; @@ -103,7 +104,7 @@ static void ng_client_put_socket(struct endpoint_sockets *es, socket_slist *link while (!success); } -bencode_item_t *ng_client_request(const endpoint_t *dst, const str *req, bencode_buffer_t *rbuf) { +bencode_item_t *ng_client_request(const endpoint_t *dst, const str *req, arena_t *rbuf) { __auto_type es = ng_client_get_entry(dst); __auto_type link = ng_client_get_socket(es); if (!link) @@ -132,7 +133,7 @@ bencode_item_t *ng_client_request(const endpoint_t *dst, const str *req, bencode STR_FMT(req)); static const size_t buflen = 4096; - char *buf = bencode_buffer_alloc(rbuf, buflen); + char *buf = arena_alloc(rbuf, buflen); ssize_t len = 0; for (unsigned int try = 0; try < rtpe_config.ng_client_retries; try++) { diff --git a/daemon/redis.c b/daemon/redis.c index f629b1ab9..ea59b11f4 100644 --- a/daemon/redis.c +++ b/daemon/redis.c @@ -2182,7 +2182,7 @@ static int redis_restore_checkpoints(call_t *c, parser_arg root) { struct redis_parsed_record { JsonParser *json; - bencode_buffer_t benc; + arena_t benc; bool benc_valid; const ng_parser_t *parser; }; @@ -2207,7 +2207,7 @@ static const char *redis_parse_record(const str *record, parser_arg *root, } if (record->s[0] == 'd') { - if (bencode_buffer_init(&out->benc)) + if (!arena_init(&out->benc, g_malloc, g_free)) return "failed to initialise bencode buffer"; out->benc_valid = true; bencode_item_t *benc_root = bencode_decode_expect_str(&out->benc, record, @@ -2226,7 +2226,7 @@ static void redis_parsed_record_free(struct redis_parsed_record *p) { if (p->json) g_object_unref(p->json); if (p->benc_valid) - bencode_buffer_free(&p->benc); + arena_free(&p->benc); } static void json_restore_call(struct redis *r, const str *callid, bool foreign) { @@ -3105,7 +3105,7 @@ static str redis_encode_json(ng_parser_ctx_t *ctx, call_t *c, void **to_free, str redis_snapshot_encode(call_t *c, struct call_monologue *ml) { struct call_monologue *scope[2] = { ml, ml }; ng_parser_ctx_t ctx; - bencode_buffer_t bbuf; + arena_t bbuf; // never leaves the daemon, so the format is ours to pick ng_parser_native.init(&ctx, &bbuf); @@ -3116,7 +3116,7 @@ str redis_snapshot_encode(call_t *c, struct call_monologue *ml) { out = str_dup_str(&encoded); g_free(to_free); - bencode_buffer_free(ctx.buffer); + arena_free(ctx.buffer); return out; } @@ -3537,7 +3537,7 @@ void redis_update_onekey(call_t *c, struct redis *r) { } ng_parser_ctx_t ctx; - bencode_buffer_t bbuf; + arena_t bbuf; redis_format_parsers[rtpe_config.redis_format]->init(&ctx, &bbuf); void *to_free = NULL; @@ -3552,7 +3552,7 @@ void redis_update_onekey(call_t *c, struct redis *r) { rwlock_unlock_r(&c->master_lock); g_free(to_free); - bencode_buffer_free(ctx.buffer); + arena_free(ctx.buffer); return; err: diff --git a/include/control_ng.h b/include/control_ng.h index 7eaf91a83..a912b5734 100644 --- a/include/control_ng.h +++ b/include/control_ng.h @@ -53,6 +53,7 @@ enum ng_opmode { #include "socket.h" #include "str.h" #include "tcp_listener.h" +#include "arena.h" #include "bencode.h" #include "types.h" #include "cli.h" @@ -77,7 +78,7 @@ struct control_ng { struct ng_buffer { struct obj obj; - bencode_buffer_t buffer; + arena_t buffer; struct obj *ref; JsonParser *json; char *sdp_out; @@ -108,7 +109,7 @@ typedef union { struct ng_parser { - void (*init)(ng_parser_ctx_t *, bencode_buffer_t *); + void (*init)(ng_parser_ctx_t *, arena_t *); str (*collapse)(ng_parser_ctx_t *, parser_arg, void **); const char *(*dict_iter)(const ng_parser_t *, parser_arg, const char *(*callback)(const ng_parser_t *, str *, parser_arg, helper_arg), @@ -151,7 +152,7 @@ struct ng_parser { }; struct ng_parser_ctx { const ng_parser_t *parser; - bencode_buffer_t *buffer; + arena_t *buffer; }; struct ng_command_ctx { ng_parser_ctx_t parser_ctx; diff --git a/include/cookie_cache.h b/include/cookie_cache.h index 5e6e9eb45..46b42f071 100644 --- a/include/cookie_cache.h +++ b/include/cookie_cache.h @@ -7,10 +7,10 @@ #include "helpers.h" #include "str.h" #include "control_ng.h" -#include "bencode.h" +#include "arena.h" struct cookie_cache_state { - bencode_buffer_t buffer; + arena_t buffer; GHashTable *in_use; GHashTable *cookies; }; diff --git a/include/memory_arena.h b/include/memory_arena.h index 76a8620a3..4abe0e85c 100644 --- a/include/memory_arena.h +++ b/include/memory_arena.h @@ -2,18 +2,15 @@ #define _MEMORY_ARENA_H_ #include "compat.h" -#include "bencode.h" +#include "arena.h" -typedef bencode_buffer_t memory_arena_t; +typedef arena_t memory_arena_t; extern __thread memory_arena_t *memory_arena; -#define memory_arena_init bencode_buffer_init -#define memory_arena_free bencode_buffer_free - INLINE void *__memory_arena_alloc(size_t len) { void *ret; - ret = bencode_buffer_alloc(memory_arena, len); + ret = arena_alloc(memory_arena, len); return ret; } #define memory_arena_alloc(type) ((type *) __memory_arena_alloc(sizeof(type))) diff --git a/include/ng_client.h b/include/ng_client.h index 0754df861..d81e24e01 100644 --- a/include/ng_client.h +++ b/include/ng_client.h @@ -2,11 +2,11 @@ #define _NG_CLIENT_H_ #include "types.h" -#include "bencode.h" +#include "arena.h" void ng_client_init(void); void ng_client_cleanup(void); -bencode_item_t *ng_client_request(const endpoint_t *dst, const str *req, bencode_buffer_t *); +bencode_item_t *ng_client_request(const endpoint_t *dst, const str *req, arena_t *); #endif diff --git a/lib/arena.c b/lib/arena.c new file mode 100644 index 000000000..a8d7569ee --- /dev/null +++ b/lib/arena.c @@ -0,0 +1,90 @@ +#include "arena.h" +#include "helpers.h" + + +// set to 0 for alloc debugging, e.g. through valgrind +#define ARENA_MIN_PIECE_LEN 4096 + +struct arena_piece { + char *tail; + size_t left; + struct arena_piece *next; + char buf[0] __attribute__ ((aligned (ARENA_ALLOC_ALIGN))); +}; + + +static struct arena_piece *arena_piece_new(size_t size, void *(*alloc_fn)(size_t)) { + struct arena_piece *ret; + + size_t alloc_size = size + sizeof(*ret) + ARENA_ALLOC_ALIGN; + alloc_size = MAX(alloc_size, ARENA_MIN_PIECE_LEN); + ret = alloc_fn(alloc_size); + if (!ret) + return NULL; + + ret->tail = ret->buf; + ret->left = alloc_size - sizeof(*ret) - ARENA_ALLOC_ALIGN; + ret->next = NULL; + + return ret; +} + +bool arena_init(arena_t *arena, void *(*alloc_fn)(size_t), void (*free_fn)(void *)) { + arena->alloc = alloc_fn; + arena->free = free_fn; + arena->pieces = arena_piece_new(0, alloc_fn); + if (!arena->pieces) + return false; + return true; +} + +void *arena_alloc(arena_t *arena, size_t size) { + struct arena_piece *piece; + void *ret; + size_t align_size = ((size + ARENA_ALLOC_ALIGN - 1) / ARENA_ALLOC_ALIGN) * ARENA_ALLOC_ALIGN; + + if (!arena) + return NULL; + + piece = arena->pieces; + + if (size <= piece->left) + goto alloc; + + piece = arena_piece_new(size, arena->alloc); + if (!piece) + return NULL; + piece->next = arena->pieces; + arena->pieces = piece; + + assert(size <= piece->left); + +alloc: + if (piece->left >= align_size) + piece->left -= align_size; + else + piece->left = 0; + ret = piece->tail; + piece->tail += align_size; + return ret; +} + +void arena_free(arena_t *arena) { + struct arena_piece *piece, *next; + + if (!arena) + return; + + for (piece = arena->pieces; piece; piece = next) { + next = piece->next; + arena->free(piece); + } +} + +void arena_merge(arena_t *to, arena_t *from) { + struct arena_piece *last = to->pieces; + while (last->next) + last = last->next; + last->next = from->pieces; + from->pieces = NULL; +} diff --git a/lib/arena.h b/lib/arena.h new file mode 100644 index 000000000..951f7db38 --- /dev/null +++ b/lib/arena.h @@ -0,0 +1,46 @@ +#ifndef _ARENA_H_ +#define _ARENA_H_ + + +#include +#include + +#include "str.h" + + +#define ARENA_ALLOC_ALIGN 8 + + +struct arena_piece; + +struct arena { + struct arena_piece *pieces; + void *(*alloc)(size_t); + void (*free)(void *); +}; +typedef struct arena arena_t; + + +bool arena_init(arena_t *, void *(*alloc_fn)(size_t), void (*free_fn)(void *)); +void *arena_alloc(arena_t *, size_t); +void arena_free(arena_t *); +void arena_merge(arena_t *to, arena_t *from); + + +INLINE char *arena_strdup(arena_t *buf, const char *s) { + char *ret = arena_alloc(buf, strlen(s) + 1); + strcpy(ret, s); + return ret; +} + +INLINE str arena_strdup_str(arena_t *buf, const char *s) { + str o = STR_NULL; + o.len = strlen(s); + o.s = arena_alloc(buf, o.len); + memcpy(o.s, s, o.len); + return o; +} + + + +#endif diff --git a/lib/bencode.c b/lib/bencode.c index c0dd87662..de3f9a832 100644 --- a/lib/bencode.c +++ b/lib/bencode.c @@ -10,25 +10,15 @@ #include "helpers.h" -/* set to 0 for alloc debugging, e.g. through valgrind */ -#define BENCODE_MIN_BUFFER_PIECE_LEN 4096 #define BENCODE_HASH_BUCKETS 31 /* prime numbers work best */ -struct __bencode_buffer_piece { - char *tail; - size_t left; - struct __bencode_buffer_piece *next; - char buf[0] __attribute__ ((aligned (BENCODE_ALLOC_ALIGN))); -}; struct __bencode_hash { struct bencode_item *buckets[BENCODE_HASH_BUCKETS]; }; - - static bencode_item_t __bencode_end_marker = { .type = BENCODE_END_MARKER, .iov = { @@ -42,9 +32,7 @@ static bencode_item_t __bencode_end_marker = { }; - - -static bencode_item_t *__bencode_decode(bencode_buffer_t *buf, const char *s, const char *end); +static bencode_item_t *__bencode_decode(arena_t *buf, const char *s, const char *end); @@ -73,84 +61,10 @@ static void __bencode_list_init(bencode_item_t *list) { __bencode_container_init(list); } -static struct __bencode_buffer_piece *__bencode_piece_new(size_t size) { - struct __bencode_buffer_piece *ret; - - size_t alloc_size = size + sizeof(*ret) + BENCODE_ALLOC_ALIGN; - alloc_size = MAX(alloc_size, BENCODE_MIN_BUFFER_PIECE_LEN); - ret = BENCODE_MALLOC(alloc_size); - if (!ret) - return NULL; - - ret->tail = ret->buf; - ret->left = alloc_size - sizeof(*ret) - BENCODE_ALLOC_ALIGN; - ret->next = NULL; - - return ret; -} - -int bencode_buffer_init(bencode_buffer_t *buf) { - buf->pieces = __bencode_piece_new(0); - if (!buf->pieces) - return -1; - return 0; -} - -void *bencode_buffer_alloc(bencode_buffer_t *buf, size_t size) { - struct __bencode_buffer_piece *piece; - void *ret; - size_t align_size = ((size + BENCODE_ALLOC_ALIGN - 1) / BENCODE_ALLOC_ALIGN) * BENCODE_ALLOC_ALIGN; - - if (!buf) - return NULL; - - piece = buf->pieces; - - if (size <= piece->left) - goto alloc; - - piece = __bencode_piece_new(size); - if (!piece) - return NULL; - piece->next = buf->pieces; - buf->pieces = piece; - - assert(size <= piece->left); - -alloc: - if (piece->left >= align_size) - piece->left -= align_size; - else - piece->left = 0; - ret = piece->tail; - piece->tail += align_size; - return ret; -} - -void bencode_buffer_free(bencode_buffer_t *buf) { - struct __bencode_buffer_piece *piece, *next; - - if (!buf) - return; - - for (piece = buf->pieces; piece; piece = next) { - next = piece->next; - BENCODE_FREE(piece); - } -} - -void bencode_buffer_merge(bencode_buffer_t *to, bencode_buffer_t *from) { - struct __bencode_buffer_piece *last = to->pieces; - while (last->next) - last = last->next; - last->next = from->pieces; - from->pieces = NULL; -} - -static bencode_item_t *__bencode_item_alloc(bencode_buffer_t *buf, size_t payload) { +static bencode_item_t *__bencode_item_alloc(arena_t *buf, size_t payload) { bencode_item_t *ret; - ret = bencode_buffer_alloc(buf, sizeof(struct bencode_item) + payload); + ret = arena_alloc(buf, sizeof(struct bencode_item) + payload); if (!ret) return NULL; ret->buffer = buf; @@ -158,7 +72,7 @@ static bencode_item_t *__bencode_item_alloc(bencode_buffer_t *buf, size_t payloa return ret; } -bencode_item_t *bencode_dictionary(bencode_buffer_t *buf) { +bencode_item_t *bencode_dictionary(arena_t *buf) { bencode_item_t *ret; ret = __bencode_item_alloc(buf, 0); @@ -168,7 +82,7 @@ bencode_item_t *bencode_dictionary(bencode_buffer_t *buf) { return ret; } -bencode_item_t *bencode_list(bencode_buffer_t *buf) { +bencode_item_t *bencode_list(arena_t *buf) { bencode_item_t *ret; ret = __bencode_item_alloc(buf, 0); @@ -201,7 +115,7 @@ static void __bencode_container_add(bencode_item_t *parent, bencode_item_t *chil } } -static bencode_item_t *__bencode_string_alloc(bencode_buffer_t *buf, const void *base, +static bencode_item_t *__bencode_string_alloc(arena_t *buf, const void *base, size_t str_len, size_t iov_len, unsigned int iov_cnt, bencode_type_t type) { bencode_item_t *ret; @@ -224,19 +138,19 @@ static bencode_item_t *__bencode_string_alloc(bencode_buffer_t *buf, const void return ret; } -bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, size_t len) { - char *sd = bencode_buffer_alloc(buf, len); +bencode_item_t *bencode_string_len_dup(arena_t *buf, const char *s, size_t len) { + char *sd = arena_alloc(buf, len); if (!sd) return NULL; memcpy(sd, s, len); return bencode_string_len(buf, sd, len); } -bencode_item_t *bencode_string_len(bencode_buffer_t *buf, const char *s, size_t len) { +bencode_item_t *bencode_string_len(arena_t *buf, const char *s, size_t len) { return __bencode_string_alloc(buf, s, len, len, 1, BENCODE_STRING); } -bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i) { +bencode_item_t *bencode_integer(arena_t *buf, long long int i) { bencode_item_t *ret; int alen, rlen; @@ -353,7 +267,7 @@ struct iovec *bencode_iovec(bencode_item_t *root, int *cnt, unsigned int head, u assert(cnt != NULL); assert(root->iov_cnt > 0); - ret = bencode_buffer_alloc(root->buffer, sizeof(*ret) * (root->iov_cnt + head + tail)); + ret = arena_alloc(root->buffer, sizeof(*ret) * (root->iov_cnt + head + tail)); if (!ret) return NULL; *cnt = __bencode_iovec_dump(ret + head, root); @@ -368,7 +282,7 @@ char *bencode_collapse(bencode_item_t *root, size_t *len) { return NULL; assert(root->str_len > 0); - ret = bencode_buffer_alloc(root->buffer, root->str_len + 1); + ret = arena_alloc(root->buffer, root->str_len + 1); if (!ret) return NULL; l = __bencode_str_dump(ret, root); @@ -385,7 +299,7 @@ char *bencode_collapse_dup(bencode_item_t *root, size_t *len) { return NULL; assert(root->str_len > 0); - ret = BENCODE_MALLOC(root->str_len + 1); + ret = root->buffer->alloc(root->str_len + 1); if (!ret) return NULL; @@ -441,7 +355,7 @@ static void __bencode_hash_insert(bencode_item_t *key, struct __bencode_hash *ha } } -static bencode_item_t *__bencode_decode_dictionary(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_dictionary(arena_t *buf, const char *s, const char *end) { bencode_item_t *ret, *key, *value; struct __bencode_hash *hash; @@ -484,7 +398,7 @@ static bencode_item_t *__bencode_decode_dictionary(bencode_buffer_t *buf, const return ret; } -static bencode_item_t *__bencode_decode_list(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_list(arena_t *buf, const char *s, const char *end) { bencode_item_t *ret, *item; if (*s != 'l') @@ -509,7 +423,7 @@ static bencode_item_t *__bencode_decode_list(bencode_buffer_t *buf, const char * return ret; } -static bencode_item_t *__bencode_decode_integer(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_integer(arena_t *buf, const char *s, const char *end) { long long int i; const char *orig = s; char *convend; @@ -555,7 +469,7 @@ done: return ret; } -static bencode_item_t *__bencode_decode_string(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_string(arena_t *buf, const char *s, const char *end) { unsigned long int sl; char *convend; const char *orig = s; @@ -596,7 +510,7 @@ colon: return ret; } -static bencode_item_t *__bencode_decode(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode(arena_t *buf, const char *s, const char *end) { if (s >= end) return NULL; @@ -625,7 +539,7 @@ static bencode_item_t *__bencode_decode(bencode_buffer_t *buf, const char *s, co } } -bencode_item_t *bencode_decode(bencode_buffer_t *buf, const char *s, size_t len) { +bencode_item_t *bencode_decode(arena_t *buf, const char *s, size_t len) { assert(s != NULL); return __bencode_decode(buf, s, s + len); } diff --git a/lib/bencode.h b/lib/bencode.h index 36634d0cd..409cd7acf 100644 --- a/lib/bencode.h +++ b/lib/bencode.h @@ -5,17 +5,14 @@ #include #include "compat.h" +#include "arena.h" -#define BENCODE_ALLOC_ALIGN 8 -struct bencode_buffer; enum bencode_type; struct bencode_item; -struct __bencode_buffer_piece; struct __bencode_free_list; typedef enum bencode_type bencode_type_t; -typedef struct bencode_buffer bencode_buffer_t; typedef struct bencode_item bencode_item_t; typedef void (*free_func_t)(void *); @@ -35,16 +32,10 @@ struct bencode_item { size_t str_len; /* length of the whole ENCODED object. NOT the length of a byte string */ long long int value; /* when decoding an integer, contains the value; otherwise used internally */ bencode_item_t *parent, *child, *last_child, *sibling; - bencode_buffer_t *buffer; - char __buf[0] __attribute__ ((aligned (BENCODE_ALLOC_ALIGN))); + arena_t *buffer; + char __buf[0] __attribute__ ((aligned (ARENA_ALLOC_ALIGN))); }; -struct bencode_buffer { - struct __bencode_buffer_piece *pieces; -}; - - - /* to embed BENCODE_STRING objects into printf-like functions */ @@ -56,47 +47,22 @@ struct bencode_buffer { /*** INIT & DESTROY ***/ -/* Initializes a bencode_buffer_t object. This object is used to group together all memory allocations - * made when encoding or decoding. Its memory usage is always growing, until it is freed, at which point - * all objects created through it become invalid. The actual object must be allocated separately, for - * example by being put on the stack. - * Returns 0 on success or -1 on failure (if no memory could be allocated). */ -int bencode_buffer_init(bencode_buffer_t *buf); - -/* Allocate a piece of memory from the given buffer object */ -void *bencode_buffer_alloc(bencode_buffer_t *, size_t); - -/* Destroys a previously initialized bencode_buffer_t object. All memory used by the object is freed - * and all objects created through it become invalid. */ -void bencode_buffer_free(bencode_buffer_t *buf); - -// Move all objects from one buffer to another. The `from` buffer will be unusable afterwards. -void bencode_buffer_merge(bencode_buffer_t *to, bencode_buffer_t *from); - -/* Creates a new empty dictionary object. Memory will be allocated from the bencode_buffer_t object. +/* Creates a new empty dictionary object. Memory will be allocated from the arena_t object. * Returns NULL if no memory could be allocated. */ -bencode_item_t *bencode_dictionary(bencode_buffer_t *buf); +bencode_item_t *bencode_dictionary(arena_t *buf); -/* Creates a new empty list object. Memory will be allocated from the bencode_buffer_t object. +/* Creates a new empty list object. Memory will be allocated from the arena_t object. * Returns NULL if no memory could be allocated. */ -bencode_item_t *bencode_list(bencode_buffer_t *buf); +bencode_item_t *bencode_list(arena_t *buf); /* Returns the buffer associated with an item, or NULL if pointer given is NULL */ -INLINE bencode_buffer_t *bencode_item_buffer(bencode_item_t *); - -/* like strdup() but uses the bencode buffer to store the string */ -INLINE char *bencode_strdup(bencode_buffer_t *, const char *); - -/* ditto but returns a str object */ -INLINE str bencode_strdup_str(bencode_buffer_t *, const char *); - - +INLINE arena_t *bencode_item_buffer(bencode_item_t *); /*** DICTIONARY BUILDING ***/ -/* Adds a new key/value pair to a dictionary. Memory will be allocated from the same bencode_buffer_t +/* Adds a new key/value pair to a dictionary. Memory will be allocated from the same arena_t * object as the dictionary was allocated from. Returns NULL if no memory could be allocated, otherwise * returns "val". * The function does not check whether the key being added is already present in the dictionary. @@ -110,7 +76,7 @@ INLINE bencode_item_t *bencode_dictionary_str_add(bencode_item_t *dict, const st bencode_item_t *bencode_dictionary_add_len(bencode_item_t *dict, const char *key, size_t keylen, bencode_item_t *val); /* Convenience function to add a string value to a dictionary, possibly duplicated into the - * bencode_buffer_t object. */ + * arena_t object. */ INLINE void bencode_dictionary_add_string(bencode_item_t *dict, const char *key, const char *val); /* Ditto, but for a "str" object */ @@ -152,21 +118,21 @@ INLINE bencode_item_t *bencode_list_add_dictionary(bencode_item_t *list); * be allocated. * Strings are not copied or duplicated, so the string pointed to by "s" must remain valid until * the complete document is finally encoded or sent out. */ -bencode_item_t *bencode_string_len(bencode_buffer_t *buf, const char *s, size_t len); +bencode_item_t *bencode_string_len(arena_t *buf, const char *s, size_t len); /* Creates a new byte-string object. The given string must be null-terminated. Otherwise identical * to bencode_string_len(). */ -INLINE bencode_item_t *bencode_string(bencode_buffer_t *buf, const char *s); +INLINE bencode_item_t *bencode_string(arena_t *buf, const char *s); /* Creates a new byte-string object from a "str" object. The string does not have to be null- * terminated. */ -INLINE bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s); +INLINE bencode_item_t *bencode_str(arena_t *buf, const str *s); -/* Identical to the above three functions, but copies the string into the bencode_buffer_t object. +/* Identical to the above three functions, but copies the string into the arena_t object. * Thus, the given string doesn't have to remain valid and accessible afterwards. */ -bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, size_t len); -INLINE bencode_item_t *bencode_string_dup(bencode_buffer_t *buf, const char *s); -INLINE bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s); +bencode_item_t *bencode_string_len_dup(arena_t *buf, const char *s, size_t len); +INLINE bencode_item_t *bencode_string_dup(arena_t *buf, const char *s); +INLINE bencode_item_t *bencode_str_dup(arena_t *buf, const str *s); /* Convenience function to compare a string object to a regular C string. Returns 2 if object * isn't a string object, otherwise returns according to strcmp(). */ @@ -183,7 +149,7 @@ INLINE str *bencode_get_str(bencode_item_t *in, str *out); /*** INTEGER BUILDING ***/ /* Creates a new integer object. Returns NULL if no memory could be allocated. */ -bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i); +bencode_item_t *bencode_integer(arena_t *buf, long long int i); // Return integer, possibly converted from string INLINE long long bencode_get_integer_str(bencode_item_t *item, long long int defval); @@ -197,7 +163,7 @@ INLINE long long bencode_get_integer_str(bencode_item_t *item, long long int def /* Collapses and encodes the complete document structure under the "root" element (which normally * is either a dictionary or a list) into an array of "iovec" structures. This array can then be * passed to functions ala writev() or sendmsg() to output the encoded document as a whole. Memory - * is allocated from the same bencode_buffer_t object as the "root" object was allocated from. + * is allocated from the same arena_t object as the "root" object was allocated from. * The "head" and "tail" parameters specify additional "iovec" structures that should be included * in the allocated array before or after (respectively) the iovec structures used by the encoded * document. Both parameters can be zero if no additional elements in the array are required. @@ -210,23 +176,23 @@ INLINE long long bencode_get_integer_str(bencode_item_t *item, long long int def * [(head) .. (head + cnt - 1)] = the encoded document * [(head + cnt) .. (head + cnt + tail - 1)] = unused and uninitialized iovec structures * - * The returned array will be freed when the corresponding bencode_buffer_t object is destroyed. */ + * The returned array will be freed when the corresponding arena_t object is destroyed. */ struct iovec *bencode_iovec(bencode_item_t *root, int *cnt, unsigned int head, unsigned int tail); /* Similar to bencode_iovec(), but instead returns the encoded document as a null-terminated string. - * Memory for the string is allocated from the same bencode_buffer_t object as the "root" object + * Memory for the string is allocated from the same arena_t object as the "root" object * was allocated from. If "len" is a non-NULL pointer, the length of the generated string is returned * in *len. This is important if the encoded document contains binary data, in which case null * termination cannot be trusted. The returned string is freed when the corresponding - * bencode_buffer_t object is destroyed. */ + * arena_t object is destroyed. */ char *bencode_collapse(bencode_item_t *root, size_t *len); /* Identical to bencode_collapse() but returns a "str" object. */ INLINE str bencode_collapse_str(bencode_item_t *root); /* Identical to bencode_collapse(), but the memory for the returned string is not allocated from - * a bencode_buffer_t object, but instead using the function defined as BENCODE_MALLOC (normally - * malloc() or pkg_malloc()), similar to strdup(). Using this function, the bencode_buffer_t + * a arena_t object, but instead using the function defined as BENCODE_MALLOC (normally + * malloc() or pkg_malloc()), similar to strdup(). Using this function, the arena_t * object can be destroyed, but the returned string remains valid and usable. */ char *bencode_collapse_dup(bencode_item_t *root, size_t *len); @@ -238,7 +204,7 @@ char *bencode_collapse_dup(bencode_item_t *root, size_t *len); /* Decodes an encoded document from a string into a tree of bencode_item_t objects. The string does * not need to be null-terminated, instead the length of the string is given through the "len" - * parameter. Memory is allocated from the bencode_buffer_t object. Returns NULL if no memory could + * parameter. Memory is allocated from the arena_t object. Returns NULL if no memory could * be allocated or if the document could not be successfully decoded. * * The returned element is the "root" of the document tree and normally is either a list object or @@ -279,16 +245,16 @@ char *bencode_collapse_dup(bencode_item_t *root, size_t *len); * length). Strings are NOT null-terminated. Decoded integer objects will contain the decoded value * in ->value. * - * All memory is freed when the bencode_buffer_t object is destroyed. + * All memory is freed when the arena_t object is destroyed. */ -bencode_item_t *bencode_decode(bencode_buffer_t *buf, const char *s, size_t len); +bencode_item_t *bencode_decode(arena_t *buf, const char *s, size_t len); /* Identical to bencode_decode(), but returns successfully only if the type of the decoded object match * "expect". */ -INLINE bencode_item_t *bencode_decode_expect(bencode_buffer_t *buf, const char *s, size_t len, bencode_type_t expect); +INLINE bencode_item_t *bencode_decode_expect(arena_t *buf, const char *s, size_t len, bencode_type_t expect); /* Identical to bencode_decode_expect() but takes a "str" argument. */ -INLINE bencode_item_t *bencode_decode_expect_str(bencode_buffer_t *buf, const str *s, bencode_type_t expect); +INLINE bencode_item_t *bencode_decode_expect_str(arena_t *buf, const str *s, bencode_type_t expect); /* Returns the number of bytes that could successfully be decoded from 's', -1 if more bytes are needed or -2 on error */ ssize_t bencode_valid(const char *s, size_t len); @@ -307,7 +273,7 @@ bencode_item_t *bencode_dictionary_get_len(bencode_item_t *dict, const char *key /* Identical to bencode_dictionary_get() but returns the value only if its type is a string, and * returns it as a pointer to the string itself. Returns NULL if the value is of some other type. The * returned string is NOT null-terminated. Length of the string is returned in *len, which must be a - * valid pointer. The returned string will be valid until dict's bencode_buffer_t object is destroyed. */ + * valid pointer. The returned string will be valid until dict's arena_t object is destroyed. */ INLINE char *bencode_dictionary_get_string(bencode_item_t *dict, const char *key, size_t *len); /* Identical to bencode_dictionary_get_string() but returns in a "str" struct. @@ -320,7 +286,7 @@ INLINE str bencode_dictionary_get_str(bencode_item_t *dict, const char *key); INLINE int bencode_dictionary_get_strcmp(bencode_item_t *dict, const char *key, const char *str); /* Identical to bencode_dictionary_get() but returns the string in a newly allocated buffer (using the - * BENCODE_MALLOC function), which remains valid even after bencode_buffer_t is destroyed. */ + * BENCODE_MALLOC function), which remains valid even after arena_t is destroyed. */ INLINE char *bencode_dictionary_get_string_dup(bencode_item_t *dict, const char *key, size_t *len); /* Combines bencode_dictionary_get_str() and bencode_dictionary_get_string_dup(). Fills in a "str" @@ -343,45 +309,31 @@ INLINE bencode_item_t *bencode_dictionary_get_expect(bencode_item_t *dict, const /**************************/ -INLINE bencode_buffer_t *bencode_item_buffer(bencode_item_t *i) { +INLINE arena_t *bencode_item_buffer(bencode_item_t *i) { if (!i) return NULL; return i->buffer; } -INLINE bencode_item_t *bencode_string(bencode_buffer_t *buf, const char *s) { +INLINE bencode_item_t *bencode_string(arena_t *buf, const char *s) { return bencode_string_len(buf, s, strlen(s)); } -INLINE bencode_item_t *bencode_string_dup(bencode_buffer_t *buf, const char *s) { +INLINE bencode_item_t *bencode_string_dup(arena_t *buf, const char *s) { return bencode_string_len_dup(buf, s, strlen(s)); } -INLINE bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s) { +INLINE bencode_item_t *bencode_str(arena_t *buf, const str *s) { return bencode_string_len(buf, s->s, s->len); } -INLINE bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s) { +INLINE bencode_item_t *bencode_str_dup(arena_t *buf, const str *s) { return bencode_string_len_dup(buf, s->s, s->len); } -INLINE char *bencode_strdup(bencode_buffer_t *buf, const char *s) { - char *ret = bencode_buffer_alloc(buf, strlen(s) + 1); - strcpy(ret, s); - return ret; -} - -INLINE str bencode_strdup_str(bencode_buffer_t *buf, const char *s) { - str o = STR_NULL; - o.len = strlen(s); - o.s = bencode_buffer_alloc(buf, o.len); - memcpy(o.s, s, o.len); - return o; -} - -INLINE str bencode_str_strdup(bencode_buffer_t *buf, const str *s) { +INLINE str bencode_str_strdup(arena_t *buf, const str *s) { str o = *s; - o.s = bencode_buffer_alloc(buf, o.len); + o.s = arena_alloc(buf, o.len); #ifdef ASAN_BUILD if (o.len) #endif @@ -389,10 +341,10 @@ INLINE str bencode_str_strdup(bencode_buffer_t *buf, const str *s) { return o; } -INLINE str *bencode_str_str_dup(bencode_buffer_t *buf, const str *s) { - str *o = bencode_buffer_alloc(buf, sizeof(*o)); +INLINE str *bencode_str_str_dup(arena_t *buf, const str *s) { + str *o = arena_alloc(buf, sizeof(*o)); *o = *s; - o->s = bencode_buffer_alloc(buf, o->len); + o->s = arena_alloc(buf, o->len); memcpy(o->s, s->s, o->len); return o; } @@ -431,7 +383,7 @@ INLINE void bencode_dictionary_add_str_dup(bencode_item_t *dict, const char *key INLINE void bencode_dictionary_add_str_dup_dup(bencode_item_t *dict, const char *key, const str *val) { if (val) - bencode_dictionary_add(dict, bencode_strdup(bencode_item_buffer(dict), key), + bencode_dictionary_add(dict, arena_strdup(bencode_item_buffer(dict), key), bencode_str_dup(bencode_item_buffer(dict), val)); } @@ -497,7 +449,7 @@ INLINE char *bencode_dictionary_get_string_dup(bencode_item_t *dict, const char s = bencode_dictionary_get_string(dict, key, len); if (!s) return NULL; - ret = BENCODE_MALLOC(*len); + ret = dict->buffer->alloc(*len); if (!ret) return NULL; memcpy(ret, s, *len); @@ -546,7 +498,7 @@ INLINE long long int bencode_dictionary_get_int_str(bencode_item_t *dict, const return bencode_get_integer_str(val, defval); } -INLINE bencode_item_t *bencode_decode_expect(bencode_buffer_t *buf, const char *s, size_t len, bencode_type_t expect) { +INLINE bencode_item_t *bencode_decode_expect(arena_t *buf, const char *s, size_t len, bencode_type_t expect) { bencode_item_t *ret; ret = bencode_decode(buf, s, len); if (!ret || ret->type != expect) @@ -554,7 +506,7 @@ INLINE bencode_item_t *bencode_decode_expect(bencode_buffer_t *buf, const char * return ret; } -INLINE bencode_item_t *bencode_decode_expect_str(bencode_buffer_t *buf, const str *s, bencode_type_t expect) { +INLINE bencode_item_t *bencode_decode_expect_str(arena_t *buf, const str *s, bencode_type_t expect) { return bencode_decode_expect(buf, s->s, s->len, expect); } diff --git a/lib/compat.h b/lib/compat.h index d39078f32..e29fcbed0 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -19,12 +19,6 @@ # define ACCESS(...) #endif - -#ifndef BENCODE_MALLOC -#define BENCODE_MALLOC malloc -#define BENCODE_FREE free -#endif - #include "str.h" #endif diff --git a/lib/loglib.c b/lib/loglib.c index bdd94567a..d60e38513 100644 --- a/lib/loglib.c +++ b/lib/loglib.c @@ -10,7 +10,7 @@ #include #include #include "auxlib.h" -#include "bencode.h" +#include "arena.h" struct log_limiter_entry { @@ -75,7 +75,7 @@ int ilog_facility = LOG_DAEMON; static GHashTable *__log_limiter; static mutex_t __log_limiter_lock = MUTEX_STATIC_INIT; static unsigned int __log_limiter_count; -static bencode_buffer_t __log_limiter_buffer; +static arena_t __log_limiter_buffer; @@ -156,19 +156,19 @@ void __vpilog(int prio, const char *prefix, const char *fmt, va_list ap) { if (__log_limiter_count > 10000) { g_hash_table_remove_all(__log_limiter); __log_limiter_count = 0; - bencode_buffer_free(&__log_limiter_buffer); - bencode_buffer_init(&__log_limiter_buffer); + arena_free(&__log_limiter_buffer); + arena_init(&__log_limiter_buffer, g_malloc, g_free); } time_t now = time(NULL); llep = g_hash_table_lookup(__log_limiter, &lle); if (!llep || (now - llep->when) >= 15) { - llep = bencode_buffer_alloc(&__log_limiter_buffer, sizeof(*llep)); + llep = arena_alloc(&__log_limiter_buffer, sizeof(*llep)); if (llep) { *llep = (__typeof(*llep)) { - .prefix = bencode_strdup(&__log_limiter_buffer, prefix), - .msg = bencode_strdup(&__log_limiter_buffer, msg), + .prefix = arena_strdup(&__log_limiter_buffer, prefix), + .msg = arena_strdup(&__log_limiter_buffer, msg), .when = now, }; g_hash_table_insert(__log_limiter, llep, llep); @@ -240,7 +240,7 @@ static int log_limiter_entry_equal(const void *a, const void *b) { void log_init(const char *handle) { __log_limiter = g_hash_table_new(log_limiter_entry_hash, log_limiter_entry_equal); - bencode_buffer_init(&__log_limiter_buffer); + arena_init(&__log_limiter_buffer, g_malloc, g_free); if (!rtpe_common_config_ptr->log_stderr) openlog(handle, LOG_PID | LOG_NDELAY, ilog_facility); @@ -248,7 +248,7 @@ void log_init(const char *handle) { void log_free(void) { g_hash_table_destroy(__log_limiter); - bencode_buffer_free(&__log_limiter_buffer); + arena_free(&__log_limiter_buffer); } int parse_log_facility(const char *name, int *dst) { diff --git a/perf-tester/Makefile b/perf-tester/Makefile index 6ecaa18d2..17cfc6102 100644 --- a/perf-tester/Makefile +++ b/perf-tester/Makefile @@ -51,6 +51,7 @@ LIBSRCS := codeclib.c loglib.c auxlib.c resample.c str.c dtmflib.c rtplib.c poll bencode.c uring.c cchain.c socket.c LIBSRCS += rescale.c LIBSRCS += $(CODEC_SRCS) +LIBSRCS += arena.c LIBASM := mvr2s_x64_avx2.S mvr2s_x64_avx512.S include ../lib/common.Makefile diff --git a/recording-daemon/Makefile b/recording-daemon/Makefile index b8f1a6231..6a2265e8b 100644 --- a/recording-daemon/Makefile +++ b/recording-daemon/Makefile @@ -47,6 +47,7 @@ LIBSRCS := loglib.c auxlib.c rtplib.c codeclib.c resample.c str.c socket.c strea dtmflib.c bufferpool.c bencode.c http.c s3utils.c oauth.c cchain.c LIBSRCS += rescale.c LIBSRCS += $(CODEC_SRCS) +LIBSRCS += arena.c LIBASM := mvr2s_x64_avx2.S mvr2s_x64_avx512.S mix_in_x64_avx2.S mix_in_x64_avx512bw.S mix_in_x64_sse2.S MDS := rtpengine-recording.ronn diff --git a/t/Makefile b/t/Makefile index 450821761..7d0dc1d18 100644 --- a/t/Makefile +++ b/t/Makefile @@ -315,6 +315,7 @@ test-mix-buffer: test-mix-buffer.o \ ../daemon/helpers.o \ ../daemon/rtp.o \ ../daemon/ssrc.o \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/bufferpool.o \ @@ -347,6 +348,7 @@ spandsp_raw_fax_tests: spandsp_send_fax_pcm spandsp_recv_fax_pcm spandsp_send_fa test-amr-decode: test-amr-decode.o \ $(COMMONOBJS) \ ../lib/amr.strhash.o \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/cchain.o \ @@ -361,6 +363,7 @@ test-amr-decode: test-amr-decode.o \ test-amr-encode: test-amr-encode.o \ $(COMMONOBJS) \ ../lib/amr.strhash.o \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/cchain.o \ @@ -374,11 +377,11 @@ test-amr-encode: test-amr-encode.o \ test-dtmf-detect: test-dtmf-detect.o -aes-crypt: aes-crypt.o $(COMMONOBJS) ../daemon/crypto.o ../lib/bencode.o log.o +aes-crypt: aes-crypt.o $(COMMONOBJS) ../daemon/crypto.o ../lib/arena.o ../lib/bencode.o log.o -aead-decrypt: aead-decrypt.o $(COMMONOBJS) ../daemon/crypto.o ../daemon/rtp.o ../lib/bencode.o log.o +aead-decrypt: aead-decrypt.o $(COMMONOBJS) ../daemon/crypto.o ../daemon/rtp.o ../lib/arena.o ../lib/bencode.o log.o -aead-aes-crypt: aead-aes-crypt.o $(COMMONOBJS) ../daemon/crypto.o ../lib/bencode.o log.o +aead-aes-crypt: aead-aes-crypt.o $(COMMONOBJS) ../daemon/crypto.o ../lib/arena.o ../lib/bencode.o log.o test-stats: test-stats.o \ $(COMMONOBJS) \ @@ -423,6 +426,7 @@ test-stats: test-stats.o \ ../daemon/timerthread.o \ ../daemon/udp_listener.o \ ../daemon/websocket.o \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/bufferpool.o \ @@ -486,6 +490,7 @@ test-transcode: test-transcode.o \ ../daemon/udp_listener.o \ ../daemon/websocket.o \ ../lib/amr.strhash.o \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/bufferpool.o \ @@ -513,6 +518,7 @@ test-transcode: test-transcode.o \ test-sequencer: test-sequencer.o \ $(COMMONOBJS) \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/cchain.o \ @@ -524,6 +530,7 @@ test-sequencer: test-sequencer.o \ test-resample: test-resample.o \ $(COMMONOBJS) \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/cchain.o \ @@ -541,6 +548,7 @@ test-payload-tracker: test-payload-tracker.o \ ../daemon/helpers.o \ ../daemon/rtp.o \ ../daemon/ssrc.o \ + ../lib/arena.o \ ../lib/avc.o \ ../lib/bencode.o \ ../lib/bufferpool.o \ @@ -558,7 +566,7 @@ test-payload-tracker: test-payload-tracker.o \ test-kernel-module: test-kernel-module.o $(COMMONOBJS) ../daemon/kernel.o -test-const_str_hash.strhash: test-const_str_hash.strhash.o $(COMMONOBJS) ../lib/bencode.o log.o +test-const_str_hash.strhash: test-const_str_hash.strhash.o $(COMMONOBJS) ../lib/arena.o ../lib/bencode.o log.o s3-auth: s3-auth.o ../lib/s3utils.o diff --git a/t/test-transcode.c b/t/test-transcode.c index 11bba0db6..26de7a6d4 100644 --- a/t/test-transcode.c +++ b/t/test-transcode.c @@ -99,7 +99,7 @@ static void __start(const char *file, int line) { obj_hold(&call); call.tags = str_ml_ht_new(); call.callid = STR("test-call"); - bencode_buffer_init(&call.buffer); + arena_init(&call.buffer, g_malloc, g_free); call_memory_arena_set(&call); ml_A = __monologue_create(&call, &call.callid); ml_B = __monologue_create(&call, &call.callid); @@ -395,7 +395,7 @@ static void end(void) { if (ml_B) __monologue_free(ml_B); __cleanup(); - bencode_buffer_free(&call.buffer); + arena_free(&call.buffer); call_memory_arena_release(); printf("\n"); }