summaryrefslogtreecommitdiff
path: root/src/kernel/memorymanagement.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/memorymanagement.c')
-rw-r--r--src/kernel/memorymanagement.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/kernel/memorymanagement.c b/src/kernel/memorymanagement.c
index 49c825c..1e18849 100644
--- a/src/kernel/memorymanagement.c
+++ b/src/kernel/memorymanagement.c
@@ -24,12 +24,12 @@ void memory_manager_init( memory_manager_t *memory_manager, size_t offset, size_
memory_manager->first = first;
}
-void *memory_manager_allocate( memory_manager_t *memory_manager, size_t size )
+void *memory_manager_allocate( memory_manager_t *memory_manager, size_t size, size_t alignment )
{
// find next free chunk with a proper size
memory_chunk_t *result = NULL;
for( memory_chunk_t *chunk = memory_manager->first; chunk != NULL; chunk = chunk->next ) {
- if( chunk->size > size + sizeof( memory_chunk_t ) && !chunk->allocated ) {
+ if( chunk->size > size + alignment + sizeof( memory_chunk_t ) && !chunk->allocated ) {
result = chunk;
break;
}
@@ -41,9 +41,9 @@ void *memory_manager_allocate( memory_manager_t *memory_manager, size_t size )
// split of a new unallocated chunk at the end of the retrieved
// chunk, put in into the right place in the list of chunks
- memory_chunk_t *tmp = (memory_chunk_t *)(( (uint32_t)result) + sizeof( memory_chunk_t ) + size );
+ memory_chunk_t *tmp = (memory_chunk_t *)(( (uint32_t)result) + sizeof( memory_chunk_t ) + size + alignment );
tmp->allocated = false;
- tmp->size = result->size - size - sizeof( memory_chunk_t );
+ tmp->size = result->size - size - alignment - sizeof( memory_chunk_t );
tmp->prev = result;
tmp->next = result->next;
if( tmp->next != NULL ) {
@@ -55,8 +55,13 @@ void *memory_manager_allocate( memory_manager_t *memory_manager, size_t size )
// return a pointer after the internal data structure as pointer
// for the user, mark the chunk as used
- void *p = (void *)(( (uint32_t)result) + sizeof( memory_chunk_t ) );
+ void *p = (void *)( ( (uint32_t)result) + sizeof( memory_chunk_t ) );
+ // align the pointer
+ if( alignment != 1 && (uint32_t)p % alignment != 0 ) {
+ p = (void *)( (uint32_t)p + alignment - ( (uint32_t)p % alignment ) );
+ }
+
return p;
}