Switch tables to use refcounting.

This commit is contained in:
Danila Fedorin 2018-04-24 11:06:21 -07:00
parent 251ce4e66e
commit cea057aaa6
2 changed files with 16 additions and 4 deletions

View File

@ -5,6 +5,7 @@
#include "custom.h" #include "custom.h"
#include "result.h" #include "result.h"
#include "trie.h" #include "trie.h"
#include "refcount.h"
/** /**
* A struct that represents a structure * A struct that represents a structure
@ -17,7 +18,7 @@ struct libab_table_s {
/** /**
* The "parent" scope of this table. * The "parent" scope of this table.
*/ */
struct libab_table_s* parent; libab_ref parent;
/** /**
* The hash table used to store the data. * The hash table used to store the data.
*/ */
@ -119,6 +120,12 @@ libab_basetype* libab_table_search_basetype(libab_table* table,
*/ */
libab_result libab_table_put(libab_table* table, const char* string, libab_result libab_table_put(libab_table* table, const char* string,
libab_table_entry* entry); libab_table_entry* entry);
/**
* Sets the parent of the given table.
* @param table the table whose parent to set.
* @param parent a valid reference to a parent table.
*/
void libab_table_set_parent(libab_table* table, libab_ref* parent);
/** /**
* Frees the resources allocated by the * Frees the resources allocated by the
* given table. * given table.

View File

@ -5,7 +5,7 @@
void libab_table_init(libab_table* table) { void libab_table_init(libab_table* table) {
libab_trie_init(&table->trie); libab_trie_init(&table->trie);
table->parent = NULL; libab_ref_null(&table->parent);
} }
libab_table_entry* libab_table_search_filter(libab_table* table, libab_table_entry* libab_table_search_filter(libab_table* table,
const char* string, void* data, const char* string, void* data,
@ -14,7 +14,7 @@ libab_table_entry* libab_table_search_filter(libab_table* table,
do { do {
const ll* matches = libab_trie_get(&table->trie, string); const ll* matches = libab_trie_get(&table->trie, string);
to_return = ll_find(matches, data, compare); to_return = ll_find(matches, data, compare);
table = table->parent; table = libab_ref_get(&table->parent);
} while (table && to_return == NULL); } while (table && to_return == NULL);
return to_return; return to_return;
} }
@ -22,7 +22,7 @@ libab_table_entry* libab_table_search(libab_table* table, const char* string) {
void* to_return = NULL; void* to_return = NULL;
do { do {
to_return = ll_head(libab_trie_get(&table->trie, string)); to_return = ll_head(libab_trie_get(&table->trie, string));
table = table->parent; table = libab_ref_get(&table->parent);
} while (table && to_return == NULL); } while (table && to_return == NULL);
return to_return; return to_return;
} }
@ -86,10 +86,15 @@ int _table_foreach_entry_free(void* data, va_list args) {
free(data); free(data);
return 0; return 0;
} }
void libab_table_set_parent(libab_table* table, libab_ref* parent) {
libab_ref_free(&table->parent);
libab_ref_copy(parent, &table->parent);
}
void libab_table_free(libab_table* table) { void libab_table_free(libab_table* table) {
libab_trie_foreach(&table->trie, NULL, compare_always, libab_trie_foreach(&table->trie, NULL, compare_always,
_table_foreach_entry_free); _table_foreach_entry_free);
libab_trie_free(&table->trie); libab_trie_free(&table->trie);
libab_ref_free(&table->parent);
} }
void libab_table_entry_free(libab_table_entry* entry) { void libab_table_entry_free(libab_table_entry* entry) {
if (entry->variant == ENTRY_OP) { if (entry->variant == ENTRY_OP) {