]>
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 | 54 | if (__builtin_expect(ptr == NULL, 0)) { |
c897c456 CF |
55 | if (size) { |
56 | /* malloc(0) is allowed to return NULL */ | |
57 | memory_oom(size, mt->name); | |
58 | } | |
fa7fe831 DL |
59 | return NULL; |
60 | } | |
61 | mt_count_alloc(mt, size); | |
62 | return ptr; | |
3b4cd783 DL |
63 | } |
64 | ||
fa7fe831 | 65 | void *qmalloc(struct memtype *mt, size_t size) |
3b4cd783 | 66 | { |
fa7fe831 | 67 | return mt_checkalloc(mt, malloc(size), size); |
3b4cd783 DL |
68 | } |
69 | ||
fa7fe831 | 70 | void *qcalloc(struct memtype *mt, size_t size) |
3b4cd783 | 71 | { |
fa7fe831 | 72 | return mt_checkalloc(mt, calloc(size, 1), size); |
3b4cd783 DL |
73 | } |
74 | ||
fa7fe831 | 75 | void *qrealloc(struct memtype *mt, void *ptr, size_t size) |
3b4cd783 | 76 | { |
fa7fe831 DL |
77 | if (ptr) |
78 | mt_count_free(mt); | |
79 | return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size); | |
3b4cd783 DL |
80 | } |
81 | ||
fa7fe831 | 82 | void *qstrdup(struct memtype *mt, const char *str) |
3b4cd783 | 83 | { |
4ac99370 | 84 | return str ? mt_checkalloc(mt, strdup(str), strlen(str) + 1) : NULL; |
3b4cd783 DL |
85 | } |
86 | ||
fa7fe831 | 87 | void qfree(struct memtype *mt, void *ptr) |
3b4cd783 | 88 | { |
fa7fe831 DL |
89 | if (ptr) |
90 | mt_count_free(mt); | |
91 | free(ptr); | |
3b4cd783 DL |
92 | } |
93 | ||
fa7fe831 | 94 | int qmem_walk(qmem_walk_fn *func, void *arg) |
3b4cd783 | 95 | { |
fa7fe831 DL |
96 | struct memgroup *mg; |
97 | struct memtype *mt; | |
98 | int rv; | |
99 | ||
100 | for (mg = mg_first; mg; mg = mg->next) { | |
101 | if ((rv = func(arg, mg, NULL))) | |
102 | return rv; | |
103 | for (mt = mg->types; mt; mt = mt->next) | |
104 | if ((rv = func(arg, mg, mt))) | |
105 | return rv; | |
106 | } | |
107 | return 0; | |
3b4cd783 | 108 | } |
7a13c923 | 109 | |
fa7fe831 | 110 | struct exit_dump_args { |
9eed278b | 111 | FILE *fp; |
fa7fe831 DL |
112 | const char *prefix; |
113 | int error; | |
7a13c923 DL |
114 | }; |
115 | ||
fa7fe831 | 116 | static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt) |
7a13c923 | 117 | { |
fa7fe831 DL |
118 | struct exit_dump_args *eda = arg; |
119 | ||
120 | if (!mt) { | |
9eed278b | 121 | fprintf(eda->fp, |
d62a17ae | 122 | "%s: showing active allocations in " |
123 | "memory group %s\n", | |
124 | eda->prefix, mg->name); | |
fa7fe831 DL |
125 | |
126 | } else if (mt->n_alloc) { | |
127 | char size[32]; | |
128 | eda->error++; | |
129 | snprintf(size, sizeof(size), "%10zu", mt->size); | |
9eed278b | 130 | fprintf(eda->fp, "%s: memstats: %-30s: %6zu * %s\n", |
d62a17ae | 131 | eda->prefix, mt->name, mt->n_alloc, |
132 | mt->size == SIZE_VAR ? "(variably sized)" : size); | |
fa7fe831 DL |
133 | } |
134 | return 0; | |
7a13c923 DL |
135 | } |
136 | ||
9eed278b | 137 | int log_memstats(FILE *fp, const char *prefix) |
7a13c923 | 138 | { |
996c9314 | 139 | struct exit_dump_args eda = {.fp = fp, .prefix = prefix, .error = 0}; |
fa7fe831 | 140 | qmem_walk(qmem_exit_walker, &eda); |
9eed278b | 141 | return eda.error; |
7a13c923 | 142 | } |