MainTree/core/src-c/shadow-lookup.c

91 lines
2.8 KiB
C

#include <shadow.h>
#include <context.h>
#include <stdlib.h>
#include <librr/memory.h>
MtShadowLookupTree mt_create_shadow_lookup_tree()
{
MtShadowLookupTree lookup_tree;
rr_memset(&lookup_tree.next[0], sizeof(MtShadow *) * 256, 0);
return lookup_tree;
}
MtShadow * mt_lookup_shadow(MtShadowRegistry *registry, u32_t identifier)
{
u8_t index_1 = identifier >> 24;
u8_t index_2 = (identifier >> 16) & 0xff;
u8_t index_3 = (identifier >> 8) & 0xff;
u8_t index_4 = identifier & 0xff;
MtShadowLookupLevel1 *level_1 = registry->lookup_tree.next[index_1];
if(level_1 == NULL)
return NULL;
MtShadowLookupLevel2 *level_2 = level_1->next[index_2];
if(level_2 == NULL)
return NULL;
MtShadowLookupLevel3 *level_3 = level_2->next[index_3];
if(level_3 == NULL)
return NULL;
MtShadow *shadow = level_3->next[index_4];
return shadow;
}
void mt_delete_shadow(MtShadowRegistry *registry, MtShadow *shadow)
{
u32_t identifier = shadow->identifier;
mt_unpool_shadow(shadow);
u8_t index_1 = identifier >> 24;
u8_t index_2 = (identifier >> 16) & 0xff;
u8_t index_3 = (identifier >> 8) & 0xff;
u8_t index_4 = identifier & 0xff;
MtShadowLookupLevel1 *level_1 = registry->lookup_tree.next[index_1];
if(level_1 == NULL)
return;
MtShadowLookupLevel2 *level_2 = level_1->next[index_2];
if(level_2 == NULL)
return;
MtShadowLookupLevel3 *level_3 = level_2->next[index_3];
if(level_3 == NULL)
return;
level_3->next[index_4] = NULL;
}
void mt_write_shadow_lookup_entry(MtShadowLookupTree *tree, MtShadow *shadow)
{
u32_t identifier = shadow->identifier;
u8_t index_1 = identifier >> 24;
u8_t index_2 = (identifier >> 16) & 0xff;
u8_t index_3 = (identifier >> 8) & 0xff;
u8_t index_4 = identifier & 0xff;
MtShadowLookupLevel1 *level_1 = tree->next[index_1];
if(level_1 == NULL)
{
tree->next[index_1] = calloc(sizeof(MtShadowLookupLevel1), 256);
level_1 = tree->next[index_1];
}
MtShadowLookupLevel2 *level_2 = level_1->next[index_2];
if(level_2 == NULL)
{
level_1->next[index_1] = calloc(sizeof(MtShadowLookupLevel2), 256);
level_2 = level_1->next[index_2];
}
MtShadowLookupLevel3 *level_3 = level_2->next[index_3];
if(level_3 == NULL)
{
level_2->next[index_1] = calloc(sizeof(MtShadowLookupLevel3), 256);
level_3 = level_2->next[index_3];
}
level_3->next[index_4] = shadow;
}
MtShadow * mt_create_shadow(MtShadowRegistry *registry)
{
MtShadow *shadow = mt_pool_shadow(&registry->root_pool);
shadow->identifier = registry->id_counter;
shadow->context = registry->context_ptr;
shadow->tag_chain_start = NULL;
mt_write_shadow_lookup_entry(&registry->lookup_tree, shadow);
return shadow;
}