Feature: Use arena-allocation for memory handling

Arena allocation make deallocation of nested structed MUCH faster, and
improves the spatial locality of allocations. It makes no sense to
deallocate only parts of a JSON structure so arenas are a good fit here.
This commit is contained in:
olemorud
2023-05-16 02:44:49 +02:00
committed by Ole Kristian Morud
parent f58c5dcfee
commit b3821cf8bf
11 changed files with 124 additions and 97 deletions

View File

@@ -2,6 +2,7 @@
#ifndef _JSON_VALUE_H
#define _JSON_VALUE_H
#include "arena.h"
#include "config.h"
#include <stdbool.h> // bool
@@ -9,7 +10,7 @@ typedef struct obj_entry {
char const* key;
struct json_value* val;
struct obj_entry* next;
} * __p_obj_entry;
}* __p_obj_entry;
typedef __p_obj_entry obj_t[OBJ_SIZE];
@@ -32,11 +33,11 @@ struct json_value {
};
void* obj_at(obj_t m, char* const key);
bool obj_insert(obj_t m, char* const key, struct json_value* value);
void obj_delete(obj_t* m);
bool obj_insert(obj_t m, char* const key, struct json_value* value, arena_t* arena);
void obj_delete(obj_t* m, arena_t* arena);
void print_json(struct json_value val, int indent);
void json_value_delete(struct json_value val);
void json_value_delete(struct json_value val, arena_t* arena);
#endif

View File

@@ -4,8 +4,9 @@
#include "json_value.h"
#include "arena.h"
#include <stdio.h> // FILE*
struct json_value parse_json_value(FILE* fp);
struct json_value parse_json_value(FILE* fp, arena_t* arena);
#endif

View File

@@ -10,6 +10,6 @@ __attribute__((__noreturn__)) void err_ctx(int exit_code, FILE* fp, const char*
void* malloc_or_die(size_t size);
void* realloc_or_die(void* ptr, size_t size);
void* calloc_or_die(size_t nmemb, size_t size);
void print_trace();
void print_trace(void);
#endif