]> git.proxmox.com Git - mirror_frr.git/blame - lib/memory.c
Merge pull request #153 from LabNConsulting/working/2.0/patch-set/rr-part2
[mirror_frr.git] / lib / memory.c
CommitLineData
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>
20
21#include "memory.h"
22
23static struct memgroup *mg_first = NULL;
24struct memgroup **mg_insert = &mg_first;
25
4a1ab8e4
DL
26DEFINE_MGROUP(LIB, "libzebra")
27DEFINE_MTYPE(LIB, TMP, "Temporary memory")
28
3b4cd783
DL
29static inline void
30mt_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
40static inline void
41mt_count_free (struct memtype *mt)
42{
43 mt->n_alloc--;
44}
45
46static inline void *
47mt_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
58void *
59qmalloc (struct memtype *mt, size_t size)
60{
61 return mt_checkalloc (mt, malloc (size), size);
62}
63
64void *
65qcalloc (struct memtype *mt, size_t size)
66{
67 return mt_checkalloc (mt, calloc (size, 1), size);
68}
69
70void *
71qrealloc (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
78void *
79qstrdup (struct memtype *mt, const char *str)
80{
81 return mt_checkalloc (mt, strdup (str), strlen (str) + 1);
82}
83
84void
85qfree (struct memtype *mt, void *ptr)
86{
87 if (ptr)
88 mt_count_free (mt);
89 free (ptr);
90}
91
92int
93qmem_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}
7a13c923
DL
109
110struct exit_dump_args
111{
112 const char *prefix;
113 int error;
114};
115
116static int
117qmem_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);
f9fe6278 131 fprintf (stderr, "%s: memstats: %-30s: %6zu * %s\n",
7a13c923
DL
132 eda->prefix, mt->name, mt->n_alloc,
133 mt->size == SIZE_VAR ? "(variably sized)" : size);
134 }
135 return 0;
136}
137
138void
139log_memstats_stderr (const char *prefix)
140{
141 struct exit_dump_args eda = { .prefix = prefix, .error = 0 };
142 qmem_walk (qmem_exit_walker, &eda);
143}