utils: Provide aligning variants of INIT/INIT_EXTRA macros

This commit is contained in:
Martin Willi 2015-03-31 17:27:30 +02:00
parent 2b40c9646c
commit 0c83225883
1 changed files with 29 additions and 0 deletions

View File

@ -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.
*