Add simple list implementation based on linux kernel
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 8 May 2017 20:50:25 +0000 (22:50 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Wed, 10 May 2017 18:42:58 +0000 (20:42 +0200)
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
src/util/list.h [new file with mode: 0644]

diff --git a/src/util/list.h b/src/util/list.h
new file mode 100644 (file)
index 0000000..f84901d
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * faultd
+ *
+ * Copyright © 2017 Samsung Electronics
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _FAULTD_LIST_H
+#define _FAULTD_LIST_H
+
+/*
+ * Simple doubly linked list based on list from linux kernel
+ */
+
+/*
+ * Poison pointers for list debugging
+ * compile with FAULTD_DEBUG_LIST
+ * to make a use of them
+ */
+#define LIST_POISON1  ((void *) 0x100)
+#define LIST_POISON2  ((void *) 0x200)
+
+struct list_head {
+       struct list_head *next;
+       struct list_head *prev;
+};
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+static inline void INIT_LIST_HEAD(struct list_head *head)
+{
+       head->next = head;
+       head->prev = head;
+}
+
+/*
+ * Add a new entry between two others
+ *
+ * Should be used only internally
+ */
+static inline void __list_add(struct list_head *new,
+                              struct list_head *prev,
+                              struct list_head *next)
+{
+       next->prev = new;
+       new->next = next;
+       new->prev = prev;
+       prev->next = new;
+}
+
+/*
+ * Add a new entry after the specified head
+ */
+static inline void list_add(struct list_head *new, struct list_head *head)
+{
+       __list_add(new, head, head->next);
+}
+
+/*
+ * Add a new entry before the specified head
+ */
+static inline void list_add_tail(struct list_head *new, struct list_head *head)
+{
+       __list_add(new, head->prev, head);
+}
+
+/*
+ * Delete all entries between prev and next
+ *
+ * Should be used only internally
+ */
+static inline void __list_del(struct list_head *prev, struct list_head *next)
+{
+       next->prev = prev;
+       prev->next = next;
+}
+
+/*
+ * Remove given element from the list
+ *
+ * Should be used only internally
+ */
+static inline void __list_del_entry(struct list_head *entry)
+{
+       __list_del(entry->prev, entry->next);
+}
+
+/*
+ * Remove given element from the list
+ */
+static inline void list_del(struct list_head *entry)
+{
+       __list_del_entry(entry);
+#ifdef FAULTD_DEBUG_LIST
+       entry->next = LIST_POISON1;
+       entry->prev = LIST_POISON2;
+#endif
+}
+
+/*
+ * Remove given element from the list and reinitialize it
+ */
+static inline void list_del_init(struct list_head *entry)
+{
+       __list_del_entry(entry);
+       INIT_LIST_HEAD(entry);
+}
+
+static inline int list_empty(const struct list_head *head)
+{
+       return head->next == head;
+}
+
+static inline int list_is_singular(const struct list_head *head)
+{
+       return !list_empty(head) && (head->next == head->prev);
+}
+
+#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_first_entry_or_null(ptr, type, member) ({           \
+       struct list_head *head__ = (ptr);                                                 \
+       struct list_head *pos__ = head__->next;                                   \
+       pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
+})
+
+#define list_last_entry(ptr, type, member)             \
+       list_entry((ptr)->prev, type, member)
+
+#define list_next_entry(pos, member)                                           \
+       list_entry((pos)->member.next, typeof(*(pos)), member)
+
+#define list_prev_entry(pos, member)                                           \
+       list_entry((pos)->member.prev, typeof(*(pos)), member)
+
+#define list_for_each(pos, head)                                                               \
+       for (pos = (head)->next; pos != (head); pos = pos->next)
+
+#define list_for_each_safe(pos, n, head)                                       \
+       for (pos = (head)->next, n = pos->next; pos != (head);  \
+                pos = n, n = 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))
+
+#define list_for_each_entry_safe(pos, n, head, member)                 \
+       for (pos = list_first_entry(head, typeof(*pos), member),        \
+                        n = list_next_entry(pos, member);                                      \
+            &pos->member != (head);                                                            \
+            pos = n, n = list_next_entry(n, member))
+
+
+#endif /* _FAULTD_LIST_H */