├── README.md └── dlmalloc.c /README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | A modified version of dlmalloc with support for multiple contiguous heaps withing single proccess. 3 | We use dlmalloc's mspaces as isolated heaps for different subsystems in Stingray game engine. The biggest problem with this approach is that heaps are non-contiguous and as a result of this - a lot of memory is wasted. 4 | We can take advantage of virtual address space to fix this, especialy if you are running x64 proccess (and you should, it's 2018 at the time of writing, and no one should write 32 bit mainstream software any more!). Reserve a lot of memory for your heap and commit and de-commit it on demand from dlmalloc, as simple as that. 5 | 6 | # How to use it 7 | Use it as you'd use dlmalloc normally, but add define `MSPACES_CONTIGUOUS` to 1 and define `MORECORE` to your routine that will extend your heap i.e. commit reserved pages for it. 8 | `create_mspace` and `create_mspace_with_base` now accept a pointer to user data that will help you to identify your name space in morecode\mmap\munmap callbacks. 9 | 10 | # Example 11 | ```C 12 | #define MSPACES 1 13 | #define MORECORE_CONTIGUOUS 1 14 | #define HAVE_MORECORE 1 15 | #define MSPACES_CONTIGUOUS 1 16 | 17 | void* dlmorecore(intptr_t size, void* heap); 18 | #define MORECORE dlmorecore 19 | 20 | #include "dlmalloc.c" 21 | 22 | void* dlmorecore(intptr_t size, void* user_data) { 23 | Heap* heap = (Heap*)user_data; 24 | void* old_sbrk = heap->_sbrk; 25 | 26 | if (heap->_commited + size > heap->_reserved) { 27 | return (void*)MAX_SIZE_T; 28 | } 29 | 30 | if (size > 0) { 31 | commit_vmemory(heap->_sbrk, size); 32 | heap->_sbrk = (char*)heap->_sbrk + size; 33 | } else if (size < 0) { 34 | heap->_sbrk = (char*)heap->_sbrk + size; 35 | decommit_vmemory(heap->_sbrk, -size); 36 | } 37 | 38 | heap->_commited += size; 39 | return old_sbrk; 40 | } 41 | ``` 42 | That's it! 43 | It decreased memory consumption in Vermintide 2 by up to 13% or up to 300MB and that is very helpful for systems with memory shortage. 44 | --------------------------------------------------------------------------------