From: Martin Willi Date: Thu, 5 Jun 2014 13:57:18 +0000 (+0200) Subject: utils: Provide an INIT_EXTRA() macro, that allocates extra data to INIT() X-Git-Tag: 5.3.1dr1~17^2~26 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1ef6c0ef0646f4ce3bfcf82fba6fc37767b842fa;p=thirdparty%2Fstrongswan.git utils: Provide an INIT_EXTRA() macro, that allocates extra data to INIT() --- diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h index 029a375182..235f283731 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. *