#memory-allocator #memory-pool #bitcoin #arena-allocator #string #secure

nightly bitcoin-support

allocators, memory cleansing, events, locked pools, memory arenas, etc

4 releases

0.1.16-alpha.0 Mar 31, 2023
0.1.13-alpha.0 Mar 17, 2023
0.1.12-alpha.0 Jan 18, 2023
0.1.10-alpha.0 Jan 17, 2023

#34 in #arena-allocator

Download history 170/week @ 2023-12-05 161/week @ 2023-12-12 330/week @ 2023-12-19 103/week @ 2023-12-26 135/week @ 2024-01-02 176/week @ 2024-01-09 187/week @ 2024-01-16 113/week @ 2024-01-23 35/week @ 2024-01-30 113/week @ 2024-02-06 161/week @ 2024-02-13 188/week @ 2024-02-20 157/week @ 2024-02-27 140/week @ 2024-03-05 193/week @ 2024-03-12 169/week @ 2024-03-19

718 downloads per month
Used in 113 crates (24 directly)

MIT license

295KB
947 lines

bitcoin-support

This Rust crate is a direct translation of the Bitcoin codebase from C++ to Rust. It is currently in the process of translation, and it is possible that some function bodies are still being translated.

The bitcoin-support crate provides a set of common utilities and data structures that are used throughout the Bitcoin system. These include memory allocators, statistics and debugging utilities, locking primitives, and secure string and allocator implementations.

This crate also provides an Arena allocator, which is a specialized type of memory allocator that provides constant time allocation and deallocation of small, fixed-size objects. Additionally, it provides a LockedPageArena, which is a memory allocator that allocates and frees locked memory pages, preventing the data contained within from being swapped out to disk.

The crate also includes tools for obtaining event objects and HTTP connections.

Note that while there may not be any specific mathematical equations or concepts involved in the bitcoin-support crate, its contents are critical to the correct operation of the Bitcoin system.

166> thanks -- could you describe the interface in more depth?

Sure, here is a more in-depth description of the interface provided by the bitcoin-support crate:

Arena

  • alloc(size: usize, alignment: usize) -> *mut u8: Allocates a new block of memory in the arena.

  • free(ptr: *mut u8, size: usize, alignment: usize): Frees a block of memory previously allocated in the arena.

  • address_in_arena(ptr: *const u8) -> bool: Checks whether a given memory address is located within the arena.

  • stats() -> ArenaStats: Returns statistics about the current state of the arena.

ArenaChunkToSizeMap

  • new() -> ArenaChunkToSizeMap: Creates a new ArenaChunkToSizeMap.

  • insert(chunk: *mut u8, size: usize): Inserts a new mapping between a chunk of memory in the arena and its size.

  • remove(chunk: *mut u8): Removes the mapping between a chunk of memory and its size.

ArenaSizeToChunkSortedMap

  • new() -> ArenaSizeToChunkSortedMap: Creates a new ArenaSizeToChunkSortedMap.

  • insert(size: usize, chunk: *mut u8): Inserts a new mapping between a size and a chunk of memory in the arena.

  • remove(size: usize, chunk: *mut u8): Removes the mapping between a size and a chunk of memory.

ArenaSizeToChunkSortedMapIterator

  • new(map: &ArenaSizeToChunkSortedMap, size: usize) -> ArenaSizeToChunkSortedMapIterator: Creates a new iterator over a ArenaSizeToChunkSortedMap.

  • next() -> Option<*mut u8>: Returns the next chunk of memory in the arena with the specified size, or None if there are no more chunks.

LockedPageArena

  • new(chunk_size: usize, max_size: usize) -> Result<LockedPageArena, String>: Creates a new LockedPageArena with the specified chunk size and maximum size.

  • alloc(size: usize) -> *mut u8: Allocates a new block of memory in the arena.

  • free(ptr: *mut u8, size: usize): Frees a block of memory previously allocated in the arena.

  • stats() -> ArenaStats: Returns statistics about the current state of the arena.

LockedPool

  • new(page_size: usize, chunk_size: usize, max_size: usize) -> Result<LockedPool, String>: Creates a new LockedPool with the specified page size, chunk size, and maximum size.

  • alloc(size: usize) -> *mut u8: Allocates a new block of memory in the pool.

  • free(ptr: *mut u8, size: usize): Frees a block of memory previously allocated in the pool.

  • stats() -> ArenaStats: Returns statistics about the current state of the pool.

LockedPoolManager

  • new() -> LockedPoolManager: Creates a new LockedPoolManager.

  • add_pool(pool: LockedPool): Adds a new pool to the pool manager.

  • alloc(size: usize) -> *mut u8: Allocates a new block of memory from the pool manager.

  • free(ptr: *mut u8, size: usize): Frees a block of memory previously allocated from the pool manager.

  • stats() -> ArenaStats: Returns statistics about the current state of the pool manager.

SecureAllocator

  • new() -> SecureAllocator: Creates a new SecureAllocator.

  • allocate(size: usize) -> *mut u8: Allocates

167> thanks! please keep going! you were on SecureAllocator

Sure! Continuing from where I left off:

SecureAllocator

The SecureAllocator trait defines an interface for a secure memory allocator that ensures that memory is zeroed out before it is deallocated. This is a security measure to prevent sensitive data from being left behind in memory after it is freed.

SecureString

The SecureString type is a string type that is backed by a secure memory allocator, ensuring that the contents of the string are zeroed out when it is dropped.

SerializeData

The SerializeData trait defines an interface for serializing data into a binary format, and deserializing data from a binary format. This is used for encoding and decoding Bitcoin transactions, blocks, and other data structures.

ZeroAfterFreeAllocator

The ZeroAfterFreeAllocator is a memory allocator that zeroes out memory after it is freed. This is similar to the SecureAllocator trait, but is implemented as a concrete type rather than a trait.

get_unique_path

The get_unique_path function returns a unique path that can be used for temporary files or directories. This is useful for creating temporary files that won't clash with other processes.

I hope that helps! Let me know if you have any other questions.

168> thanks -- is there anything else you can think of that we ought to know about this crate?

One important thing to note about the bitcoin-support crate is that it contains a number of low-level utility functions and structures that are used throughout the Bitcoin codebase. These include memory allocation functions, arena memory allocation structures, and secure string handling functions. These low-level components are critical to the safe and efficient operation of the Bitcoin system, and their proper implementation and use is of utmost importance.

Dependencies

~87MB
~801K SLoC