yaffs_list.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License version 2.1 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  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. #ifndef __YAFFS_LIST_H__
  22. #define __YAFFS_LIST_H__
  23. /*
  24. * This is a simple doubly linked list implementation that matches the
  25. * way the Linux kernel doubly linked list implementation works.
  26. */
  27. struct list_head {
  28. struct list_head *next; /* next in chain */
  29. struct list_head *prev; /* previous in chain */
  30. };
  31. /* Initialise a static list */
  32. #define LIST_HEAD(name) \
  33. struct list_head name = { &(name), &(name)}
  34. /* Initialise a list head to an empty list */
  35. #define INIT_LIST_HEAD(p) \
  36. do { \
  37. (p)->next = (p);\
  38. (p)->prev = (p); \
  39. } while (0)
  40. /* Add an element to a list */
  41. static inline void list_add(struct list_head *new_entry,
  42. struct list_head *list)
  43. {
  44. struct list_head *list_next = list->next;
  45. list->next = new_entry;
  46. new_entry->prev = list;
  47. new_entry->next = list_next;
  48. list_next->prev = new_entry;
  49. }
  50. static inline void list_add_tail(struct list_head *new_entry,
  51. struct list_head *list)
  52. {
  53. struct list_head *list_prev = list->prev;
  54. list->prev = new_entry;
  55. new_entry->next = list;
  56. new_entry->prev = list_prev;
  57. list_prev->next = new_entry;
  58. }
  59. /* Take an element out of its current list, with or without
  60. * reinitialising the links.of the entry*/
  61. static inline void list_del(struct list_head *entry)
  62. {
  63. struct list_head *list_next = entry->next;
  64. struct list_head *list_prev = entry->prev;
  65. list_next->prev = list_prev;
  66. list_prev->next = list_next;
  67. }
  68. static inline void list_del_init(struct list_head *entry)
  69. {
  70. list_del(entry);
  71. entry->next = entry->prev = entry;
  72. }
  73. /* Test if the list is empty */
  74. static inline int list_empty(struct list_head *entry)
  75. {
  76. return (entry->next == entry);
  77. }
  78. /* list_entry takes a pointer to a list entry and offsets it to that
  79. * we can find a pointer to the object it is embedded in.
  80. */
  81. #define list_entry(entry, type, member) \
  82. ((type *)((char *)(entry)-(unsigned long)(&((type *)NULL)->member)))
  83. /* list_for_each and list_for_each_safe iterate over lists.
  84. * list_for_each_safe uses temporary storage to make the list delete safe
  85. */
  86. #define list_for_each(itervar, list) \
  87. for (itervar = (list)->next; itervar != (list); itervar = itervar->next)
  88. #define list_for_each_safe(itervar, save_var, list) \
  89. for (itervar = (list)->next, save_var = (list)->next->next; \
  90. itervar != (list); \
  91. itervar = save_var, save_var = save_var->next)
  92. #endif