Phoenix
Object-oriented orthogonally persistent operating system
|
Universal buddy allocator. More...
#include <BuddyAllocator.h>
Public Types | |
typedef uintptr_t | Addr |
Address type for representing all ranges with which the allocator operates. | |
Public Member Functions | |
BuddyAllocatorBase () | |
The allocator constructor. | |
RetCode | Initialize (Addr startAddress, Addr endAddress, int minOrder=0, int maxOrder=-1, size_t cacheSize=8192) |
Initialize the allocator. |
Universal buddy allocator.
The allocator is abstracted from underlying resource type, i.e. it does not know what kind of resource is allocated – is it physical or virtual memory, some ID space or anything else. It operates with address ranges only.
Note that the allocator is capable for allocations and freeings only – it does not provide any way to associate some client meta-information with allocated blocks. If such meta-information is required, the client should implement some mapping between block address and its meta-information block (e.g. using binary search trees).
The buddy allocator is implemented using blocks bitmaps and free blocks cache. When the allocator is initialized it allocates several internal structures – a set of block bitmaps and a pool of cached free blocks. The set of bitmaps is a map of currently free blocks. Each bitmap corresponds to one order with which the allocator operates. Each set bit in a bitmap corresponds to a free block of that order.
Free blocks cache is a number of lists (one list for each order) and a tree which store some amount of free blocks of each order. Tree is used because when a block is freed, its buddy can be coalesced if it is free. In such case it must be removed from free blocks cache of one order and placed to another one. In case lists only are used without a tree for free blocks cache then linear search should be used in order to find coalesced block and delete it from cache.
BuddyAllocatorBase::BuddyAllocatorBase | ( | ) |
The allocator constructor.
After the object is constructed it still is not usable until Initialize method is called.
RetCode BuddyAllocatorBase::Initialize | ( | Addr | startAddress, |
Addr | endAddress, | ||
int | minOrder = 0 , |
||
int | maxOrder = -1 , |
||
size_t | cacheSize = 8192 |
||
) |
Initialize the allocator.
It will require some memory allocations for the allocator internal data structures.
startAddress | Start address of the managed address range. |
endAddress | One-past-end address of the managed address range. |
minOrder | Minimal order of allocated blocks, i.e. minimal allocation size is (2 ^ minOrder) units. |
maxOrder | Maximal order of allocated blocks, i.e. maximal allocation size is (2 ^ maxOrder) units. Default value -1 indicates that maximal order is limited by the provided range size. |
cacheSize | Size of the free blocks cache in elements. Should not exceed MAX_CACHE_SIZE value. |