comparison vmalloc.c @ 66:bf08108405cc

Added recycle pool -- will merge later -- need to get PLDI results for now
author Me
date Mon, 15 Nov 2010 12:11:24 -0800
parents 13b22ffb8a2f
children
comparison
equal deleted inserted replaced
5:b9bccc28ac9e 6:2b27ab1c897a
9 9
10 #include <malloc.h> 10 #include <malloc.h>
11 #include <stdlib.h> 11 #include <stdlib.h>
12 12
13 #include "VMS.h" 13 #include "VMS.h"
14 #include "DynArray/DynArray.h"
15 #include "Queue_impl/PrivateQueue.h"
14 16
15 /*Helper function 17 /*Helper function
16 *Insert a newly generated free chunk into the first spot on the free list. 18 *Insert a newly generated free chunk into the first spot on the free list.
17 * The chunk is cast as a MallocProlog, so the various pointers in it are 19 * The chunk is cast as a MallocProlog, so the various pointers in it are
18 * accessed with C's help -- and the size of the prolog is easily added to 20 * accessed with C's help -- and the size of the prolog is easily added to
350 352
351 //don't free the head -- it'll be in an array eventually -- free whole 353 //don't free the head -- it'll be in an array eventually -- free whole
352 // array when all the free lists linked from it have already been freed 354 // array when all the free lists linked from it have already been freed
353 } 355 }
354 356
357
358
359 void
360 add_array_to_recycle_pool( VMSRecyclePool *pool )
361 { void *newArray;
362 int32 idxToNewArray;
363
364 newArray = VMS__malloc( pool->currArraySize );
365 idxToNewArray = addToDynArray( newArray, pool->arraysInfo );
366
367 pool->ptrToNextStruct = pool->arrays[ idxToNewArray ];
368 pool->ptrToTopOfCurrArray = pool->ptrToNextStruct +
369 pool->currArraySize - 1;
370
371 pool->currArraySize += pool->currArraySize >> 2; //mult by 1.25
372 pool->hasNewStructs = TRUE;
373 }
374
375 VMSRecyclePool *
376 VMS__make_recycle_pool( int32 sizeOfStructsToHold )
377 { VMSRecyclePool *pool;
378
379 pool = VMS__malloc( sizeof(VMSRecyclePool) );
380 pool->recycleQ = makeVMSPrivQ();
381 pool->arraysInfo = makePrivDynArrayOfSize( &(pool->arrays), 64 );
382 pool->sizeOfStructs = sizeOfStructsToHold;
383 pool->currArraySize = 64 * sizeOfStructsToHold; //64 makes aligned as well
384 add_array_to_recycle_pool( pool );
385 }
386
387 void *
388 VMS__get_from_recycle_pool( VMSRecyclePool *pool )
389 { void *retStruct;
390
391 if( pool->hasNewStructs )
392 {
393 ReStart:
394 retStruct = pool->ptrToNextStruct;
395 pool->ptrToNextStruct += pool->sizeOfStructs;
396 if( pool->ptrToNextStruct < pool->ptrToTopOfCurrArray )
397 return retStruct;
398 else
399 { pool->hasNewStructs = FALSE;
400 //fall-through to code after top IF
401 }
402 }
403
404 retStruct = readPrivQ( pool->recycleQ );
405 if( retStruct != NULL ) return retStruct;
406 else
407 { add_array_to_recycle_pool( pool );
408 goto ReStart;
409 }
410 }
411
412 void
413 VMS__recycle( void *chunkToRecycle, VMSRecyclePool *pool )
414 { //PrivQ self-expands when writing
415 writePrivQ( chunkToRecycle, pool->recycleQ );
416 }
417
418 void
419 VMS__free_recycle_pool( VMSRecyclePool *pool )
420 { int32 i;
421
422 freeDynArrayDeep( pool->arraysInfo, &VMS__free );
423 freePrivQ( pool->recycleQ );
424 VMS__free( pool );
425 }