cache: reduce allocation of cache entries to one chunk of memory, using calloc_a
authorFelix Fietkau <nbd@openwrt.org>
Tue, 3 Jun 2014 21:39:40 +0000 (23:39 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 3 Jun 2014 21:39:40 +0000 (23:39 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
cache.c

diff --git a/cache.c b/cache.c
index da65b1f4b4d327b2eaa8608a460864b01ddf40ce..07a08575aa3639c819eb605f760017a6180b571b 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -66,10 +66,6 @@ cache_entry_free(struct cache_entry *s)
 {
        DBG(2, "%s\n", s->entry);
        avl_delete(&entries, &s->avl);
-       if (s->host)
-               free(s->host);
-       if (s->entry)
-               free(s->entry);
        free(s);
 }
 
@@ -162,21 +158,26 @@ static struct cache_entry*
 cache_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
 {
        struct cache_entry *s;
+       char *entry_buf;
+       char *host_buf;
        char *type;
 
        s = avl_find_element(&entries, entry, s, avl);
        if (s)
                return s;
 
-       s = malloc(sizeof(struct cache_entry));
-       memset(s, 0, sizeof(struct cache_entry));
-       s->avl.key = s->entry = strdup(entry);
+       s = calloc_a(sizeof(*s),
+               &entry_buf, strlen(entry) + 1,
+               &host_buf, hlen ? hlen + 1 : 0);
+
+       s->avl.key = s->entry = strcpy(entry_buf, entry);
        s->time = time(NULL);
        s->ttl = ttl;
 
        if (hlen)
-               s->host = strndup(s->entry, hlen);
-       type = strstr(s->entry, "._");
+               s->host = strncpy(host_buf, s->entry, hlen);
+
+       type = strstr(entry_buf, "._");
        if (type)
                type++;
        if (type)