]>
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> | |
602a6584 DL |
20 | #ifdef HAVE_MALLOC_H |
21 | #include <malloc.h> | |
22 | #endif | |
324be174 DL |
23 | #ifdef HAVE_MALLOC_NP_H |
24 | #include <malloc_np.h> | |
25 | #endif | |
602a6584 DL |
26 | #ifdef HAVE_MALLOC_MALLOC_H |
27 | #include <malloc/malloc.h> | |
28 | #endif | |
3b4cd783 DL |
29 | |
30 | #include "memory.h" | |
f65e2d40 | 31 | #include "log.h" |
3b4cd783 DL |
32 | |
33 | static struct memgroup *mg_first = NULL; | |
34 | struct memgroup **mg_insert = &mg_first; | |
35 | ||
55c72803 | 36 | DEFINE_MGROUP(LIB, "libfrr") |
4a1ab8e4 | 37 | DEFINE_MTYPE(LIB, TMP, "Temporary memory") |
9a14899b | 38 | DEFINE_MTYPE(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec") |
4a1ab8e4 | 39 | |
602a6584 | 40 | static inline void mt_count_alloc(struct memtype *mt, size_t size, void *ptr) |
3b4cd783 | 41 | { |
13f1ba41 | 42 | size_t current; |
fa7fe831 | 43 | size_t oldsize; |
3b4cd783 | 44 | |
7a153339 LB |
45 | current = 1 + atomic_fetch_add_explicit(&mt->n_alloc, 1, |
46 | memory_order_relaxed); | |
13f1ba41 LB |
47 | |
48 | oldsize = atomic_load_explicit(&mt->n_max, memory_order_relaxed); | |
49 | if (current > oldsize) | |
50 | /* note that this may fail, but approximation is sufficient */ | |
51 | atomic_compare_exchange_weak_explicit(&mt->n_max, &oldsize, | |
52 | current, | |
53 | memory_order_relaxed, | |
54 | memory_order_relaxed); | |
a31446a8 | 55 | |
fa7fe831 DL |
56 | oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed); |
57 | if (oldsize == 0) | |
d62a17ae | 58 | oldsize = atomic_exchange_explicit(&mt->size, size, |
59 | memory_order_relaxed); | |
fa7fe831 | 60 | if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR) |
d62a17ae | 61 | atomic_store_explicit(&mt->size, SIZE_VAR, |
62 | memory_order_relaxed); | |
602a6584 DL |
63 | |
64 | #ifdef HAVE_MALLOC_USABLE_SIZE | |
65 | size_t mallocsz = malloc_usable_size(ptr); | |
66 | ||
7a153339 LB |
67 | current = mallocsz + atomic_fetch_add_explicit(&mt->total, mallocsz, |
68 | memory_order_relaxed); | |
13f1ba41 LB |
69 | oldsize = atomic_load_explicit(&mt->max_size, memory_order_relaxed); |
70 | if (current > oldsize) | |
71 | /* note that this may fail, but approximation is sufficient */ | |
72 | atomic_compare_exchange_weak_explicit(&mt->max_size, &oldsize, | |
73 | current, | |
74 | memory_order_relaxed, | |
75 | memory_order_relaxed); | |
602a6584 | 76 | #endif |
3b4cd783 DL |
77 | } |
78 | ||
602a6584 | 79 | static inline void mt_count_free(struct memtype *mt, void *ptr) |
3b4cd783 | 80 | { |
fa7fe831 DL |
81 | assert(mt->n_alloc); |
82 | atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed); | |
602a6584 DL |
83 | |
84 | #ifdef HAVE_MALLOC_USABLE_SIZE | |
85 | size_t mallocsz = malloc_usable_size(ptr); | |
86 | ||
87 | atomic_fetch_sub_explicit(&mt->total, mallocsz, memory_order_relaxed); | |
88 | #endif | |
3b4cd783 DL |
89 | } |
90 | ||
fa7fe831 | 91 | static inline void *mt_checkalloc(struct memtype *mt, void *ptr, size_t size) |
3b4cd783 | 92 | { |
fa7fe831 | 93 | if (__builtin_expect(ptr == NULL, 0)) { |
c897c456 CF |
94 | if (size) { |
95 | /* malloc(0) is allowed to return NULL */ | |
96 | memory_oom(size, mt->name); | |
97 | } | |
fa7fe831 DL |
98 | return NULL; |
99 | } | |
602a6584 | 100 | mt_count_alloc(mt, size, ptr); |
fa7fe831 | 101 | return ptr; |
3b4cd783 DL |
102 | } |
103 | ||
fa7fe831 | 104 | void *qmalloc(struct memtype *mt, size_t size) |
3b4cd783 | 105 | { |
fa7fe831 | 106 | return mt_checkalloc(mt, malloc(size), size); |
3b4cd783 DL |
107 | } |
108 | ||
fa7fe831 | 109 | void *qcalloc(struct memtype *mt, size_t size) |
3b4cd783 | 110 | { |
fa7fe831 | 111 | return mt_checkalloc(mt, calloc(size, 1), size); |
3b4cd783 DL |
112 | } |
113 | ||
fa7fe831 | 114 | void *qrealloc(struct memtype *mt, void *ptr, size_t size) |
3b4cd783 | 115 | { |
fa7fe831 | 116 | if (ptr) |
602a6584 | 117 | mt_count_free(mt, ptr); |
fa7fe831 | 118 | return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size); |
3b4cd783 DL |
119 | } |
120 | ||
fa7fe831 | 121 | void *qstrdup(struct memtype *mt, const char *str) |
3b4cd783 | 122 | { |
4ac99370 | 123 | return str ? mt_checkalloc(mt, strdup(str), strlen(str) + 1) : NULL; |
3b4cd783 DL |
124 | } |
125 | ||
fa7fe831 | 126 | void qfree(struct memtype *mt, void *ptr) |
3b4cd783 | 127 | { |
fa7fe831 | 128 | if (ptr) |
602a6584 | 129 | mt_count_free(mt, ptr); |
fa7fe831 | 130 | free(ptr); |
3b4cd783 DL |
131 | } |
132 | ||
fa7fe831 | 133 | int qmem_walk(qmem_walk_fn *func, void *arg) |
3b4cd783 | 134 | { |
fa7fe831 DL |
135 | struct memgroup *mg; |
136 | struct memtype *mt; | |
137 | int rv; | |
138 | ||
139 | for (mg = mg_first; mg; mg = mg->next) { | |
140 | if ((rv = func(arg, mg, NULL))) | |
141 | return rv; | |
142 | for (mt = mg->types; mt; mt = mt->next) | |
143 | if ((rv = func(arg, mg, mt))) | |
144 | return rv; | |
145 | } | |
146 | return 0; | |
3b4cd783 | 147 | } |
7a13c923 | 148 | |
fa7fe831 | 149 | struct exit_dump_args { |
9eed278b | 150 | FILE *fp; |
fa7fe831 DL |
151 | const char *prefix; |
152 | int error; | |
7a13c923 DL |
153 | }; |
154 | ||
fa7fe831 | 155 | static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt) |
7a13c923 | 156 | { |
fa7fe831 DL |
157 | struct exit_dump_args *eda = arg; |
158 | ||
159 | if (!mt) { | |
9eed278b | 160 | fprintf(eda->fp, |
d62a17ae | 161 | "%s: showing active allocations in " |
162 | "memory group %s\n", | |
163 | eda->prefix, mg->name); | |
fa7fe831 DL |
164 | |
165 | } else if (mt->n_alloc) { | |
166 | char size[32]; | |
167 | eda->error++; | |
168 | snprintf(size, sizeof(size), "%10zu", mt->size); | |
9eed278b | 169 | fprintf(eda->fp, "%s: memstats: %-30s: %6zu * %s\n", |
d62a17ae | 170 | eda->prefix, mt->name, mt->n_alloc, |
171 | mt->size == SIZE_VAR ? "(variably sized)" : size); | |
fa7fe831 DL |
172 | } |
173 | return 0; | |
7a13c923 DL |
174 | } |
175 | ||
9eed278b | 176 | int log_memstats(FILE *fp, const char *prefix) |
7a13c923 | 177 | { |
996c9314 | 178 | struct exit_dump_args eda = {.fp = fp, .prefix = prefix, .error = 0}; |
fa7fe831 | 179 | qmem_walk(qmem_exit_walker, &eda); |
9eed278b | 180 | return eda.error; |
7a13c923 | 181 | } |