[systemd-devel] [PATCH] bus: Use memalign() if aligned_alloc() isn't available

Henrik Grindal Bakken hgb at ifi.uio.no
Wed Apr 17 08:49:25 PDT 2013


Use <features.h> and _GNU_SOURCE to determine if _ISOC11_SOURCE is
available, and with that, aligned_alloc().
---
 src/libsystemd-bus/bus-kernel.c |   19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 086877e..bb63a4e 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -19,11 +19,17 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#define _GNU_SOURCE
+#include <features.h>
+
 #ifdef HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
 #endif
 
 #include <fcntl.h>
+#ifndef _ISOC11_SOURCE
+# include <malloc.h> /* For memalign(), if needed. */
+#endif
 
 #include "util.h"
 
@@ -39,6 +45,15 @@
              (uint8_t *)(item) < (uint8_t *)(head) + (head)->size;              \
              item = KDBUS_ITEM_NEXT(item))
 
+static void *align(size_t alignment, size_t size)
+{
+#ifdef _ISOC11_SOURCE
+        return aligned_alloc(alignment, size);
+#else
+        return memalign(alignment, size);
+#endif
+}
+
 static int parse_unique_name(const char *s, uint64_t *id) {
         int r;
 
@@ -199,7 +214,7 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
                 sz += ALIGN8(offsetof(struct kdbus_msg_item, str) + dl + 1);
         }
 
-        m->kdbus = aligned_alloc(8, sz);
+        m->kdbus = align(8, sz);
         if (!m->kdbus)
                 return -ENOMEM;
 
@@ -500,7 +515,7 @@ int bus_kernel_read_message(sd_bus *bus, sd_bus_message **m) {
         for (;;) {
                 void *q;
 
-                q = aligned_alloc(8, sz);
+                q = align(8, sz);
                 if (!q)
                         return -errno;
 
-- 
1.7.10.1



More information about the systemd-devel mailing list