diff options
author | Gustavo Sverzut Barbieri <barbieri@profusion.mobi> | 2011-12-08 14:50:29 -0200 |
---|---|---|
committer | Gustavo Sverzut Barbieri <barbieri@profusion.mobi> | 2011-12-08 16:35:36 -0200 |
commit | 1433ba9ef544d8aa5803ba96adefd5eb18c9b9f2 (patch) | |
tree | 3984244e592baabec9e2d05f71ce5fc9b150a3ba /libkmod | |
parent | 435ad788e25effe953816ed39b4ae914ce22cf04 (diff) | |
download | kmod-1433ba9ef544d8aa5803ba96adefd5eb18c9b9f2.tar.gz |
index: avoid strlen() whenever possible.
Diffstat (limited to 'libkmod')
-rw-r--r-- | libkmod/libkmod-index.c | 29 | ||||
-rw-r--r-- | libkmod/libkmod-index.h | 1 |
2 files changed, 17 insertions, 13 deletions
diff --git a/libkmod/libkmod-index.c b/libkmod/libkmod-index.c index a0cb510..fddcf51 100644 --- a/libkmod/libkmod-index.c +++ b/libkmod/libkmod-index.c @@ -43,11 +43,10 @@ void index_values_free(struct index_value *values) } static int add_value(struct index_value **values, - const char *value, unsigned int priority) + const char *value, unsigned len, unsigned int priority) { struct index_value *v; int duplicate = 0; - int len; /* report the presence of duplicate values */ for (v = *values; v; v = v->next) { @@ -59,11 +58,14 @@ static int add_value(struct index_value **values, while (*values && (*values)->priority < priority) values = &(*values)->next; - len = strlen(value); - v = NOFAIL(calloc(sizeof(struct index_value) + len + 1, 1)); + v = malloc(sizeof(struct index_value) + len + 1); + if (!v) + return -1; v->next = *values; v->priority = priority; - memcpy(v->value, value, len + 1); + v->len = len; + memcpy(v->value, value, len); + v->value[len] = '\0'; *values = v; return duplicate; @@ -272,7 +274,7 @@ static struct index_node_f *index_read(FILE *in, uint32_t offset) priority = read_long(in); buf_freadchars(&buf, in); value = buf_str(&buf); - add_value(&node->values, value, priority); + add_value(&node->values, value, buf.used, priority); buf_clear(&buf); } buf_release(&buf); @@ -410,7 +412,7 @@ static void index_searchwild__allvalues(struct index_node_f *node, struct index_value *v; for (v = node->values; v != NULL; v = v->next) - add_value(out, v->value, v->priority); + add_value(out, v->value, v->len, v->priority); index_close(node); } @@ -585,11 +587,11 @@ static inline char *read_alloc_chars_mm(void **p) return memdup(s, len); } -static inline char *read_chars_mm(void **p) +static inline char *read_chars_mm(void **p, unsigned *rlen) { char *s = *((char **)p); - size_t len = strlen(s) + 1; - *p = ((char *)p) + len; + size_t len = *rlen = strlen(s); + *p = ((char *)p) + len + 1; return s; } @@ -638,10 +640,11 @@ static struct index_mm_node *index_mm_read_node(struct index_mm *idx, for (j = read_long_mm(&p); j > 0; j--) { unsigned int priority; const char *value; + unsigned len; priority = read_long_mm(&p); - value = read_chars_mm(&p); - add_value(&node->values, value, priority); + value = read_chars_mm(&p, &len); + add_value(&node->values, value, len, priority); } } @@ -810,7 +813,7 @@ static void index_mm_searchwild_allvalues(struct index_mm_node *node, struct index_value *v; for (v = node->values; v != NULL; v = v->next) - add_value(out, v->value, v->priority); + add_value(out, v->value, v->len, v->priority); index_mm_free_node(node); } diff --git a/libkmod/libkmod-index.h b/libkmod/libkmod-index.h index 8dfac98..ae44b25 100644 --- a/libkmod/libkmod-index.h +++ b/libkmod/libkmod-index.h @@ -105,6 +105,7 @@ struct index_value { struct index_value *next; unsigned int priority; + unsigned int len; char value[0]; }; |