Panda3D
Public Member Functions | Protected Member Functions | Protected Attributes | Friends
SimpleAllocator Class Reference

An implementation of a very simple block allocator. More...

#include "simpleAllocator.h"

Inheritance diagram for SimpleAllocator:
LinkedListNode VertexDataPage VertexDataSaveFile

List of all members.

Public Member Functions

 SimpleAllocator (size_t max_size, Mutex &lock)
SimpleAllocatorBlockalloc (size_t size)
 Allocates a new block.
size_t get_contiguous () const
 Returns an upper-bound estimate of the size of the largest contiguous block that may be allocated.
SimpleAllocatorBlockget_first_block () const
 Returns a pointer to the first allocated block, or NULL if there are no allocated blocks.
size_t get_max_size () const
 Returns the available space for allocated objects.
size_t get_total_size () const
 Returns the total size of allocated objects.
bool is_empty () const
 Returns true if there are no blocks allocated on this page, or false if there is at least one.
void output (ostream &out) const
void set_max_size (size_t max_size)
 Changes the available space for allocated objects.
void write (ostream &out) const

Protected Member Functions

virtual void changed_contiguous ()
 This callback function is made whenever the estimate of contiguous available space changes, either through an alloc or free.
SimpleAllocatorBlockdo_alloc (size_t size)
 Allocates a new block.
bool do_is_empty () const
 Returns true if there are no blocks allocated on this page, or false if there is at least one.
virtual SimpleAllocatorBlockmake_block (size_t start, size_t size)
 Creates a new SimpleAllocatorBlock object.
void mark_contiguous (const LinkedListNode *block)
 Some space has been made available following the indicated block.

Protected Attributes

size_t _contiguous
Mutex_lock
size_t _max_size
size_t _total_size

Friends

class SimpleAllocatorBlock

Detailed Description

An implementation of a very simple block allocator.

This class can allocate ranges of nonnegative integers within a specified upper limit; it uses a simple first-fit algorithm to find the next available space.

Definition at line 33 of file simpleAllocator.h.


Member Function Documentation

SimpleAllocatorBlock * SimpleAllocator::alloc ( size_t  size) [inline]

Allocates a new block.

Returns NULL if a block of the requested size cannot be allocated.

To free the allocated block, call block->free(), or simply delete the block pointer.

Reimplemented in VertexDataPage.

Definition at line 41 of file simpleAllocator.I.

References do_alloc().

void SimpleAllocator::changed_contiguous ( ) [protected, virtual]

This callback function is made whenever the estimate of contiguous available space changes, either through an alloc or free.

The lock will be held.

Reimplemented in VertexDataPage.

Definition at line 177 of file simpleAllocator.cxx.

Referenced by do_alloc(), and mark_contiguous().

SimpleAllocatorBlock * SimpleAllocator::do_alloc ( size_t  size) [protected]

Allocates a new block.

Returns NULL if a block of the requested size cannot be allocated.

To free the allocated block, call block->free(), or simply delete the block pointer.

Assumes the lock is already held.

Definition at line 78 of file simpleAllocator.cxx.

References changed_contiguous(), SimpleAllocatorBlock::get_allocator(), LinkedListNode::insert_before(), and make_block().

Referenced by alloc().

bool SimpleAllocator::do_is_empty ( ) const [inline, protected]

Returns true if there are no blocks allocated on this page, or false if there is at least one.

Assumes the lock is already held.

Definition at line 130 of file simpleAllocator.I.

Referenced by VertexDataPage::changed_contiguous(), and is_empty().

size_t SimpleAllocator::get_contiguous ( ) const [inline]

Returns an upper-bound estimate of the size of the largest contiguous block that may be allocated.

It is guaranteed that an attempt to allocate a block larger than this will fail, though it is not guaranteed that an attempt to allocate a block this size or smaller will succeed.

Definition at line 104 of file simpleAllocator.I.

Returns a pointer to the first allocated block, or NULL if there are no allocated blocks.

Reimplemented in VertexDataPage.

Definition at line 116 of file simpleAllocator.I.

size_t SimpleAllocator::get_max_size ( ) const [inline]

Returns the available space for allocated objects.

Definition at line 75 of file simpleAllocator.I.

size_t SimpleAllocator::get_total_size ( ) const [inline]

Returns the total size of allocated objects.

Definition at line 64 of file simpleAllocator.I.

Referenced by VertexDataSaveFile::get_used_file_size().

bool SimpleAllocator::is_empty ( ) const [inline]

Returns true if there are no blocks allocated on this page, or false if there is at least one.

Definition at line 53 of file simpleAllocator.I.

References do_is_empty().

SimpleAllocatorBlock * SimpleAllocator::make_block ( size_t  start,
size_t  size 
) [protected, virtual]

Creates a new SimpleAllocatorBlock object.

Override this function to specialize the block type returned.

Reimplemented in VertexDataPage, and VertexDataSaveFile.

Definition at line 165 of file simpleAllocator.cxx.

Referenced by do_alloc().

void SimpleAllocator::mark_contiguous ( const LinkedListNode block) [inline, protected]

Some space has been made available following the indicated block.

Increase the contiguous space accordingly.

Assumes the lock is already held.

Definition at line 144 of file simpleAllocator.I.

References changed_contiguous().

Referenced by SimpleAllocatorBlock::do_free(), and SimpleAllocatorBlock::do_realloc().

void SimpleAllocator::set_max_size ( size_t  max_size) [inline]

Changes the available space for allocated objects.

This will not affect any already-allocated objects, but will have an effect on future calls to alloc().

Definition at line 88 of file simpleAllocator.I.


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerations