]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: ISC |
a5b38c5b DL |
2 | /* |
3 | * Copyright (c) 2016 David Lamparter, for NetDEF, Inc. | |
a5b38c5b DL |
4 | */ |
5 | ||
b45ac5f5 DL |
6 | #ifdef HAVE_CONFIG_H |
7 | #include "config.h" | |
8 | #endif | |
9 | ||
8d0a2918 DL |
10 | #include <string.h> |
11 | ||
a5b38c5b DL |
12 | #include "memory.h" |
13 | #include "hook.h" | |
14 | ||
bf8d3d6a | 15 | DEFINE_MTYPE_STATIC(LIB, HOOK_ENTRY, "Hook entry"); |
a5b38c5b | 16 | |
8d0a2918 DL |
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) | |
a5b38c5b | 20 | { |
8d0a2918 DL |
21 | struct hookent *he, **pos; |
22 | ||
23 | if (!stackent->ent_used) | |
24 | he = stackent; | |
25 | else { | |
26 | he = XCALLOC(MTYPE_HOOK_ENTRY, sizeof(*he)); | |
27 | he->ent_on_heap = true; | |
28 | } | |
29 | he->ent_used = true; | |
a5b38c5b DL |
30 | he->hookfn = funcptr; |
31 | he->hookarg = arg; | |
32 | he->has_arg = has_arg; | |
33 | he->module = module; | |
34 | he->fnname = funcname; | |
08c4c73b | 35 | he->priority = priority; |
a5b38c5b | 36 | |
08c4c73b | 37 | for (pos = &hook->entries; *pos; pos = &(*pos)->next) |
996c9314 LB |
38 | if (hook->reverse ? (*pos)->priority < priority |
39 | : (*pos)->priority >= priority) | |
08c4c73b DL |
40 | break; |
41 | ||
42 | he->next = *pos; | |
43 | *pos = he; | |
a5b38c5b DL |
44 | } |
45 | ||
d62a17ae | 46 | void _hook_unregister(struct hook *hook, void *funcptr, void *arg, bool has_arg) |
a5b38c5b DL |
47 | { |
48 | struct hookent *he, **prev; | |
49 | ||
50 | for (prev = &hook->entries; (he = *prev) != NULL; prev = &he->next) | |
51 | if (he->hookfn == funcptr && he->hookarg == arg | |
d62a17ae | 52 | && he->has_arg == has_arg) { |
a5b38c5b | 53 | *prev = he->next; |
8d0a2918 DL |
54 | if (he->ent_on_heap) |
55 | XFREE(MTYPE_HOOK_ENTRY, he); | |
56 | else | |
57 | memset(he, 0, sizeof(*he)); | |
a5b38c5b DL |
58 | break; |
59 | } | |
60 | } |