From 1ef6c0ef0646f4ce3bfcf82fba6fc37767b842fa Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Thu, 5 Jun 2014 15:57:18 +0200 Subject: [PATCH] utils: Provide an INIT_EXTRA() macro, that allocates extra data to INIT() --- src/libstrongswan/utils/utils.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h index 029a37518..235f28373 100644 --- a/src/libstrongswan/utils/utils.h +++ b/src/libstrongswan/utils/utils.h @@ -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. * -- 2.39.2