yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev()
[yaffs2.git] / direct / yaffs_list.h
1 /*
2  * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1 as
10  * published by the Free Software Foundation.
11  *
12  * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13  */
14
15 /*
16  * This file is just holds extra declarations of macros that would normally
17  * be providesd in the Linux kernel. These macros have been written from
18  * scratch but are functionally equivalent to the Linux ones.
19  *
20  */
21
22 #ifndef __YAFFS_LIST_H__
23 #define __YAFFS_LIST_H__
24
25
26 /*
27  * This is a simple doubly linked list implementation that matches the
28  * way the Linux kernel doubly linked list implementation works.
29  */
30
31 struct list_head {
32         struct list_head *next; /* next in chain */
33         struct list_head *prev; /* previous in chain */
34 };
35
36
37 /* Initialise a static list */
38 #ifdef LIST_HEAD
39 #undef LIST_HEAD
40 #endif
41
42 #define LIST_HEAD(name) \
43 struct list_head name = { &(name), &(name)}
44
45
46
47 /* Initialise a list head to an empty list */
48 #define INIT_LIST_HEAD(p) \
49 do { \
50         (p)->next = (p);\
51         (p)->prev = (p); \
52 } while (0)
53
54
55 /* Add an element to a list */
56 static inline void list_add(struct list_head *new_entry,
57                                 struct list_head *list)
58 {
59         struct list_head *list_next = list->next;
60
61         list->next = new_entry;
62         new_entry->prev = list;
63         new_entry->next = list_next;
64         list_next->prev = new_entry;
65
66 }
67
68 static inline void list_add_tail(struct list_head *new_entry,
69                                  struct list_head *list)
70 {
71         struct list_head *list_prev = list->prev;
72
73         list->prev = new_entry;
74         new_entry->next = list;
75         new_entry->prev = list_prev;
76         list_prev->next = new_entry;
77
78 }
79
80
81 /* Take an element out of its current list, with or without
82  * reinitialising the links.of the entry*/
83 static inline void list_del(struct list_head *entry)
84 {
85         struct list_head *list_next = entry->next;
86         struct list_head *list_prev = entry->prev;
87
88         list_next->prev = list_prev;
89         list_prev->next = list_next;
90
91 }
92
93 static inline void list_del_init(struct list_head *entry)
94 {
95         list_del(entry);
96         entry->next = entry->prev = entry;
97 }
98
99
100 /* Test if the list is empty */
101 static inline int list_empty(struct list_head *entry)
102 {
103         return (entry->next == entry);
104 }
105
106
107 /* list_entry takes a pointer to a list entry and offsets it to that
108  * we can find a pointer to the object it is embedded in.
109  */
110
111
112 #define list_entry(entry, type, member) \
113         ((type *)((char *)(entry)-(unsigned long)(&((type *)NULL)->member)))
114
115
116 /* list_for_each and list_for_each_safe  iterate over lists.
117  * list_for_each_safe uses temporary storage to make the list delete safe
118  */
119
120 #define list_for_each(itervar, list) \
121         for (itervar = (list)->next; itervar != (list); itervar = itervar->next)
122
123 #define list_for_each_safe(itervar, save_var, list) \
124         for (itervar = (list)->next, save_var = (list)->next->next; \
125                 itervar != (list); \
126                 itervar = save_var, save_var = save_var->next)
127
128
129 #endif