Files
mie/mie/dialect/builtin/string-cache.c
Max Wash da630ce382 mie: replace fixed value system with an extensible attribute interface
under this new system, dialects can define their own custom attributes,
complete with their own print() and parse() callbacks, which can then be
used as values in an op's attribute dictionary.

alongside custom dialect attributes, the former int, float, and string
constant values have been converted to attributes provided by the
arith and builtin dialects respectively. the caches for these attributes
have also been moved from mie_ctx to their respective dialect data
structures.

this system will allow new types of attributes to be implemented,
including dictionaries, arrays, and references to types themselves
(rather than just particular values of a given type).
2026-01-13 22:37:43 +00:00

81 lines
1.6 KiB
C

#include <blue/core/btree.h>
#include <blue/ds/hashmap.h>
#include <mie/ctx.h>
#include <mie/dialect/builtin.h>
#include <stdlib.h>
#include <string.h>
struct mie_string_cache {
b_hashmap *c_entries;
};
static struct mie_string *mie_string_create(
struct mie_ctx *ctx, const char *s, size_t len)
{
struct mie_string *out = malloc(sizeof *out);
if (!out) {
return NULL;
}
out->str_base.a_def
= mie_ctx_get_attribute_definition(ctx, "builtin", "string");
out->str_val = b_strdup(s);
if (!out->str_val) {
return NULL;
}
out->str_len = len;
return out;
}
static void mie_string_destroy(struct mie_string *str)
{
}
struct mie_string_cache *mie_string_cache_create(void)
{
struct mie_string_cache *out = malloc(sizeof *out);
if (!out) {
return NULL;
}
memset(out, 0x0, sizeof *out);
out->c_entries = b_hashmap_create(
NULL, (b_hashmap_value_destructor)mie_string_destroy);
if (!out->c_entries) {
free(out);
return NULL;
}
return out;
}
void mie_string_cache_destroy(struct mie_string_cache *cache)
{
b_hashmap_unref(cache->c_entries);
free(cache);
}
struct mie_string *mie_string_cache_get(
struct mie_string_cache *cache, struct mie_ctx *ctx, const char *s)
{
size_t s_len = strlen(s);
b_hashmap_key key = {.key_data = s, .key_size = s_len};
const b_hashmap_value *value = b_hashmap_get(cache->c_entries, &key);
if (value) {
return value->value_data;
}
struct mie_string *str = mie_string_create(ctx, s, s_len);
if (!str) {
return NULL;
}
b_hashmap_value new_val = {.value_data = str, .value_size = sizeof *str};
b_hashmap_put(cache->c_entries, &key, &new_val);
return str;
}