1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Copyright (c) 2001 Sasha Vasko <sasha at aftercode.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
/*#define LOCAL_DEBUG*/
#include "astypes.h"
#include "output.h"
#include "safemalloc.h"
#include "selfdiag.h"
#include "aslist.h"
#include "audit.h"
#define DEALLOC_CACHE_SIZE 1024
static ASBiDirElem* deallocated_mem[DEALLOC_CACHE_SIZE+10] ;
static unsigned int deallocated_used = 0 ;
static void dealloc_bidirelem( ASBiDirElem *e )
{
if( deallocated_used < DEALLOC_CACHE_SIZE )
{
deallocated_mem[deallocated_used++] = e ;
}else
free( e );
}
static ASBiDirElem *alloc_bidirelem()
{
if( deallocated_used > 0 )
{
ASBiDirElem* e = deallocated_mem[--deallocated_used];
e->next = e->prev = e->data = NULL;
return e;
}else
return safecalloc( 1, sizeof(ASBiDirElem));
}
void
flush_asbidirlist_memory_pool()
{
/* we better disable errors as some of this data will belong to memory audit : */
/* int old_cleanup_mode = set_audit_cleanup_mode(1); */
while( deallocated_used > 0 )
free( deallocated_mem[--deallocated_used] );
/* set_audit_cleanup_mode(old_cleanup_mode); */
}
static inline ASBiDirElem *
find_bidirelem( ASBiDirList *l, void *data )
{
ASBiDirElem *elem ;
for( elem = l->head ; elem ; elem = elem->next )
if( elem->data == data )
return elem;
return NULL;
}
ASBiDirList *create_asbidirlist(destroy_list_data_handler destroy_func)
{
ASBiDirList *l = safecalloc( 1, sizeof(ASBiDirList) ) ;
l->destroy_func = destroy_func ;
return l;
}
void
purge_asbidirlist( register ASBiDirList *l )
{
if( l->destroy_func )
while( l->head )
{
ASBiDirElem *e = l->head ;
l->head = e->next ;
if( e->data )
l->destroy_func( e->data );
dealloc_bidirelem( e );
--(l->count);
}
else
while( l->head )
{
ASBiDirElem *e = l->head ;
l->head = e->next ;
dealloc_bidirelem( e );
--(l->count);
}
}
void destroy_asbidirlist( ASBiDirList **pl )
{
if( pl )
if( *pl )
{
purge_asbidirlist( *pl );
free( *pl );
*pl = NULL ;
}
}
void
iterate_asbidirlist( ASBiDirList *l,
iter_list_data_handler iter_func, void *aux_data,
void *start_from, Bool reverse)
{
if( l && iter_func )
{
ASBiDirElem *curr = (start_from != NULL)?find_bidirelem( l, start_from ):
(reverse?l->tail:l->head);
while( curr != NULL )
{
if( !iter_func( curr->data, aux_data ) )
break;
curr = reverse?curr->prev:curr->next ;
}
}
}
void *
append_bidirelem( ASBiDirList *l, void *data )
{
ASBiDirElem *e ;
if( l == NULL ) return data ;
e = alloc_bidirelem();
e->data = data ;
e->prev = l->tail ;
if( l->tail )
l->tail->next = e ;
else
l->head = e ;
l->tail = e ;
++(l->count);
return data;
}
void *
prepend_bidirelem( ASBiDirList *l, void *data )
{
ASBiDirElem *e ;
if( l == NULL ) return data ;
e = alloc_bidirelem();
e->data = data ;
e->next = l->head ;
if( l->head )
l->head->prev = e ;
else
l->tail = e ;
l->head = e ;
++(l->count);
return data;
}
void *
insert_bidirelem_after( ASBiDirList *l, void *data, ASBiDirElem *after )
{
ASBiDirElem *e ;
if( l == NULL ) return data ;
/* don't check if after is in the list since it could be time-consuming */
if( after == NULL )
return append_bidirelem( l, data );
e = alloc_bidirelem();
e->data = data ;
e->prev = after ;
e->next = after->next ;
if( after->next )
after->next->prev = e ;
after->next = e ;
if( after == l->tail )
l->tail = e ;
++(l->count);
return data;
}
void *
insert_bidirelem_before( ASBiDirList *l, void *data, ASBiDirElem *before )
{
ASBiDirElem *e ;
if( l == NULL ) return data ;
/* don't check if after is in the list since it could be time-consuming */
if( before == NULL )
return prepend_bidirelem( l, data );
e = alloc_bidirelem();
e->data = data ;
e->next = before ;
e->prev = before->prev ;
if( before->prev )
before->prev->next = e ;
before->prev = e ;
if( before == l->head )
l->head = e ;
++(l->count);
return data;
}
void
destroy_bidirelem( ASBiDirList *l, ASBiDirElem *elem )
{
if( l == NULL || elem == NULL )
return ;
if( elem == l->head )
l->head = elem->next ;
if( elem == l->tail )
l->tail = elem->prev ;
if( elem->next )
elem->next->prev = elem->prev ;
if( elem->prev )
elem->prev->next = elem->next ;
if( l->destroy_func && elem->data )
l->destroy_func( elem->data );
--(l->count);
dealloc_bidirelem( elem );
}
void
pop_bidirelem( ASBiDirList *l, ASBiDirElem *elem )
{
if( l == NULL || elem == NULL )
return ;
if( elem == l->head )
return ;
if( elem == l->tail )
l->tail = elem->prev ;
if( elem->next )
elem->next->prev = elem->prev ;
if( elem->prev )
elem->prev->next = elem->next ;
elem->prev = NULL ;
elem->next = l->head ;
if( l->head )
l->head->prev = elem ;
l->head = elem ;
}
Bool
discard_bidirelem( ASBiDirList *l, void *data )
{
ASBiDirElem *elem ;
if( l )
if( (elem = find_bidirelem( l, data ))!=NULL )
{
destroy_bidirelem( l, elem );
return True;
}
return False;
}
void *
extract_first_bidirelem( ASBiDirList *l )
{
void *data = NULL ;
if( l != NULL && l->head != NULL )
{
data = l->head->data ;
l->head->data = NULL ;
destroy_bidirelem( l, l->head );
}
return data;
}
void *
extract_last_bidirelem( ASBiDirList *l)
{
void *data = NULL ;
if( l != NULL && l->tail != NULL )
{
data = l->tail->data ;
l->tail->data = NULL;
destroy_bidirelem( l, l->tail);
}
return data;
}
void
bubblesort_asbidirlist( ASBiDirList *l, compare_data_handler compare_func )
{
if( l && compare_func )
{
int swaps;
do
{
ASBiDirElem *curr = l->head;
swaps = 0;
while( curr != NULL && curr->next != NULL )
{
ASBiDirElem *next = curr->next ;
if( compare_func( curr->data, next->data ) > 0 )
{ /* swapping elements */
ASBiDirElem *prev = curr->prev ;
next->prev = prev ;
curr->next = next->next ;
curr->prev = next ;
next->next = curr ;
if( prev == NULL )
l->head = next ;
else
prev->next = next ;
if( l->tail == next )
l->tail = curr ;
++swaps ;
}else
curr = next ;
}
}while( swaps > 0 );
}
}
|