Skip to main content

Module settings

Module settings 

Source
Expand description

Contains types to configure bump allocation.

You can configure various settings of the bump allocator:

  • MIN_ALIGN default: 1 — The alignment the bump pointer maintains when doing allocations.

    When allocating a type in a bump allocator with a sufficient minimum alignment, the bump pointer will not have to be aligned for the allocation but the allocation size will need to be rounded up to the next multiple of the minimum alignment.

    For the performance impact see crates/callgrind-benches.

  • UP default: true — Controls the bump direction.

    Bumping upwards has the advantage that the most recent allocation can be grown and shrunk in place. This benefits collections as well as alloc_iter(_mut) and alloc_fmt(_mut) with the exception of MutBumpVecRev and alloc_iter_mut_rev which can be grown and shrunk in place if and only if bumping downwards.

    Bumping downwards can be done in less instructions.

    For the performance impact see crates/callgrind-benches.

  • GUARANTEED_ALLOCATED default: true — Whether at least one chunk has been allocated.

    The unallocated constructor will create a bump allocator without allocating a chunk. It will only compile when GUARANTEED_ALLOCATED is false.

    The constructors new(_in), default, with_size(_in) and with_capacity(_in) will allocate a chunk and are always available.

    Setting GUARANTEED_ALLOCATED to false adds additional checks and code paths for handling the no-chunk-allocated state when calling reset_to, exiting scopes or calling by_value.

  • CLAIMABLE default: true — Enables the claim api.

    When this is false, calling claim will fail to compile.

  • DEALLOCATES default: true — Toggles deallocation.

    When this is false, Allocator::deallocate does nothing.

  • SHRINKS default: true — Toggles shrinking.

    When this is false, Allocator::shrink and BumpAllocatorTyped::shrink_slice do nothing1.

    This also affects the temporary collections used in alloc_iter, alloc_fmt, etc.

  • MINIMUM_CHUNK_SIZE default: 512 — Configures the minimum chunk size.

    The final chunk size is calculated like described in with_size, thus it can be slightly smaller than requested.

§Example

You can configure the allocator settings using BumpSettings:

use bump_scope::{ Bump, alloc::Global, settings::BumpSettings };

type MyBumpSettings = BumpSettings<
    /* MIN_ALIGN */ 8,
    /* UP */ false,
    /* GUARANTEED_ALLOCATED */ true,
    /* CLAIMABLE */ false,
    /* DEALLOCATES */ false,
    /* SHRINKS */ false,
    /* MINIMUM_CHUNK_SIZE */ 4096,
>;

type MyBump = Bump<Global, MyBumpSettings>;

let bump = MyBump::with_size(0);
assert_eq!(bump.stats().size(), 4096 - size_of::<[usize; 2]>());

bump.alloc_str("Hello, world!");

  1. Calling shrink with a new layout of a greater alignment does still shift bytes around and may cause an allocation. 

Structs§

Bool
Used to create True and False types.
BumpSettings
Implementor of BumpAllocatorSettings.
MinimumAlignment
Specifies the current minimum alignment of a bump allocator.

Traits§

Boolean
Either True or False.
BumpAllocatorSettings
The trait powering bump allocator configuration.
SupportedMinimumAlignment
Statically guarantees that a minimum alignment is supported.

Type Aliases§

False
A type representing false.
True
A type representing true.