Skip to contentSkip to conversation

malloc: Fix memalign not honoring alignment prior to full malloc init

PATCH In review Andreas Dannenberg 3 participants 4 messages Mar 27, 2019 – Apr 24, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 malloc: Fix memalign not honoring alignment prior to full malloc init

When using memalign() in a scenario where U-Boot is configured for full malloc support with simple malloc not explicitly enabled and before the full malloc support is initialized, a memory block is being allocated and returned without the alignment parameter getting honored.

Fix this issue by replacing the existing memalign pre-full malloc init logic with a call to memalign_simple() this way ensuring proper alignment of the returned memory block.

Fixes ee038c58d519 ("malloc: Use malloc simple before malloc is fully initialized in memalign()")
---
 common/dlmalloc.c | 3 +--
 include/malloc.h  | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
common/dlmalloc.c +1 -2
index edaad299bb..6f12a18d54 100644
@@ -1893,8 +1893,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
 
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
-		nb = roundup(bytes, alignment);
-		return malloc_simple(nb);
+		return memalign_simple(alignment, bytes);
 	}
 #endif
 
include/malloc.h +1 -1
index b714fedf45..5efa6920b2 100644
@@ -878,7 +878,6 @@ extern Void_t*     sbrk();
 #define memalign memalign_simple
 static inline void free(void *ptr) {}
 void *calloc(size_t nmemb, size_t size);
-void *memalign_simple(size_t alignment, size_t bytes);
 void *realloc_simple(void *ptr, size_t size);
 void malloc_simple_info(void);
 #else
@@ -914,6 +913,7 @@ int initf_malloc(void);
 
 /* Simple versions which can be used when space is tight */
 void *malloc_simple(size_t size);
+void *memalign_simple(size_t alignment, size_t bytes);
 
 #pragma GCC visibility push(hidden)
 # if __STD_C
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 malloc: Fix memalign not honoring alignment prior to full malloc init

Quoted 5 lines · Andreas Dannenberg

When using memalign() in a scenario where U-Boot is configured for full malloc support with simple malloc not explicitly enabled and before the full malloc support is initialized, a memory block is being allocated and returned without the alignment parameter getting honored.

Quoted 7 lines

Fix this issue by replacing the existing memalign pre-full malloc init logic with a call to memalign_simple() this way ensuring proper alignment of the returned memory block.

Fixes ee038c58d519 ("malloc: Use malloc simple before malloc is fully initialized in memalign()")

---

Good catch.

Thanks and regards,
Lokesh

#2 lore raw

PATCH 1/1 malloc: Fix memalign not honoring alignment prior to full malloc init

In reply to Andreas Dannenberg

Hi Tom et al.,

Quoted 11 lines · Andreas Dannenberg

When using memalign() in a scenario where U-Boot is configured for full malloc support with simple malloc not explicitly enabled and before the full malloc support is initialized, a memory block is being allocated and returned without the alignment parameter getting honored.

Fix this issue by replacing the existing memalign pre-full malloc init logic with a call to memalign_simple() this way ensuring proper alignment of the returned memory block.

Fixes ee038c58d519 ("malloc: Use malloc simple before malloc is fully initialized in memalign()")

Any additonal feedback/comments on this patch?

Thanks, Andreas

Quoted 42 lines
---
 common/dlmalloc.c | 3 +--
 include/malloc.h  | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
common/dlmalloc.c +1 -2
index edaad299bb..6f12a18d54 100644
@@ -1893,8 +1893,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
 
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
-		nb = roundup(bytes, alignment);
-		return malloc_simple(nb);
+		return memalign_simple(alignment, bytes);
 	}
 #endif
 
include/malloc.h +1 -1
index b714fedf45..5efa6920b2 100644
@@ -878,7 +878,6 @@ extern Void_t*     sbrk();
 #define memalign memalign_simple
 static inline void free(void *ptr) {}
 void *calloc(size_t nmemb, size_t size);
-void *memalign_simple(size_t alignment, size_t bytes);
 void *realloc_simple(void *ptr, size_t size);
 void malloc_simple_info(void);
 #else
@@ -914,6 +913,7 @@ int initf_malloc(void);
 
 /* Simple versions which can be used when space is tight */
 void *malloc_simple(size_t size);
+void *memalign_simple(size_t alignment, size_t bytes);
 
 #pragma GCC visibility push(hidden)
 # if __STD_C
Signature · 1 line
2.17.1
#3 lore raw
In reply to Andreas Dannenberg
Quoted 5 lines · Andreas Dannenberg

When using memalign() in a scenario where U-Boot is configured for full malloc support with simple malloc not explicitly enabled and before the full malloc support is initialized, a memory block is being allocated and returned without the alignment parameter getting honored.

Quoted 7 lines

Fix this issue by replacing the existing memalign pre-full malloc init logic with a call to memalign_simple() this way ensuring proper alignment of the returned memory block.

Fixes ee038c58d519 ("malloc: Use malloc simple before malloc is fully initialized in memalign()")

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190424/30216b31/attachment.sig>