Memory Allocator Implementations
Unikraft provides multiple memory allocator implementations, each optimized for different use cases and performance characteristics. This page documents the allocator interface and the various concrete implementations available.Overview
The allocator subsystem provides a unified interface (uk_alloc) for memory management, with multiple backend implementations:
- ukalloc - Base allocator interface and common functionality
- ukallocbbuddy - Binary buddy allocator for general-purpose allocation
- ukallocpool - Fixed-size object pool allocator (LIFO)
- ukallocregion - Fast bump-pointer region allocator
- ukallocstack - Specialized stack allocator with guard pages
uk_alloc interface.
Common Allocator Interface (ukalloc)
Core Structure
Object Allocation API
uk_malloc
size bytes.
Parameters:
a- Allocatorsize- Number of bytes to allocate
NULL on failure
Location: lib/ukalloc/include/uk/alloc.h:159
uk_calloc
nmemb elements of size bytes each, zeroed.
Parameters:
a- Allocatornmemb- Number of elementssize- Size of each element
NULL on failure
Location: lib/ukalloc/include/uk/alloc.h:186
uk_realloc
ptr to size bytes, preserving contents.
Parameters:
a- Allocatorptr- Existing allocationsize- New size in bytes
NULL on failure
Location: lib/ukalloc/include/uk/alloc.h:222
uk_memalign
size bytes aligned to align boundary.
Parameters:
a- Allocatoralign- Alignment requirement (must be power of 2)size- Number of bytes to allocate
NULL on failure
Location: lib/ukalloc/include/uk/alloc.h:280
uk_posix_memalign
a- Allocatormemptr- Pointer to store allocation addressalign- Alignment requirementsize- Number of bytes to allocate
lib/ukalloc/include/uk/alloc.h:251
uk_free
ptr.
Parameters:
a- Allocatorptr- Pointer to free (orNULLto do nothing)
lib/ukalloc/include/uk/alloc.h:304
Page Allocation API
uk_palloc
num_pages contiguous pages.
Parameters:
a- Allocatornum_pages- Number of pages to allocate
NULL on failure
Location: lib/ukalloc/include/uk/alloc.h:325
uk_pfree
num_pages previously allocated pages.
Parameters:
a- Allocatorptr- Start of page region (must be page-aligned)num_pages- Number of pages to free
Pages obtained in a single
palloc() call may be freed through any combination of pfree() calls, as long as each page is freed exactly once.lib/ukalloc/include/uk/alloc.h:353
Allocator Management
uk_alloc_get_default
uk_alloc_foreach
Query Functions
Statistics API
WhenCONFIG_LIBUKALLOC_IFSTATS is enabled:
Binary Buddy Allocator (ukallocbbuddy)
A general-purpose allocator based on the binary buddy system from Xen.Algorithm
The buddy allocator:- Manages memory in power-of-two sized blocks
- Maintains free lists for each order (size)
- Coalesces adjacent free blocks into larger ones
- Splits larger blocks when smaller allocations needed
- Allocation: O(log n) where n is the maximum order
- Deallocation: O(log n) with coalescing
- Bitmap: 1 bit per page
- Free lists: Minimal overhead per block
Initialization
[base, base+len).
Parameters:
base- Base address of memory regionlen- Length of memory region in bytes
NULL on failure
Location: lib/ukallocbbuddy/include/uk/allocbbuddy.h:43
Usage Example
Configuration
CONFIG_LIBUKALLOCBBUDDY_FREELIST_SANITY- Enable sanity checking of free lists
Internal Structure
- Multiple memory regions supported
- Bitmap tracking for allocation state
- Separate free lists per order
- Sanity checking available for debugging
Best For
- General-purpose allocation
- Long-running systems with varied allocation sizes
- When fragmentation is a concern
- Systems requiring good coalescing behavior
Pool Allocator (ukallocpool)
A fixed-size object pool using LIFO (Last-In-First-Out) principle.Algorithm
The pool allocator:- Pre-allocates all objects at initialization
- Maintains a free list of available objects
- Returns objects in LIFO order (cache-friendly)
- O(1) allocation and deallocation
- Allocation: O(1)
- Deallocation: O(1)
- Fixed overhead per pool
- No per-object metadata once allocated
Initialization
uk_allocpool_reqmem
obj_count- Number of objectsobj_len- Size of each objectobj_align- Alignment requirement
lib/ukallocpool/include/uk/allocpool.h:60
uk_allocpool_alloc
parent- Parent allocatorobj_count- Number of objects to allocateobj_len- Size of each objectobj_align- Alignment requirement
NULL on failure
Location: lib/ukallocpool/include/uk/allocpool.h:78
uk_allocpool_init
base- Base addresslen- Length of memory rangeobj_len- Size of each objectobj_align- Alignment requirement
NULL on failure
Location: lib/ukallocpool/include/uk/allocpool.h:110
Pool Operations
uk_allocpool_take
uk_malloc()).
Returns: Pointer to object or NULL if pool exhausted
Location: lib/ukallocpool/include/uk/allocpool.h:167
uk_allocpool_return
uk_free()).
Location: lib/ukallocpool/include/uk/allocpool.h:195
Batch Operations
Query Functions
Usage Example
Memory Layout
Best For
- Fixed-size object allocation
- Network packet buffers
- Event structures
- Cache-friendly allocation patterns
- Avoiding fragmentation
- Predictable memory usage
Region Allocator (ukallocregion)
A minimalist bump-pointer allocator for fast allocation without deallocation.Algorithm
The region allocator:- Maintains a single pointer to free space
- Allocation bumps pointer forward
- No per-object metadata
- No deallocation support (region-granularity only)
- Allocation: O(1)
- Deallocation: Not supported
- Minimal overhead (single pointer)
Initialization
[base, base+len).
Parameters:
base- Base addresslen- Length in bytes
NULL
Location: lib/ukallocregion/include/uk/allocregion.h:94
Operations
uk_allocregion_bump
size bytes from the region.
Parameters:
a- Region allocatorsize- Number of bytes
NULL if exhausted
Location: lib/ukallocregion/include/uk/allocregion.h:59
uk_allocregion_availmem
lib/ukallocregion/include/uk/allocregion.h:69
Usage Example
Best For
- Bootstrap/initialization code
- Temporary allocations with region lifetime
- Phase-based memory management
- Maximum speed allocation
- Baseline performance measurements
- Nested allocator context
Stack Allocator (ukallocstack)
Specialized allocator for thread stacks with guard pages and automatic growth.Features
- Automatic integration with
ukvmemfor virtual memory management - Configurable guard pages at top and bottom
- Pre-paged-in size support to reduce page faults
- Consistent stack configuration across allocations
Initialization
a- Parent allocator for internal structuresvas- Virtual address space for stack VMAs (whenCONFIG_LIBUKVMEMenabled)premapped_len- Initial pre-paged-in size from top to bottom
NULL on error
Maximum alignment for
memalign()/posix_memalign() is PAGE_SIZE when guard pages are enabled.lib/ukallocstack/include/uk/allocstack.h:53
Usage Example
Configuration
Guard page sizes are configurable:CONFIG_LIBUKVMEM_STACK_GUARD_PAGES_TOPCONFIG_LIBUKVMEM_STACK_GUARD_PAGES_BOTTOM
Pre-mapped Length Behavior
Thepremapped_len parameter controls how many pages are pre-faulted:
premapped_len == 0: No pages pre-mapped (maximum laziness)premapped_len == PAGE_SIZE * 2: Top two pages paged-inpremapped_len == stack_size: Entire stack paged-in (no page faults)
premapped_len, the entire stack is paged-in.
Best For
- Thread stack allocation
- Consistent stack configuration
- Guard page protection
- Reducing page faults on stack growth
Comparison Matrix
| Feature | Buddy | Pool | Region | Stack |
|---|---|---|---|---|
| Allocation Speed | O(log n) | O(1) | O(1) | O(1) |
| Deallocation | Yes | Yes | No | Yes |
| Fragmentation | Low (coalescing) | None | None | None |
| Memory Overhead | Low | Low | Minimal | Low |
| Variable Sizes | Yes | No | Yes | Stack-specific |
| Best Use Case | General | Fixed objects | Bootstrap | Thread stacks |
Choosing an Allocator
Use Binary Buddy When:
- Need general-purpose allocation
- Variable allocation sizes
- Long-running system with mixed workload
- Fragmentation is a concern
Use Pool When:
- Allocating many same-sized objects
- Need predictable performance
- Allocation/deallocation patterns are frequent
- Cache locality matters
Use Region When:
- Fast bootstrap required
- Phase-based memory usage
- No individual deallocation needed
- Temporary scratch space
Use Stack When:
- Allocating thread stacks
- Need guard page protection
- Want consistent stack configuration
- Integration with
ukvmemrequired
Advanced: Custom Allocators
To implement a custom allocator:- Define allocation functions
- Create
uk_allocstructure - Initialize function pointers
- Optionally implement statistics
See Also
- ukvmem - Virtual Memory Management
- ukmmap - Memory Mapping
lib/ukalloc/- Base allocator implementationlib/ukallocbbuddy/- Buddy allocator sourcelib/ukallocpool/- Pool allocator sourcelib/ukallocregion/- Region allocator sourcelib/ukallocstack/- Stack allocator source