MentOS  0.8.0
The Mentoring Operating System
Classes | Macros | Typedefs | Functions
kheap.c File Reference

Classes

struct  block_t
 Represents a block of memory within the heap. This structure includes metadata for managing memory allocation and free status. More...
 
struct  heap_header_t
 Maps the heap memory to easily accessible values. This structure contains pointers to the list of all blocks and the list of free blocks. More...
 

Macros

#define __DEBUG_HEADER__   "[KHEAP ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_INFO
 Set log level.
 
#define HEAP_SIZE   (4 * M)
 The size of the heap in bytes, defined as 4 megabytes.
 
#define HEAP_VM_LB   0x40000000
 The lower bound address for virtual memory area placement. This address marks the starting point of the heap.
 
#define HEAP_VM_UB   0x50000000
 The upper bound address for virtual memory area placement. This address marks the endpoint of the heap, ensuring no overlap with other memory regions.
 
#define OVERHEAD   sizeof(block_t)
 The overhead introduced by the block_t structure itself. This is used for memory management and bookkeeping.
 
#define ADDR_ALIGN(addr)   ((((uint32_t)(addr)) & 0xFFFFF000) + 0x1000)
 Aligns the given address to the nearest upper page boundary. The address will be aligned to 4096 bytes (0x1000). More...
 
#define IS_ALIGN(addr)   ((((uint32_t)(addr)) & 0x00000FFF) == 0)
 Checks if the given address is aligned to a page boundary. More...
 

Typedefs

typedef struct block_t block_t
 Represents a block of memory within the heap. This structure includes metadata for managing memory allocation and free status.
 

Functions

static uint32_t __blkmngr_get_rounded_size (uint32_t size)
 Returns the given size, rounded to the nearest multiple of 16. This is useful for ensuring memory alignment requirements are met. More...
 
static int __blkmngr_does_it_fit (block_t *block, uint32_t size)
 Checks if the given size fits inside the block. This function verifies whether the specified size can be accommodated within the block's available size. More...
 
static const char * __block_to_string (block_t *block)
 Prepares a string that represents the block. This function formats the information of the specified block into a human-readable string. More...
 
static void __blkmngr_dump (heap_header_t *header)
 Dumpts debug information about the heap. More...
 
static block_t__blkmngr_find_best_fitting (heap_header_t *header, uint32_t size)
 Find the best fitting block in the memory pool. More...
 
static block_t__blkmngr_get_previous_block (heap_header_t *header, block_t *block)
 Given a block, finds its previous block. More...
 
static block_t__blkmngr_get_next_block (heap_header_t *header, block_t *block)
 Given a block, finds its next block in the memory pool. More...
 
static int __blkmngr_is_previous_block (block_t *block, block_t *previous)
 Checks if the given previous block is actually the block that comes before block in the memory pool. More...
 
static int __blkmngr_is_next_block (block_t *block, block_t *next)
 Checks if the given next block is actually the block that comes after block in the memory pool. More...
 
static int __blkmngr_split_block (heap_header_t *header, block_t *block, uint32_t size)
 Splits a block into two blocks based on the specified size for the first block. More...
 
static int __blkmngr_merge_blocks (heap_header_t *header, block_t *block, block_t *other)
 Merges two adjacent blocks into the first block, effectively expanding its size. More...
 
static void * __do_brk (vm_area_struct_t *heap, uint32_t increment)
 Extends the provided heap by a specified increment. More...
 
static void * __do_malloc (vm_area_struct_t *heap, size_t size)
 Allocates a specified number of bytes of uninitialized storage from the heap. More...
 
static int __do_free (vm_area_struct_t *heap, void *ptr)
 Deallocates previously allocated space, returning it to the heap. More...
 
void * sys_brk (void *addr)
 User malloc. More...
 

Detailed Description

Macro Definition Documentation

◆ ADDR_ALIGN

#define ADDR_ALIGN (   addr)    ((((uint32_t)(addr)) & 0xFFFFF000) + 0x1000)

Aligns the given address to the nearest upper page boundary. The address will be aligned to 4096 bytes (0x1000).

Parameters
addrThe address to align.
Returns
The aligned address.

◆ IS_ALIGN

#define IS_ALIGN (   addr)    ((((uint32_t)(addr)) & 0x00000FFF) == 0)

Checks if the given address is aligned to a page boundary.

Parameters
addrThe address to check.
Returns
Non-zero value if the address is aligned, zero otherwise.

Function Documentation

◆ __blkmngr_does_it_fit()

static int __blkmngr_does_it_fit ( block_t block,
uint32_t  size 
)
inlinestatic

Checks if the given size fits inside the block. This function verifies whether the specified size can be accommodated within the block's available size.

Parameters
blockThe given block to check. Must not be NULL.
sizeThe size to check against the block's size.
Returns
1 if the size fits within the block, -1 if the block is NULL.

◆ __blkmngr_dump()

static void __blkmngr_dump ( heap_header_t header)
inlinestatic

Dumpts debug information about the heap.

Parameters
headerthe heap header.

◆ __blkmngr_find_best_fitting()

static block_t* __blkmngr_find_best_fitting ( heap_header_t header,
uint32_t  size 
)
inlinestatic

Find the best fitting block in the memory pool.

Parameters
headerheader describing the heap.
sizethe size we want.
Returns
a block that should fit our needs.

◆ __blkmngr_get_next_block()

static block_t* __blkmngr_get_next_block ( heap_header_t header,
block_t block 
)
inlinestatic

Given a block, finds its next block in the memory pool.

Parameters
headerThe heap header containing information about the heap.
blockThe current block for which the next block is to be found.
Returns
A pointer to the next block if it exists, or NULL if an error occurs or if the block is the last one.

◆ __blkmngr_get_previous_block()

static block_t* __blkmngr_get_previous_block ( heap_header_t header,
block_t block 
)
inlinestatic

Given a block, finds its previous block.

Parameters
headerthe heap header.
blockthe block.
Returns
a pointer to the previous block or NULL if an error occurs.

◆ __blkmngr_get_rounded_size()

static uint32_t __blkmngr_get_rounded_size ( uint32_t  size)
inlinestatic

Returns the given size, rounded to the nearest multiple of 16. This is useful for ensuring memory alignment requirements are met.

Parameters
sizeThe given size to be rounded.
Returns
The size rounded to the nearest multiple of 16.

◆ __blkmngr_is_next_block()

static int __blkmngr_is_next_block ( block_t block,
block_t next 
)
inlinestatic

Checks if the given next block is actually the block that comes after block in the memory pool.

Parameters
blockThe current block from which we are checking the next block.
nextThe block that is supposedly the next block.
Returns
1 if next is the actual next block of block, 0 otherwise. Returns -1 on error.

◆ __blkmngr_is_previous_block()

static int __blkmngr_is_previous_block ( block_t block,
block_t previous 
)
inlinestatic

Checks if the given previous block is actually the block that comes before block in the memory pool.

Parameters
blockThe current block from which we are checking the previous block.
previousThe block that is supposedly the previous block.
Returns
1 if previous is the actual previous block of block, 0 otherwise. Returns -1 on error.

◆ __blkmngr_merge_blocks()

static int __blkmngr_merge_blocks ( heap_header_t header,
block_t block,
block_t other 
)
inlinestatic

Merges two adjacent blocks into the first block, effectively expanding its size.

Parameters
headerThe heap header containing metadata about the memory pool.
blockThe first block that will be expanded.
otherThe second block that will be merged into the first block, becoming invalid in the process.
Returns
0 on success, -1 on error.

◆ __blkmngr_split_block()

static int __blkmngr_split_block ( heap_header_t header,
block_t block,
uint32_t  size 
)
inlinestatic

Splits a block into two blocks based on the specified size for the first block.

Parameters
headerThe heap header containing metadata about the memory pool.
blockThe block to be split, which must be free.
sizeThe size of the first of the two new blocks. Must be less than the original block's size minus overhead.
Returns
0 on success, -1 on error.

◆ __block_to_string()

static const char* __block_to_string ( block_t block)
inlinestatic

Prepares a string that represents the block. This function formats the information of the specified block into a human-readable string.

Parameters
blockThe block to represent. Can be NULL.
Returns
A string containing the block's address, size, and free status.

◆ __do_brk()

static void* __do_brk ( vm_area_struct_t heap,
uint32_t  increment 
)
static

Extends the provided heap by a specified increment.

Parameters
heapPointer to the heap structure that tracks the heap's memory area.
incrementThe amount by which to increase the heap size.
Returns
Pointer to the old top of the heap, or NULL if an error occurred.

◆ __do_free()

static int __do_free ( vm_area_struct_t heap,
void *  ptr 
)
static

Deallocates previously allocated space, returning it to the heap.

Parameters
heapPointer to the heap to which the allocated memory is returned.
ptrPointer to the previously allocated memory to be deallocated.
Returns
0 on success, -1 on error.

◆ __do_malloc()

static void* __do_malloc ( vm_area_struct_t heap,
size_t  size 
)
static

Allocates a specified number of bytes of uninitialized storage from the heap.

Parameters
heapPointer to the heap from which we will allocate memory.
sizeThe size of the desired memory area to allocate.
Returns
Pointer to the allocated memory area, or NULL if allocation fails.

◆ sys_brk()

void* sys_brk ( void *  addr)

User malloc.

Parameters
addrThis argument is treated as an address of a dynamically allocated memory if falls inside the process heap area. Otherwise, it is treated as an amount of memory that should be allocated.
Returns
NULL if there is no more memory available or we were freeing a previously allocated memory area, the address of the allocated space otherwise.