Re: [RFC PATCH v2 6/8] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()`

From: Ammar Faizi
Date: Tue Mar 22 2022 - 08:18:44 EST


On 3/22/22 6:52 PM, David Laight wrote:

[...]
+struct nolibc_heap {
+ size_t len;
+ char user_p[] __attribute__((__aligned__));

Doesn't that need (number) in the attribute?

The number is not mandatory.

Specifying no alignment argument implies the maximum alignment for
the target, which is often, but by no means always, 8 or 16 bytes.

This has been discussed in the RFC v1, see the full message here:

https://lore.kernel.org/lkml/c7129520-5e9a-f9d1-cc12-5af9456c917f@xxxxxxxxxxx/


+static __attribute__((unused))
+void *malloc(size_t len)
+{
+ struct nolibc_heap *heap;

If you do (say):
len = ROUNDUP(len + sizeof *heap, 4096)
you can optimise a lot of the realloc() calls.

I actually wonder if compiling a mini-libc.a
and then linking the programs against it might
be better than all these static functions?
-ffunction-sections can help a bit (where supported).

Rounding up is not useful here, because we don't have any free list to keep
track the unused block of memory. I mean, even if it's rounded up, the extra
space after rounded up cannot be utilized with this design. There is no
book-keeping that tracks it.

Though, the kernel still allocates the size in multiple page size.

--
Ammar Faizi