Skip to content

Commit e7173fc

Browse files
committed
Improve alloc feature
1 parent 8f0fd09 commit e7173fc

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/pinocchio/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ unexpected_cfgs = { level = "warn", check-cfg = [
1818
] }
1919

2020
[features]
21+
alloc = []
2122
copy = ["solana-account-view/copy", "solana-address/copy"]
2223
cpi = ["dep:solana-instruction-view"]
23-
alloc = []
24+
default = ["alloc"]
2425

2526
[dependencies]
2627
solana-account-view = { workspace = true }
@@ -32,5 +33,4 @@ solana-program-error = { workspace = true }
3233
solana-define-syscall = { workspace = true }
3334

3435
[dev-dependencies]
35-
pinocchio = { path = ".", features = ["alloc"] }
3636
five8_const = { workspace = true }

sdk/pinocchio/src/entrypoint/mod.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ pub use lazy::{InstructionContext, MaybeAccount};
77

88
#[cfg(not(feature = "alloc"))]
99
use core::alloc::{GlobalAlloc, Layout};
10-
11-
#[cfg(target_os = "solana")]
12-
pub use alloc::BumpAllocator;
1310
use core::{
1411
cmp::min,
1512
mem::{size_of, MaybeUninit},
@@ -22,6 +19,9 @@ use crate::{
2219
Address, BPF_ALIGN_OF_U128, MAX_TX_ACCOUNTS,
2320
};
2421

22+
#[cfg(all(target_os = "solana", feature = "alloc"))]
23+
pub use alloc::BumpAllocator;
24+
2525
/// Start address of the memory region used for program heap.
2626
pub const HEAP_START_ADDRESS: u64 = 0x300000000;
2727

@@ -125,6 +125,7 @@ const STATIC_ACCOUNT_DATA: usize = size_of::<Account>() + MAX_PERMITTED_DATA_INC
125125
/// manually.
126126
///
127127
/// [`crate::nostd_panic_handler`]: https://docs.rs/pinocchio/latest/pinocchio/macro.nostd_panic_handler.html
128+
#[cfg(feature = "alloc")]
128129
#[macro_export]
129130
macro_rules! entrypoint {
130131
( $process_instruction:expr ) => {
@@ -279,9 +280,9 @@ macro_rules! process_accounts {
279280
//
280281
// Note: The function is marked as `cold` to stop the compiler from optimizing the parsing of
281282
// duplicated accounts, which leads to an overall increase in CU consumption.
282-
#[allow(clippy::clone_on_copy)]
283283
#[cold]
284284
#[inline(always)]
285+
#[allow(clippy::clone_on_copy)]
285286
unsafe fn clone_account_info(
286287
accounts: *mut AccountView,
287288
accounts_slice: *const AccountView,
@@ -459,14 +460,12 @@ macro_rules! default_panic_handler {
459460
/// This macro sets up a default panic handler that logs the location (file, line and column) where
460461
/// the panic occurred and then calls the syscall `abort()`.
461462
///
462-
/// This macro can only be used when all crates are `no_std` and the `"alloc"` feature is disabled.
463-
#[cfg(not(feature = "alloc"))]
463+
/// This macro should be used when all crates are `no_std`.
464464
#[macro_export]
465465
macro_rules! nostd_panic_handler {
466466
() => {
467467
/// A panic handler for `no_std`.
468468
#[cfg(target_os = "solana")]
469-
#[no_mangle]
470469
#[panic_handler]
471470
fn handler(info: &core::panic::PanicInfo<'_>) -> ! {
472471
if let Some(location) = info.location() {
@@ -502,6 +501,7 @@ macro_rules! nostd_panic_handler {
502501
/// Default global allocator.
503502
///
504503
/// This macro sets up a default global allocator that uses a bump allocator to allocate memory.
504+
#[cfg(feature = "alloc")]
505505
#[macro_export]
506506
macro_rules! default_allocator {
507507
() => {
@@ -602,12 +602,27 @@ macro_rules! no_allocator {
602602
};
603603
}
604604

605-
#[cfg(target_os = "solana")]
606-
mod alloc {
607-
//! The bump allocator used as the default rust heap when running programs.
605+
#[cfg(not(feature = "alloc"))]
606+
/// An allocator that does not allocate memory.
607+
#[cfg_attr(feature = "copy", derive(Copy))]
608+
#[derive(Clone, Debug)]
609+
pub struct NoAllocator;
608610

609-
extern crate alloc;
611+
#[cfg(not(feature = "alloc"))]
612+
unsafe impl GlobalAlloc for NoAllocator {
613+
#[inline]
614+
unsafe fn alloc(&self, _: Layout) -> *mut u8 {
615+
panic!("** NO ALLOCATOR **");
616+
}
610617

618+
#[inline]
619+
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
620+
// I deny all allocations, so I don't need to free.
621+
}
622+
}
623+
624+
#[cfg(all(target_os = "solana", feature = "alloc"))]
625+
mod alloc {
611626
use core::{
612627
alloc::{GlobalAlloc, Layout},
613628
mem::size_of,
@@ -653,25 +668,6 @@ mod alloc {
653668
}
654669
}
655670

656-
#[cfg(not(feature = "alloc"))]
657-
/// An allocator that does not allocate memory.
658-
#[cfg_attr(feature = "copy", derive(Copy))]
659-
#[derive(Clone, Debug)]
660-
pub struct NoAllocator;
661-
662-
#[cfg(not(feature = "alloc"))]
663-
unsafe impl GlobalAlloc for NoAllocator {
664-
#[inline]
665-
unsafe fn alloc(&self, _: Layout) -> *mut u8 {
666-
panic!("** NO ALLOCATOR **");
667-
}
668-
669-
#[inline]
670-
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
671-
// I deny all allocations, so I don't need to free.
672-
}
673-
}
674-
675671
#[cfg(all(test, not(target_os = "solana")))]
676672
mod tests {
677673
extern crate alloc;

0 commit comments

Comments
 (0)