Skip to content

improvements on allocators #136

Open
@publicqi

Description

@publicqi

background

as described in #126, svm runtime gives the user heap to use anyways with or without defining a heap allocator.

default BumpAllocator will store two usize on bss section, one indicating the free pointer and the other max len.

NoAllocator now does not allow alloc generated heap allocation, and #126 adds a manual allocation for NoAllocator.

a few thoughts

  1. NoAllocator's name is confusing. it is able to let user to claim heap memory, but it cannot let alloc crate to get memory.
  2. NoAllocator is very dev-unfriendly (maybe we can introduce some static check to avoid overlap?)
  3. using allocating functions in crate alloc like Box::new will introduces some rust runtime check, like null ptr check. subsequently it emits panic stuff like in use get_unchecked over [] to prevent fmt code emission #85 .
  4. allocate_unchecked needs the annoying #![cfg_attr(target_os = "solana", feature(const_mut_refs))]

some easy todo improvements:

  • remove len field of BumpAllocator
  • remove const for allocate_unchecked (or we wait for toolchain updating to 1.83.0)
  • add same interface (allocate_unchecked) for BumpAllocator
  • allocate_unchecked should return &'static mut MaybeUninit<T>
  • introduce static_assertions for allocate_unchecked to through compile-time error for assertion (e.g. heap oob)

some challenging ones:

  • compile time overlap check (not sure if it's possible without touching the compiler)
  • bypass alloc runtime check (i.e. lightweight panic if allocation fails). i tried but failed. compiler change needed for this one

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions