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