[PATCH 2/2] malloc_simple: add mem_malloc_init_simple()

Simon Glass sjg at chromium.org
Thu Sep 21 03:02:43 CEST 2023


Hi Rasmus,

On Fri, 15 Sept 2023 at 11:51, Rasmus Villemoes
<rasmus.villemoes at prevas.dk> wrote:
>
> I was running out of malloc() in SPL, and the message told me to look
> at CONFIG_SYS_SPL_MALLOC_SIZE. So I did, and bumped it quite a bit,
> but that had no effect whatsoever.
>
> The reason for that was that I also have
> CONFIG_SPL_SYS_MALLOC_SIMPLE=y. So while board_init_r() in spl.c duly
> calls
>
>   mem_malloc_init(SYS_SPL_MALLOC_START, CONFIG_SYS_SPL_MALLOC_SIZE);
>
> that doesn't actually do anything, because that function just sets
> some variables in dlmalloc.c, and (as the linker map shows), the rest
> of dlmalloc.o has been garbage-collected.
>
> I don't want to have the full dlmalloc implementation in SPL - code
> size is still precious. However, once SDRAM is initialized, the heap
> is practically infinite, if only CONFIG_SYS_SPL_MALLOC_SIZE actually
> had an effect.
>
> So just as CONFIG_SPL_SYS_MALLOC_SIMPLE redirects malloc() and friends
> at build-time to the _simple variants, add a _simple variant of
> mem_malloc_init() which will actually update the bookkeeping variables
> relevant to the actual and active malloc() implementation.
>
> Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
> ---
>  common/dlmalloc.c      | 2 +-
>  common/malloc_simple.c | 7 +++++++
>  include/malloc.h       | 7 +++++--
>  3 files changed, 13 insertions(+), 3 deletions(-)

Reviewed-by: Simon Glass <sjg at chromium.org>

width change below

>
> diff --git a/common/dlmalloc.c b/common/dlmalloc.c
> index dcecdb8623..d42b26410f 100644
> --- a/common/dlmalloc.c
> +++ b/common/dlmalloc.c
> @@ -619,7 +619,7 @@ void *sbrk(ptrdiff_t increment)
>         return (void *)old;
>  }
>
> -void mem_malloc_init(ulong start, ulong size)
> +void mem_dlmalloc_init(ulong start, ulong size)
>  {
>         mem_malloc_start = start;
>         mem_malloc_end = start + size;
> diff --git a/common/malloc_simple.c b/common/malloc_simple.c
> index 0a004d40e1..9ecf05cf2e 100644
> --- a/common/malloc_simple.c
> +++ b/common/malloc_simple.c
> @@ -17,6 +17,13 @@
>
>  DECLARE_GLOBAL_DATA_PTR;
>
> +void mem_malloc_init_simple(ulong start, ulong size)
> +{
> +       gd->malloc_base = start;
> +       gd->malloc_ptr = 0;
> +       gd->malloc_limit = size;
> +}
> +
>  static void *alloc_simple(size_t bytes, int align)
>  {
>         ulong addr, new_ptr;
> diff --git a/include/malloc.h b/include/malloc.h
> index 161ccbd129..f59942115b 100644
> --- a/include/malloc.h
> +++ b/include/malloc.h
> @@ -899,6 +899,7 @@ void malloc_disable_testing(void);
>  #define malloc malloc_simple
>  #define realloc realloc_simple
>  #define memalign memalign_simple
> +#define mem_malloc_init mem_malloc_init_simple
>  #if IS_ENABLED(CONFIG_VALGRIND)
>  #define free free_simple
>  #else
> @@ -908,6 +909,9 @@ void *calloc(size_t nmemb, size_t size);
>  void *realloc_simple(void *ptr, size_t size);
>  #else
>
> +#define mem_malloc_init mem_dlmalloc_init
> +void mem_dlmalloc_init(ulong start, ulong size);

Please add a full comment here

> +
>  # ifdef USE_DL_PREFIX
>  # define cALLOc                dlcalloc
>  # define fREe          dlfree
> @@ -955,6 +959,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);
> +void mem_malloc_init_simple(ulong start, ulong size);

Please add a full comment here

>
>  #pragma GCC visibility push(hidden)
>  # if __STD_C
> @@ -997,8 +1002,6 @@ extern ulong mem_malloc_start;
>  extern ulong mem_malloc_end;
>  extern ulong mem_malloc_brk;
>
> -void mem_malloc_init(ulong start, ulong size);
> -
>  #ifdef __cplusplus
>  };  /* end of extern "C" */
>  #endif
> --
> 2.37.2
>

Regards,
Simon


More information about the U-Boot mailing list