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