# SMP-safe memory allocation
We currently have to choices for moving forward:
## 1. Use just an internal, synhcronized allocator.
- probably best candidate: bbuddy
- first iteration of lock based synchronization, needs updates: https://github.com/unikraft/unikraft/pull/801
- we have the possibility to implement a lock free allocator, but it requires significant changes / implementing an atomic list
## 2. Use an external "user-space" synchronized internal allocator, fed by an internal allocator / mmap API.
- At some point, this will anyway be required to achieve high performance in various situations.
- However, these are frontend allocators that run in the user-space on common operating systems. Once they consome their memory pool, they rely on the underling OS to increase their pool.
- Therefore, we will still need to syncronizing the internal memory allocation.
## 3. Mimalloc

- mimalloc is lock free and it is already ported (need to check it with the current version of Unikraft): https://github.com/microsoft/mimalloc **BUT** we still need a synchronized provisioner
- mimalloc is currently patched to call our internal region allocator: https://github.com/unikraft/lib-mimalloc/blob/staging/patches/adapt-to-unikraft-interface.patch#L117
- when the mimalloc memory pool is fully occupied, it relies on the internal region allocator to increase the heap base; one option is to simply synchronize this
- an alternative is to implement an optimized backend allocator to feed mimalloc (the same discussion with the synchronized bbuddy allocator) and replace the call to the region allocator
- since the default behavour of mimalloc is to call `mmap()` when it is out of memory, another way of doing this is relying on posix-mmap, which also needs synchronization
**Conclusion:** Even if we decide to continue with an external allocator, we will still need internal memory management synchronziation. We should decide between implementing an efficiently synchronized internal allocator, synchronize `posix-mmap` or doing both of them.
**Proposal:**
*Step 1*: Focus on making mimalloc work in SMP situations, synchronizing the internal backend allocator
*Step 2*: Optimize the internal allocator
*Step 3*: Research into porting other SMP-safe external allocators e.g. jemalloc
*Intermediary step*: Find relevant benchmarks for SMP allocations