]>
Commit | Line | Data |
---|---|---|
2a6a7a65 DL |
1 | .. highlight:: c |
2 | ||
3 | Memtypes | |
4 | ======== | |
5 | ||
56f0bea7 | 6 | FRR includes wrappers around ``malloc()`` and ``free()`` that count the number |
2a6a7a65 DL |
7 | of objects currently allocated, for each of a defined ``MTYPE``. |
8 | ||
75ca3b11 | 9 | To this extent, there are *memory groups* and *memory types*. Each memory |
2a6a7a65 DL |
10 | type must belong to a memory group, this is used just to provide some basic |
11 | structure. | |
12 | ||
13 | Example: | |
14 | ||
15 | .. code-block:: c | |
75ca3b11 | 16 | :caption: mydaemon.h |
2a6a7a65 | 17 | |
75ca3b11 QY |
18 | DECLARE_MGROUP(MYDAEMON) |
19 | DECLARE_MTYPE(MYNEIGHBOR) | |
2a6a7a65 DL |
20 | |
21 | .. code-block:: c | |
75ca3b11 QY |
22 | :caption: mydaemon.c |
23 | ||
24 | DEFINE_MGROUP( MYDAEMON, "My daemon's memory") | |
25 | DEFINE_MTYPE( MYDAEMON, MYNEIGHBOR, "Neighbor entry") | |
26 | DEFINE_MTYPE_STATIC(MYDAEMON, MYNEIGHBORNAME, "Neighbor name") | |
27 | ||
28 | struct neigh *neighbor_new(const char *name) | |
29 | { | |
30 | struct neigh *n = XMALLOC(MYNEIGHBOR, sizeof(*n)); | |
31 | n->name = XSTRDUP(MYNEIGHBORNAME, name); | |
32 | return n; | |
33 | } | |
34 | ||
35 | void neighbor_free(struct neigh *n) | |
36 | { | |
37 | XFREE(MYNEIGHBORNAME, n->name); | |
38 | XFREE(MYNEIGHBOR, n); | |
39 | } | |
2a6a7a65 DL |
40 | |
41 | ||
42 | Definition | |
43 | ---------- | |
44 | ||
056830ba DL |
45 | .. c:type:: struct memtype |
46 | ||
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. | |
50 | ||
51 | The ``MTYPE_name`` created by the macros is declared as an array, i.e. | |
52 | a function taking a ``struct memtype *`` argument can be called with an | |
53 | ``MTYPE_name`` argument (as opposed to ``&MTYPE_name``.) | |
54 | ||
2a6a7a65 DL |
55 | .. c:macro:: DECLARE_MGROUP(name) |
56 | ||
57 | This macro forward-declares a memory group and should be placed in a | |
58 | ``.h`` file. It expands to an ``extern struct memgroup`` statement. | |
59 | ||
60 | .. c:macro:: DEFINE_MGROUP(mname, description) | |
61 | ||
62 | Defines/implements a memory group. Must be placed into exactly one ``.c`` | |
63 | file (multiple inclusion will result in a link-time symbol conflict). | |
64 | ||
65 | Contains additional logic (constructor and destructor) to register the | |
66 | memory group in a global list. | |
67 | ||
68 | .. c:macro:: DECLARE_MTYPE(name) | |
69 | ||
70 | Forward-declares a memory type and makes ``MTYPE_name`` available for use. | |
71 | Note that the ``MTYPE_`` prefix must not be included in the name, it is | |
72 | automatically prefixed. | |
73 | ||
74 | ``MTYPE_name`` is created as a `static const` symbol, i.e. a compile-time | |
75 | constant. It refers to an ``extern struct memtype _mt_name``, where `name` | |
76 | is replaced with the actual name. | |
77 | ||
78 | .. c:macro:: DEFINE_MTYPE(group, name, description) | |
79 | ||
80 | Define/implement a memory type, must be placed into exactly one ``.c`` | |
81 | file (multiple inclusion will result in a link-time symbol conflict). | |
82 | ||
83 | Like ``DEFINE_MGROUP``, this contains actual code to register the MTYPE | |
84 | under its group. | |
85 | ||
86 | .. c:macro:: DEFINE_MTYPE_STATIC(group, name, description) | |
87 | ||
88 | Same as ``DEFINE_MTYPE``, but the ``DEFINE_MTYPE_STATIC`` variant places | |
89 | the C ``static`` keyword on the definition, restricting the MTYPE's | |
90 | availability to the current source file. This should be appropriate in | |
91 | >80% of cases. | |
92 | ||
93 | .. todo:: | |
76bd1499 | 94 | |
2a6a7a65 DL |
95 | Daemons currently have ``daemon_memory.[ch]`` files listing all of |
96 | their MTYPEs. This is not how it should be, most of these types | |
97 | should be moved into the appropriate files where they are used. | |
98 | Only a few MTYPEs should remain non-static after that. | |
99 | ||
100 | ||
101 | Usage | |
102 | ----- | |
103 | ||
104 | .. c:function:: void *XMALLOC(struct memtype *mtype, size_t size) | |
105 | ||
106 | .. c:function:: void *XCALLOC(struct memtype *mtype, size_t size) | |
107 | ||
5ba33197 | 108 | .. c:function:: void *XSTRDUP(struct memtype *mtype, const char *name) |
2a6a7a65 DL |
109 | |
110 | Allocation wrappers for malloc/calloc/realloc/strdup, taking an extra | |
111 | mtype parameter. | |
112 | ||
113 | .. c:function:: void *XREALLOC(struct memtype *mtype, void *ptr, size_t size) | |
114 | ||
115 | Wrapper around realloc() with MTYPE tracking. Note that ``ptr`` may | |
116 | be NULL, in which case the function does the same as XMALLOC (regardless | |
117 | of whether the system realloc() supports this.) | |
118 | ||
119 | .. c:function:: void XFREE(struct memtype *mtype, void *ptr) | |
120 | ||
121 | Wrapper around free(), again taking an extra mtype parameter. This is | |
122 | actually a macro, with the following additional properties: | |
123 | ||
124 | - the macro contains ``ptr = NULL`` | |
125 | - if ptr is NULL, no operation is performed (as is guaranteed by system | |
126 | implementations.) Do not surround XFREE with ``if (ptr != NULL)`` | |
127 | checks. |