Files
mie/mie/dialect/builtin/index-cache.c

82 lines
1.7 KiB
C

#include <blue/core/btree.h>
#include <mie/ctx.h>
#include <mie/dialect/builtin.h>
#include <stdlib.h>
#include <string.h>
struct index_cache_entry {
b_btree_node e_node;
struct mie_index e_value;
};
struct mie_index_cache {
b_btree c_entries;
};
static B_BTREE_DEFINE_SIMPLE_INSERT(
struct index_cache_entry, e_node, e_value.i_value, put_index);
static B_BTREE_DEFINE_SIMPLE_GET(
struct index_cache_entry, size_t, e_node, e_value.i_value, get_index);
static struct index_cache_entry *index_cache_entry_create(
struct mie_ctx *ctx, size_t val)
{
struct index_cache_entry *out = malloc(sizeof *out);
if (!out) {
return NULL;
}
memset(out, 0x0, sizeof *out);
out->e_value.i_base.a_def
= mie_ctx_get_attribute_definition(ctx, "builtin", "index");
out->e_value.i_value = val;
return out;
}
struct mie_index_cache *mie_index_cache_create(void)
{
struct mie_index_cache *out = malloc(sizeof *out);
if (!out) {
return NULL;
}
memset(out, 0x0, sizeof *out);
return out;
}
void mie_index_cache_destroy(struct mie_index_cache *cache)
{
b_btree_node *cur = b_btree_first(&cache->c_entries);
while (cur) {
b_btree_node *next = b_btree_next(cur);
b_btree_delete(&cache->c_entries, cur);
struct index_cache_entry *entry
= b_unbox(struct index_cache_entry, cur, e_node);
free(entry);
cur = next;
}
free(cache);
}
struct mie_index *mie_index_cache_get(
struct mie_index_cache *cache, struct mie_ctx *ctx, size_t value)
{
struct index_cache_entry *entry = get_index(&cache->c_entries, value);
if (!entry) {
entry = index_cache_entry_create(ctx, value);
if (!entry) {
return NULL;
}
put_index(&cache->c_entries, entry);
}
return &entry->e_value;
}