X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftimer%2Ftimer.c;h=2685d3efde96f780fbbc3dc946d6acc4b9def9e1;hb=2c148ec2ad0459d29ba19ec5b1c5515717cc8d80;hp=c55e23dd21cf287d64b6d1ce6482b7cf0d33df9d;hpb=4ba104904176bc059167757c10d461703b6b06dc;p=ccan diff --git a/ccan/timer/timer.c b/ccan/timer/timer.c index c55e23dd..2685d3ef 100644 --- a/ccan/timer/timer.c +++ b/ccan/timer/timer.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -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]); }