| 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 2 | * Mupen64plus - util.h * |
| 3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
| 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 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | #include <stddef.h> |
| 30 | #include "osal/preproc.h" |
| 31 | |
| 32 | struct list_head { |
| 33 | struct list_head *prev; |
| 34 | struct list_head *next; |
| 35 | }; |
| 36 | |
| 37 | #define LIST_HEAD(list) \ |
| 38 | struct list_head list = { &(list), &(list) } |
| 39 | |
| 40 | static osal_inline void INIT_LIST_HEAD(struct list_head *head) |
| 41 | { |
| 42 | head->next = head; |
| 43 | head->prev = head; |
| 44 | } |
| 45 | |
| 46 | static osal_inline void list_add(struct list_head *new_item, struct list_head *head) |
| 47 | { |
| 48 | struct list_head *next = head->next; |
| 49 | |
| 50 | next->prev = new_item; |
| 51 | new_item->next = next; |
| 52 | new_item->prev = head; |
| 53 | head->next = new_item; |
| 54 | } |
| 55 | |
| 56 | static osal_inline void list_add_tail(struct list_head *new_item, struct list_head *head) |
| 57 | { |
| 58 | struct list_head *prev = head->prev; |
| 59 | |
| 60 | prev->next = new_item; |
| 61 | new_item->next = head; |
| 62 | new_item->prev = prev; |
| 63 | head->prev = new_item; |
| 64 | } |
| 65 | |
| 66 | static osal_inline void list_del(struct list_head *entry) |
| 67 | { |
| 68 | struct list_head *next = entry->next; |
| 69 | struct list_head *prev = entry->prev; |
| 70 | |
| 71 | next->prev = prev; |
| 72 | prev->next = next; |
| 73 | } |
| 74 | |
| 75 | static osal_inline void list_del_init(struct list_head *entry) |
| 76 | { |
| 77 | list_del(entry); |
| 78 | INIT_LIST_HEAD(entry); |
| 79 | } |
| 80 | |
| 81 | static osal_inline int list_empty(const struct list_head *head) |
| 82 | { |
| 83 | return (head->next == head); |
| 84 | } |
| 85 | |
| 86 | #ifdef __GNUC__ |
| 87 | |
| 88 | #define container_of(ptr, type, member) ({ \ |
| 89 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 90 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 91 | |
| 92 | #else |
| 93 | |
| 94 | #define container_of(ptr, type, member) \ |
| 95 | ((type *)((char *)(ptr) - offsetof(type, member))) |
| 96 | |
| 97 | #endif |
| 98 | |
| 99 | #define list_entry(ptr, type, member) container_of(ptr, type, member) |
| 100 | |
| 101 | #define list_first_entry(ptr, type, member) \ |
| 102 | list_entry((ptr)->next, type, member) |
| 103 | |
| 104 | #define list_for_each(pos, head) \ |
| 105 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 106 | |
| 107 | #define list_for_each_entry(pos, head, type, member) \ |
| 108 | for (pos = list_entry((head)->next, type, member); \ |
| 109 | &pos->member != (head); \ |
| 110 | pos = list_entry(pos->member.next, type, member)) |
| 111 | |
| 112 | #define list_for_each_safe(pos, safe, head) \ |
| 113 | for (pos = (head)->next, safe = pos->next; pos != (head); \ |
| 114 | pos = safe, safe = pos->next) |
| 115 | |
| 116 | #define list_for_each_entry_safe(pos, safe, head, type, member) \ |
| 117 | for (pos = list_entry((head)->next, type, member), \ |
| 118 | safe = list_entry(pos->member.next, type, member); \ |
| 119 | &pos->member != (head); \ |
| 120 | pos = safe, \ |
| 121 | safe = list_entry(safe->member.next, type, member)) |
| 122 | |
| 123 | #ifdef __cplusplus |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | #endif |