Fix to the hashtable support

Renamed alloc_dcb, free_dcb and connect_dcb to be more consistant, e.g. dcb_alloc, dcb_free and dcb_connect
This commit is contained in:
Mark Riddoch
2013-06-24 15:09:37 +02:00
parent 1d3da6d18f
commit 2ec19bf7ca
7 changed files with 28 additions and 51 deletions

View File

@ -59,7 +59,7 @@ static SPINLOCK *dcbspin = NULL;
* @return A newly allocated DCB or NULL if non could be allocated. * @return A newly allocated DCB or NULL if non could be allocated.
*/ */
DCB * DCB *
alloc_dcb() dcb_alloc()
{ {
DCB *rval; DCB *rval;
@ -101,7 +101,7 @@ DCB *rval;
* @param dcb The DCB to free * @param dcb The DCB to free
*/ */
void void
free_dcb(DCB *dcb) dcb_free(DCB *dcb)
{ {
dcb->state = DCB_STATE_FREED; dcb->state = DCB_STATE_FREED;
@ -132,18 +132,18 @@ free_dcb(DCB *dcb)
* @param protocol The protocol module to use * @param protocol The protocol module to use
*/ */
DCB * DCB *
connect_dcb(SERVER *server, SESSION *session, const char *protocol) dcb_connect(SERVER *server, SESSION *session, const char *protocol)
{ {
DCB *dcb; DCB *dcb;
GWPROTOCOL *funcs; GWPROTOCOL *funcs;
if ((dcb = alloc_dcb()) == NULL) if ((dcb = dcb_alloc()) == NULL)
{ {
return NULL; return NULL;
} }
if ((funcs = (GWPROTOCOL *)load_module(protocol, MODULE_PROTOCOL)) == NULL) if ((funcs = (GWPROTOCOL *)load_module(protocol, MODULE_PROTOCOL)) == NULL)
{ {
free_dcb(dcb); dcb_free(dcb);
return NULL; return NULL;
} }
memcpy(&(dcb->func), funcs, sizeof(GWPROTOCOL)); memcpy(&(dcb->func), funcs, sizeof(GWPROTOCOL));
@ -151,7 +151,7 @@ GWPROTOCOL *funcs;
if ((dcb->fd = dcb->func.connect(dcb, server, session)) == -1) if ((dcb->fd = dcb->func.connect(dcb, server, session)) == -1)
{ {
free_dcb(dcb); dcb_free(dcb);
return NULL; return NULL;
} }
server->stats.n_connections++; server->stats.n_connections++;

View File

@ -96,12 +96,12 @@ HASHTABLE *rval;
rval->n_readers = 0; rval->n_readers = 0;
rval->writelock = 0; rval->writelock = 0;
spinlock_init(&rval->spin); spinlock_init(&rval->spin);
if ((rval->entries = calloc(size, sizeof(HASHENTRIES))) == NULL) if ((rval->entries = (HASHENTRIES **)calloc(size, sizeof(HASHENTRIES *))) == NULL)
{ {
free(rval); free(rval);
return NULL; return NULL;
} }
memset(rval->entries, 0, size * sizeof(HASHENTRIES)); memset(rval->entries, 0, size * sizeof(HASHENTRIES *));
return rval; return rval;
} }
@ -121,12 +121,6 @@ HASHENTRIES *entry, *ptr;
for (i = 0; i < table->hashsize; i++) for (i = 0; i < table->hashsize; i++)
{ {
entry = table->entries[i]; entry = table->entries[i];
if (entry->key)
{
table->freefn(entry->key);
table->freefn(entry->value);
}
entry = entry->next;
while (entry) while (entry)
{ {
ptr = entry->next; ptr = entry->next;
@ -172,17 +166,11 @@ HASHENTRIES *entry;
hashtable_write_lock(table); hashtable_write_lock(table);
entry = table->entries[hashkey % table->hashsize]; entry = table->entries[hashkey % table->hashsize];
while (entry->next && entry->key && table->cmpfn(key, entry->key) != 0) while (entry && table->cmpfn(key, entry->key) != 0)
{ {
entry = entry->next; entry = entry->next;
} }
if (entry->key == NULL) if (entry && table->cmpfn(key, entry->key) == 0)
{
/* Entry is empty - special case for first insert */
entry->key = table->copyfn(key);
entry->value = table->copyfn(value);
}
else if (table->cmpfn(key, entry->key) == 0)
{ {
/* Duplicate key value */ /* Duplicate key value */
hashtable_write_unlock(table); hashtable_write_unlock(table);
@ -198,8 +186,8 @@ HASHENTRIES *entry;
} }
ptr->key = table->copyfn(key); ptr->key = table->copyfn(key);
ptr->value = table->copyfn(value); ptr->value = table->copyfn(value);
ptr->next = NULL; ptr->next = table->entries[hashkey % table->hashsize];
entry->next = ptr; table->entries[hashkey % table->hashsize] = ptr;
} }
hashtable_write_unlock(table); hashtable_write_unlock(table);
return 1; return 1;
@ -224,7 +212,7 @@ HASHENTRIES *entry, *ptr;
{ {
entry = entry->next; entry = entry->next;
} }
if (entry == NULL || entry->key == NULL) if (entry == NULL)
{ {
/* Not found */ /* Not found */
hashtable_write_unlock(table); hashtable_write_unlock(table);
@ -233,24 +221,13 @@ HASHENTRIES *entry, *ptr;
if (entry == table->entries[hashkey % table->hashsize]) if (entry == table->entries[hashkey % table->hashsize])
{ {
/* We are removing from the special first entry */ /* We are removing from the first entry */
if (entry->next) table->entries[hashkey % table->hashsize] = entry->next;
{ table->freefn(entry->key);
table->freefn(entry->key); table->freefn(entry->value);
table->freefn(entry->value); entry->key = entry->next->key;
entry->key = entry->next->key; entry->value = entry->next->value;
entry->value = entry->next->value; free(entry);
ptr = entry->next;
entry->next = ptr->next;
free(ptr);
}
else
{
table->freefn(entry->key);
table->freefn(entry->value);
entry->key = NULL;
entry->value = NULL;
}
} }
else else
{ {
@ -290,7 +267,7 @@ HASHENTRIES *entry;
{ {
entry = entry->next; entry = entry->next;
} }
if (entry == NULL || entry->key == NULL) if (entry == NULL)
{ {
hashtable_read_unlock(table); hashtable_read_unlock(table);
return NULL; return NULL;

View File

@ -105,7 +105,7 @@ GWPROTOCOL *funcs;
port = service->ports; port = service->ports;
while (port) while (port)
{ {
if ((port->listener = alloc_dcb()) == NULL) if ((port->listener = dcb_alloc()) == NULL)
{ {
break; break;
} }

View File

@ -129,9 +129,9 @@ typedef struct dcb {
#define DCB_SESSION(x) (x)->session #define DCB_SESSION(x) (x)->session
#define DCB_PROTOCOL(x, type) (type *)((x)->protocol) #define DCB_PROTOCOL(x, type) (type *)((x)->protocol)
extern DCB *alloc_dcb(); /* Allocate a DCB */ extern DCB *dcb_alloc(); /* Allocate a DCB */
extern void free_dcb(DCB *); /* Free a DCB */ extern void dcb_free(DCB *); /* Free a DCB */
extern DCB *connect_dcb(struct server *, struct session *, const char *); extern DCB *dcb_connect(struct server *, struct session *, const char *);
extern int dcb_read(DCB *, GWBUF **); /* Generic read routine */ extern int dcb_read(DCB *, GWBUF **); /* Generic read routine */
extern int dcb_write(DCB *, GWBUF *); /* Generic write routine */ extern int dcb_write(DCB *, GWBUF *); /* Generic write routine */
extern int dcb_drain_writeq(DCB *); /* Generic write routine */ extern int dcb_drain_writeq(DCB *); /* Generic write routine */

View File

@ -1036,7 +1036,7 @@ int gw_MySQLAccept(DCB *listener) {
setsockopt(c_sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, optlen); setsockopt(c_sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, optlen);
setnonblocking(c_sock); setnonblocking(c_sock);
client = alloc_dcb(); client = dcb_alloc();
client->service = listener->session->service; client->service = listener->session->service;
client->fd = c_sock; client->fd = c_sock;
client->remote = strdup(inet_ntoa(local.sin_addr)); client->remote = strdup(inet_ntoa(local.sin_addr));

View File

@ -220,7 +220,7 @@ int n_connect = 0;
else else
{ {
atomic_add(&dcb->stats.n_accepts, 1); atomic_add(&dcb->stats.n_accepts, 1);
client = alloc_dcb(); client = dcb_alloc();
client->fd = so; client->fd = so;
client->remote = strdup(inet_ntoa(addr.sin_addr)); client->remote = strdup(inet_ntoa(addr.sin_addr));
memcpy(&client->func, &MyObject, sizeof(GWPROTOCOL)); memcpy(&client->func, &MyObject, sizeof(GWPROTOCOL));

View File

@ -210,7 +210,7 @@ int i;
* connection in the client->dcb * connection in the client->dcb
*/ */
client->dcb = connect_dcb(candidate->server, session, candidate->server->protocol); client->dcb = dcb_connect(candidate->server, session, candidate->server->protocol);
/* Add this session to the list of active sessions */ /* Add this session to the list of active sessions */
spinlock_acquire(&inst->lock); spinlock_acquire(&inst->lock);