Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: ISC |
2 | | /* |
3 | | * Copyright (c) 2016 David Lamparter, for NetDEF, Inc. |
4 | | */ |
5 | | |
6 | | #ifdef HAVE_CONFIG_H |
7 | | #include "config.h" |
8 | | #endif |
9 | | |
10 | | #include <string.h> |
11 | | |
12 | | #include "memory.h" |
13 | | #include "hook.h" |
14 | | |
15 | | DEFINE_MTYPE_STATIC(LIB, HOOK_ENTRY, "Hook entry"); |
16 | | |
17 | | void _hook_register(struct hook *hook, struct hookent *stackent, void *funcptr, |
18 | | void *arg, bool has_arg, struct frrmod_runtime *module, |
19 | | const char *funcname, int priority) |
20 | 63 | { |
21 | 63 | struct hookent *he, **pos; |
22 | | |
23 | 63 | if (!stackent->ent_used) |
24 | 63 | he = stackent; |
25 | 0 | else { |
26 | 0 | he = XCALLOC(MTYPE_HOOK_ENTRY, sizeof(*he)); |
27 | 0 | he->ent_on_heap = true; |
28 | 0 | } |
29 | 63 | he->ent_used = true; |
30 | 63 | he->hookfn = funcptr; |
31 | 63 | he->hookarg = arg; |
32 | 63 | he->has_arg = has_arg; |
33 | 63 | he->module = module; |
34 | 63 | he->fnname = funcname; |
35 | 63 | he->priority = priority; |
36 | | |
37 | 65 | for (pos = &hook->entries; *pos; pos = &(*pos)->next) |
38 | 9 | if (hook->reverse ? (*pos)->priority < priority |
39 | 9 | : (*pos)->priority >= priority) |
40 | 7 | break; |
41 | | |
42 | 63 | he->next = *pos; |
43 | 63 | *pos = he; |
44 | 63 | } |
45 | | |
46 | | void _hook_unregister(struct hook *hook, void *funcptr, void *arg, bool has_arg) |
47 | 0 | { |
48 | 0 | struct hookent *he, **prev; |
49 | |
|
50 | 0 | for (prev = &hook->entries; (he = *prev) != NULL; prev = &he->next) |
51 | 0 | if (he->hookfn == funcptr && he->hookarg == arg |
52 | 0 | && he->has_arg == has_arg) { |
53 | 0 | *prev = he->next; |
54 | 0 | if (he->ent_on_heap) |
55 | 0 | XFREE(MTYPE_HOOK_ENTRY, he); |
56 | 0 | else |
57 | 0 | memset(he, 0, sizeof(*he)); |
58 | 0 | break; |
59 | 0 | } |
60 | 0 | } |