summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dpmcgee@gmail.com>2009-05-03 19:56:21 -0500
committerDan McGee <dpmcgee@gmail.com>2009-05-03 19:56:21 -0500
commitd8ef15fe1a787cb04527fab8f440058ad4976cb0 (patch)
tree3346e4ec04737df7933d5e5880186bd4e68d3810
parent41266b5d6c45c059a77f61e4c6b00ba330252136 (diff)
downloadonkyocontrol-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.c30
1 files 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);