chiark / gitweb /
util: modernize a few functions with automatic cleanup variables
[elogind.git] / src / shared / macro.h
index 1355aac90879ca287b4f08423287057c1d450624..eb9a8c0a72c1233247b875cd8e8617b093864bfd 100644 (file)
@@ -1,7 +1,6 @@
 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
-#ifndef foomacrohfoo
-#define foomacrohfoo
+#pragma once
 
 /***
   This file is part of systemd.
@@ -45,6 +44,7 @@
 #define _hidden_ __attribute__ ((visibility("hidden")))
 #define _weakref_(x) __attribute__((weakref(#x)))
 #define _introspect_(x) __attribute__((section("introspect." x)))
+#define _alignas_(x) __attribute__((aligned(__alignof(x))))
 
 #define XSTRINGIFY(x) #x
 #define STRINGIFY(x) XSTRINGIFY(x)
@@ -57,6 +57,17 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
 
 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
 
+/*
+ * container_of - cast a member of a structure out to the containing structure
+ * @ptr: the pointer to the member.
+ * @type: the type of the container struct this is embedded in.
+ * @member: the name of the member within the struct.
+ *
+ */
+#define container_of(ptr, type, member) ({ \
+        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+        (type *)( (char *)__mptr - offsetof(type,member) );})
+
 #ifndef MAX
 #define MAX(a,b)                                \
         __extension__ ({                        \
@@ -176,6 +187,8 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
         return k;
 }
 
-#include "log.h"
+#define _cleanup_free_ __attribute__((cleanup(freep)))
+#define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
+#define _cleanup_close_ __attribute__((cleanup(closep)))
 
-#endif
+#include "log.h"