]> git.proxmox.com Git - mirror_frr.git/blob - lib/memory.c
Merge pull request #1927 from pguibert6WIND/issue_1926
[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
21 #include "memory.h"
22 #include "log.h"
23
24 static struct memgroup *mg_first = NULL;
25 struct memgroup **mg_insert = &mg_first;
26
27 DEFINE_MGROUP(LIB, "libfrr")
28 DEFINE_MTYPE(LIB, TMP, "Temporary memory")
29 DEFINE_MTYPE(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec")
30
31 static inline void mt_count_alloc(struct memtype *mt, size_t size)
32 {
33 size_t oldsize;
34
35 atomic_fetch_add_explicit(&mt->n_alloc, 1, memory_order_relaxed);
36
37 oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed);
38 if (oldsize == 0)
39 oldsize = atomic_exchange_explicit(&mt->size, size,
40 memory_order_relaxed);
41 if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR)
42 atomic_store_explicit(&mt->size, SIZE_VAR,
43 memory_order_relaxed);
44 }
45
46 static inline void mt_count_free(struct memtype *mt)
47 {
48 assert(mt->n_alloc);
49 atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed);
50 }
51
52 static inline void *mt_checkalloc(struct memtype *mt, void *ptr, size_t size)
53 {
54 if (__builtin_expect(ptr == NULL, 0)) {
55 memory_oom(size, mt->name);
56 return NULL;
57 }
58 mt_count_alloc(mt, size);
59 return ptr;
60 }
61
62 void *qmalloc(struct memtype *mt, size_t size)
63 {
64 return mt_checkalloc(mt, malloc(size), size);
65 }
66
67 void *qcalloc(struct memtype *mt, size_t size)
68 {
69 return mt_checkalloc(mt, calloc(size, 1), size);
70 }
71
72 void *qrealloc(struct memtype *mt, void *ptr, size_t size)
73 {
74 if (ptr)
75 mt_count_free(mt);
76 return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size);
77 }
78
79 void *qstrdup(struct memtype *mt, const char *str)
80 {
81 return mt_checkalloc(mt, strdup(str), strlen(str) + 1);
82 }
83
84 void qfree(struct memtype *mt, void *ptr)
85 {
86 if (ptr)
87 mt_count_free(mt);
88 free(ptr);
89 }
90
91 int qmem_walk(qmem_walk_fn *func, void *arg)
92 {
93 struct memgroup *mg;
94 struct memtype *mt;
95 int rv;
96
97 for (mg = mg_first; mg; mg = mg->next) {
98 if ((rv = func(arg, mg, NULL)))
99 return rv;
100 for (mt = mg->types; mt; mt = mt->next)
101 if ((rv = func(arg, mg, mt)))
102 return rv;
103 }
104 return 0;
105 }
106
107 struct exit_dump_args {
108 FILE *fp;
109 const char *prefix;
110 int error;
111 };
112
113 static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt)
114 {
115 struct exit_dump_args *eda = arg;
116
117 if (!mt) {
118 fprintf(eda->fp,
119 "%s: showing active allocations in "
120 "memory group %s\n",
121 eda->prefix, mg->name);
122
123 } else if (mt->n_alloc) {
124 char size[32];
125 eda->error++;
126 snprintf(size, sizeof(size), "%10zu", mt->size);
127 fprintf(eda->fp, "%s: memstats: %-30s: %6zu * %s\n",
128 eda->prefix, mt->name, mt->n_alloc,
129 mt->size == SIZE_VAR ? "(variably sized)" : size);
130 }
131 return 0;
132 }
133
134 int log_memstats(FILE *fp, const char *prefix)
135 {
136 struct exit_dump_args eda = {.fp = fp, .prefix = prefix, .error = 0};
137 qmem_walk(qmem_exit_walker, &eda);
138 return eda.error;
139 }