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