1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/oneway-malloc: only allocate word-aligned chunks

If an unevenly sized allocation is requested, the next buffer handed
out would start at an unaligned address.

Fix this by rounding up to the next word-sized allocation size.
This commit is contained in:
Benjamin Valentin 2021-01-24 15:28:18 +01:00
parent 67e5a37495
commit 65ab38a894

View File

@ -20,15 +20,23 @@
#include <string.h>
#include "architecture.h"
#include "malloc.h"
#define ENABLE_DEBUG 0
#include "debug.h"
#define ARCHITECTURE_WORD_MASK (ARCHITECTURE_WORD_BYTES - 1)
extern void *sbrk(int incr);
void __attribute__((weak)) *malloc(size_t size)
{
/* ensure we always allocate word-aligned blocks */
if (size & ARCHITECTURE_WORD_MASK) {
size += ARCHITECTURE_WORD_BYTES - (size & ARCHITECTURE_WORD_MASK);
}
if (size != 0) {
void *ptr = sbrk(size);