daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest May 16th, 2018 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module tango.util.container.mgmt.ChunkAllocator;
  2.  
  3. public import tango.util.container.mgmt.Allocator;
  4.  
  5. /*******************************************************************************
  6.  
  7.         Can save approximately 30% memory for small elements (tested with
  8.         integer elements and a chunk size of 8192), and is at least twice
  9.         as fast at adding elements when compared to the default allocator
  10.         (nine or ten times faster when reusing prior allocations)
  11.  
  12. *******************************************************************************/
  13.  
  14. class ChunkAllocator(T) : Allocator!(T)
  15. {
  16.         private T[]     list;
  17.         private T[][]   lists;
  18.         private int     index;
  19.         private int     chunks;
  20.         private int     freelists;
  21.  
  22.         /***********************************************************************
  23.  
  24.                 set the chunk size
  25.                
  26.         ***********************************************************************/
  27.  
  28.         this (int chunks = 1024)
  29.         {
  30.                 this.chunks = chunks;
  31.         }
  32.        
  33.         /***********************************************************************
  34.  
  35.                 allocate a fixed-size (T) chunk of memory
  36.                
  37.         ***********************************************************************/
  38.  
  39.         final override void[] allocate ()
  40.         {
  41.                 if (index >= list.length)
  42.                     newlist;
  43.  
  44.                 return (&list[index++]) [0..1];
  45.         }
  46.  
  47.         /***********************************************************************
  48.  
  49.                 allocate an arbitrary chunk of memory
  50.                
  51.         ***********************************************************************/
  52.  
  53.         final override void[] allocate (uint size)
  54.         {
  55.                     return new void [size];
  56.         }                    
  57.  
  58.         /***********************************************************************
  59.  
  60.                 Invoked when clear/reset is called on the host. Parameter
  61.                 all indicates whether all memory usage should be collected
  62.                 or just the fixed-size blocks. We can safely ingore it
  63.                
  64.         ***********************************************************************/
  65.  
  66.         final override void collect (bool all)
  67.         {
  68.                 freelists = 0;
  69.                 newlist;
  70.         }
  71.        
  72.         /***********************************************************************
  73.  
  74.                 Invoked during a client .dup to clone an attached allocator
  75.                
  76.         ***********************************************************************/
  77.  
  78.         final override ChunkAllocator factory ()
  79.         {
  80.                 return new ChunkAllocator (chunks);
  81.         }
  82.  
  83.         /***********************************************************************
  84.  
  85.                 list manager
  86.                
  87.         ***********************************************************************/
  88.  
  89.         private void newlist ()
  90.         {
  91.                 index = 0;
  92.                 if (freelists >= lists.length)
  93.                    {
  94.                    lists.length = lists.length + 1;
  95.                    lists[$-1] = new T[chunks];
  96.                    }
  97.                 list = lists[freelists++];
  98.         }
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top