]>
Commit | Line | Data |
---|---|---|
3b4cd783 DL |
1 | /* |
2 | * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc. | |
3 | * | |
db2d8df6 DL |
4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
3b4cd783 | 7 | * |
db2d8df6 DL |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
3b4cd783 DL |
15 | */ |
16 | ||
17 | #include <zebra.h> | |
18 | ||
19 | #include <stdlib.h> | |
20 | ||
21 | #include "memory.h" | |
f65e2d40 | 22 | #include "log.h" |
3b4cd783 DL |
23 | |
24 | static struct memgroup *mg_first = NULL; | |
25 | struct memgroup **mg_insert = &mg_first; | |
26 | ||
55c72803 | 27 | DEFINE_MGROUP(LIB, "libfrr") |
4a1ab8e4 | 28 | DEFINE_MTYPE(LIB, TMP, "Temporary memory") |
9a14899b | 29 | DEFINE_MTYPE(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec") |
4a1ab8e4 | 30 | |
fa7fe831 | 31 | static inline void mt_count_alloc(struct memtype *mt, size_t size) |
3b4cd783 | 32 | { |
fa7fe831 | 33 | size_t oldsize; |
3b4cd783 | 34 | |
fa7fe831 | 35 | atomic_fetch_add_explicit(&mt->n_alloc, 1, memory_order_relaxed); |
a31446a8 | 36 | |
fa7fe831 DL |
37 | oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed); |
38 | if (oldsize == 0) | |
d62a17ae | 39 | oldsize = atomic_exchange_explicit(&mt->size, size, |
40 | memory_order_relaxed); | |
fa7fe831 | 41 | if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR) |
d62a17ae | 42 | atomic_store_explicit(&mt->size, SIZE_VAR, |
43 | memory_order_relaxed); | |
3b4cd783 DL |
44 | } |
45 | ||
fa7fe831 | 46 | static inline void mt_count_free(struct memtype *mt) |
3b4cd783 | 47 | { |
fa7fe831 DL |
48 | assert(mt->n_alloc); |
49 | atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed); | |
3b4cd783 DL |
50 | } |
51 | ||
fa7fe831 | 52 | static inline void *mt_checkalloc(struct memtype *mt, void *ptr, size_t size) |
3b4cd783 | 53 | { |
fa7fe831 DL |
54 | if (__builtin_expect(ptr == NULL, 0)) { |
55 | memory_oom(size, mt->name); | |
56 | return NULL; | |
57 | } | |
58 | mt_count_alloc(mt, size); | |
59 | return ptr; | |
3b4cd783 DL |
60 | } |
61 | ||
fa7fe831 | 62 | void *qmalloc(struct memtype *mt, size_t size) |
3b4cd783 | 63 | { |
fa7fe831 | 64 | return mt_checkalloc(mt, malloc(size), size); |
3b4cd783 DL |
65 | } |
66 | ||
fa7fe831 | 67 | void *qcalloc(struct memtype *mt, size_t size) |
3b4cd783 | 68 | { |
fa7fe831 | 69 | return mt_checkalloc(mt, calloc(size, 1), size); |
3b4cd783 DL |
70 | } |
71 | ||
fa7fe831 | 72 | void *qrealloc(struct memtype *mt, void *ptr, size_t size) |
3b4cd783 | 73 | { |
fa7fe831 DL |
74 | if (ptr) |
75 | mt_count_free(mt); | |
76 | return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size); | |
3b4cd783 DL |
77 | } |
78 | ||
fa7fe831 | 79 | void *qstrdup(struct memtype *mt, const char *str) |
3b4cd783 | 80 | { |
fa7fe831 | 81 | return mt_checkalloc(mt, strdup(str), strlen(str) + 1); |
3b4cd783 DL |
82 | } |
83 | ||
fa7fe831 | 84 | void qfree(struct memtype *mt, void *ptr) |
3b4cd783 | 85 | { |
fa7fe831 DL |
86 | if (ptr) |
87 | mt_count_free(mt); | |
88 | free(ptr); | |
3b4cd783 DL |
89 | } |
90 | ||
fa7fe831 | 91 | int qmem_walk(qmem_walk_fn *func, void *arg) |
3b4cd783 | 92 | { |
fa7fe831 DL |
93 | struct memgroup *mg; |
94 | struct memtype *mt; | |
95 | int rv; | |
96 | ||
97 | for (mg = mg_first; mg; mg = mg->next) { | |
98 | if ((rv = func(arg, mg, NULL))) | |
99 | return rv; | |
100 | for (mt = mg->types; mt; mt = mt->next) | |
101 | if ((rv = func(arg, mg, mt))) | |
102 | return rv; | |
103 | } | |
104 | return 0; | |
3b4cd783 | 105 | } |
7a13c923 | 106 | |
fa7fe831 | 107 | struct exit_dump_args { |
9eed278b | 108 | FILE *fp; |
fa7fe831 DL |
109 | const char *prefix; |
110 | int error; | |
7a13c923 DL |
111 | }; |
112 | ||
fa7fe831 | 113 | static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt) |
7a13c923 | 114 | { |
fa7fe831 DL |
115 | struct exit_dump_args *eda = arg; |
116 | ||
117 | if (!mt) { | |
9eed278b | 118 | fprintf(eda->fp, |
d62a17ae | 119 | "%s: showing active allocations in " |
120 | "memory group %s\n", | |
121 | eda->prefix, mg->name); | |
fa7fe831 DL |
122 | |
123 | } else if (mt->n_alloc) { | |
124 | char size[32]; | |
125 | eda->error++; | |
126 | snprintf(size, sizeof(size), "%10zu", mt->size); | |
9eed278b | 127 | fprintf(eda->fp, "%s: memstats: %-30s: %6zu * %s\n", |
d62a17ae | 128 | eda->prefix, mt->name, mt->n_alloc, |
129 | mt->size == SIZE_VAR ? "(variably sized)" : size); | |
fa7fe831 DL |
130 | } |
131 | return 0; | |
7a13c923 DL |
132 | } |
133 | ||
9eed278b | 134 | int log_memstats(FILE *fp, const char *prefix) |
7a13c923 | 135 | { |
996c9314 | 136 | struct exit_dump_args eda = {.fp = fp, .prefix = prefix, .error = 0}; |
fa7fe831 | 137 | qmem_walk(qmem_exit_walker, &eda); |
9eed278b | 138 | return eda.error; |
7a13c923 | 139 | } |