]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
utils: Provide an INIT_EXTRA() macro, that allocates extra data to INIT()
authorMartin Willi <martin@revosec.ch>
Thu, 5 Jun 2014 13:57:18 +0000 (15:57 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 15 Apr 2015 09:35:26 +0000 (11:35 +0200)
src/libstrongswan/utils/utils.h

index 029a3751826f4dc69731487277f8510df5174cc9..235f283731b86ec42824735a1d365c0490aecba6 100644 (file)
@@ -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.
  *