]>
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 DL |
28 | DEFINE_MTYPE(LIB, TMP, "Temporary memory") |
29 | ||
fa7fe831 | 30 | static inline void mt_count_alloc(struct memtype *mt, size_t size) |
3b4cd783 | 31 | { |
fa7fe831 | 32 | size_t oldsize; |
3b4cd783 | 33 | |
fa7fe831 | 34 | atomic_fetch_add_explicit(&mt->n_alloc, 1, memory_order_relaxed); |
a31446a8 | 35 | |
fa7fe831 DL |
36 | oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed); |
37 | if (oldsize == 0) | |
d62a17ae | 38 | oldsize = atomic_exchange_explicit(&mt->size, size, |
39 | memory_order_relaxed); | |
fa7fe831 | 40 | if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR) |
d62a17ae | 41 | atomic_store_explicit(&mt->size, SIZE_VAR, |
42 | memory_order_relaxed); | |
3b4cd783 DL |
43 | } |
44 | ||
fa7fe831 | 45 | static inline void mt_count_free(struct memtype *mt) |
3b4cd783 | 46 | { |
fa7fe831 DL |
47 | assert(mt->n_alloc); |
48 | atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed); | |
3b4cd783 DL |
49 | } |
50 | ||
fa7fe831 | 51 | static inline void *mt_checkalloc(struct memtype *mt, void *ptr, size_t size) |
3b4cd783 | 52 | { |
fa7fe831 DL |
53 | if (__builtin_expect(ptr == NULL, 0)) { |
54 | memory_oom(size, mt->name); | |
55 | return NULL; | |
56 | } | |
57 | mt_count_alloc(mt, size); | |
58 | return ptr; | |
3b4cd783 DL |
59 | } |
60 | ||
fa7fe831 | 61 | void *qmalloc(struct memtype *mt, size_t size) |
3b4cd783 | 62 | { |
fa7fe831 | 63 | return mt_checkalloc(mt, malloc(size), size); |
3b4cd783 DL |
64 | } |
65 | ||
fa7fe831 | 66 | void *qcalloc(struct memtype *mt, size_t size) |
3b4cd783 | 67 | { |
fa7fe831 | 68 | return mt_checkalloc(mt, calloc(size, 1), size); |
3b4cd783 DL |
69 | } |
70 | ||
fa7fe831 | 71 | void *qrealloc(struct memtype *mt, void *ptr, size_t size) |
3b4cd783 | 72 | { |
fa7fe831 DL |
73 | if (ptr) |
74 | mt_count_free(mt); | |
75 | return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size); | |
3b4cd783 DL |
76 | } |
77 | ||
fa7fe831 | 78 | void *qstrdup(struct memtype *mt, const char *str) |
3b4cd783 | 79 | { |
fa7fe831 | 80 | return mt_checkalloc(mt, strdup(str), strlen(str) + 1); |
3b4cd783 DL |
81 | } |
82 | ||
fa7fe831 | 83 | void qfree(struct memtype *mt, void *ptr) |
3b4cd783 | 84 | { |
fa7fe831 DL |
85 | if (ptr) |
86 | mt_count_free(mt); | |
87 | free(ptr); | |
3b4cd783 DL |
88 | } |
89 | ||
fa7fe831 | 90 | int qmem_walk(qmem_walk_fn *func, void *arg) |
3b4cd783 | 91 | { |
fa7fe831 DL |
92 | struct memgroup *mg; |
93 | struct memtype *mt; | |
94 | int rv; | |
95 | ||
96 | for (mg = mg_first; mg; mg = mg->next) { | |
97 | if ((rv = func(arg, mg, NULL))) | |
98 | return rv; | |
99 | for (mt = mg->types; mt; mt = mt->next) | |
100 | if ((rv = func(arg, mg, mt))) | |
101 | return rv; | |
102 | } | |
103 | return 0; | |
3b4cd783 | 104 | } |
7a13c923 | 105 | |
fa7fe831 DL |
106 | struct exit_dump_args { |
107 | const char *prefix; | |
108 | int error; | |
7a13c923 DL |
109 | }; |
110 | ||
fa7fe831 | 111 | static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt) |
7a13c923 | 112 | { |
fa7fe831 DL |
113 | struct exit_dump_args *eda = arg; |
114 | ||
115 | if (!mt) { | |
d62a17ae | 116 | fprintf(stderr, |
117 | "%s: showing active allocations in " | |
118 | "memory group %s\n", | |
119 | eda->prefix, mg->name); | |
fa7fe831 DL |
120 | |
121 | } else if (mt->n_alloc) { | |
122 | char size[32]; | |
123 | eda->error++; | |
124 | snprintf(size, sizeof(size), "%10zu", mt->size); | |
125 | fprintf(stderr, "%s: memstats: %-30s: %6zu * %s\n", | |
d62a17ae | 126 | eda->prefix, mt->name, mt->n_alloc, |
127 | mt->size == SIZE_VAR ? "(variably sized)" : size); | |
fa7fe831 DL |
128 | } |
129 | return 0; | |
7a13c923 DL |
130 | } |
131 | ||
fa7fe831 | 132 | void log_memstats_stderr(const char *prefix) |
7a13c923 | 133 | { |
d62a17ae | 134 | struct exit_dump_args eda = {.prefix = prefix, .error = 0}; |
fa7fe831 | 135 | qmem_walk(qmem_exit_walker, &eda); |
7a13c923 | 136 | } |