#resources #space #allocation

spacetoken

Space allocator that is decoupled from actual storage

1 unstable release

new 0.1.0 Mar 26, 2025

#153 in #allocation

0BSD license

11KB
198 lines

spacetoken

spacetoken can be used to keep track of how much of a resource has been allocted. It can be thought of as an allocator that is decoupled from the actual allocatable resource.


lib.rs:

Space can be used to track how much has been reserved of a (limited) amount of space.

The idea is that the Space is decoupled from the actual storage space it is meant to represent. (It can refer to persistent disk storage, in-process memory, or any other sized storage).

Usage

An application creates a Space object, passing into it the amount of total space it can hold.

Once the Space object has been created (representing the total amount of space), the application can reserve space from this pool using:

  • [Space::reserve_blocking()] will block the calling thread and wait for the requested amount of space to become available.
  • [Space::reserve_async()] is similar to Space::reserve_blocking, but is intended for async contexts.
  • Space::try_reserve will immediately fail if the reservation request can not be fulfilled.
  • Space::force_reserve will always immediately succeed (this will allow the used storage space to overflow).

All of the reservation functions return a SpaceToken instance, which represents the allocated space. This is returned to the Space context when the SpaceToken is dropped.

Dependencies

~1–6MB
~22K SLoC