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