<commit_before>#include <jive/arch/memlayout-simple.h>

#include <jive/bitstring/type.h>
#include <jive/vsdg/recordtype.h>
#include <jive/vsdg/uniontype.h>

JIVE_DEFINE_HASH_TYPE(jive_memlayout_record_hash, jive_memlayout_record_entry, const struct jive_record_declaration *, decl, hash_chain);

JIVE_DEFINE_HASH_TYPE(jive_memlayout_union_hash, jive_memlayout_union_entry, const struct jive_union_declaration *, decl, hash_chain);

JIVE_DEFINE_RANGEMAP_TYPE(jive_memlayout_bitstring_map, jive_dataitem_memlayout *, NULL);

void
jive_memlayout_mapper_cached_init_(jive_memlayout_mapper_cached * self, jive_context * context)
{
	self->context = context;
	jive_memlayout_record_hash_init(&self->record_hash, context);
	jive_memlayout_union_hash_init(&self->union_hash, context);
	jive_memlayout_bitstring_map_init(&self->bitstring_map);
}

void
jive_memlayout_mapper_cached_fini_(jive_memlayout_mapper_cached * self)
{
	struct jive_memlayout_record_hash_iterator i;
	
	i = jive_memlayout_record_hash_begin(&self->record_hash);
	while (i.entry) {
		jive_memlayout_record_entry * entry = i.entry;
		
		jive_memlayout_record_hash_remove(&self->record_hash, entry);
		jive_context_free(self->context, entry->layout.element);
		jive_context_free(self->context, entry);
		
		jive_memlayout_record_hash_iterator_next(&i);
	}
	
	struct jive_memlayout_union_hash_iterator j;
	j = jive_memlayout_union_hash_begin(&self->union_hash);
	while (j.entry) {
		jive_memlayout_union_entry * entry = j.entry;
		
		jive_memlayout_union_hash_remove(&self->union_hash, entry);
		jive_context_free(self->context, entry);
		
		jive_memlayout_union_hash_iterator_next(&j);
	}
	
	ssize_t k;
	for (k = self->bitstring_map.low; k < self->bitstring_map.high; k++) {
		jive_dataitem_memlayout ** playout = jive_memlayout_bitstring_map_lookup(&self->bitstring_map, k);
		if (*playout)
			jive_context_free(self->context, *playout);
	}
	
	jive_memlayout_record_hash_fini(&self->record_hash);
	jive_memlayout_union_hash_fini(&self->union_hash);
	jive_memlayout_bitstring_map_fini(&self->bitstring_map);
}

jive_record_memlayout *
jive_memlayout_mapper_cached_map_record_(jive_memlayout_mapper_cached * self, const struct jive_record_declaration * decl)
{
	jive_memlayout_record_entry * entry = jive_memlayout_record_hash_lookup(&self->record_hash, decl);
	if (entry)
		return &entry->layout;
	else
		return NULL;
}

jive_union_memlayout *
jive_memlayout_mapper_cached_map_union_(jive_memlayout_mapper_cached * self, const struct jive_union_declaration * decl)
{
	jive_memlayout_union_entry * entry = jive_memlayout_union_hash_lookup(&self->union_hash, decl);
	if (entry)
		return &entry->layout;
	else<commit_msg>arch: fix clearing of hash table in memlayout destructor<commit_after>#include <jive/arch/memlayout-simple.h>

#include <jive/bitstring/type.h>
#include <jive/vsdg/recordtype.h>
#include <jive/vsdg/uniontype.h>

JIVE_DEFINE_HASH_TYPE(jive_memlayout_record_hash, jive_memlayout_record_entry, const struct jive_record_declaration *, decl, hash_chain);

JIVE_DEFINE_HASH_TYPE(jive_memlayout_union_hash, jive_memlayout_union_entry, const struct jive_union_declaration *, decl, hash_chain);

JIVE_DEFINE_RANGEMAP_TYPE(jive_memlayout_bitstring_map, jive_dataitem_memlayout *, NULL);

void
jive_memlayout_mapper_cached_init_(jive_memlayout_mapper_cached * self, jive_context * context)
{
	self->context = context;
	jive_memlayout_record_hash_init(&self->record_hash, context);
	jive_memlayout_union_hash_init(&self->union_hash, context);
	jive_memlayout_bitstring_map_init(&self->bitstring_map);
}

void
jive_memlayout_mapper_cached_fini_(jive_memlayout_mapper_cached * self)
{
	struct jive_memlayout_record_hash_iterator i;
	
	i = jive_memlayout_record_hash_begin(&self->record_hash);
	while (i.entry) {
		jive_memlayout_record_entry * entry = i.entry;
		jive_memlayout_record_hash_iterator_next(&i);
		
		jive_memlayout_record_hash_remove(&self->record_hash, entry);
		jive_context_free(self->context, entry->layout.element);
		jive_context_free(self->context, entry);
		
	}
	
	struct jive_memlayout_union_hash_iterator j;
	j = jive_memlayout_union_hash_begin(&self->union_hash);
	while (j.entry) {
		jive_memlayout_union_entry * entry = j.entry;
		jive_memlayout_union_hash_iterator_next(&j);
		
		jive_memlayout_union_hash_remove(&self->union_hash, entry);
		jive_context_free(self->context, entry);
		
	}
	
	ssize_t k;
	for (k = self->bitstring_map.low; k < self->bitstring_map.high; k++) {
		jive_dataitem_memlayout ** playout = jive_memlayout_bitstring_map_lookup(&self->bitstring_map, k);
		if (*playout)
			jive_context_free(self->context, *playout);
	}
	
	jive_memlayout_record_hash_fini(&self->record_hash);
	jive_memlayout_union_hash_fini(&self->union_hash);
	jive_memlayout_bitstring_map_fini(&self->bitstring_map);
}

jive_record_memlayout *
jive_memlayout_mapper_cached_map_record_(jive_memlayout_mapper_cached * self, const struct jive_record_declaration * decl)
{
	jive_memlayout_record_entry * entry = jive_memlayout_record_hash_lookup(&self->record_hash, decl);
	if (entry)
		return &entry->layout;
	else
		return NULL;
}

jive_union_memlayout *
jive_memlayout_mapper_cached_map_union_(jive_memlayout_mapper_cached * self, const struct jive_union_declaration * decl)
{
	jive_memlayout_union_entry * entry = jive_memlayout_union_hash_lookup(&self->union_hash, decl);
	if (entry)
		return &entry->layout;
	else
{'fraction_non_alphanumeric': 0.09801741708356494, 'fraction_numerical': 0.0, 'mean_word_length': 8.164685908319186, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5740535', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2007, 'n_words': 257}