]> git.proxmox.com Git - mirror_frr.git/blob - lib/memory.c
debianpkg: Remove -werror from Ubuntu 14.04 and 12.04 build to skip warnings from...
[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
30 static inline void 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 mt_count_free(struct memtype *mt)
41 {
42 assert(mt->n_alloc);
43 mt->n_alloc--;
44 }
45
46 static inline void *mt_checkalloc(struct memtype *mt, void *ptr, size_t size)
47 {
48 if (__builtin_expect(ptr == NULL, 0)) {
49 memory_oom(size, mt->name);
50 return NULL;
51 }
52 mt_count_alloc(mt, size);
53 return ptr;
54 }
55
56 void *qmalloc(struct memtype *mt, size_t size)
57 {
58 return mt_checkalloc(mt, malloc(size), size);
59 }
60
61 void *qcalloc(struct memtype *mt, size_t size)
62 {
63 return mt_checkalloc(mt, calloc(size, 1), size);
64 }
65
66 void *qrealloc(struct memtype *mt, void *ptr, size_t size)
67 {
68 if (ptr)
69 mt_count_free(mt);
70 return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size);
71 }
72
73 void *qstrdup(struct memtype *mt, const char *str)
74 {
75 return mt_checkalloc(mt, strdup(str), strlen(str) + 1);
76 }
77
78 void qfree(struct memtype *mt, void *ptr)
79 {
80 if (ptr)
81 mt_count_free(mt);
82 free(ptr);
83 }
84
85 int qmem_walk(qmem_walk_fn *func, void *arg)
86 {
87 struct memgroup *mg;
88 struct memtype *mt;
89 int rv;
90
91 for (mg = mg_first; mg; mg = mg->next) {
92 if ((rv = func(arg, mg, NULL)))
93 return rv;
94 for (mt = mg->types; mt; mt = mt->next)
95 if ((rv = func(arg, mg, mt)))
96 return rv;
97 }
98 return 0;
99 }
100
101 struct exit_dump_args {
102 const char *prefix;
103 int error;
104 };
105
106 static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt)
107 {
108 struct exit_dump_args *eda = arg;
109
110 if (!mt) {
111 fprintf(stderr,
112 "%s: showing active allocations in memory group %s\n",
113 eda->prefix, mg->name);
114 } else if (mt->n_alloc) {
115 char size[32];
116 eda->error++;
117 snprintf(size, sizeof(size), "%10zu", mt->size);
118 fprintf(stderr, "%s: memstats: %-30s: %6zu * %s\n",
119 eda->prefix, mt->name, mt->n_alloc,
120 mt->size == SIZE_VAR ? "(variably sized)" : size);
121 }
122 return 0;
123 }
124
125 void log_memstats_stderr(const char *prefix)
126 {
127 struct exit_dump_args eda = {.prefix = prefix, .error = 0};
128 qmem_walk(qmem_exit_walker, &eda);
129 }