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