diff options
author | Dan McGee <dpmcgee@gmail.com> | 2009-05-03 19:56:21 -0500 |
---|---|---|
committer | Dan McGee <dpmcgee@gmail.com> | 2009-05-03 19:56:21 -0500 |
commit | d8ef15fe1a787cb04527fab8f440058ad4976cb0 (patch) | |
tree | 3346e4ec04737df7933d5e5880186bd4e68d3810 | |
parent | 41266b5d6c45c059a77f61e4c6b00ba330252136 (diff) | |
download | onkyocontrol-d8ef15fe1a787cb04527fab8f440058ad4976cb0.tar.gz onkyocontrol-d8ef15fe1a787cb04527fab8f440058ad4976cb0.zip |
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 <dpmcgee@gmail.com>
-rw-r--r-- | receiver.c | 30 |
1 files changed, 11 insertions, 19 deletions
@@ -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); |