Drizzled Public API Documentation

ut0list.cc

00001 /*****************************************************************************
00002 
00003 Copyright (C) 2006, 2009, Innobase Oy. All Rights Reserved.
00004 
00005 This program is free software; you can redistribute it and/or modify it under
00006 the terms of the GNU General Public License as published by the Free Software
00007 Foundation; version 2 of the License.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License along with
00014 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
00015 St, Fifth Floor, Boston, MA 02110-1301 USA
00016 
00017 *****************************************************************************/
00018 
00019 /*******************************************************************/
00026 #include "ut0list.h"
00027 #ifdef UNIV_NONINL
00028 #include "ut0list.ic"
00029 #endif
00030 
00031 /****************************************************************/
00034 UNIV_INTERN
00035 ib_list_t*
00036 ib_list_create(void)
00037 /*=================*/
00038 {
00039   ib_list_t*  list = static_cast<ib_list_t*>(mem_alloc(sizeof(ib_list_t)));
00040 
00041   list->first = NULL;
00042   list->last = NULL;
00043   list->is_heap_list = FALSE;
00044 
00045   return(list);
00046 }
00047 
00048 /****************************************************************/
00052 UNIV_INTERN
00053 ib_list_t*
00054 ib_list_create_heap(
00055 /*================*/
00056   mem_heap_t* heap) 
00057 {
00058   ib_list_t*  list = static_cast<ib_list_t *>(mem_heap_alloc(heap, sizeof(ib_list_t)));
00059 
00060   list->first = NULL;
00061   list->last = NULL;
00062   list->is_heap_list = TRUE;
00063 
00064   return(list);
00065 }
00066 
00067 /****************************************************************/
00069 UNIV_INTERN
00070 void
00071 ib_list_free(
00072 /*=========*/
00073   ib_list_t*  list) 
00074 {
00075   ut_a(!list->is_heap_list);
00076 
00077   /* We don't check that the list is empty because it's entirely valid
00078   to e.g. have all the nodes allocated from a single heap that is then
00079   freed after the list itself is freed. */
00080 
00081   mem_free(list);
00082 }
00083 
00084 /****************************************************************/
00087 UNIV_INTERN
00088 ib_list_node_t*
00089 ib_list_add_first(
00090 /*==============*/
00091   ib_list_t*  list, 
00092   void*   data, 
00093   mem_heap_t* heap) 
00094 {
00095   return(ib_list_add_after(list, ib_list_get_first(list), data, heap));
00096 }
00097 
00098 /****************************************************************/
00101 UNIV_INTERN
00102 ib_list_node_t*
00103 ib_list_add_last(
00104 /*=============*/
00105   ib_list_t*  list, 
00106   void*   data, 
00107   mem_heap_t* heap) 
00108 {
00109   return(ib_list_add_after(list, ib_list_get_last(list), data, heap));
00110 }
00111 
00112 /****************************************************************/
00115 UNIV_INTERN
00116 ib_list_node_t*
00117 ib_list_add_after(
00118 /*==============*/
00119   ib_list_t*  list,   
00120   ib_list_node_t* prev_node,  
00122   void*   data,   
00123   mem_heap_t* heap)   
00124 {
00125   ib_list_node_t* node = static_cast<ib_list_node_t *>(mem_heap_alloc(heap, sizeof(ib_list_node_t)));
00126 
00127   node->data = data;
00128 
00129   if (!list->first) {
00130     /* Empty list. */
00131 
00132     ut_a(!prev_node);
00133 
00134     node->prev = NULL;
00135     node->next = NULL;
00136 
00137     list->first = node;
00138     list->last = node;
00139   } else if (!prev_node) {
00140     /* Start of list. */
00141 
00142     node->prev = NULL;
00143     node->next = list->first;
00144 
00145     list->first->prev = node;
00146 
00147     list->first = node;
00148   } else {
00149     /* Middle or end of list. */
00150 
00151     node->prev = prev_node;
00152     node->next = prev_node->next;
00153 
00154     prev_node->next = node;
00155 
00156     if (node->next) {
00157       node->next->prev = node;
00158     } else {
00159       list->last = node;
00160     }
00161   }
00162 
00163   return(node);
00164 }
00165 
00166 /****************************************************************/
00168 UNIV_INTERN
00169 void
00170 ib_list_remove(
00171 /*===========*/
00172   ib_list_t*  list, 
00173   ib_list_node_t* node) 
00174 {
00175   if (node->prev) {
00176     node->prev->next = node->next;
00177   } else {
00178     /* First item in list. */
00179 
00180     ut_ad(list->first == node);
00181 
00182     list->first = node->next;
00183   }
00184 
00185   if (node->next) {
00186     node->next->prev = node->prev;
00187   } else {
00188     /* Last item in list. */
00189 
00190     ut_ad(list->last == node);
00191 
00192     list->last = node->prev;
00193   }
00194 }