#include #include #include #include #include "rifle/symbol.h" int symbol_table_init(struct symbol_table *table) { if (table == NULL) { return -1; } table->symbol_count = 0; TAILQ_INIT(&table->entries); return 0; } void symbol_table_destroy(struct symbol_table *table) { struct symbol *symbol; if (table == NULL) { return; } symbol = TAILQ_FIRST(&table->entries); while (symbol != NULL) { TAILQ_REMOVE(&table->entries, symbol, link); free(symbol->name); free(symbol); symbol = TAILQ_FIRST(&table->entries); } } struct symbol * symbol_from_name(struct symbol_table *table, const char *name) { struct symbol *symbol; if (table == NULL || name == NULL) { return NULL; } TAILQ_FOREACH(symbol, &table->entries, link) { if (*symbol->name != *name) { continue; } if (strcmp(symbol->name, name) == 0) { return symbol; } } return NULL; } int symbol_new(struct symbol_table *table, const char *name, symtype_t type, struct symbol **res) { struct symbol *symbol; if (table == NULL || name == NULL) { return -1; } if ((symbol = malloc(sizeof(*symbol))) == NULL) { return -1; } /* Initialize the symbol itself */ memset(symbol, 0, sizeof(*symbol)); symbol->name = strdup(name); symbol->type = type; /* Add symbol to symbol table */ ++table->symbol_count; TAILQ_INSERT_TAIL(&table->entries, symbol, link); if (res != NULL) { *res = symbol; } return 0; }