From 1945dbf8345a2b59e9057d21e38c78913272bdaa Mon Sep 17 00:00:00 2001 From: kartofen Date: Sat, 16 Nov 2024 17:20:23 +0200 Subject: cleanup and minor refactoring --- src/env.c | 75 ++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 22 deletions(-) (limited to 'src/env.c') diff --git a/src/env.c b/src/env.c index ee2e65e..840b87c 100644 --- a/src/env.c +++ b/src/env.c @@ -25,9 +25,21 @@ struct env_kv { int flags; }; -#include "mempool.h" -MEMPOOL_GENERATE(env, struct env, 256) -MEMPOOL_GENERATE(env_kv, struct env_kv, 1024) +// #ifdef ENABLE_MEMPOOL + #include "mempool.h" + MEMPOOL_GENERATE(env, struct env, 256) + MEMPOOL_GENERATE(env_kv, struct env_kv, 1024) + + #define env_alloc() env_mempool_allocate() + #define env_free(v) env_mempool_free(v) + #define env_kv_alloc() env_kv_mempool_allocate() + #define env_kv_free(v) env_kv_mempool_free(v) +// #else + // #define env_alloc() malloc(sizeof(struct env)) + // #define env_free(v) free(v) + // #define env_kv_alloc() malloc(sizeof(struct env_kv)) + // #define env_kv_free(v) free(v) +// #endif static int equal(char *key1, char *key2) { @@ -36,7 +48,7 @@ static int equal(char *key1, char *key2) static struct env_kv *env_kv_create(char *key, value_t value, int flags) { - struct env_kv *pair = env_kv_mempool_allocate(); + struct env_kv *pair = env_kv_alloc(); pair->key = key; pair->value = value; @@ -52,12 +64,12 @@ static void env_kv_destroy(struct env_kv *pair) free(pair->key); value_destroy(pair->value); - env_kv_mempool_free(pair); + env_kv_free(pair); } env_t env_create(env_t parent) { - env_t env = env_mempool_allocate(); + env_t env = env_alloc(); env->parent = env_copy(parent); env->head = NULL; @@ -83,7 +95,7 @@ void env_destroy(env_t env) } env_destroy(env->parent); - env_mempool_free(env); + env_free(env); } int env_insert(env_t env, char *key, _value_t value, @@ -139,18 +151,37 @@ env_t env_copy(env_t env) return env; } -// void env_print(env_t env) -// { -// printf("REFS: %d\n", env->refs); -// printf("CREFS: %d\n", env->circular_refs); -// if(!env || !env->head) return; -// list_for_each_entry(struct env_kv, pair, list, env->head) { -// printf("ENTRY: %s %s\n", pair->key, pair->value); -// } - -// if(env->parent) { -// printf("---- PARENT ----\n"); -// env_print(env->parent); -// printf("-- END PARENT --\n"); -// } -// } +int env_depend(env_t parent, env_t dep) +{ + if(!dep || !parent) return 0; + if(dep == parent) return 1; + + return env_depend(parent, dep->parent); +} + +#ifdef DEBUG +static void _env_print(env_t env, int depth) +{ + (void)depth; + + printf("refs: %d\n", env->refs); + printf("crefs: %d\n", env->circular_refs); + if(!env || !env->head) return; + list_for_each_entry(struct env_kv, pair, list, env->head) { + printf(" %s %s\n", pair->key, pair->value); + } + + if(env->parent) { + printf("--- parent start ---\n"); + _env_print(env->parent, depth+2); + printf("---- parent end ----\n"); + } +} + +void env_print(env_t env) +{ + printf("--- env start ---\n"); + _env_print(env, 0); + printf("---- env end ----\n"); +} +#endif -- cgit v1.2.3