From 056830ba69eb2e513e9148c6e914e1353a556790 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Wed, 19 Jun 2019 08:16:22 +0200 Subject: lib: improve MTYPE definitions The "static struct mtype * const MTYPE_FOO" doesn't quite make a "constant" that is usable for initializers. An 1-element array works better. Signed-off-by: David Lamparter --- doc/developer/memtypes.rst | 10 ++++++++++ lib/memory.h | 23 ++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/doc/developer/memtypes.rst b/doc/developer/memtypes.rst index 153131bab..13f6b43bb 100644 --- a/doc/developer/memtypes.rst +++ b/doc/developer/memtypes.rst @@ -42,6 +42,16 @@ Example: Definition ---------- +.. c:type:: struct memtype + + This is the (internal) type used for MTYPE definitions. The macros below + should be used to create these, but in some cases it is useful to pass a + ``struct memtype *`` pointer to some helper function. + + The ``MTYPE_name`` created by the macros is declared as an array, i.e. + a function taking a ``struct memtype *`` argument can be called with an + ``MTYPE_name`` argument (as opposed to ``&MTYPE_name``.) + .. c:macro:: DECLARE_MGROUP(name) This macro forward-declares a memory group and should be placed in a diff --git a/lib/memory.h b/lib/memory.h index 0002ea334..28c3bb752 100644 --- a/lib/memory.h +++ b/lib/memory.h @@ -102,9 +102,14 @@ struct memgroup { } +/* the array is a trick to make the "MTYPE_FOO" name work as a pointer without + * putting a & in front of it, so we can do "XMALLOC(MTYPE_FOO, ...)" instead + * of "XMALLOC(&MTYPE_FOO, ...)". + */ #define DECLARE_MTYPE(name) \ extern struct memtype _mt_##name; \ - static struct memtype *const MTYPE_##name = &_mt_##name; + extern struct memtype MTYPE_##name[1]; \ + /* end */ #define DEFINE_MTYPE_ATTR(group, mname, attr, desc) \ attr struct memtype _mt_##mname \ @@ -130,12 +135,20 @@ struct memgroup { if (_mt_##mname.next) \ _mt_##mname.next->ref = _mt_##mname.ref; \ *_mt_##mname.ref = _mt_##mname.next; \ - } - -#define DEFINE_MTYPE(group, name, desc) DEFINE_MTYPE_ATTR(group, name, , desc) + } \ + /* end */ + +/* can't quite get gcc to emit the alias correctly, so asm-alias it is :/ */ +#define DEFINE_MTYPE(group, name, desc) \ + DEFINE_MTYPE_ATTR(group, name, , desc) \ + __asm__(".equiv MTYPE_" #name ", _mt_" #name "\n\t" \ + ".global MTYPE_" #name "\n"); \ + /* end */ #define DEFINE_MTYPE_STATIC(group, name, desc) \ DEFINE_MTYPE_ATTR(group, name, static, desc) \ - static struct memtype *const MTYPE_##name = &_mt_##name; + static struct memtype MTYPE_##name[1] \ + __attribute__((alias("_mt_" #name))); \ + /* end */ DECLARE_MGROUP(LIB) DECLARE_MTYPE(TMP) -- cgit v1.2.3