diff 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
line diff
     1.1 --- a/vmalloc.c	Sun Nov 14 11:17:52 2010 -0800
     1.2 +++ b/vmalloc.c	Mon Nov 15 12:11:24 2010 -0800
     1.3 @@ -11,6 +11,8 @@
     1.4  #include <stdlib.h>
     1.5  
     1.6  #include "VMS.h"
     1.7 +#include "DynArray/DynArray.h"
     1.8 +#include "Queue_impl/PrivateQueue.h"
     1.9  
    1.10  /*Helper function
    1.11   *Insert a newly generated free chunk into the first spot on the free list.
    1.12 @@ -352,3 +354,72 @@
    1.13     // array when all the free lists linked from it have already been freed
    1.14   }
    1.15  
    1.16 +
    1.17 +
    1.18 +void
    1.19 +add_array_to_recycle_pool( VMSRecyclePool *pool )
    1.20 + { void  *newArray;
    1.21 +   int32  idxToNewArray;
    1.22 + 
    1.23 +   newArray = VMS__malloc( pool->currArraySize );
    1.24 +   idxToNewArray = addToDynArray( newArray, pool->arraysInfo );
    1.25 +   
    1.26 +   pool->ptrToNextStruct = pool->arrays[ idxToNewArray ];
    1.27 +   pool->ptrToTopOfCurrArray = pool->ptrToNextStruct + 
    1.28 +                               pool->currArraySize - 1;
    1.29 +
    1.30 +   pool->currArraySize += pool->currArraySize >> 2; //mult by 1.25
    1.31 +   pool->hasNewStructs = TRUE;
    1.32 + }
    1.33 +
    1.34 +VMSRecyclePool *
    1.35 +VMS__make_recycle_pool( int32 sizeOfStructsToHold )
    1.36 + { VMSRecyclePool *pool;
    1.37 +
    1.38 +   pool = VMS__malloc( sizeof(VMSRecyclePool) );
    1.39 +   pool->recycleQ = makeVMSPrivQ();
    1.40 +   pool->arraysInfo = makePrivDynArrayOfSize( &(pool->arrays), 64 );
    1.41 +   pool->sizeOfStructs = sizeOfStructsToHold;
    1.42 +   pool->currArraySize = 64 * sizeOfStructsToHold; //64 makes aligned as well
    1.43 +   add_array_to_recycle_pool( pool );
    1.44 + }
    1.45 +
    1.46 +void *
    1.47 +VMS__get_from_recycle_pool( VMSRecyclePool *pool )
    1.48 + { void *retStruct;
    1.49 +
    1.50 +   if( pool->hasNewStructs )
    1.51 +    {
    1.52 +      ReStart:
    1.53 +      retStruct = pool->ptrToNextStruct;
    1.54 +      pool->ptrToNextStruct += pool->sizeOfStructs;
    1.55 +      if( pool->ptrToNextStruct < pool->ptrToTopOfCurrArray )
    1.56 +       return retStruct;
    1.57 +      else
    1.58 +       { pool->hasNewStructs = FALSE;
    1.59 +         //fall-through to code after top IF
    1.60 +       }
    1.61 +    }
    1.62 +
    1.63 +   retStruct = readPrivQ( pool->recycleQ );
    1.64 +   if( retStruct != NULL ) return retStruct;
    1.65 +   else
    1.66 +    { add_array_to_recycle_pool( pool );
    1.67 +      goto ReStart;
    1.68 +    }
    1.69 + }
    1.70 +
    1.71 +void
    1.72 +VMS__recycle( void *chunkToRecycle, VMSRecyclePool *pool )
    1.73 + {    //PrivQ self-expands when writing
    1.74 +   writePrivQ( chunkToRecycle, pool->recycleQ );
    1.75 + }
    1.76 +
    1.77 +void
    1.78 +VMS__free_recycle_pool( VMSRecyclePool *pool )
    1.79 + { int32 i;
    1.80 + 
    1.81 +   freeDynArrayDeep( pool->arraysInfo, &VMS__free );
    1.82 +   freePrivQ( pool->recycleQ );
    1.83 +   VMS__free( pool );
    1.84 + }