diff options
Diffstat (limited to 'lib/memory.h')
| -rw-r--r-- | lib/memory.h | 150 | 
1 files changed, 80 insertions, 70 deletions
diff --git a/lib/memory.h b/lib/memory.h index 9e8803a8b2..d5facad583 100644 --- a/lib/memory.h +++ b/lib/memory.h @@ -37,25 +37,25 @@ struct memgroup {  };  #if defined(__clang__) -# if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 5) +#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 5)  #  define _RET_NONNULL  , returns_nonnull -# endif +#endif  # define _CONSTRUCTOR(x) constructor(x)  #elif defined(__GNUC__) -# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)  #  define _RET_NONNULL  , returns_nonnull -# endif -# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#endif +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)  #  define _CONSTRUCTOR(x) constructor(x)  #  define _DESTRUCTOR(x)  destructor(x)  #  define _ALLOC_SIZE(x)  alloc_size(x) -# endif +#endif  #endif  #ifdef __sun  /* Solaris doesn't do constructor priorities due to linker restrictions */ -# undef _CONSTRUCTOR -# undef _DESTRUCTOR +#undef _CONSTRUCTOR +#undef _DESTRUCTOR  #endif  #ifndef _RET_NONNULL @@ -96,81 +96,91 @@ struct memgroup {   *         but MGROUP_* aren't.   */ -#define DECLARE_MGROUP(name) \ -	extern struct memgroup _mg_##name; -#define DEFINE_MGROUP(mname, desc) \ -	struct memgroup _mg_##mname \ -	__attribute__ ((section (".data.mgroups"))) = { \ -		.name = desc, \ -		.types = NULL, .next = NULL, .insert = NULL, .ref = NULL, \ -	}; \ -	static void _mginit_##mname (void) \ -	  __attribute__ ((_CONSTRUCTOR (1000))); \ -	static void _mginit_##mname (void) \ -	{	extern struct memgroup **mg_insert; \ -		_mg_##mname.ref = mg_insert; \ -		*mg_insert = &_mg_##mname; \ -		mg_insert =  &_mg_##mname.next; } \ -	static void _mgfini_##mname (void) \ -	  __attribute__ ((_DESTRUCTOR (1000))); \ -	static void _mgfini_##mname (void) \ -	{	if (_mg_##mname.next) \ -			_mg_##mname.next->ref = _mg_##mname.ref; \ -		*_mg_##mname.ref = _mg_##mname.next; } - - -#define DECLARE_MTYPE(name) \ -	extern struct memtype _mt_##name; \ -	static struct memtype * const MTYPE_ ## name = &_mt_##name; - -#define DEFINE_MTYPE_ATTR(group, mname, attr, desc) \ -	attr struct memtype _mt_##mname \ -	__attribute__ ((section (".data.mtypes"))) = { \ -		.name = desc, \ -		.next = NULL, .n_alloc = 0, .size = 0, .ref = NULL, \ -	}; \ -	static void _mtinit_##mname (void) \ -	  __attribute__ ((_CONSTRUCTOR (1001))); \ -	static void _mtinit_##mname (void) \ -	{	if (_mg_##group.insert == NULL) \ -			_mg_##group.insert = &_mg_##group.types; \ -		_mt_##mname.ref = _mg_##group.insert; \ -		*_mg_##group.insert = &_mt_##mname; \ -		_mg_##group.insert =  &_mt_##mname.next; } \ -	static void _mtfini_##mname (void) \ -	  __attribute__ ((_DESTRUCTOR (1001))); \ -	static void _mtfini_##mname (void) \ -	{	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) -#define DEFINE_MTYPE_STATIC(group, name, desc) \ -	DEFINE_MTYPE_ATTR(group, name, static, desc) \ -	static struct memtype * const MTYPE_ ## name = &_mt_##name; +#define DECLARE_MGROUP(name) extern struct memgroup _mg_##name; +#define DEFINE_MGROUP(mname, desc)                                             \ +	struct memgroup _mg_##mname                                            \ +		__attribute__((section(".data.mgroups"))) = {                  \ +			.name = desc,                                          \ +			.types = NULL,                                         \ +			.next = NULL,                                          \ +			.insert = NULL,                                        \ +			.ref = NULL,                                           \ +	};                                                                     \ +	static void _mginit_##mname(void) __attribute__((_CONSTRUCTOR(1000))); \ +	static void _mginit_##mname(void)                                      \ +	{                                                                      \ +		extern struct memgroup **mg_insert;                            \ +		_mg_##mname.ref = mg_insert;                                   \ +		*mg_insert = &_mg_##mname;                                     \ +		mg_insert = &_mg_##mname.next;                                 \ +	}                                                                      \ +	static void _mgfini_##mname(void) __attribute__((_DESTRUCTOR(1000)));  \ +	static void _mgfini_##mname(void)                                      \ +	{                                                                      \ +		if (_mg_##mname.next)                                          \ +			_mg_##mname.next->ref = _mg_##mname.ref;               \ +		*_mg_##mname.ref = _mg_##mname.next;                           \ +	} + + +#define DECLARE_MTYPE(name)                                                    \ +	extern struct memtype _mt_##name;                                      \ +	static struct memtype *const MTYPE_##name = &_mt_##name; + +#define DEFINE_MTYPE_ATTR(group, mname, attr, desc)                            \ +	attr struct memtype _mt_##mname                                        \ +		__attribute__((section(".data.mtypes"))) = {                   \ +			.name = desc,                                          \ +			.next = NULL,                                          \ +			.n_alloc = 0,                                          \ +			.size = 0,                                             \ +			.ref = NULL,                                           \ +	};                                                                     \ +	static void _mtinit_##mname(void) __attribute__((_CONSTRUCTOR(1001))); \ +	static void _mtinit_##mname(void)                                      \ +	{                                                                      \ +		if (_mg_##group.insert == NULL)                                \ +			_mg_##group.insert = &_mg_##group.types;               \ +		_mt_##mname.ref = _mg_##group.insert;                          \ +		*_mg_##group.insert = &_mt_##mname;                            \ +		_mg_##group.insert = &_mt_##mname.next;                        \ +	}                                                                      \ +	static void _mtfini_##mname(void) __attribute__((_DESTRUCTOR(1001)));  \ +	static void _mtfini_##mname(void)                                      \ +	{                                                                      \ +		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) +#define DEFINE_MTYPE_STATIC(group, name, desc)                                 \ +	DEFINE_MTYPE_ATTR(group, name, static, desc)                           \ +	static struct memtype *const MTYPE_##name = &_mt_##name;  DECLARE_MGROUP(LIB)  DECLARE_MTYPE(TMP)  extern void *qmalloc(struct memtype *mt, size_t size) -	__attribute__ ((malloc, _ALLOC_SIZE(2), nonnull (1) _RET_NONNULL)); +	__attribute__((malloc, _ALLOC_SIZE(2), nonnull(1) _RET_NONNULL));  extern void *qcalloc(struct memtype *mt, size_t size) -	__attribute__ ((malloc, _ALLOC_SIZE(2), nonnull (1) _RET_NONNULL)); +	__attribute__((malloc, _ALLOC_SIZE(2), nonnull(1) _RET_NONNULL));  extern void *qrealloc(struct memtype *mt, void *ptr, size_t size) -	__attribute__ ((_ALLOC_SIZE(3), nonnull (1) _RET_NONNULL)); -extern void *qstrdup (struct memtype *mt, const char *str) -	__attribute__ ((malloc, nonnull (1) _RET_NONNULL)); -extern void qfree(struct memtype *mt, void *ptr) -	__attribute__ ((nonnull (1))); +	__attribute__((_ALLOC_SIZE(3), nonnull(1) _RET_NONNULL)); +extern void *qstrdup(struct memtype *mt, const char *str) +	__attribute__((malloc, nonnull(1) _RET_NONNULL)); +extern void qfree(struct memtype *mt, void *ptr) __attribute__((nonnull(1)));  #define XMALLOC(mtype, size)		qmalloc(mtype, size)  #define XCALLOC(mtype, size)		qcalloc(mtype, size)  #define XREALLOC(mtype, ptr, size)	qrealloc(mtype, ptr, size)  #define XSTRDUP(mtype, str)		qstrdup(mtype, str) -#define XFREE(mtype, ptr)		do { qfree(mtype, ptr); ptr = NULL; } \ -					while (0) +#define XFREE(mtype, ptr)                                                      \ +	do {                                                                   \ +		qfree(mtype, ptr);                                             \ +		ptr = NULL;                                                    \ +	} while (0)  static inline size_t mtype_stats_alloc(struct memtype *mt)  {  | 
