Advertisement
Guest User

Untitled

a guest
Sep 27th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.65 KB | None | 0 0
  1. struct DoingTheNastyAllocator(Allocator, size_t chunkSize) {
  2.     enum size_t alignment = 16;
  3.    
  4.     Allocator allocator;
  5.    
  6.     void[] heap;
  7.     void[] allocate(size_t size) {
  8.         immutable actSize = (size + 15) & ~15;
  9.        
  10.         if (actSize <= heap.length) {
  11.             void[] buf = heap[0..size];
  12.             heap = heap[actSize..$];
  13.             return buf;
  14.         }
  15.        
  16.         if (actSize > chunkSize)
  17.             return allocator.alloc(size);
  18.        
  19.         void[] buf = allocator.alloc(chunkSize);
  20.         heap = buf[actSize..$];
  21.         return buf[0..size];
  22.     }
  23.  
  24.     void deallocate(void[] buf) { /* lol */ }
  25.     void deallocateAll() { allocator.deallocateAll() } // lol here too?
  26.    
  27.     static typeof(this) it = typeof(this)();
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement