utils: Provide an INIT_EXTRA() macro, that allocates extra data to INIT()

This commit is contained in:
Martin Willi
2015-04-15 11:35:26 +02:00
parent ed6295944b
commit 1ef6c0ef06
+15
View File
@@ -273,6 +273,21 @@ static inline void *memset_noop(void *s, int c, size_t n)
#define INIT(this, ...) { (this) = malloc(sizeof(*(this))); \
*(this) = (typeof(*(this))){ __VA_ARGS__ }; }
/**
* Object allocation/initialization macro, with extra allocated bytes at tail.
*
* The extra space gets zero-initialized.
*
* @param this pointer to object to allocate memory for
* @param extra number of bytes to allocate at end of this
* @param ... initializer
*/
#define INIT_EXTRA(this, extra, ...) { \
typeof(extra) _extra = (extra); \
(this) = malloc(sizeof(*(this)) + _extra); \
*(this) = (typeof(*(this))){ __VA_ARGS__ }; \
memset((this) + 1, 0, _extra); }
/**
* Method declaration/definition macro, providing private and public interface.
*