Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions sdk/pinocchio/src/entrypoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub mod lazy;

pub use lazy::{InstructionContext, MaybeAccount};

#[cfg(not(feature = "std"))]
use core::alloc::{GlobalAlloc, Layout};

#[cfg(target_os = "solana")]
pub use alloc::BumpAllocator;
use core::{
Expand Down Expand Up @@ -620,6 +623,12 @@ mod alloc {

extern crate alloc;

use core::{
alloc::{GlobalAlloc, Layout},
mem::size_of,
ptr::null_mut,
};

/// The bump allocator used as the default rust heap when running programs.
pub struct BumpAllocator {
pub start: usize,
Expand All @@ -630,10 +639,10 @@ mod alloc {
/// prescribed [`HEAP_START_ADDRESS`] and [`HEAP_LENGTH`]. Any other use may overflow and is
/// thus unsupported and at one's own risk.
#[allow(clippy::arithmetic_side_effects)]
unsafe impl alloc::alloc::GlobalAlloc for BumpAllocator {
unsafe impl GlobalAlloc for BumpAllocator {
/// Allocates memory as a bump allocator.
#[inline]
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let pos_ptr = self.start as *mut usize;

let mut pos = *pos_ptr;
Expand All @@ -643,15 +652,15 @@ mod alloc {
}
pos = pos.saturating_sub(layout.size());
pos &= !(layout.align().wrapping_sub(1));
if pos < self.start + core::mem::size_of::<*mut u8>() {
return core::ptr::null_mut();
if pos < self.start + size_of::<*mut u8>() {
return null_mut();
}
*pos_ptr = pos;
pos as *mut u8
}

#[inline]
unsafe fn dealloc(&self, _: *mut u8, _: core::alloc::Layout) {
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
// I'm a bump allocator, I don't free.
}
}
Expand All @@ -662,14 +671,14 @@ mod alloc {
pub struct NoAllocator;

#[cfg(not(feature = "std"))]
unsafe impl core::alloc::GlobalAlloc for NoAllocator {
unsafe impl GlobalAlloc for NoAllocator {
#[inline]
unsafe fn alloc(&self, _: core::alloc::Layout) -> *mut u8 {
unsafe fn alloc(&self, _: Layout) -> *mut u8 {
panic!("** NO ALLOCATOR **");
}

#[inline]
unsafe fn dealloc(&self, _: *mut u8, _: core::alloc::Layout) {
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
// I deny all allocations, so I don't need to free.
}
}
Expand Down