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.
void insert_after (LinkedListNode *node)
 Adds a LinkedListNode record after the indicated node in the doubly-linked list.
void insert_before (LinkedListNode *node)
 Adds a LinkedListNode record before the indicated node in the doubly-linked list.
bool is_on_list () const
 Returns true if the node is member of any list, false if it has been removed or never added.
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.
void remove_from_list ()
 Removes a LinkedListNode record from the doubly-linked list.
void take_list_from (LinkedListNode *other_root)
 Given that this LinkedListNode represents the root of a list, and the other pointer represents the root of a different list, move all of the nodes (except the root itself) from other_root onto this list.

Protected Attributes

size_t _contiguous
Mutex_lock
size_t _max_size
LinkedListNode_next
LinkedListNode_prev
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.

SimpleAllocatorBlock * SimpleAllocator::get_first_block ( ) const [inline]

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().

void LinkedListNode::insert_after ( LinkedListNode node) [inline, protected, inherited]

Adds a LinkedListNode record after the indicated node in the doubly-linked list.

Definition at line 108 of file linkedListNode.I.

void LinkedListNode::insert_before ( LinkedListNode node) [inline, protected, inherited]

Adds a LinkedListNode record before the indicated node in the doubly-linked list.

Definition at line 91 of file linkedListNode.I.

Referenced by do_alloc(), AdaptiveLru::do_partial_lru_update(), and SimpleLruPage::enqueue_lru().

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().

bool LinkedListNode::is_on_list ( ) const [inline, protected, inherited]

Returns true if the node is member of any list, false if it has been removed or never added.

The head of a list generally appears to to always be a member of itself.

Definition at line 62 of file linkedListNode.I.

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 LinkedListNode::remove_from_list ( ) [inline, protected, inherited]

Removes a LinkedListNode record from the doubly-linked list.

Definition at line 73 of file linkedListNode.I.

Referenced by SimpleLruPage::dequeue_lru(), SimpleAllocatorBlock::do_free(), AdaptiveLru::do_partial_lru_update(), and SimpleLruPage::enqueue_lru().

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.

void LinkedListNode::take_list_from ( LinkedListNode other_root) [inline, protected, inherited]

Given that this LinkedListNode represents the root of a list, and the other pointer represents the root of a different list, move all of the nodes (except the root itself) from other_root onto this list.

Definition at line 127 of file linkedListNode.I.

Referenced by BufferContextChain::take_from().


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