]>
Commit | Line | Data |
---|---|---|
2a6a7a65 DL |
1 | .. highlight:: c |
2 | ||
3 | Memtypes | |
4 | ======== | |
5 | ||
6 | FRR includes wrappers arround ``malloc()`` and ``free()`` that count the number | |
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 | ||
45 | .. c:macro:: DECLARE_MGROUP(name) | |
46 | ||
47 | This macro forward-declares a memory group and should be placed in a | |
48 | ``.h`` file. It expands to an ``extern struct memgroup`` statement. | |
49 | ||
50 | .. c:macro:: DEFINE_MGROUP(mname, description) | |
51 | ||
52 | Defines/implements a memory group. Must be placed into exactly one ``.c`` | |
53 | file (multiple inclusion will result in a link-time symbol conflict). | |
54 | ||
55 | Contains additional logic (constructor and destructor) to register the | |
56 | memory group in a global list. | |
57 | ||
58 | .. c:macro:: DECLARE_MTYPE(name) | |
59 | ||
60 | Forward-declares a memory type and makes ``MTYPE_name`` available for use. | |
61 | Note that the ``MTYPE_`` prefix must not be included in the name, it is | |
62 | automatically prefixed. | |
63 | ||
64 | ``MTYPE_name`` is created as a `static const` symbol, i.e. a compile-time | |
65 | constant. It refers to an ``extern struct memtype _mt_name``, where `name` | |
66 | is replaced with the actual name. | |
67 | ||
68 | .. c:macro:: DEFINE_MTYPE(group, name, description) | |
69 | ||
70 | Define/implement a memory type, must be placed into exactly one ``.c`` | |
71 | file (multiple inclusion will result in a link-time symbol conflict). | |
72 | ||
73 | Like ``DEFINE_MGROUP``, this contains actual code to register the MTYPE | |
74 | under its group. | |
75 | ||
76 | .. c:macro:: DEFINE_MTYPE_STATIC(group, name, description) | |
77 | ||
78 | Same as ``DEFINE_MTYPE``, but the ``DEFINE_MTYPE_STATIC`` variant places | |
79 | the C ``static`` keyword on the definition, restricting the MTYPE's | |
80 | availability to the current source file. This should be appropriate in | |
81 | >80% of cases. | |
82 | ||
83 | .. todo:: | |
76bd1499 | 84 | |
2a6a7a65 DL |
85 | Daemons currently have ``daemon_memory.[ch]`` files listing all of |
86 | their MTYPEs. This is not how it should be, most of these types | |
87 | should be moved into the appropriate files where they are used. | |
88 | Only a few MTYPEs should remain non-static after that. | |
89 | ||
90 | ||
91 | Usage | |
92 | ----- | |
93 | ||
94 | .. c:function:: void *XMALLOC(struct memtype *mtype, size_t size) | |
95 | ||
96 | .. c:function:: void *XCALLOC(struct memtype *mtype, size_t size) | |
97 | ||
98 | .. c:function:: void *XSTRDUP(struct memtype *mtype, size_t size) | |
99 | ||
100 | Allocation wrappers for malloc/calloc/realloc/strdup, taking an extra | |
101 | mtype parameter. | |
102 | ||
103 | .. c:function:: void *XREALLOC(struct memtype *mtype, void *ptr, size_t size) | |
104 | ||
105 | Wrapper around realloc() with MTYPE tracking. Note that ``ptr`` may | |
106 | be NULL, in which case the function does the same as XMALLOC (regardless | |
107 | of whether the system realloc() supports this.) | |
108 | ||
109 | .. c:function:: void XFREE(struct memtype *mtype, void *ptr) | |
110 | ||
111 | Wrapper around free(), again taking an extra mtype parameter. This is | |
112 | actually a macro, with the following additional properties: | |
113 | ||
114 | - the macro contains ``ptr = NULL`` | |
115 | - if ptr is NULL, no operation is performed (as is guaranteed by system | |
116 | implementations.) Do not surround XFREE with ``if (ptr != NULL)`` | |
117 | checks. |