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