]> git.ozlabs.org Git - ccan/blobdiff - ccan/timer/timer.c
timer: clean up hook allocator API
[ccan] / ccan / timer / timer.c
index c55e23dd21cf287d64b6d1ce6482b7cf0d33df9d..2685d3efde96f780fbbc3dc946d6acc4b9def9e1 100644 (file)
@@ -2,7 +2,6 @@
 #include <ccan/timer/timer.h>
 #include <ccan/array_size/array_size.h>
 #include <ccan/ilog/ilog.h>
-#include <ccan/likely/likely.h>
 #include <stdlib.h>
 #include <stdio.h>
 
@@ -12,6 +11,30 @@ struct timer_level {
        struct list_head list[PER_LEVEL];
 };
 
+static void *timer_default_alloc(struct timers *timers, size_t len)
+{
+       return malloc(len);
+}
+
+static void timer_default_free(struct timers *timers, void *p)
+{
+       free(p);
+}
+
+static void *(*timer_alloc)(struct timers *, size_t) = timer_default_alloc;
+static void (*timer_free)(struct timers *, void *) = timer_default_free;
+
+void timers_set_allocator(void *(*alloc)(struct timers *, size_t len),
+                         void (*free)(struct timers *, void *p))
+{
+       if (!alloc)
+               alloc = timer_default_alloc;
+       if (!free)
+               free = timer_default_free;
+       timer_alloc = alloc;
+       timer_free = free;
+}
+
 static uint64_t time_to_grains(struct timemono t)
 {
        return t.ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
@@ -114,7 +137,7 @@ void timer_addmono(struct timers *timers, struct timer *t, struct timemono when)
 }
 
 /* FIXME: inline */
-void timer_del(struct timers *timers, struct timer *t)
+void timer_del(struct timers *timers UNNEEDED, struct timer *t)
 {
        list_del_init(&t->list);
 }
@@ -140,7 +163,7 @@ static void add_level(struct timers *timers, unsigned int level)
        unsigned int i;
        struct list_head from_far;
 
-       l = malloc(sizeof(*l));
+       l = timer_alloc(timers, sizeof(*l));
        if (!l)
                return;
 
@@ -521,5 +544,5 @@ void timers_cleanup(struct timers *timers)
        unsigned int l;
 
        for (l = 0; l < ARRAY_SIZE(timers->level); l++)
-               free(timers->level[l]);
+               timer_free(timers, timers->level[l]);
 }