SHOW:
|
|
- or go back to the newest paste.
| 1 | - | struct DoingTheNastyAllocator(Allocator, size_t chunkSize) {
|
| 1 | + | struct DoingTheNastyAllocator(Allocator, size_t chunkSize = 4096*16) {
|
| 2 | enum size_t alignment = 16; | |
| 3 | ||
| 4 | Allocator allocator; | |
| 5 | void[] heap; | |
| 6 | ||
| 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 | } |