list.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. *
  5. * Created by Jonathan Larmour <jlarmour@redhat.com>
  6. *
  7. *===========================================================================
  8. * ####ECOSGPLCOPYRIGHTBEGIN####
  9. * -------------------------------------------
  10. * This file is part of eCos, the Embedded Configurable Operating System.
  11. * Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  12. *
  13. * eCos is free software; you can redistribute it and/or modify it under
  14. * the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 or (at your option) any later
  16. * version.
  17. *
  18. * eCos is distributed in the hope that it will be useful, but WITHOUT
  19. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  21. * for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with eCos; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. *
  27. * As a special exception, if other files instantiate templates or use
  28. * macros or inline functions from this file, or you compile this file
  29. * and link it with other works to produce a work based on this file,
  30. * this file does not by itself cause the resulting work to be covered by
  31. * the GNU General Public License. However the source code for this file
  32. * must still be made available in accordance with section (3) of the GNU
  33. * General Public License v2.
  34. *
  35. * This exception does not invalidate any other reasons why a work based
  36. * on this file might be covered by the GNU General Public License.
  37. * -------------------------------------------
  38. * ####ECOSGPLCOPYRIGHTEND####
  39. *===========================================================================
  40. *
  41. */
  42. #ifndef CYGONCE_FS_JFFS2_LIST_H
  43. #define CYGONCE_FS_JFFS2_LIST_H
  44. /* -----------------------------------------------------------------------*/
  45. /* Doubly linked list implementation to replace the GPL'd one used in
  46. the Linux kernel. */
  47. #include <stddef.h>
  48. #include <cyg/infra/cyg_type.h>
  49. /* TYPES */
  50. struct list_head {
  51. struct list_head *next;
  52. struct list_head *prev;
  53. };
  54. /* MACROS */
  55. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  56. #define LIST_HEAD(name) \
  57. struct list_head name = LIST_HEAD_INIT(name)
  58. #define INIT_LIST_HEAD( _list_ ) \
  59. CYG_MACRO_START \
  60. (_list_)->next = (_list_)->prev = (_list_); \
  61. CYG_MACRO_END
  62. /* FUNCTIONS */
  63. /* Insert an entry _after_ the specified entry */
  64. static __inline__ void
  65. list_add( struct list_head *newent, struct list_head *afterthisent )
  66. {
  67. struct list_head *next = afterthisent->next;
  68. newent->next = next;
  69. newent->prev = afterthisent;
  70. afterthisent->next = newent;
  71. next->prev = newent;
  72. } /* list_add() */
  73. /* Insert an entry _before_ the specified entry */
  74. static __inline__ void
  75. list_add_tail( struct list_head *newent, struct list_head *beforethisent )
  76. {
  77. struct list_head *prev = beforethisent->prev;
  78. newent->prev = prev;
  79. newent->next = beforethisent;
  80. beforethisent->prev = newent;
  81. prev->next = newent;
  82. } /* list_add_tail() */
  83. /* Delete the specified entry */
  84. static __inline__ void
  85. list_del( struct list_head *ent )
  86. {
  87. ent->prev->next = ent->next;
  88. ent->next->prev = ent->prev;
  89. } /* list_del() */
  90. /* Is this list empty? */
  91. static __inline__ int
  92. list_empty( struct list_head *list )
  93. {
  94. return ( list->next == list );
  95. } /* list_empty() */
  96. /* list_entry - Assuming you have a struct of type _type_ that contains a
  97. list which has the name _member_ in that struct type, then given the
  98. address of that list in the struct, _list_, this returns the address
  99. of the container structure */
  100. #define list_entry( _list_, _type_, _member_ ) \
  101. ((_type_ *)((char *)(_list_)-(char *)(offsetof(_type_,_member_))))
  102. /* list_for_each - using _ent_, iterate through list _list_ */
  103. #define list_for_each( _ent_, _list_ ) \
  104. for ( (_ent_) = (_list_)->next; \
  105. (_ent_) != (_list_); \
  106. (_ent_) = (_ent_)->next )
  107. /*
  108. * list_for_each_entry - this function can be use to iterate over all
  109. * items in a list* _list_ with it's head at _head_ and link _item_
  110. */
  111. #if defined (__GNUC__)
  112. #define list_for_each_entry(_list_, _head_, _item_) \
  113. for ((_list_) = list_entry((_head_)->next, typeof(*_list_), _item_); \
  114. &((_list_)->_item_) != (_head_); \
  115. (_list_) = list_entry((_list_)->_item_.next, typeof(*_list_), _item_))
  116. #elif defined (MSVC)
  117. #define list_for_each_entry(_list_, _head_, _item_) \
  118. for ((_list_) = list_entry((_head_)->next, struct jffs2_compressor, _item_); \
  119. &((_list_)->_item_) != (_head_); \
  120. (_list_) = list_entry((_list_)->_item_.next, struct jffs2_compressor, _item_))
  121. #else
  122. #endif
  123. /* -----------------------------------------------------------------------*/
  124. #endif /* #ifndef CYGONCE_FS_JFFS2_LIST_H */
  125. /* EOF list.h */