26 X("mm::allocation", test_mm_allocation), \
27 X("mm::block_allocation", test_mm_allocation_in_block), \
28 X("mm::multiblock_allocation", test_mm_allocation_multiblock), \
29 X("mm::block_bitmap", test_mm_block_bitmap), \
30 X("mm::block_size_properties", test_mm_block_size_properties), \
31 X("mm::aligned_block_allocation", test_mm_aligned_block_allocation), \
32 X("mm::immortal_multiblock", test_mm_immortal_multiblock), \
33 X("mm::get_block_out_of_range", test_mm_get_block_out_of_range)
36# define MM_TEST_LOG(...) DBG("MM_TEST", __VA_ARGS__)
38# define MM_TEST_LOG(...)
50static inline size_t padding(
size_t size,
size_t align) {
51 return (align - (size & (align - 1U))) & (align - 1);
60 for (
int i = 0; i < 200; i++) {
77 for (
int i = 0; i < 200; i++) {
99 for (
int i = 0; i < 0xffff; i++) {
104 for (
int i = 0; i < 0xffff; i++) {
111 for (
int i = 0; i < 200; i++) {
140 for (
int i = 0; i < 4; i++) {
148 MM_TEST_LOG(
"Allocated block ids: %zu, %zu, %zu", (
size_t)b0, (
size_t)b1,
172 MM_TEST_LOG(
"OS page size: %zu, block size: %zu", page_size, block_size);
191 MM_TEST_LOG(
"Allocated block id for aligned test: %zu", (
size_t)b);
193 size_t alignment = 32;
199 MM_TEST_LOG(
"Aligned allocations: p1=%p, p2=%p", p1, p2);
212 uintptr_t base = (uintptr_t)&block->
base;
215 TEST_CHECK((uintptr_t)p1 >= base && (uintptr_t)p1 + size <= end);
216 TEST_CHECK((uintptr_t)p2 >= base && (uintptr_t)p2 + size <= end);
226 MM_TEST_LOG(
"Allocating mm_64k instances in immortal block");
236 for (
int i = 0; i < 0xffff; i++) {
246 MM_TEST_LOG(
"immortal root = %p, next = %p", (
void *)root,
#define ASSERT_NOT_NULL(x)
size_t srn_block_id_t
The block id is effectively just an index in the blocks array in srn_mm_t.
void * srn_mm_allocate_in_block_aligned(srn_mm_t *mm, srn_block_id_t block_id, size_t size, size_t alignment)
Allocate memory on a block with the given block_id.
srn_block_t * srn_mm_get_block(srn_mm_t *mm, srn_block_id_t block_id)
Return the block object associated by the given block_id
srn_block_id_t srn_mm_allocate_block(srn_mm_t *mm)
Allocate a new block in the memory manager and return its ID.
size_t srn_mm_get_os_page_size(void)
Retutrns the OS page size.
srn_mm_t * srn_mm_init()
Initialize the memory manager, this function will panic on error.
void srn_mm_shutdown(srn_mm_t *mm)
Shut down the memory manager and release the resources.
size_t srn_mm_get_block_size(void)
Calculates and return the block size based on our desired size and the OS page size.
#define DESIRED_BLOCK_SIZE
This is the value that we want for our blocks but we want it to be a multiple of the page size on the...
#define srn_mm_immortal_allocate(mm, T)
#define srn_mm_allocate_in_block(mm, id, T)
#define DEFAULT_BLOCK_ALIGNMENT
We strictly use 16 bytes alignment for blocks.
static void test_mm_allocation()
static void test_mm_immortal_multiblock(void)
static void test_mm_block_bitmap(void)
static void test_mm_allocation_in_block()
static void test_mm_block_size_properties(void)
static void test_mm_aligned_block_allocation(void)
static size_t padding(size_t size, size_t align)
static void test_mm_allocation_multiblock()
static void test_mm_get_block_out_of_range(void)
uint8_t base[]
Where the data area starts.
size_t size
This is the TOTAL size of the block, header + payloud.
struct srn_block_t * next
when the block does not have space to allocate a request, we will allocate a new block and point to i...
Main memory manager structure that will own all the allocated blocks and data.
srn_block_t * immortal_block
Immortal block is a chain of blocks which will never die.
uint64_t block_bitmap[4]
This is a 256bit bitmap we treat it as a whole.