summaryrefslogtreecommitdiff
path: root/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'utils.h')
-rw-r--r--utils.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/utils.h b/utils.h
new file mode 100644
index 0000000..548d444
--- /dev/null
+++ b/utils.h
@@ -0,0 +1,104 @@
+
+struct list_head {
+ struct list_head *next;
+ struct list_head *prev;
+};
+
+static inline void list_init(struct list_head *list)
+{
+ list->next = list;
+ list->prev = list;
+}
+
+static inline void list_del(struct list_head *list)
+{
+ list->next->prev = list->prev;
+ list->prev->next = list->next;
+}
+
+static inline void list_add(struct list_head *new, struct list_head *list)
+{
+ list->next->prev = new;
+ new->next = list->next;
+ new->prev = list;
+ list->next = new;
+}
+
+static inline bool list_empty(struct list_head *list)
+{
+ return list->next == list;
+}
+
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+
+#define container_of(ptr, type, member) ({ \
+ const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+ (type *)( (char *)__mptr - offsetof(type,member) );})
+
+#define list_entry(ptr, type, member) \
+ container_of(ptr, type, member)
+
+#define list_first_entry(ptr, type, member) \
+ list_entry((ptr)->next, type, member)
+
+#define list_next_entry(pos, member) \
+ list_entry((pos)->member.next, typeof(*(pos)), member)
+
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+#define list_for_each_entry(pos, head, member) \
+ for (pos = list_first_entry(head, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_next_entry(pos, member))
+
+static inline void* zmalloc(size_t size)
+{
+ return calloc(1, size);
+}
+
+#define _cleanup_(x) __attribute__((cleanup(x)))
+
+#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
+ static inline void func##p(type *p) { \
+ if (*p) \
+ func(*p); \
+ } \
+ struct __useless_struct_to_allow_trailing_semicolon__
+
+
+static inline void sd_bus_close_unrefp(sd_bus **bus) {
+ if (*bus) {
+ sd_bus_flush(*bus);
+ sd_bus_close(*bus);
+ sd_bus_unref(*bus);
+ }
+}
+#define _cleanup_bus_close_unref_ _cleanup_(sd_bus_close_unrefp)
+
+static inline void strv_free(char **l) {
+ char **k;
+ if (l) {
+ for (k = l; *k; k++)
+ free(k);
+ free(l);
+ }
+}
+DEFINE_TRIVIAL_CLEANUP_FUNC(char **, strv_free);
+#define _cleanup_strv_free_ _cleanup_(strv_freep)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(void *, free);
+#define _cleanup_free_ _cleanup_(freep)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus *, sd_bus_unref);
+#define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_slot *, sd_bus_slot_unref);
+#define _cleanup_bus_slot_unref_ _cleanup_(sd_bus_slot_unrefp)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message *, sd_bus_message_unref);
+#define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source *, sd_event_source_unref);
+#define _cleanup_event_source_unref_ _cleanup_(sd_event_source_unrefp)
+