symbol.c 1.24 KB
Newer Older
1
#include <config.h>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <glib.h>
#include <string.h>
#include "symbol.h"

static GHashTable *symbol_hash_table;

Symbol *
symbol_lookup (char *str)
{
	Symbol *sym;

	g_return_val_if_fail (str != NULL, NULL);
	sym = (Symbol *) g_hash_table_lookup (symbol_hash_table, str);
	if (sym)
		symbol_ref (sym);
	return sym;
}

Symbol *
symbol_lookup_substr (char *buffer, int len)
{
	char *str;
	Symbol *sym;
	
	g_return_val_if_fail (buffer != NULL, NULL);
	str = g_new (char, len + 1);
	strncpy (str, buffer, len);
	str [len] = 0;

	sym = symbol_lookup (str);
	g_free (str);
	
	return sym;
}

Symbol *
symbol_install (char *str, SymbolType type, void *data)
{
	Symbol *sym;

	g_return_val_if_fail (str != NULL, NULL);

	sym = g_new (Symbol, 1);
	sym->ref_count = 1;
	sym->type = type;
	sym->data = data;
	sym->str  = g_strdup (str);
	
	g_hash_table_insert (symbol_hash_table, str, sym);
	
	return sym;
}

void
symbol_ref (Symbol *sym)
{
	g_return_if_fail (sym != NULL);

	sym->ref_count++;
}

void
symbol_unref (Symbol *sym)
{
	g_return_if_fail (sym != NULL);
	g_return_if_fail (sym->ref_count == 0);
	
	if (--(sym->ref_count) == 0){
		g_free (sym->str);
		g_free (sym);
	}
}

void
symbol_init (void)
{
	symbol_hash_table = g_hash_table_new (g_str_hash, g_str_equal);
}