Skip to content

Kernel: Heap alignment bug - #45

Merged
vedantmalkar merged 1 commit into
vedantmalkar:mainfrom
sohshri796-cell:kernel/heap
Aug 3, 2026
Merged

Kernel: Heap alignment bug#45
vedantmalkar merged 1 commit into
vedantmalkar:mainfrom
sohshri796-cell:kernel/heap

Conversation

@sohshri796-cell

@sohshri796-cell sohshri796-cell commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

There's a bug when allocating memory in heap.c.
The memory we allocated wasnt aligned as the mcu needs it to be. So i modified the file to allocate the memory in only multiples of 8. So it allocates memory interms of nearest higher multiple of 8.
This needs to be done as without proper alignment if user asks for some memory which isnt multiple of 4, the cpu stalls due to unaligned memory access.
It allocates memory in multiples of 8 rather than in 4's is because if we ever need other data types like double which need more than 4 bytes of space.

I tested this using this code snippet:

#include "system_init.h"
#include "uart.h"
#include "task.h"
#include "heap.h"

void task(void){
while(1){
uart_send_string(USART1_BASE, "TASK IS RUNNING\r\n");
    }
}

int main(){
system_init();
uart_init(USART1_BASE);
systick_init();
os_heap_init();
os_task_create(task, 2, 129);
os_malloc(13);
os_malloc(7);
os_start();
return 0;
}

when we run this without proper alignment in memory it would just stall.
I tested this on both hardware as well as QEMU.
image

Signed-off-by: Soham Aney <sohshri796@gmail.com>
@vedantmalkar

Copy link
Copy Markdown
Owner

LGTM

@vedantmalkar
vedantmalkar merged commit 2449cc4 into vedantmalkar:main Aug 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants