diff --git a/core/dcb.c b/core/dcb.c index d1699e458..df2f23edf 100644 --- a/core/dcb.c +++ b/core/dcb.c @@ -59,7 +59,7 @@ static SPINLOCK *dcbspin = NULL; * @return A newly allocated DCB or NULL if non could be allocated. */ DCB * -alloc_dcb() +dcb_alloc() { DCB *rval; @@ -101,7 +101,7 @@ DCB *rval; * @param dcb The DCB to free */ void -free_dcb(DCB *dcb) +dcb_free(DCB *dcb) { dcb->state = DCB_STATE_FREED; @@ -132,18 +132,18 @@ free_dcb(DCB *dcb) * @param protocol The protocol module to use */ DCB * -connect_dcb(SERVER *server, SESSION *session, const char *protocol) +dcb_connect(SERVER *server, SESSION *session, const char *protocol) { DCB *dcb; GWPROTOCOL *funcs; - if ((dcb = alloc_dcb()) == NULL) + if ((dcb = dcb_alloc()) == NULL) { return NULL; } if ((funcs = (GWPROTOCOL *)load_module(protocol, MODULE_PROTOCOL)) == NULL) { - free_dcb(dcb); + dcb_free(dcb); return NULL; } memcpy(&(dcb->func), funcs, sizeof(GWPROTOCOL)); @@ -151,7 +151,7 @@ GWPROTOCOL *funcs; if ((dcb->fd = dcb->func.connect(dcb, server, session)) == -1) { - free_dcb(dcb); + dcb_free(dcb); return NULL; } server->stats.n_connections++; diff --git a/core/hashtable.c b/core/hashtable.c index 2455d1281..08e3d95f9 100644 --- a/core/hashtable.c +++ b/core/hashtable.c @@ -96,12 +96,12 @@ HASHTABLE *rval; rval->n_readers = 0; rval->writelock = 0; spinlock_init(&rval->spin); - if ((rval->entries = calloc(size, sizeof(HASHENTRIES))) == NULL) + if ((rval->entries = (HASHENTRIES **)calloc(size, sizeof(HASHENTRIES *))) == NULL) { free(rval); return NULL; } - memset(rval->entries, 0, size * sizeof(HASHENTRIES)); + memset(rval->entries, 0, size * sizeof(HASHENTRIES *)); return rval; } @@ -121,12 +121,6 @@ HASHENTRIES *entry, *ptr; for (i = 0; i < table->hashsize; i++) { entry = table->entries[i]; - if (entry->key) - { - table->freefn(entry->key); - table->freefn(entry->value); - } - entry = entry->next; while (entry) { ptr = entry->next; @@ -172,17 +166,11 @@ HASHENTRIES *entry; hashtable_write_lock(table); 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; } - if (entry->key == NULL) - { - /* 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) + if (entry && table->cmpfn(key, entry->key) == 0) { /* Duplicate key value */ hashtable_write_unlock(table); @@ -198,8 +186,8 @@ HASHENTRIES *entry; } ptr->key = table->copyfn(key); ptr->value = table->copyfn(value); - ptr->next = NULL; - entry->next = ptr; + ptr->next = table->entries[hashkey % table->hashsize]; + table->entries[hashkey % table->hashsize] = ptr; } hashtable_write_unlock(table); return 1; @@ -224,7 +212,7 @@ HASHENTRIES *entry, *ptr; { entry = entry->next; } - if (entry == NULL || entry->key == NULL) + if (entry == NULL) { /* Not found */ hashtable_write_unlock(table); @@ -233,24 +221,13 @@ HASHENTRIES *entry, *ptr; if (entry == table->entries[hashkey % table->hashsize]) { - /* We are removing from the special first entry */ - if (entry->next) - { - table->freefn(entry->key); - table->freefn(entry->value); - entry->key = entry->next->key; - entry->value = entry->next->value; - ptr = entry->next; - entry->next = ptr->next; - free(ptr); - } - else - { - table->freefn(entry->key); - table->freefn(entry->value); - entry->key = NULL; - entry->value = NULL; - } + /* We are removing from the first entry */ + table->entries[hashkey % table->hashsize] = entry->next; + table->freefn(entry->key); + table->freefn(entry->value); + entry->key = entry->next->key; + entry->value = entry->next->value; + free(entry); } else { @@ -290,7 +267,7 @@ HASHENTRIES *entry; { entry = entry->next; } - if (entry == NULL || entry->key == NULL) + if (entry == NULL) { hashtable_read_unlock(table); return NULL; diff --git a/core/service.c b/core/service.c index 36c9ffbe7..8682f054a 100644 --- a/core/service.c +++ b/core/service.c @@ -105,7 +105,7 @@ GWPROTOCOL *funcs; port = service->ports; while (port) { - if ((port->listener = alloc_dcb()) == NULL) + if ((port->listener = dcb_alloc()) == NULL) { break; } diff --git a/include/dcb.h b/include/dcb.h index 74ebb9329..ccb038e6d 100644 --- a/include/dcb.h +++ b/include/dcb.h @@ -129,9 +129,9 @@ typedef struct dcb { #define DCB_SESSION(x) (x)->session #define DCB_PROTOCOL(x, type) (type *)((x)->protocol) -extern DCB *alloc_dcb(); /* Allocate a DCB */ -extern void free_dcb(DCB *); /* Free a DCB */ -extern DCB *connect_dcb(struct server *, struct session *, const char *); +extern DCB *dcb_alloc(); /* Allocate a DCB */ +extern void dcb_free(DCB *); /* Free a DCB */ +extern DCB *dcb_connect(struct server *, struct session *, const char *); extern int dcb_read(DCB *, GWBUF **); /* Generic read routine */ extern int dcb_write(DCB *, GWBUF *); /* Generic write routine */ extern int dcb_drain_writeq(DCB *); /* Generic write routine */ diff --git a/modules/protocol/mysql_client.c b/modules/protocol/mysql_client.c index 938e2bb7c..7e7c76fbd 100644 --- a/modules/protocol/mysql_client.c +++ b/modules/protocol/mysql_client.c @@ -1036,7 +1036,7 @@ int gw_MySQLAccept(DCB *listener) { setsockopt(c_sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, optlen); setnonblocking(c_sock); - client = alloc_dcb(); + client = dcb_alloc(); client->service = listener->session->service; client->fd = c_sock; client->remote = strdup(inet_ntoa(local.sin_addr)); diff --git a/modules/protocol/telnetd.c b/modules/protocol/telnetd.c index 0119cdc67..2a8f1381c 100644 --- a/modules/protocol/telnetd.c +++ b/modules/protocol/telnetd.c @@ -220,7 +220,7 @@ int n_connect = 0; else { atomic_add(&dcb->stats.n_accepts, 1); - client = alloc_dcb(); + client = dcb_alloc(); client->fd = so; client->remote = strdup(inet_ntoa(addr.sin_addr)); memcpy(&client->func, &MyObject, sizeof(GWPROTOCOL)); diff --git a/modules/routing/readconnroute.c b/modules/routing/readconnroute.c index 4033a6f0a..acf9e1ef9 100644 --- a/modules/routing/readconnroute.c +++ b/modules/routing/readconnroute.c @@ -210,7 +210,7 @@ int i; * 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 */ spinlock_acquire(&inst->lock);