From 22628b176d993daa3fb770ed94250a227ced1ff9 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Tue, 1 Aug 2017 13:39:16 +0200 Subject: memory_manager_allocate takes an alignment parameter now added C11 aligned_alloc to stdlib --- src/kernel/memorymanagement.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/kernel/memorymanagement.c') 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; } -- cgit v1.2.3-54-g00ecf