1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - util.h *
3 * Mupen64Plus homepage: https://mupen64plus.org/ *
4 * Copyright (C) 2012 Mupen64plus development team *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
22#ifndef __LIST_H__
23#define __LIST_H__
24
25#include <stddef.h>
26
27#include "osal/preproc.h"
28
29struct list_head {
30 struct list_head *prev;
31 struct list_head *next;
32};
33
34#define LIST_HEAD(list) \
35 struct list_head list = { &(list), &(list) }
36
37static osal_inline void INIT_LIST_HEAD(struct list_head *head)
38{
39 head->next = head;
40 head->prev = head;
41}
42
43static osal_inline void list_add(struct list_head *new_item, struct list_head *head)
44{
45 struct list_head *next = head->next;
46
47 next->prev = new_item;
48 new_item->next = next;
49 new_item->prev = head;
50 head->next = new_item;
51}
52
53static osal_inline void list_add_tail(struct list_head *new_item, struct list_head *head)
54{
55 struct list_head *prev = head->prev;
56
57 prev->next = new_item;
58 new_item->next = head;
59 new_item->prev = prev;
60 head->prev = new_item;
61}
62
63static osal_inline void list_del(struct list_head *entry)
64{
65 struct list_head *next = entry->next;
66 struct list_head *prev = entry->prev;
67
68 next->prev = prev;
69 prev->next = next;
70}
71
72static osal_inline void list_del_init(struct list_head *entry)
73{
74 list_del(entry);
75 INIT_LIST_HEAD(entry);
76}
77
78static osal_inline int list_empty(const struct list_head *head)
79{
80 return (head->next == head);
81}
82
83#ifdef __GNUC__
84
85#define container_of(ptr, type, member) __extension__ ({ \
86 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
87 (type *)( (char *)__mptr - offsetof(type,member) );})
88
89#else
90
91#define container_of(ptr, type, member) \
92 ((type *)((char *)(ptr) - offsetof(type, member)))
93
94#endif
95
96#define list_entry(ptr, type, member) container_of(ptr, type, member)
97
98#define list_first_entry(ptr, type, member) \
99 list_entry((ptr)->next, type, member)
100
101#define list_for_each(pos, head) \
102 for (pos = (head)->next; pos != (head); pos = pos->next)
103
104#define list_for_each_entry_t(pos, head, type, member) \
105 for (pos = list_entry((head)->next, type, member); \
106 &pos->member != (head); \
107 pos = list_entry(pos->member.next, type, member))
108
109#define list_for_each_safe(pos, safe, head) \
110 for (pos = (head)->next, safe = pos->next; pos != (head); \
111 pos = safe, safe = pos->next)
112
113#define list_for_each_entry_safe_t(pos, safe, head, type, member) \
114 for (pos = list_entry((head)->next, type, member), \
115 safe = list_entry(pos->member.next, type, member); \
116 &pos->member != (head); \
117 pos = safe, \
118 safe = list_entry(safe->member.next, type, member))
119
120#endif
121