Skip to contentSkip to conversation

malloc: dlmalloc: add an ability for the malloc to be re-init/init multiple times

PATCH Superseded marek.bykowski at gmail.com 2 participants 4 messages Apr 29, 2020 – May 1, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 malloc: dlmalloc: add an ability for the malloc to be re-init/init multiple times

From: Marek Bykowski <[email protected]>

Malloc gets initialized with a call to mem_malloc_init() with the address the allocation starts to and its size. Currently it is not possible to move the malloc from one memory area to another as the malloc would eventually fail.

This patch adds in the ability to re-init the malloc with the updated start address and the size.

One of the use cases of this feature is SPL U-Boot running from within the static memory and calling to malloc init from within board_init_f():

	arch/arm/cpu/armv8/start.S:reset vector
	arch/arm/cpu/armv8/start.S:main()
	arch/arm/lib/crt0_64.S:board_init_f()
	board/<my_board>/common/spl.c:board_init_f()
        board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SYS_SPL_MALLOC_START,
                        CONFIG_SYS_SPL_MALLOC_SIZE);

Shortly after the DDR (main) memory is init and ready we call to malloc init again but this time with the start address in the DDR memory and a much greater size we can allow ourselves for moving the allocation off the static to the DDR memory:

	board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SPL_MALLOC_OFFSET,
			CONFIG_SPL_MALLOC_SIZE);

with CONFIG_SYS_SPL_MALLOC_START and CONFIG_SPL_MALLOC_OFFSET being the start addresses of the malloc in the static and DDR memories respectively and SZ_16K CONFIG_SYS_SPL_MALLOC_SIZE and SZ_2M CONFIG_SPL_MALLOC_SIZE being the sizes of the mallocs in the respective memories. Note now we have much greater memory, from 16K to 2M, available for allocation.

There is an alternative approach already existing in U-Boot with a use of an early (simplified) malloc and the proper (dlamalloc) malloc however this approach necessitates managing the two mallocs whereas this one proposes uses a single dlmalloc.

---
 Kconfig           | 14 ++++++++++++++
 common/dlmalloc.c | 41 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 3 deletions(-)
Kconfig +14 -0
index 1f0904f704..958553c90a 100644
@@ -209,6 +209,20 @@ if EXPERT
 	  When disabling this, please check if malloc calls, maybe
 	  should be replaced by calloc - if one expects zeroed memory.
 
+config SYS_MALLOC_DEFAULT_TO_INIT
+	bool "Default malloc to init while reserving the memory for it"
+	default n
+	help
+	  It may happen that one needs to move the dynamic allocation
+	  from one to another memory range, eg. when moving the malloc
+	  from the limited static to a potentially large dynamic (DDR)
+	  memory.
+
+	  If so then on top of setting the updated memory aside one
+	  needs to bring the malloc init.
+
+	  If such a scenario is sought choose yes.
+
 config TOOLS_DEBUG
 	bool "Enable debug information for tools"
 	help
common/dlmalloc.c +38 -3

72 lines not on this page · raw message

Signature · 1 line
2.21.0.896.g6a6c0f1
#1 lore raw

PATCH v2 1/1 malloc: dlmalloc: add an ability for the malloc to be re-init/init multiple times

From: Marek Bykowski <[email protected]>

Malloc gets initialized with a call to mem_malloc_init() with the address the allocation starts to and its size. Currently it is not possible to move the malloc from one memory area to another as the malloc would eventually fail.

This patch adds in the ability to re-init the malloc with the updated start address and the size.

One of the use cases of this feature is SPL U-Boot running from within the static memory and calling to malloc init from within board_init_f():

	arch/arm/cpu/armv8/start.S:reset vector
	arch/arm/cpu/armv8/start.S:main()
	arch/arm/lib/crt0_64.S:board_init_f()
	board/<my_board>/common/spl.c:board_init_f()
        board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SYS_SPL_MALLOC_START,
                        CONFIG_SYS_SPL_MALLOC_SIZE);

Shortly after the DDR (main) memory is init and ready we call to malloc init again but this time with the start address in the DDR memory and a much greater size for moving the allocation off the static to the DDR memory:

	board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SPL_MALLOC_OFFSET,
			CONFIG_SPL_MALLOC_SIZE);

with CONFIG_SYS_SPL_MALLOC_START and CONFIG_SPL_MALLOC_OFFSET being the start addresses of the malloc in the static and DDR memories respectively and SZ_16K CONFIG_SYS_SPL_MALLOC_SIZE and SZ_2M CONFIG_SPL_MALLOC_SIZE being the sizes of the mallocs in the respective memories. Note now we have much greater memory, from 16K to 2M, available for allocation.

There is an alternative approach already existing in U-Boot with a use of an early (simplified) malloc and the proper (dlamalloc) malloc however this approach necessitates managing the two mallocs whereas this one proposes using a single dlmalloc.

---
Changes v1 -> v2: just corrected the spelling in the message body

 Kconfig           | 14 ++++++++++++++
 common/dlmalloc.c | 41 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 3 deletions(-)
Kconfig +14 -0
index 1f0904f704..958553c90a 100644
@@ -209,6 +209,20 @@ if EXPERT
 	  When disabling this, please check if malloc calls, maybe
 	  should be replaced by calloc - if one expects zeroed memory.
 
+config SYS_MALLOC_DEFAULT_TO_INIT
+	bool "Default malloc to init while reserving the memory for it"
+	default n
+	help
+	  It may happen that one needs to move the dynamic allocation
+	  from one to another memory range, eg. when moving the malloc
+	  from the limited static to a potentially large dynamic (DDR)
+	  memory.
+
+	  If so then on top of setting the updated memory aside one
+	  needs to bring the malloc init.
+
+	  If such a scenario is sought choose yes.
+
 config TOOLS_DEBUG
 	bool "Enable debug information for tools"
 	help
common/dlmalloc.c +38 -3

72 lines not on this page · raw message

Signature · 1 line
2.21.0.896.g6a6c0f1
#2 lore raw

PATCH v3 1/1 malloc: dlmalloc: add an ability for the malloc to be re-init/init multiple times

From: Marek Bykowski <[email protected]>

Malloc gets initialized with a call to mem_malloc_init() with the address the allocation starts to and its size. Currently it is not possible to move the malloc from one memory area to another as the malloc would eventually fail.

This patch adds in the ability to re-init the malloc with the updated start address and the size.

One of the use cases of this feature is SPL U-Boot running from within the static memory and calling to malloc init from within board_init_f():

	arch/arm/cpu/armv8/start.S:reset vector
	arch/arm/cpu/armv8/start.S:main()
	arch/arm/lib/crt0_64.S:board_init_f()
	board/<my_board>/common/spl.c:board_init_f()
        board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SYS_SPL_MALLOC_START,
                        CONFIG_SYS_SPL_MALLOC_SIZE);

Shortly after the DDR (main) memory is init and ready we call to malloc init again but this time with the start address in the DDR memory and a much greater size for moving the allocation off the static to the DDR memory:

	board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SPL_MALLOC_OFFSET,
			CONFIG_SPL_MALLOC_SIZE);

Where CONFIG_SYS_SPL_MALLOC_START and CONFIG_SPL_MALLOC_OFFSET are the start addresses of the malloc in the static and DDR memories respectively and CONFIG_SYS_SPL_MALLOC_SIZE=SZ_16K and CONFIG_SPL_MALLOC_SIZE=SZ_2M are the sizes of the mallocs in these memories. Note, now we have a much greater memory, enlarging from 16K to 2M, available for allocation.

There is an alternative approach already existing in U-Boot with the use of an early (simplified) malloc and the proper (dlamalloc) malloc however necessitating managing the two mallocs whereas this approach proposes using a single dlmalloc.

---
Changes v2 -> v1:
- even better phrased commit massage

 Kconfig           | 14 ++++++++++++++
 common/dlmalloc.c | 41 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 3 deletions(-)
Kconfig +14 -0
index 1f0904f704..958553c90a 100644
@@ -209,6 +209,20 @@ if EXPERT
 	  When disabling this, please check if malloc calls, maybe
 	  should be replaced by calloc - if one expects zeroed memory.
 
+config SYS_MALLOC_DEFAULT_TO_INIT
+	bool "Default malloc to init while reserving the memory for it"
+	default n
+	help
+	  It may happen that one needs to move the dynamic allocation
+	  from one to another memory range, eg. when moving the malloc
+	  from the limited static to a potentially large dynamic (DDR)
+	  memory.
+
+	  If so then on top of setting the updated memory aside one
+	  needs to bring the malloc init.
+
+	  If such a scenario is sought choose yes.
+
 config TOOLS_DEBUG
 	bool "Enable debug information for tools"
 	help
common/dlmalloc.c +38 -3

72 lines not on this page · raw message

Signature · 1 line
2.21.0.896.g6a6c0f1
#3 lore raw

PATCH v3 1/1 malloc: dlmalloc: add an ability for the malloc to be re-init/init multiple times

Quoted 33 lines · marek.bykowski at gmail.com

From: Marek Bykowski <[email protected]>

Malloc gets initialized with a call to mem_malloc_init() with the address the allocation starts to and its size. Currently it is not possible to move the malloc from one memory area to another as the malloc would eventually fail.

This patch adds in the ability to re-init the malloc with the updated start address and the size.

One of the use cases of this feature is SPL U-Boot running from within the static memory and calling to malloc init from within board_init_f():

	arch/arm/cpu/armv8/start.S:reset vector
	arch/arm/cpu/armv8/start.S:main()
	arch/arm/lib/crt0_64.S:board_init_f()
	board/<my_board>/common/spl.c:board_init_f()
        board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SYS_SPL_MALLOC_START,
                        CONFIG_SYS_SPL_MALLOC_SIZE);

Shortly after the DDR (main) memory is init and ready we call to malloc init again but this time with the start address in the DDR memory and a much greater size for moving the allocation off the static to the DDR memory:

	board/<my_board>/common/spl.c:mem_malloc_init((ulong)CONFIG_SPL_MALLOC_OFFSET,
			CONFIG_SPL_MALLOC_SIZE);

Where CONFIG_SYS_SPL_MALLOC_START and CONFIG_SPL_MALLOC_OFFSET are the start addresses of the malloc in the static and DDR memories respectively and CONFIG_SYS_SPL_MALLOC_SIZE=SZ_16K and CONFIG_SPL_MALLOC_SIZE=SZ_2M are the sizes of the mallocs in these memories. Note, now we have a much greater memory, enlarging from 16K to 2M, available for allocation.

Quoted 6 lines

There is an alternative approach already existing in U-Boot with the use of an early (simplified) malloc and the proper (dlamalloc) malloc however necessitating managing the two mallocs whereas this approach proposes using a single dlmalloc.

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: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200501/7a7ae6ea/attachment-0001.sig>