From 27b06c6ddb95f66b05c1cd8d327c36fab0ef2e7e Mon Sep 17 00:00:00 2001 From: Danny Milosavljevic Date: Fri, 8 Jan 2021 22:08:39 +0100 Subject: [PATCH] mescc: Mes C Library: Make malloc align the blocks it gives out. * include/stddef.h (max_align_t): Add typedef for max_align_t. * lib/stdlib/malloc.c (malloc): Align the blocks it gives out to multiples of max_align_t. --- include/stddef.h | 5 +++++ lib/stdlib/malloc.c | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/include/stddef.h b/include/stddef.h index a597c9bb..8983a320 100644 --- a/include/stddef.h +++ b/include/stddef.h @@ -1,6 +1,7 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software * Copyright © 2017 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 Danny Milosavljevic * * This file is part of GNU Mes. * @@ -37,6 +38,10 @@ #endif // !__MESC__ #endif // offsetof +/* Note: on banana gcc, max_align_t is 16 Byte big instead! */ + +typedef double max_align_t; + #endif // ! SYSTEM_LIBC #endif // __MES_STDDEF_H diff --git a/lib/stdlib/malloc.c b/lib/stdlib/malloc.c index f4be4de1..54f89af4 100644 --- a/lib/stdlib/malloc.c +++ b/lib/stdlib/malloc.c @@ -1,6 +1,7 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 Danny Milosavljevic * * This file is part of GNU Mes. * @@ -20,6 +21,8 @@ #include #include +#include +#include /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. Therfore we cannot remove stdlib/malloc from libc_SOURCES, which is @@ -37,6 +40,9 @@ malloc (size_t size) { if (!__brk) __brk = (char *) brk (0); + /* align what we give back. */ + __brk = (char*) (((uintptr_t) __brk + + sizeof (max_align_t) - 1) & -sizeof (max_align_t)); if (brk (__brk + size) == -1) return 0; char *p = __brk;