]> git.proxmox.com Git - mirror_frr.git/blob - lib/memory.h
Merge pull request #12645 from gpnaveen/ospf_error_msg_enhancements
[mirror_frr.git] / lib / memory.h
1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 */
5
6 #ifndef _QUAGGA_MEMORY_H
7 #define _QUAGGA_MEMORY_H
8
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <frratomic.h>
13 #include "compiler.h"
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #if defined(HAVE_MALLOC_SIZE) && !defined(HAVE_MALLOC_USABLE_SIZE)
20 #define malloc_usable_size(x) malloc_size(x)
21 #define HAVE_MALLOC_USABLE_SIZE
22 #endif
23
24 #define SIZE_VAR ~0UL
25 struct memtype {
26 struct memtype *next, **ref;
27 const char *name;
28 atomic_size_t n_alloc;
29 atomic_size_t n_max;
30 atomic_size_t size;
31 #ifdef HAVE_MALLOC_USABLE_SIZE
32 atomic_size_t total;
33 atomic_size_t max_size;
34 #endif
35 };
36
37 struct memgroup {
38 struct memgroup *next, **ref;
39 struct memtype *types, **insert;
40 const char *name;
41 /* ignore group on dumping memleaks at exit */
42 bool active_at_exit;
43 };
44
45 /* macro usage:
46 *
47 * mydaemon.h
48 * DECLARE_MGROUP(MYDAEMON);
49 * DECLARE_MTYPE(MYDAEMON_COMMON);
50 *
51 * mydaemon.c
52 * DEFINE_MGROUP(MYDAEMON, "my daemon memory");
53 * DEFINE_MTYPE(MYDAEMON, MYDAEMON_COMMON,
54 * "this mtype is used in multiple files in mydaemon");
55 * foo = qmalloc(MTYPE_MYDAEMON_COMMON, sizeof(*foo))
56 *
57 * mydaemon_io.c
58 * bar = qmalloc(MTYPE_MYDAEMON_COMMON, sizeof(*bar))
59 *
60 * DEFINE_MTYPE_STATIC(MYDAEMON, MYDAEMON_IO,
61 * "this mtype is used only in this file");
62 * baz = qmalloc(MTYPE_MYDAEMON_IO, sizeof(*baz))
63 *
64 * Note: Naming conventions (MGROUP_ and MTYPE_ prefixes are enforced
65 * by not having these as part of the macro arguments)
66 * Note: MTYPE_* are symbols to the compiler (of type struct memtype *),
67 * but MGROUP_* aren't.
68 */
69
70 #define DECLARE_MGROUP(name) extern struct memgroup _mg_##name
71 #define _DEFINE_MGROUP(mname, desc, ...) \
72 struct memgroup _mg_##mname \
73 __attribute__((section(".data.mgroups"))) = { \
74 .name = desc, \
75 .types = NULL, \
76 .next = NULL, \
77 .insert = NULL, \
78 .ref = NULL, \
79 __VA_ARGS__ \
80 }; \
81 static void _mginit_##mname(void) __attribute__((_CONSTRUCTOR(1000))); \
82 static void _mginit_##mname(void) \
83 { \
84 extern struct memgroup **mg_insert; \
85 _mg_##mname.ref = mg_insert; \
86 *mg_insert = &_mg_##mname; \
87 mg_insert = &_mg_##mname.next; \
88 } \
89 static void _mgfini_##mname(void) __attribute__((_DESTRUCTOR(1000))); \
90 static void _mgfini_##mname(void) \
91 { \
92 if (_mg_##mname.next) \
93 _mg_##mname.next->ref = _mg_##mname.ref; \
94 *_mg_##mname.ref = _mg_##mname.next; \
95 } \
96 MACRO_REQUIRE_SEMICOLON() /* end */
97
98 #define DEFINE_MGROUP(mname, desc) \
99 _DEFINE_MGROUP(mname, desc, )
100 #define DEFINE_MGROUP_ACTIVEATEXIT(mname, desc) \
101 _DEFINE_MGROUP(mname, desc, .active_at_exit = true)
102
103 #define DECLARE_MTYPE(name) \
104 extern struct memtype MTYPE_##name[1] \
105 /* end */
106
107 #define DEFINE_MTYPE_ATTR(group, mname, attr, desc) \
108 attr struct memtype MTYPE_##mname[1] \
109 __attribute__((section(".data.mtypes"))) = { { \
110 .name = desc, \
111 .next = NULL, \
112 .n_alloc = 0, \
113 .size = 0, \
114 .ref = NULL, \
115 } }; \
116 static void _mtinit_##mname(void) __attribute__((_CONSTRUCTOR(1001))); \
117 static void _mtinit_##mname(void) \
118 { \
119 if (_mg_##group.insert == NULL) \
120 _mg_##group.insert = &_mg_##group.types; \
121 MTYPE_##mname->ref = _mg_##group.insert; \
122 *_mg_##group.insert = MTYPE_##mname; \
123 _mg_##group.insert = &MTYPE_##mname->next; \
124 } \
125 static void _mtfini_##mname(void) __attribute__((_DESTRUCTOR(1001))); \
126 static void _mtfini_##mname(void) \
127 { \
128 if (MTYPE_##mname->next) \
129 MTYPE_##mname->next->ref = MTYPE_##mname->ref; \
130 *MTYPE_##mname->ref = MTYPE_##mname->next; \
131 } \
132 MACRO_REQUIRE_SEMICOLON() /* end */
133
134 #define DEFINE_MTYPE(group, name, desc) \
135 DEFINE_MTYPE_ATTR(group, name, , desc) \
136 /* end */
137
138 #define DEFINE_MTYPE_STATIC(group, name, desc) \
139 DEFINE_MTYPE_ATTR(group, name, static, desc) \
140 /* end */
141
142 DECLARE_MGROUP(LIB);
143 DECLARE_MTYPE(TMP);
144
145
146 extern void *qmalloc(struct memtype *mt, size_t size)
147 __attribute__((malloc, _ALLOC_SIZE(2), nonnull(1) _RET_NONNULL));
148 extern void *qcalloc(struct memtype *mt, size_t size)
149 __attribute__((malloc, _ALLOC_SIZE(2), nonnull(1) _RET_NONNULL));
150 extern void *qrealloc(struct memtype *mt, void *ptr, size_t size)
151 __attribute__((_ALLOC_SIZE(3), nonnull(1) _RET_NONNULL));
152 extern void *qstrdup(struct memtype *mt, const char *str)
153 __attribute__((malloc, nonnull(1) _RET_NONNULL));
154 extern void qcountfree(struct memtype *mt, void *ptr)
155 __attribute__((nonnull(1)));
156 extern void qfree(struct memtype *mt, void *ptr) __attribute__((nonnull(1)));
157
158 #define XMALLOC(mtype, size) qmalloc(mtype, size)
159 #define XCALLOC(mtype, size) qcalloc(mtype, size)
160 #define XREALLOC(mtype, ptr, size) qrealloc(mtype, ptr, size)
161 #define XSTRDUP(mtype, str) qstrdup(mtype, str)
162 #define XCOUNTFREE(mtype, ptr) qcountfree(mtype, ptr)
163 #define XFREE(mtype, ptr) \
164 do { \
165 qfree(mtype, ptr); \
166 ptr = NULL; \
167 } while (0)
168
169 static inline size_t mtype_stats_alloc(struct memtype *mt)
170 {
171 return mt->n_alloc;
172 }
173
174 /* NB: calls are ordered by memgroup; and there is a call with mt == NULL for
175 * each memgroup (so that a header can be printed, and empty memgroups show)
176 *
177 * return value: 0: continue, !0: abort walk. qmem_walk will return the
178 * last value from qmem_walk_fn. */
179 typedef int qmem_walk_fn(void *arg, struct memgroup *mg, struct memtype *mt);
180 extern int qmem_walk(qmem_walk_fn *func, void *arg);
181 extern int log_memstats(FILE *fp, const char *);
182 #define log_memstats_stderr(prefix) log_memstats(stderr, prefix)
183
184 extern __attribute__((__noreturn__)) void memory_oom(size_t size,
185 const char *name);
186
187 #ifdef __cplusplus
188 }
189 #endif
190
191 #endif /* _QUAGGA_MEMORY_H */