]>
git.proxmox.com Git - mirror_frr.git/blob - lib/memory.h
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
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.
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.
17 #ifndef _QUAGGA_MEMORY_H
18 #define _QUAGGA_MEMORY_H
23 #include <frratomic.h>
30 #if defined(HAVE_MALLOC_SIZE) && !defined(HAVE_MALLOC_USABLE_SIZE)
31 #define malloc_usable_size(x) malloc_size(x)
32 #define HAVE_MALLOC_USABLE_SIZE
37 struct memtype
*next
, **ref
;
39 atomic_size_t n_alloc
;
42 #ifdef HAVE_MALLOC_USABLE_SIZE
44 atomic_size_t max_size
;
49 struct memgroup
*next
, **ref
;
50 struct memtype
*types
, **insert
;
52 /* ignore group on dumping memleaks at exit */
59 * DECLARE_MGROUP(MYDAEMON);
60 * DECLARE_MTYPE(MYDAEMON_COMMON);
63 * DEFINE_MGROUP(MYDAEMON, "my daemon memory");
64 * DEFINE_MTYPE(MYDAEMON, MYDAEMON_COMMON,
65 * "this mtype is used in multiple files in mydaemon");
66 * foo = qmalloc(MTYPE_MYDAEMON_COMMON, sizeof(*foo))
69 * bar = qmalloc(MTYPE_MYDAEMON_COMMON, sizeof(*bar))
71 * DEFINE_MTYPE_STATIC(MYDAEMON, MYDAEMON_IO,
72 * "this mtype is used only in this file");
73 * baz = qmalloc(MTYPE_MYDAEMON_IO, sizeof(*baz))
75 * Note: Naming conventions (MGROUP_ and MTYPE_ prefixes are enforced
76 * by not having these as part of the macro arguments)
77 * Note: MTYPE_* are symbols to the compiler (of type struct memtype *),
78 * but MGROUP_* aren't.
81 #define DECLARE_MGROUP(name) extern struct memgroup _mg_##name
82 #define _DEFINE_MGROUP(mname, desc, ...) \
83 struct memgroup _mg_##mname \
84 __attribute__((section(".data.mgroups"))) = { \
92 static void _mginit_##mname(void) __attribute__((_CONSTRUCTOR(1000))); \
93 static void _mginit_##mname(void) \
95 extern struct memgroup **mg_insert; \
96 _mg_##mname.ref = mg_insert; \
97 *mg_insert = &_mg_##mname; \
98 mg_insert = &_mg_##mname.next; \
100 static void _mgfini_##mname(void) __attribute__((_DESTRUCTOR(1000))); \
101 static void _mgfini_##mname(void) \
103 if (_mg_##mname.next) \
104 _mg_##mname.next->ref = _mg_##mname.ref; \
105 *_mg_##mname.ref = _mg_##mname.next; \
107 MACRO_REQUIRE_SEMICOLON() /* end */
109 #define DEFINE_MGROUP(mname, desc) \
110 _DEFINE_MGROUP(mname, desc, )
111 #define DEFINE_MGROUP_ACTIVEATEXIT(mname, desc) \
112 _DEFINE_MGROUP(mname, desc, .active_at_exit = true)
114 #define DECLARE_MTYPE(name) \
115 extern struct memtype MTYPE_##name[1] \
118 #define DEFINE_MTYPE_ATTR(group, mname, attr, desc) \
119 attr struct memtype MTYPE_##mname[1] \
120 __attribute__((section(".data.mtypes"))) = { { \
127 static void _mtinit_##mname(void) __attribute__((_CONSTRUCTOR(1001))); \
128 static void _mtinit_##mname(void) \
130 if (_mg_##group.insert == NULL) \
131 _mg_##group.insert = &_mg_##group.types; \
132 MTYPE_##mname->ref = _mg_##group.insert; \
133 *_mg_##group.insert = MTYPE_##mname; \
134 _mg_##group.insert = &MTYPE_##mname->next; \
136 static void _mtfini_##mname(void) __attribute__((_DESTRUCTOR(1001))); \
137 static void _mtfini_##mname(void) \
139 if (MTYPE_##mname->next) \
140 MTYPE_##mname->next->ref = MTYPE_##mname->ref; \
141 *MTYPE_##mname->ref = MTYPE_##mname->next; \
143 MACRO_REQUIRE_SEMICOLON() /* end */
145 #define DEFINE_MTYPE(group, name, desc) \
146 DEFINE_MTYPE_ATTR(group, name, , desc) \
149 #define DEFINE_MTYPE_STATIC(group, name, desc) \
150 DEFINE_MTYPE_ATTR(group, name, static, desc) \
157 extern void *qmalloc(struct memtype
*mt
, size_t size
)
158 __attribute__((malloc
, _ALLOC_SIZE(2), nonnull(1) _RET_NONNULL
));
159 extern void *qcalloc(struct memtype
*mt
, size_t size
)
160 __attribute__((malloc
, _ALLOC_SIZE(2), nonnull(1) _RET_NONNULL
));
161 extern void *qrealloc(struct memtype
*mt
, void *ptr
, size_t size
)
162 __attribute__((_ALLOC_SIZE(3), nonnull(1) _RET_NONNULL
));
163 extern void *qstrdup(struct memtype
*mt
, const char *str
)
164 __attribute__((malloc
, nonnull(1) _RET_NONNULL
));
165 extern void qcountfree(struct memtype
*mt
, void *ptr
)
166 __attribute__((nonnull(1)));
167 extern void qfree(struct memtype
*mt
, void *ptr
) __attribute__((nonnull(1)));
169 #define XMALLOC(mtype, size) qmalloc(mtype, size)
170 #define XCALLOC(mtype, size) qcalloc(mtype, size)
171 #define XREALLOC(mtype, ptr, size) qrealloc(mtype, ptr, size)
172 #define XSTRDUP(mtype, str) qstrdup(mtype, str)
173 #define XCOUNTFREE(mtype, ptr) qcountfree(mtype, ptr)
174 #define XFREE(mtype, ptr) \
180 static inline size_t mtype_stats_alloc(struct memtype
*mt
)
185 /* NB: calls are ordered by memgroup; and there is a call with mt == NULL for
186 * each memgroup (so that a header can be printed, and empty memgroups show)
188 * return value: 0: continue, !0: abort walk. qmem_walk will return the
189 * last value from qmem_walk_fn. */
190 typedef int qmem_walk_fn(void *arg
, struct memgroup
*mg
, struct memtype
*mt
);
191 extern int qmem_walk(qmem_walk_fn
*func
, void *arg
);
192 extern int log_memstats(FILE *fp
, const char *);
193 #define log_memstats_stderr(prefix) log_memstats(stderr, prefix)
195 extern __attribute__((__noreturn__
)) void memory_oom(size_t size
,
202 #endif /* _QUAGGA_MEMORY_H */