info: lazily initialise the cache
authorFerry Huberts <ferry.huberts@pelagic.nl>
Fri, 8 Apr 2016 08:27:25 +0000 (10:27 +0200)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Fri, 8 Apr 2016 08:37:50 +0000 (10:37 +0200)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/info/info_types.h
lib/info/olsrd_info.c

index 17ae186..a033a41 100644 (file)
@@ -129,6 +129,7 @@ typedef struct {
 } info_plugin_functions_t;
 
 struct info_cache_entry_t {
+    bool initialised;
     long long timestamp;
     struct autobuf buf;
 };
index a0cd9cf..72ff570 100644 (file)
@@ -188,15 +188,25 @@ static void info_plugin_cache_init(bool init) {
     }
 
     if (init) {
-      entry->timestamp = 0;
-      abuf_init(&entry->buf, AUTOBUFCHUNK);
+      entry->initialised = false;
     } else {
-      abuf_free(&entry->buf);
+      if (entry->initialised) {
+        abuf_free(&entry->buf);
+        entry->initialised = false;
+      }
       entry->timestamp = 0;
     }
   }
 }
 
+static INLINE void info_plugin_cache_init_entry(struct info_cache_entry_t * entry) {
+  if (!entry->initialised) {
+    entry->timestamp = 0;
+    abuf_init(&entry->buf, AUTOBUFCHUNK);
+    entry->initialised = true;
+  }
+}
+
 static unsigned int determine_single_action(char *requ) {
   unsigned int i;
 
@@ -380,6 +390,7 @@ static void send_info_from_table(struct autobuf *abuf, unsigned int send_what, S
         } else {
           long long now = olsr_times();
           long long age = abs(now - cache_entry->timestamp);
+          info_plugin_cache_init_entry(cache_entry);
           if (!cache_entry->timestamp || (age >= cache_timeout)) {
             /* cache is never used before or cache is too old */
             cache_entry->buf.buf[0] = '\0';