summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Franzke <benjaminfranzke@googlemail.com>2011-04-25 10:44:14 +0200
committerBenjamin Franzke <benjaminfranzke@googlemail.com>2011-04-25 10:44:14 +0200
commit81989944e6947176541acb10d45db658595eb61c (patch)
treede1da0782600eed3e3c59aa600475e51da8925b7
parent540e39248d3818fd41204072d55c1fdd3b49c546 (diff)
wfhandle: Just use an array for handle storage
-rw-r--r--src/wfhandle.c53
1 files changed, 35 insertions, 18 deletions
diff --git a/src/wfhandle.c b/src/wfhandle.c
index 7363ae5..3294bcd 100644
--- a/src/wfhandle.c
+++ b/src/wfhandle.c
@@ -24,7 +24,6 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
-#include <glib.h>
#include "wfhandle.h"
struct handle_entry {
@@ -32,16 +31,15 @@ struct handle_entry {
void *object;
};
-GHashTable *hash_table = NULL;
+void **hash_table = NULL;
uint32_t next_handle = 1;
-#define uint_to_ptr(x) ((void*)(uintptr_t)(x))
-#define ptr_to_uint(x) ((unsigned)(uintptr_t)(void*)(x))
+uint32_t current_size = 16;
static void
wf_hash_table_destroy(void)
{
- g_hash_table_unref(hash_table);
+ free(hash_table);
hash_table = NULL;
next_handle = 1;
}
@@ -49,24 +47,37 @@ wf_hash_table_destroy(void)
static bool
wf_hash_table_init(void)
{
- hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
+ hash_table = calloc(current_size, sizeof *hash_table);
atexit(wf_hash_table_destroy);
return hash_table != NULL;
}
+static bool
+wf_hash_table_resize(void)
+{
+
+ current_size += 16;
+ hash_table = realloc(hash_table, current_size * sizeof *hash_table);
+
+ return hash_table != NULL;
+}
+
uint32_t
wf_handle_create(void *object, enum wf_type type)
{
struct handle_entry *entry;
uint32_t handle;
- handle = next_handle++;
-
if (hash_table == NULL && !wf_hash_table_init())
return WF_HANDLE_INVALID;
+ if (next_handle == current_size && !wf_hash_table_resize())
+ return WF_HANDLE_INVALID;
+
+ handle = next_handle++;
+
entry = malloc(sizeof *entry);
if (entry == NULL)
return WF_HANDLE_INVALID;
@@ -74,7 +85,7 @@ wf_handle_create(void *object, enum wf_type type)
entry->type = type;
entry->object = object;
- g_hash_table_insert(hash_table, (gpointer)(intptr_t) handle, entry);
+ hash_table[handle] = entry;
return handle;
}
@@ -84,7 +95,10 @@ wf_handle_get_object(uint32_t handle, enum wf_type type)
{
struct handle_entry *entry;
- entry = g_hash_table_lookup(hash_table, (gpointer)(intptr_t) handle);
+ if (handle >= next_handle)
+ return NULL;
+
+ entry = hash_table[handle];
if (entry == NULL)
return NULL;
@@ -101,11 +115,14 @@ wf_handle_destroy(uint32_t handle)
printf("destroy handle: %d\n", handle);
- entry = g_hash_table_lookup(hash_table, (gpointer)(intptr_t) handle);
+ if (handle >= next_handle)
+ return;
+
+ entry = hash_table[handle];
if (entry == NULL)
return;
- g_hash_table_remove(hash_table, (gpointer)(intptr_t) handle);
+ hash_table[handle] = NULL;
free(entry);
}
@@ -114,15 +131,15 @@ wf_handle_foreach(enum wf_type type,
void (*func)(void *object, void *user_data),
void *user_data)
{
- GHashTableIter iter;
struct handle_entry *entry;
- gpointer key, value;
+ int i;
- g_hash_table_iter_init(&iter, hash_table);
- while (g_hash_table_iter_next(&iter, &key, &value)) {
- entry = value;
+ for (i = 0; i < next_handle; ++i) {
+ entry = hash_table[i];
+ if (entry == NULL)
+ continue;
- if (type == entry->type || type == ANY_HANDLE)
+ if (entry->type == type || type == ANY_HANDLE)
func(entry->object, user_data);
}
}