]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
utils: Provide aligning variants of INIT/INIT_EXTRA macros
authorMartin Willi <martin@revosec.ch>
Tue, 31 Mar 2015 15:27:30 +0000 (17:27 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 15 Apr 2015 11:44:40 +0000 (13:44 +0200)
src/libstrongswan/utils/utils.h

index a9791ed77175b6b10a3fe7e1ef70e20a5e29661b..c42f88168700670f819aec2befadb5deeb0e3d97 100644 (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.
  *