]> git.proxmox.com Git - mirror_frr.git/blob - lib/memory.c
lib: enforce vrf_name_to_id by returning default_vrf when name is null
[mirror_frr.git] / lib / memory.c
1 /*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
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.
7 *
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.
15 */
16
17 #include <zebra.h>
18
19 #include <stdlib.h>
20 #ifdef HAVE_MALLOC_H
21 #include <malloc.h>
22 #endif
23 #ifdef HAVE_MALLOC_NP_H
24 #include <malloc_np.h>
25 #endif
26 #ifdef HAVE_MALLOC_MALLOC_H
27 #include <malloc/malloc.h>
28 #endif
29
30 #include "memory.h"
31 #include "log.h"
32
33 static struct memgroup *mg_first = NULL;
34 struct memgroup **mg_insert = &mg_first;
35
36 DEFINE_MGROUP(LIB, "libfrr")
37 DEFINE_MTYPE(LIB, TMP, "Temporary memory")
38 DEFINE_MTYPE(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec")
39
40 static inline void mt_count_alloc(struct memtype *mt, size_t size, void *ptr)
41 {
42 size_t current;
43 size_t oldsize;
44
45 current = 1 + atomic_fetch_add_explicit(&mt->n_alloc, 1,
46 memory_order_relaxed);
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);
55
56 oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed);
57 if (oldsize == 0)
58 oldsize = atomic_exchange_explicit(&mt->size, size,
59 memory_order_relaxed);
60 if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR)
61 atomic_store_explicit(&mt->size, SIZE_VAR,
62 memory_order_relaxed);
63
64 #ifdef HAVE_MALLOC_USABLE_SIZE
65 size_t mallocsz = malloc_usable_size(ptr);
66
67 current = mallocsz + atomic_fetch_add_explicit(&mt->total, mallocsz,
68 memory_order_relaxed);
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);
76 #endif
77 }
78
79 static inline void mt_count_free(struct memtype *mt, void *ptr)
80 {
81 assert(mt->n_alloc);
82 atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed);
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
89 }
90
91 static inline void *mt_checkalloc(struct memtype *mt, void *ptr, size_t size)
92 {
93 if (__builtin_expect(ptr == NULL, 0)) {
94 if (size) {
95 /* malloc(0) is allowed to return NULL */
96 memory_oom(size, mt->name);
97 }
98 return NULL;
99 }
100 mt_count_alloc(mt, size, ptr);
101 return ptr;
102 }
103
104 void *qmalloc(struct memtype *mt, size_t size)
105 {
106 return mt_checkalloc(mt, malloc(size), size);
107 }
108
109 void *qcalloc(struct memtype *mt, size_t size)
110 {
111 return mt_checkalloc(mt, calloc(size, 1), size);
112 }
113
114 void *qrealloc(struct memtype *mt, void *ptr, size_t size)
115 {
116 if (ptr)
117 mt_count_free(mt, ptr);
118 return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size);
119 }
120
121 void *qstrdup(struct memtype *mt, const char *str)
122 {
123 return str ? mt_checkalloc(mt, strdup(str), strlen(str) + 1) : NULL;
124 }
125
126 void qfree(struct memtype *mt, void *ptr)
127 {
128 if (ptr)
129 mt_count_free(mt, ptr);
130 free(ptr);
131 }
132
133 int qmem_walk(qmem_walk_fn *func, void *arg)
134 {
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;
147 }
148
149 struct exit_dump_args {
150 FILE *fp;
151 const char *prefix;
152 int error;
153 };
154
155 static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt)
156 {
157 struct exit_dump_args *eda = arg;
158
159 if (!mt) {
160 fprintf(eda->fp,
161 "%s: showing active allocations in "
162 "memory group %s\n",
163 eda->prefix, mg->name);
164
165 } else if (mt->n_alloc) {
166 char size[32];
167 eda->error++;
168 snprintf(size, sizeof(size), "%10zu", mt->size);
169 fprintf(eda->fp, "%s: memstats: %-30s: %6zu * %s\n",
170 eda->prefix, mt->name, mt->n_alloc,
171 mt->size == SIZE_VAR ? "(variably sized)" : size);
172 }
173 return 0;
174 }
175
176 int log_memstats(FILE *fp, const char *prefix)
177 {
178 struct exit_dump_args eda = {.fp = fp, .prefix = prefix, .error = 0};
179 qmem_walk(qmem_exit_walker, &eda);
180 return eda.error;
181 }