You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.5 KiB

17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 2006, 2009, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /*******************************************************************//**
  14. @file include/ut0list.h
  15. A double-linked list
  16. Created 4/26/2006 Osku Salerma
  17. ************************************************************************/
  18. /*******************************************************************//**
  19. A double-linked list. This differs from the one in ut0lst.h in that in this
  20. one, each list node contains a pointer to the data, whereas the one in
  21. ut0lst.h uses a strategy where the list pointers are embedded in the data
  22. items themselves.
  23. Use this one when you need to store arbitrary data in the list where you
  24. can't embed the list pointers in the data, if a data item needs to be
  25. stored in multiple lists, etc.
  26. Note about the memory management: ib_list_t is a fixed-size struct whose
  27. allocation/deallocation is done through ib_list_create/ib_list_free, but the
  28. memory for the list nodes is allocated through a user-given memory heap,
  29. which can either be the same for all nodes or vary per node. Most users will
  30. probably want to create a memory heap to store the item-specific data, and
  31. pass in this same heap to the list node creation functions, thus
  32. automatically freeing the list node when the item's heap is freed.
  33. ************************************************************************/
  34. #ifndef IB_LIST_H
  35. #define IB_LIST_H
  36. #include "mem0mem.h"
  37. struct ib_list_t;
  38. struct ib_list_node_t;
  39. /****************************************************************//**
  40. Create a new list using mem_alloc. Lists created with this function must be
  41. freed with ib_list_free.
  42. @return list */
  43. UNIV_INTERN
  44. ib_list_t*
  45. ib_list_create(void);
  46. /*=================*/
  47. /****************************************************************//**
  48. Create a new list using the given heap. ib_list_free MUST NOT BE CALLED for
  49. lists created with this function.
  50. @return list */
  51. UNIV_INTERN
  52. ib_list_t*
  53. ib_list_create_heap(
  54. /*================*/
  55. mem_heap_t* heap); /*!< in: memory heap to use */
  56. /****************************************************************//**
  57. Free a list. */
  58. UNIV_INTERN
  59. void
  60. ib_list_free(
  61. /*=========*/
  62. ib_list_t* list); /*!< in: list */
  63. /****************************************************************//**
  64. Add the data to the start of the list.
  65. @return new list node */
  66. UNIV_INTERN
  67. ib_list_node_t*
  68. ib_list_add_first(
  69. /*==============*/
  70. ib_list_t* list, /*!< in: list */
  71. void* data, /*!< in: data */
  72. mem_heap_t* heap); /*!< in: memory heap to use */
  73. /****************************************************************//**
  74. Add the data to the end of the list.
  75. @return new list node */
  76. UNIV_INTERN
  77. ib_list_node_t*
  78. ib_list_add_last(
  79. /*=============*/
  80. ib_list_t* list, /*!< in: list */
  81. void* data, /*!< in: data */
  82. mem_heap_t* heap); /*!< in: memory heap to use */
  83. /****************************************************************//**
  84. Add the data after the indicated node.
  85. @return new list node */
  86. UNIV_INTERN
  87. ib_list_node_t*
  88. ib_list_add_after(
  89. /*==============*/
  90. ib_list_t* list, /*!< in: list */
  91. ib_list_node_t* prev_node, /*!< in: node preceding new node (can
  92. be NULL) */
  93. void* data, /*!< in: data */
  94. mem_heap_t* heap); /*!< in: memory heap to use */
  95. /****************************************************************//**
  96. Remove the node from the list. */
  97. UNIV_INTERN
  98. void
  99. ib_list_remove(
  100. /*===========*/
  101. ib_list_t* list, /*!< in: list */
  102. ib_list_node_t* node); /*!< in: node to remove */
  103. /****************************************************************//**
  104. Get the first node in the list.
  105. @return first node, or NULL */
  106. UNIV_INLINE
  107. ib_list_node_t*
  108. ib_list_get_first(
  109. /*==============*/
  110. ib_list_t* list); /*!< in: list */
  111. /****************************************************************//**
  112. Get the last node in the list.
  113. @return last node, or NULL */
  114. UNIV_INLINE
  115. ib_list_node_t*
  116. ib_list_get_last(
  117. /*=============*/
  118. ib_list_t* list); /*!< in: list */
  119. /********************************************************************
  120. Check if list is empty. */
  121. UNIV_INLINE
  122. ibool
  123. ib_list_is_empty(
  124. /*=============*/
  125. /* out: TRUE if empty else */
  126. const ib_list_t* list); /* in: list */
  127. /* List. */
  128. struct ib_list_t {
  129. ib_list_node_t* first; /*!< first node */
  130. ib_list_node_t* last; /*!< last node */
  131. ibool is_heap_list; /*!< TRUE if this list was
  132. allocated through a heap */
  133. };
  134. /* A list node. */
  135. struct ib_list_node_t {
  136. ib_list_node_t* prev; /*!< previous node */
  137. ib_list_node_t* next; /*!< next node */
  138. void* data; /*!< user data */
  139. };
  140. /* Quite often, the only additional piece of data you need is the per-item
  141. memory heap, so we have this generic struct available to use in those
  142. cases. */
  143. struct ib_list_helper_t {
  144. mem_heap_t* heap; /*!< memory heap */
  145. void* data; /*!< user data */
  146. };
  147. #ifndef UNIV_NONINL
  148. #include "ut0list.ic"
  149. #endif
  150. #endif