Module buddy_allocator

Module buddy_allocator 

Source
👎Deprecated since 0.2.0: Use jemalloc feature instead. See buddy_allocator module docs for migration.
Expand description

Buddy Allocator for SochDB Memory Management

§DEPRECATION NOTICE

This module is deprecated and will be removed in a future release.

§Recommendation: Use jemalloc Instead

Enable the jemalloc feature in your Cargo.toml:

sochdb-core = { version = "...", features = ["jemalloc"] }

§Why jemalloc is preferred:

  1. Production-proven: Used by Firefox, Facebook, Redis, RocksDB
  2. Thread-local caching: Eliminates lock contention on hot paths
  3. Better fragmentation handling: Size-class based allocation
  4. Automatic memory return: Returns memory to OS via madvise
  5. No maintenance burden: Well-tested, battle-hardened code

§Issues with this custom allocator:

  1. Virtual address tracking only: Does not manage real memory
  2. Lock contention: RwLock on every allocation
  3. No memory reclamation: Never returns memory to OS
  4. Internal fragmentation: Buddy allocation wastes ~50% for small allocs

§Migration Path

If you are using BuddyAllocator or SlabAllocator directly:

ⓘ
// Before (deprecated):
let allocator = BuddyAllocator::new(64 * 1024 * 1024)?;
let addr = allocator.allocate(1024)?;

// After (recommended):
// Simply enable the jemalloc feature and use standard allocation:
let buffer: Vec<u8> = Vec::with_capacity(1024);

§Original Documentation

Implements a buddy system allocator for efficient power-of-2 memory block allocation. Key features:

  • O(1) allocation and deallocation for available blocks
  • Automatic block splitting and merging
  • Memory coalescing to reduce fragmentation
  • Thread-safe with fine-grained locking
  • Support for multiple memory pools

Structs§

BlockHeaderDeprecated
Block header stored at the beginning of each block
BuddyAllocatorDeprecated
Multi-pool buddy allocator
BuddyArenaDeprecated
Arena allocator using buddy allocator for backing storage
BuddyStatsDeprecated
Statistics for the buddy allocator
MemoryPoolDeprecated
A single memory pool managed by the buddy allocator
SlabAllocatorDeprecated
Slab allocator built on top of buddy allocator for fixed-size objects
TypedBuddyAllocatorDeprecated
A typed buddy allocator for allocating objects of a specific type

Enums§

BuddyErrorDeprecated
Error types for buddy allocator operations

Constants§

DEFAULT_POOL_SIZEDeprecated
Default pool size (64 MB)
MAX_BLOCK_SIZEDeprecated
Maximum block size (1 GB)
MIN_BLOCK_SIZEDeprecated
Minimum block size (16 bytes)

Functions§

buddy_addrDeprecated
Calculate buddy address for a given address and order
order_to_sizeDeprecated
Calculate the size for a given order
size_to_orderDeprecated
Calculate the order (log2) for a given size