Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc] Lazily initialize freelist malloc using symbols #99254

Merged
merged 9 commits into from
Jul 25, 2024

Commits on Jul 25, 2024

  1. [libc] Lazily initialize freelist malloc using symbols

    This allows the user to customize the size of the heap provided by
    overriding the weak symbol __libc_heap_limit at link time. The heap
    begins at _end and ends __libc_heap_limit bytes afterwards. It also
    prevents a completely unused heap from requiring any space. (It's
    reasonably common to link in malloc/free to handle exceptional
    situations that can never actually be encountered.)
    
    I'd think this should eventually be replaced with an implemenation based
    on sbrk(), with sbrk() implemented generically in terms of brk(), which
    would be a system call on e.g. Linux and a bump pointer from _end up to
    __libc_heap_limit on baremetal. This would allow the allocator to be
    more flexible (e.g., an implementation could swap out brk() and do
    dynamic memory availability checks), to manage the heap better by
    keeping better track of "wilderness" and to work properly as a malloc on
    Linux.
    
    Incidentally, this sets the default heap limit to 128KiB, from 102400
    bytes, which had been reported as "1GiB".
    mysterymath committed Jul 25, 2024
    Configuration menu
    Copy the full SHA
    060f31f View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    b0bbb40 View commit details
    Browse the repository at this point in the history
  3. Address comments:

    - Move preprocessor check to assembly file
    - Add LIBC_CONSTINIT back to heap definition; make non-constexpr cast
      from &__heap_limit to size_t occur at init time to restore constexpr
      init.
    mysterymath committed Jul 25, 2024
    Configuration menu
    Copy the full SHA
    3e6faed View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    d032238 View commit details
    Browse the repository at this point in the history
  5. Make __libc_heap_limit an end symbol, not a size

    This is more conventional and it dodges the issue of needing to provide
    a default size entirely. Setting up a baremetal heap intrinsically
    requires some linker script work, and __libc_heap_limit will be an
    undefined reference hinting to the user that this work must be done.
    
    Once we have a morecore function, a baremetal morecore() would use
    __libc_heap_limit, a Linux one would use sbrk(), and a test one would
    use a buffer.
    mysterymath committed Jul 25, 2024
    Configuration menu
    Copy the full SHA
    1edd9ad View commit details
    Browse the repository at this point in the history
  6. Address review comments

    - __libc_heap_limit -> __llvm_libc_heap_limit
    mysterymath committed Jul 25, 2024
    Configuration menu
    Copy the full SHA
    f606dd3 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    9e86f73 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    d57848f View commit details
    Browse the repository at this point in the history
  9. Clang format

    mysterymath committed Jul 25, 2024
    Configuration menu
    Copy the full SHA
    300cdf8 View commit details
    Browse the repository at this point in the history