list.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /**
  2. *
  3. * I grub it from linux kernel source code and fix it for user space
  4. * program. Of course, this is a GPL licensed header file.
  5. *
  6. * Here is a recipe to cook list.h for user space program
  7. *
  8. * 1. copy list.h from linux/include/list.h
  9. * 2. remove
  10. * - #ifdef __KERNE__ and its #endif
  11. * - all #include line
  12. * - prefetch() and rcu related functions
  13. * 3. add macro offsetof() and container_of
  14. *
  15. * - kazutomo@mcs.anl.gov
  16. */
  17. #ifndef _LINUX_LIST_H
  18. #define _LINUX_LIST_H
  19. /**
  20. * @name from other kernel headers
  21. */
  22. /*@{*/
  23. /**
  24. * Get offset of a member
  25. */
  26. #ifndef offsetof // stddef.h contain it
  27. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  28. #endif
  29. /**
  30. * Casts a member of a structure out to the containing structure
  31. * @param ptr the pointer to the member.
  32. * @param type the type of the container struct this is embedded in.
  33. * @param member the name of the member within the struct.
  34. *
  35. */
  36. #define container_of(ptr, type, member) ({ \
  37. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  38. (type *)( (char *)__mptr - offsetof(type,member) );})
  39. /*@}*/
  40. /*
  41. * These are non-NULL pointers that will result in page faults
  42. * under normal circumstances, used to verify that nobody uses
  43. * non-initialized list entries.
  44. */
  45. #define LIST_POISON1 ((void *) 0x00100100)
  46. #define LIST_POISON2 ((void *) 0x00200200)
  47. /**
  48. * Simple doubly linked list implementation.
  49. *
  50. * Some of the internal functions ("__xxx") are useful when
  51. * manipulating whole lists rather than single entries, as
  52. * sometimes we already know the next/prev entries and we can
  53. * generate better code by using them directly rather than
  54. * using the generic single-entry routines.
  55. */
  56. struct list_head {
  57. struct list_head *next, *prev;
  58. };
  59. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  60. #define LIST_HEAD(name) \
  61. struct list_head name = LIST_HEAD_INIT(name)
  62. #define INIT_LIST_HEAD(ptr) do { \
  63. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  64. } while (0)
  65. /*
  66. * Insert a new entry between two known consecutive entries.
  67. *
  68. * This is only for internal list manipulation where we know
  69. * the prev/next entries already!
  70. */
  71. static inline void __list_add(struct list_head *new,
  72. struct list_head *prev,
  73. struct list_head *next)
  74. {
  75. next->prev = new;
  76. new->next = next;
  77. new->prev = prev;
  78. prev->next = new;
  79. }
  80. /**
  81. * list_add - add a new entry
  82. * @new: new entry to be added
  83. * @head: list head to add it after
  84. *
  85. * Insert a new entry after the specified head.
  86. * This is good for implementing stacks.
  87. */
  88. static inline void list_add(struct list_head *new, struct list_head *head)
  89. {
  90. __list_add(new, head, head->next);
  91. }
  92. /**
  93. * list_add_tail - add a new entry
  94. * @new: new entry to be added
  95. * @head: list head to add it before
  96. *
  97. * Insert a new entry before the specified head.
  98. * This is useful for implementing queues.
  99. */
  100. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  101. {
  102. __list_add(new, head->prev, head);
  103. }
  104. /*
  105. * Delete a list entry by making the prev/next entries
  106. * point to each other.
  107. *
  108. * This is only for internal list manipulation where we know
  109. * the prev/next entries already!
  110. */
  111. static inline void __list_del(struct list_head * prev, struct list_head * next)
  112. {
  113. next->prev = prev;
  114. prev->next = next;
  115. }
  116. /**
  117. * list_del - deletes entry from list.
  118. * @entry: the element to delete from the list.
  119. * Note: list_empty on entry does not return true after this, the entry is
  120. * in an undefined state.
  121. */
  122. static inline void list_del(struct list_head *entry)
  123. {
  124. __list_del(entry->prev, entry->next);
  125. entry->next = LIST_POISON1;
  126. entry->prev = LIST_POISON2;
  127. }
  128. /**
  129. * list_del_init - deletes entry from list and reinitialize it.
  130. * @entry: the element to delete from the list.
  131. */
  132. static inline void list_del_init(struct list_head *entry)
  133. {
  134. __list_del(entry->prev, entry->next);
  135. INIT_LIST_HEAD(entry);
  136. }
  137. /**
  138. * list_move - delete from one list and add as another's head
  139. * @list: the entry to move
  140. * @head: the head that will precede our entry
  141. */
  142. static inline void list_move(struct list_head *list, struct list_head *head)
  143. {
  144. __list_del(list->prev, list->next);
  145. list_add(list, head);
  146. }
  147. /**
  148. * list_move_tail - delete from one list and add as another's tail
  149. * @list: the entry to move
  150. * @head: the head that will follow our entry
  151. */
  152. static inline void list_move_tail(struct list_head *list,
  153. struct list_head *head)
  154. {
  155. __list_del(list->prev, list->next);
  156. list_add_tail(list, head);
  157. }
  158. /**
  159. * list_empty - tests whether a list is empty
  160. * @head: the list to test.
  161. */
  162. static inline int list_empty(const struct list_head *head)
  163. {
  164. return head->next == head;
  165. }
  166. static inline void __list_splice(struct list_head *list,
  167. struct list_head *head)
  168. {
  169. struct list_head *first = list->next;
  170. struct list_head *last = list->prev;
  171. struct list_head *at = head->next;
  172. first->prev = head;
  173. head->next = first;
  174. last->next = at;
  175. at->prev = last;
  176. }
  177. /**
  178. * list_splice - join two lists
  179. * @list: the new list to add.
  180. * @head: the place to add it in the first list.
  181. */
  182. static inline void list_splice(struct list_head *list, struct list_head *head)
  183. {
  184. if (!list_empty(list))
  185. __list_splice(list, head);
  186. }
  187. /**
  188. * list_splice_init - join two lists and reinitialise the emptied list.
  189. * @list: the new list to add.
  190. * @head: the place to add it in the first list.
  191. *
  192. * The list at @list is reinitialised
  193. */
  194. static inline void list_splice_init(struct list_head *list,
  195. struct list_head *head)
  196. {
  197. if (!list_empty(list)) {
  198. __list_splice(list, head);
  199. INIT_LIST_HEAD(list);
  200. }
  201. }
  202. /**
  203. * list_entry - get the struct for this entry
  204. * @ptr: the &struct list_head pointer.
  205. * @type: the type of the struct this is embedded in.
  206. * @member: the name of the list_struct within the struct.
  207. */
  208. #define list_entry(ptr, type, member) \
  209. container_of(ptr, type, member)
  210. /**
  211. * list_for_each - iterate over a list
  212. * @pos: the &struct list_head to use as a loop counter.
  213. * @head: the head for your list.
  214. */
  215. #define list_for_each(pos, head) \
  216. for (pos = (head)->next; pos != (head); \
  217. pos = pos->next)
  218. /**
  219. * __list_for_each - iterate over a list
  220. * @pos: the &struct list_head to use as a loop counter.
  221. * @head: the head for your list.
  222. *
  223. * This variant differs from list_for_each() in that it's the
  224. * simplest possible list iteration code, no prefetching is done.
  225. * Use this for code that knows the list to be very short (empty
  226. * or 1 entry) most of the time.
  227. */
  228. #define __list_for_each(pos, head) \
  229. for (pos = (head)->next; pos != (head); pos = pos->next)
  230. /**
  231. * list_for_each_prev - iterate over a list backwards
  232. * @pos: the &struct list_head to use as a loop counter.
  233. * @head: the head for your list.
  234. */
  235. #define list_for_each_prev(pos, head) \
  236. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  237. pos = pos->prev)
  238. /**
  239. * list_for_each_safe - iterate over a list safe against removal of list entry
  240. * @pos: the &struct list_head to use as a loop counter.
  241. * @n: another &struct list_head to use as temporary storage
  242. * @head: the head for your list.
  243. */
  244. #define list_for_each_safe(pos, n, head) \
  245. for (pos = (head)->next, n = pos->next; pos != (head); \
  246. pos = n, n = pos->next)
  247. /**
  248. * list_for_each_entry - iterate over list of given type
  249. * @pos: the type * to use as a loop counter.
  250. * @head: the head for your list.
  251. * @member: the name of the list_struct within the struct.
  252. */
  253. #define list_for_each_entry(pos, head, member) \
  254. for (pos = list_entry((head)->next, typeof(*pos), member); \
  255. &pos->member != (head); \
  256. pos = list_entry(pos->member.next, typeof(*pos), member))
  257. /**
  258. * list_for_each_entry_reverse - iterate backwards over list of given type.
  259. * @pos: the type * to use as a loop counter.
  260. * @head: the head for your list.
  261. * @member: the name of the list_struct within the struct.
  262. */
  263. #define list_for_each_entry_reverse(pos, head, member) \
  264. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  265. &pos->member != (head); \
  266. pos = list_entry(pos->member.prev, typeof(*pos), member))
  267. /**
  268. * list_prepare_entry - prepare a pos entry for use as a start point in
  269. * list_for_each_entry_continue
  270. * @pos: the type * to use as a start point
  271. * @head: the head of the list
  272. * @member: the name of the list_struct within the struct.
  273. */
  274. #define list_prepare_entry(pos, head, member) \
  275. ((pos) ? : list_entry(head, typeof(*pos), member))
  276. /**
  277. * list_for_each_entry_continue - iterate over list of given type
  278. * continuing after existing point
  279. * @pos: the type * to use as a loop counter.
  280. * @head: the head for your list.
  281. * @member: the name of the list_struct within the struct.
  282. */
  283. #define list_for_each_entry_continue(pos, head, member) \
  284. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  285. &pos->member != (head); \
  286. pos = list_entry(pos->member.next, typeof(*pos), member))
  287. /**
  288. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  289. * @pos: the type * to use as a loop counter.
  290. * @n: another type * to use as temporary storage
  291. * @head: the head for your list.
  292. * @member: the name of the list_struct within the struct.
  293. */
  294. #define list_for_each_entry_safe(pos, n, head, member) \
  295. for (pos = list_entry((head)->next, typeof(*pos), member), \
  296. n = list_entry(pos->member.next, typeof(*pos), member); \
  297. &pos->member != (head); \
  298. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  299. /**
  300. * list_for_each_entry_safe_continue - iterate over list of given type
  301. * continuing after existing point safe against removal of list entry
  302. * @pos: the type * to use as a loop counter.
  303. * @n: another type * to use as temporary storage
  304. * @head: the head for your list.
  305. * @member: the name of the list_struct within the struct.
  306. */
  307. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  308. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  309. n = list_entry(pos->member.next, typeof(*pos), member); \
  310. &pos->member != (head); \
  311. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  312. /**
  313. * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
  314. * removal of list entry
  315. * @pos: the type * to use as a loop counter.
  316. * @n: another type * to use as temporary storage
  317. * @head: the head for your list.
  318. * @member: the name of the list_struct within the struct.
  319. */
  320. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  321. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  322. n = list_entry(pos->member.prev, typeof(*pos), member); \
  323. &pos->member != (head); \
  324. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  325. /*
  326. * Double linked lists with a single pointer list head.
  327. * Mostly useful for hash tables where the two pointer list head is
  328. * too wasteful.
  329. * You lose the ability to access the tail in O(1).
  330. */
  331. struct hlist_head {
  332. struct hlist_node *first;
  333. };
  334. struct hlist_node {
  335. struct hlist_node *next, **pprev;
  336. };
  337. #define HLIST_HEAD_INIT { .first = NULL }
  338. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  339. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  340. #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
  341. static inline int hlist_unhashed(const struct hlist_node *h)
  342. {
  343. return !h->pprev;
  344. }
  345. static inline int hlist_empty(const struct hlist_head *h)
  346. {
  347. return !h->first;
  348. }
  349. static inline void __hlist_del(struct hlist_node *n)
  350. {
  351. struct hlist_node *next = n->next;
  352. struct hlist_node **pprev = n->pprev;
  353. *pprev = next;
  354. if (next)
  355. next->pprev = pprev;
  356. }
  357. static inline void hlist_del(struct hlist_node *n)
  358. {
  359. __hlist_del(n);
  360. n->next = LIST_POISON1;
  361. n->pprev = LIST_POISON2;
  362. }
  363. static inline void hlist_del_init(struct hlist_node *n)
  364. {
  365. if (n->pprev) {
  366. __hlist_del(n);
  367. INIT_HLIST_NODE(n);
  368. }
  369. }
  370. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  371. {
  372. struct hlist_node *first = h->first;
  373. n->next = first;
  374. if (first)
  375. first->pprev = &n->next;
  376. h->first = n;
  377. n->pprev = &h->first;
  378. }
  379. /* next must be != NULL */
  380. static inline void hlist_add_before(struct hlist_node *n,
  381. struct hlist_node *next)
  382. {
  383. n->pprev = next->pprev;
  384. n->next = next;
  385. next->pprev = &n->next;
  386. *(n->pprev) = n;
  387. }
  388. static inline void hlist_add_after(struct hlist_node *n,
  389. struct hlist_node *next)
  390. {
  391. next->next = n->next;
  392. n->next = next;
  393. next->pprev = &n->next;
  394. if(next->next)
  395. next->next->pprev = &next->next;
  396. }
  397. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  398. #define hlist_for_each(pos, head) \
  399. for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  400. pos = pos->next)
  401. #define hlist_for_each_safe(pos, n, head) \
  402. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  403. pos = n)
  404. /**
  405. * hlist_for_each_entry - iterate over list of given type
  406. * @tpos: the type * to use as a loop counter.
  407. * @pos: the &struct hlist_node to use as a loop counter.
  408. * @head: the head for your list.
  409. * @member: the name of the hlist_node within the struct.
  410. */
  411. #define hlist_for_each_entry(tpos, pos, head, member) \
  412. for (pos = (head)->first; \
  413. pos && ({ prefetch(pos->next); 1;}) && \
  414. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  415. pos = pos->next)
  416. /**
  417. * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
  418. * @tpos: the type * to use as a loop counter.
  419. * @pos: the &struct hlist_node to use as a loop counter.
  420. * @member: the name of the hlist_node within the struct.
  421. */
  422. #define hlist_for_each_entry_continue(tpos, pos, member) \
  423. for (pos = (pos)->next; \
  424. pos && ({ prefetch(pos->next); 1;}) && \
  425. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  426. pos = pos->next)
  427. /**
  428. * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
  429. * @tpos: the type * to use as a loop counter.
  430. * @pos: the &struct hlist_node to use as a loop counter.
  431. * @member: the name of the hlist_node within the struct.
  432. */
  433. #define hlist_for_each_entry_from(tpos, pos, member) \
  434. for (; pos && ({ prefetch(pos->next); 1;}) && \
  435. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  436. pos = pos->next)
  437. /**
  438. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  439. * @tpos: the type * to use as a loop counter.
  440. * @pos: the &struct hlist_node to use as a loop counter.
  441. * @n: another &struct hlist_node to use as temporary storage
  442. * @head: the head for your list.
  443. * @member: the name of the hlist_node within the struct.
  444. */
  445. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
  446. for (pos = (head)->first; \
  447. pos && ({ n = pos->next; 1; }) && \
  448. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  449. pos = n)
  450. #endif