summaryrefslogtreecommitdiff
path: root/src/xcb_ext.c
diff options
context:
space:
mode:
authorJamey Sharp <jamey@minilop.net>2006-02-26 01:27:01 -0800
committerJamey Sharp <jamey@minilop.net>2006-02-26 01:27:01 -0800
commita1eff0c49a2dbb328a79edbeb2b851fa1dfe9e61 (patch)
tree1cb66a6dea0e1ad076382db6fac3e714c4677575 /src/xcb_ext.c
parent46a754998149c5f4a1670787b3ea36731caf6506 (diff)
Replace my old generic map ADT with a growable array for the extension cache.
Diffstat (limited to 'src/xcb_ext.c')
-rw-r--r--src/xcb_ext.c45
1 files changed, 23 insertions, 22 deletions
diff --git a/src/xcb_ext.c b/src/xcb_ext.c
index 46a5519..610ec57 100644
--- a/src/xcb_ext.c
+++ b/src/xcb_ext.c
@@ -32,14 +32,29 @@
#include "xcbext.h"
#include "xcbint.h"
-typedef struct {
- enum { LAZY_COOKIE, LAZY_FORCED } tag;
+typedef struct lazyreply {
+ enum { LAZY_NONE = 0, LAZY_COOKIE, LAZY_FORCED } tag;
union {
XCBQueryExtensionCookie cookie;
XCBQueryExtensionRep *reply;
} value;
} lazyreply;
+static lazyreply *get_index(XCBConnection *c, int index)
+{
+ if(index > c->ext.extensions_size)
+ {
+ int new_size = index << 1;
+ lazyreply *new_extensions = realloc(c->ext.extensions, sizeof(lazyreply) * new_size);
+ if(!new_extensions)
+ return 0;
+ memset(new_extensions + c->ext.extensions_size, 0, sizeof(lazyreply) * (new_size - c->ext.extensions_size));
+ c->ext.extensions = new_extensions;
+ c->ext.extensions_size = new_size;
+ }
+ return c->ext.extensions + index - 1;
+}
+
static lazyreply *get_lazyreply(XCBConnection *c, XCBExtension *ext)
{
static pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
@@ -52,28 +67,16 @@ static lazyreply *get_lazyreply(XCBConnection *c, XCBExtension *ext)
ext->global_id = ++next_global_id;
pthread_mutex_unlock(&global_lock);
- data = _xcb_map_get(c->ext.extensions, ext->global_id);
- if(!data)
+ data = get_index(c, ext->global_id);
+ if(data && data->tag == LAZY_NONE)
{
/* cache miss: query the server */
- data = malloc(sizeof(lazyreply));
- if(!data)
- return 0;
data->tag = LAZY_COOKIE;
data->value.cookie = XCBQueryExtension(c, strlen(ext->name), ext->name);
- _xcb_map_put(c->ext.extensions, ext->global_id, data);
}
return data;
}
-static void free_lazyreply(void *p)
-{
- lazyreply *data = p;
- if(data->tag == LAZY_FORCED)
- free(data->value.reply);
- free(data);
-}
-
/* Public interface */
/* Do not free the returned XCBQueryExtensionRep - on return, it's aliased
@@ -107,16 +110,14 @@ int _xcb_ext_init(XCBConnection *c)
{
if(pthread_mutex_init(&c->ext.lock, 0))
return 0;
-
- c->ext.extensions = _xcb_map_new();
- if(!c->ext.extensions)
- return 0;
-
return 1;
}
void _xcb_ext_destroy(XCBConnection *c)
{
pthread_mutex_destroy(&c->ext.lock);
- _xcb_map_delete(c->ext.extensions, free_lazyreply);
+ while(c->ext.extensions_size-- > 0)
+ if(c->ext.extensions[c->ext.extensions_size].tag == LAZY_FORCED)
+ free(c->ext.extensions[c->ext.extensions_size].value.reply);
+ free(c->ext.extensions);
}