Skip to content

program-entrypoint: Add a BumpAllocator constructor - #284

Merged
joncinque merged 3 commits into
anza-xyz:masterfrom
joncinque:alloc-constructor
Aug 13, 2025
Merged

program-entrypoint: Add a BumpAllocator constructor#284
joncinque merged 3 commits into
anza-xyz:masterfrom
joncinque:alloc-constructor

Conversation

@joncinque

Copy link
Copy Markdown
Collaborator

Problem

The current version of program-entrypoint doesn't work because the allocator can't be instantiated in the macros. The start and len fields are private, and so can't be written to in the macro, which exists in the scope of the program.

Summary of changes

Add a new constructor that can be used by the macro.

#### Problem

The current version of program-entrypoint doesn't work because the
allocator can't be instantiated in the macros. The start and len fields
are private, and so can't be written to in the macro, which exists in
the scope of the program.

#### Summary of changes

Add a new constructor that can be used by the macro.
@joncinque
joncinque requested a review from febo August 13, 2025 15:39
Comment thread program-entrypoint/src/lib.rs Outdated
Comment on lines +224 to +227
static A: $crate::BumpAllocator = $crate::BumpAllocator::new_with_fixed_address_range(
$crate::HEAP_START_ADDRESS as usize,
$crate::HEAP_LENGTH,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to wrap this in an unsafe block:

Suggested change
static A: $crate::BumpAllocator = $crate::BumpAllocator::new_with_fixed_address_range(
$crate::HEAP_START_ADDRESS as usize,
$crate::HEAP_LENGTH,
);
static A: $crate::BumpAllocator = unsafe {
$crate::BumpAllocator::new_with_fixed_address_range(
$crate::HEAP_START_ADDRESS as usize,
$crate::HEAP_LENGTH,
)
};

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right! I also had to make the function const

Comment thread program-entrypoint/src/lib.rs Outdated
///
/// For Solana on-chain programs, a certain address range is reserved, so
/// the allocator can be given those addresses.
pub const unsafe fn new_with_fixed_address_range(start: usize, len: usize) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can drop the "new" from the name, like the one in Vec type:

Suggested change
pub const unsafe fn new_with_fixed_address_range(start: usize, len: usize) -> Self {
pub const unsafe fn with_fixed_address_range(start: usize, len: usize) -> Self {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me!

febo
febo previously approved these changes Aug 13, 2025

@febo febo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just left a comment about the name, but that is cosmetic.

@febo febo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@joncinque
joncinque merged commit 334c7ec into anza-xyz:master Aug 13, 2025
25 checks passed
@joncinque
joncinque deleted the alloc-constructor branch August 13, 2025 16:18
febo pushed a commit to febo/solana-sdk that referenced this pull request Sep 21, 2025
* program-entrypoint: Add a BumpAllocator constructor

#### Problem

The current version of program-entrypoint doesn't work because the
allocator can't be instantiated in the macros. The start and len fields
are private, and so can't be written to in the macro, which exists in
the scope of the program.

#### Summary of changes

Add a new constructor that can be used by the macro.

* Make function const, call from unsafe block

* Rename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants