From: Martin Willi Date: Tue, 31 Mar 2015 15:27:30 +0000 (+0200) Subject: utils: Provide aligning variants of INIT/INIT_EXTRA macros X-Git-Tag: 5.3.1dr1~17^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0c83225883b46f8e6a17532374dc9211ece900a5;p=thirdparty%2Fstrongswan.git utils: Provide aligning variants of INIT/INIT_EXTRA macros --- diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h index a9791ed771..c42f881687 100644 --- a/src/libstrongswan/utils/utils.h +++ b/src/libstrongswan/utils/utils.h @@ -273,6 +273,19 @@ static inline void *memset_noop(void *s, int c, size_t n) #define INIT(this, ...) { (this) = malloc(sizeof(*(this))); \ *(this) = (typeof(*(this))){ __VA_ARGS__ }; } +/** + * Aligning version of INIT(). + * + * The returned pointer must be freed using free_align(), not free(). + * + * @param this object to allocate/initialize + * @param align alignment for allocation, in bytes + * @param ... initializer + */ +#define INIT_ALIGN(this, align, ...) { \ + (this) = malloc_align(sizeof(*(this)), align); \ + *(this) = (typeof(*(this))){ __VA_ARGS__ }; } + /** * Object allocation/initialization macro, with extra allocated bytes at tail. * @@ -288,6 +301,22 @@ static inline void *memset_noop(void *s, int c, size_t n) *(this) = (typeof(*(this))){ __VA_ARGS__ }; \ memset((this) + 1, 0, _extra); } +/** + * Aligning version of INIT_EXTRA(). + * + * The returned pointer must be freed using free_align(), not free(). + * + * @param this object to allocate/initialize + * @param extra number of bytes to allocate at end of this + * @param align alignment for allocation, in bytes + * @param ... initializer + */ +#define INIT_EXTRA_ALIGN(this, extra, align, ...) { \ + typeof(extra) _extra = (extra); \ + (this) = malloc_align(sizeof(*(this)) + _extra, align); \ + *(this) = (typeof(*(this))){ __VA_ARGS__ }; \ + memset((this) + 1, 0, _extra); } + /** * Method declaration/definition macro, providing private and public interface. *