6 FRR includes wrappers around ``malloc()`` and ``free()`` that count the number
7 of objects currently allocated, for each of a defined ``MTYPE``.
9 To this extent, there are *memory groups* and *memory types*. Each memory
10 type must belong to a memory group, this is used just to provide some basic
18 DECLARE_MGROUP(MYDAEMON);
19 DECLARE_MTYPE(MYNEIGHBOR);
24 DEFINE_MGROUP( MYDAEMON, "My daemon's memory");
25 DEFINE_MTYPE( MYDAEMON, MYNEIGHBOR, "Neighbor entry");
26 DEFINE_MTYPE_STATIC(MYDAEMON, MYNEIGHBORNAME, "Neighbor name");
28 struct neigh *neighbor_new(const char *name)
30 struct neigh *n = XMALLOC(MYNEIGHBOR, sizeof(*n));
31 n->name = XSTRDUP(MYNEIGHBORNAME, name);
35 void neighbor_free(struct neigh *n)
37 XFREE(MYNEIGHBORNAME, n->name);
47 This is the (internal) type used for MTYPE definitions. The macros below
48 should be used to create these, but in some cases it is useful to pass a
49 ``struct memtype *`` pointer to some helper function.
51 The ``MTYPE_name`` created by the macros is declared as a pointer, i.e.
52 a function taking a ``struct memtype *`` argument can be called with an
53 ``MTYPE_name`` argument (as opposed to ``&MTYPE_name``.)
57 As ``MTYPE_name`` is a variable assigned from ``&_mt_name`` and not a
58 constant expression, it cannot be used as initializer for static
59 variables. In the case please fall back to ``&_mt_name``.
61 .. c:macro:: DECLARE_MGROUP(name)
63 This macro forward-declares a memory group and should be placed in a
64 ``.h`` file. It expands to an ``extern struct memgroup`` statement.
66 .. c:macro:: DEFINE_MGROUP(mname, description)
68 Defines/implements a memory group. Must be placed into exactly one ``.c``
69 file (multiple inclusion will result in a link-time symbol conflict).
71 Contains additional logic (constructor and destructor) to register the
72 memory group in a global list.
74 .. c:macro:: DECLARE_MTYPE(name)
76 Forward-declares a memory type and makes ``MTYPE_name`` available for use.
77 Note that the ``MTYPE_`` prefix must not be included in the name, it is
78 automatically prefixed.
80 ``MTYPE_name`` is created as a `static const` symbol, i.e. a compile-time
81 constant. It refers to an ``extern struct memtype _mt_name``, where `name`
82 is replaced with the actual name.
84 .. c:macro:: DEFINE_MTYPE(group, name, description)
86 Define/implement a memory type, must be placed into exactly one ``.c``
87 file (multiple inclusion will result in a link-time symbol conflict).
89 Like ``DEFINE_MGROUP``, this contains actual code to register the MTYPE
92 .. c:macro:: DEFINE_MTYPE_STATIC(group, name, description)
94 Same as ``DEFINE_MTYPE``, but the ``DEFINE_MTYPE_STATIC`` variant places
95 the C ``static`` keyword on the definition, restricting the MTYPE's
96 availability to the current source file. This should be appropriate in
101 Daemons currently have ``daemon_memory.[ch]`` files listing all of
102 their MTYPEs. This is not how it should be, most of these types
103 should be moved into the appropriate files where they are used.
104 Only a few MTYPEs should remain non-static after that.
110 .. c:function:: void *XMALLOC(struct memtype *mtype, size_t size)
112 .. c:function:: void *XCALLOC(struct memtype *mtype, size_t size)
114 .. c:function:: void *XSTRDUP(struct memtype *mtype, const char *name)
116 Allocation wrappers for malloc/calloc/realloc/strdup, taking an extra
119 .. c:function:: void *XREALLOC(struct memtype *mtype, void *ptr, size_t size)
121 Wrapper around realloc() with MTYPE tracking. Note that ``ptr`` may
122 be NULL, in which case the function does the same as XMALLOC (regardless
123 of whether the system realloc() supports this.)
125 .. c:function:: void XFREE(struct memtype *mtype, void *ptr)
127 Wrapper around free(), again taking an extra mtype parameter. This is
128 actually a macro, with the following additional properties:
130 - the macro contains ``ptr = NULL``
131 - if ptr is NULL, no operation is performed (as is guaranteed by system
132 implementations.) Do not surround XFREE with ``if (ptr != NULL)``
135 .. c:function:: void XCOUNTFREE(struct memtype *mtype, void *ptr)
137 This macro is used to count the ``ptr`` as freed without actually freeing
138 it. This may be needed in some very specific cases, for example, when the
139 ``ptr`` was allocated using any of the above wrappers and will be freed
140 by some external library using simple ``free()``.