summaryrefslogtreecommitdiff
path: root/lib/hook.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@opensourcerouting.org>2017-08-08 09:00:28 +0200
committerDavid Lamparter <equinox@opensourcerouting.org>2017-08-15 13:25:41 +0200
commit08c4c73be6073282e73a9d7074212df39e27aa5c (patch)
tree250d8c9e53322475e88dc0d8d61db22c3874bcec /lib/hook.c
parent865f5bb8cb3baa2c8df576c6cd0af3d489e97b5f (diff)
lib: hooks: support priority ordering & reversing
Allow registering callbacks with a priority value used to order them relative to each other. Plus a reverse variant that just flips the direction on priorities. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/hook.c')
-rw-r--r--lib/hook.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/hook.c b/lib/hook.c
index 2c877cbf45..1468c4d329 100644
--- a/lib/hook.c
+++ b/lib/hook.c
@@ -26,17 +26,25 @@
DEFINE_MTYPE_STATIC(LIB, HOOK_ENTRY, "Hook entry")
void _hook_register(struct hook *hook, void *funcptr, void *arg, bool has_arg,
- struct frrmod_runtime *module, const char *funcname)
+ struct frrmod_runtime *module, const char *funcname,
+ int priority)
{
- struct hookent *he = XCALLOC(MTYPE_HOOK_ENTRY, sizeof(*he));
+ struct hookent *he = XCALLOC(MTYPE_HOOK_ENTRY, sizeof(*he)), **pos;
he->hookfn = funcptr;
he->hookarg = arg;
he->has_arg = has_arg;
he->module = module;
he->fnname = funcname;
+ he->priority = priority;
- he->next = hook->entries;
- hook->entries = he;
+ for (pos = &hook->entries; *pos; pos = &(*pos)->next)
+ if (hook->reverse
+ ? (*pos)->priority < priority
+ : (*pos)->priority >= priority)
+ break;
+
+ he->next = *pos;
+ *pos = he;
}
void _hook_unregister(struct hook *hook, void *funcptr, void *arg, bool has_arg)