From d8ef15fe1a787cb04527fab8f440058ad4976cb0 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sun, 3 May 2009 19:56:21 -0500 Subject: Use an array for statuses We know the size ahead of time so we can cut out the need for a linked list and get it all in one big chunk of memory. Probably better than calling malloc in a tight loop anyway. Signed-off-by: Dan McGee --- receiver.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/receiver.c b/receiver.c index a3839f5..e01eb79 100644 --- a/receiver.c +++ b/receiver.c @@ -34,7 +34,6 @@ struct status { unsigned long hash; const char *key; const char *value; - struct status *next; }; /** @@ -209,6 +208,9 @@ static const char * const statuses[][2] = { { "HDO00", "OK:hdmiout:No\n" }, { "HDO01", "OK:hdmiout:Yes\n" }, + + /* Do not remove! */ + { NULL, NULL }, }; /** @@ -222,21 +224,14 @@ void init_statuses(void) unsigned int i, loopsize; /* compile-time constant, should be # rows in statuses */ loopsize = sizeof(statuses) / sizeof(*statuses); - struct status *ptr = status_list; + status_list = malloc(loopsize * sizeof(struct status)); + struct status *st = status_list; + for(i = 0; i < loopsize; i++) { - struct status *st = malloc(sizeof(struct status)); st->hash = hash_sdbm(statuses[i][0]); st->key = statuses[i][0]; st->value = statuses[i][1]; - st->next = NULL; - - if(!ptr) { - status_list = st; - ptr = st; - } else { - ptr->next = st; - ptr = ptr->next; - } + st++; } printf("%u status messages prehashed in status list.\n", loopsize); } @@ -248,11 +243,7 @@ void free_statuses(void) { struct status *st = status_list; status_list = NULL; - while(st) { - struct status *stnext = st->next; - free(st); - st = stnext; - } + free(st); } /** @@ -285,12 +276,13 @@ static char *parse_status(int size, char *status) } hashval = hash_sdbm(sptr); - while(statuses) { + /* this depends on the {NULL, NULL} keypair at the end of the list */ + while(statuses->hash != 0) { if(statuses->hash == hashval) { ret = strdup(statuses->value); break; } - statuses = statuses->next; + statuses++; } if(ret) { return(ret); -- cgit v1.2.3-55-g3dc8