Pigweed
Loading...
Searching...
No Matches
pw::allocator::internal::Bucket Class Referencefinal

#include <bucket.h>

Classes

struct  Chunk
 

Public Member Functions

 Bucket ()
 Constructs a bucket with an unbounded chunk size.
 
 Bucket (size_t chunk_size)
 
 Bucket (const Bucket &other)=delete
 
Bucketoperator= (const Bucket &other)=delete
 
 Bucket (Bucket &&other)
 
Bucketoperator= (Bucket &&other)
 
void Init (size_t chunk_size=std::numeric_limits< size_t >::max())
 
size_t chunk_size () const
 
bool empty () const
 
size_t count () const
 
void Add (std::byte *ptr)
 
void Visit (const Function< void(const std::byte *)> &visitor) const
 Applies the given function to each chunk in the bucket.
 
std::byte * Remove ()
 
std::byte * RemoveIf (const Function< bool(const std::byte *)> &cond)
 

Static Public Member Functions

static void Init (span< Bucket > buckets, size_t min_chunk_size)
 Creates a list of buckets, with each twice as large as the one before it.
 
static std::byte * Remove (std::byte *ptr)
 

Detailed Description

Doubly linked list of free memory regions, or "chunks", of a maximum size or less.

Constructor & Destructor Documentation

◆ Bucket()

pw::allocator::internal::Bucket::Bucket ( size_t  chunk_size)
explicit

Construct a bucket.

Parameters
chunk_sizeThe maximum size of the memory chunks in this bucket. Must be at least sizeof(Chunk).

Member Function Documentation

◆ Add()

void pw::allocator::internal::Bucket::Add ( std::byte *  ptr)

Adds a memory region to this bucket.

Parameters
ptrThe memory region to be added.

◆ count()

size_t pw::allocator::internal::Bucket::count ( ) const

Returns the number of the chunks in the bucket.

Note: This method runs in O(n) time.

◆ Remove() [1/2]

std::byte * pw::allocator::internal::Bucket::Remove ( )

Removes a chunk from this bucket.

Return values
Theremoved region, or null if the bucket is empty.

◆ Remove() [2/2]

static std::byte * pw::allocator::internal::Bucket::Remove ( std::byte *  ptr)
static

Removes a chunk from any bucket it is a part of.

Return values
Theremoved region, for convenience.
Parameters
ptrThe memory region to be removed.

◆ RemoveIf()

std::byte * pw::allocator::internal::Bucket::RemoveIf ( const Function< bool(const std::byte *)> &  cond)

Removes a chunk for which a given condition is met.

This will remove at most one chunk.

Return values
Thefirst chunk for which the condition evaluates to true, null if the bucket does not contain any such chunk.
Parameters
condThe condition to be tested on the chunks in this bucket.

The documentation for this class was generated from the following file: