aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
blob: 840b87c82f86a92a26f1289d44f451afbfa3815d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "env.h"

#include "list.h"
#include "value.h"

#define HAS(v, flag) (((v) & (flag)) == (flag))

struct env {
    struct env *parent;
    struct list_head *head;

    size_t refs;
    size_t circular_refs;
};

struct env_kv {
    char *key;
    _value_t value;
    struct list_head list;

    int flags;
};

// #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)
{
    return (strcmp(key1, key2) == 0);
}

static struct env_kv *env_kv_create(char *key, value_t value, int flags)
{
    struct env_kv *pair = env_kv_alloc();

    pair->key = key;
    pair->value = value;
    LIST_EMPTY(&pair->list);
    pair->flags = flags;

    return pair;
}

static void env_kv_destroy(struct env_kv *pair)
{
    if(HAS(pair->flags, ENV_KV_FREE_KEY))
        free(pair->key);

    value_destroy(pair->value);
    env_kv_free(pair);
}

env_t env_create(env_t parent)
{
    env_t env = env_alloc();
    env->parent = env_copy(parent);
    env->head = NULL;

    env->refs = 0;
    env->circular_refs = 0;
    return env;
}

void env_destroy(env_t env)
{
    if(env == ENV_EMPTY) return;

    if(env->refs > env->circular_refs) {
        env->refs--;
        return;
    }

    list_for_each_safe(head, env->head) {
        struct env_kv *p = list_entry(head, struct env_kv, list);

        if(HAS(p->flags, ENV_KV_CIRCULAR_REF)) env->circular_refs--;
        env_kv_destroy(p);
    }

    env_destroy(env->parent);
    env_free(env);
}

int env_insert(env_t env, char *key, _value_t value,
               _value_t *prevval, int flags)
{
    if(env->head)
        list_for_each_entry(struct env_kv, p, list, env->head) {
            if(!equal(p->key, key)) continue;

            env->circular_refs +=
                (HAS(flags, ENV_KV_CIRCULAR_REF) -
                 HAS(p->flags, ENV_KV_CIRCULAR_REF));
            p->flags = flags;

            if(prevval) *prevval = p->value;
            p->value = value;
            return 0;
        }

    if(HAS(flags, ENV_KV_CIRCULAR_REF)) env->circular_refs++;

    struct env_kv *pair = env_kv_create(key, value, flags);
    if(env->head) list_append(&pair->list, env->head);
    env->head = &pair->list;

    return 0;
}

int env_query(env_t env, char *key, _value_t *data)
{
    struct env_kv *pair = NULL;

    for(env_t e = env; e; e = e->parent) {
        if(e->head == NULL) continue;
        list_for_each_entry(struct env_kv, p, list, e->head) {
            if(equal(p->key, key)) pair = p;
        }

        if(pair) break;
    }

    if(!pair) return 1;

    *data = pair->value;
    return 0;
}

env_t env_copy(env_t env)
{
    if(!env) return env;

    env->refs++;
    return env;
}

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