pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/mina86/solana-allocator

us" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-a33d805aa3bce2cb.css" /> GitHub - mina86/solana-allocator · GitHub
Skip to content

mina86/solana-allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

solana-allocator

A custom Solana allocator with the following features:

  • Supports heaps of arbitrary size. Default Solana allocator assumes 32 KiB of available space regardless of heap size configured via the ComputeBudgetInstruction. This allocator will use all the allotted space.

  • Supports opportunistic freeing and resizing. The default Solana allocator never frees memory and calling msg! in a loop will eventually exhaust all memory. This allocator frees at least the last allocated object addressing simple case of temporary buffers.

  • Allows declaring mutable global variables which is something Solana Virtual Machine doesn’t normally support. (Any program with mutable static variable will fail to deploy). This allocator offers a way to define such state.

Usage

The crate provides custom_heap and custom_global macros which make use of the allocator relatively straightforward. Note however, that solana_program::entrypoint and anchor::program macros define global allocator of their own unless custom-heap Cargo feature is enabled.

Simple usage

For a simple usage (without support for mutable static variables), add the dependency and use custom_heap macro. First in Cargo.toml:

[dependencies.solana-allocator]
name = "solana-allocator"
version = "0.1"
optional = true

[features]
default = ["custom-heap"]
custom-heap = ["dep:solana-allocator"]

And than in lib.rs:

#[cfg(feature = "custom-heap")]
solana_allocator::custom_heap();

Usage with mutable global variables

For usage with the mutable global variables, additional bytemuck dependency must be added and custom_global macro used instead. First in Cargo.toml:

[dependencies.bytemuck]
version = "*"
optional = true

[dependencies.solana-allocator]
name = "solana-allocator"
version = "0.1"
optional = true

[features]
default = ["custom-heap"]
custom-heap = ["dep:bytemuck", "dep:solana-allocator"]

And then in lib.rs, for example:

solana_allocator::custom_global!(struct GlobalData {
    counter: Cell<usize>,
});

#[cfg(target_os = "solana")]
pub fn unique() -> usize {
    let counter = &global().counter;
    let value = counter.get();
    counter.set(value + 1);
    value
}

#[cfg(not(target_os = "solana"))]
pub fn unique() -> usize {
    use std::sync::atomic::{AtomicUsize, Ordering};
    static COUNTER: AtomicUsize = AtomicUsize::new(0);
    COUNTER.fetch_add(1, Ordering::SeqCst)
}

See documentation or Mutable global state in Solana article for more detailed description.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy