pyglet.graphics.allocation

Memory allocation algorithm for vertex arrays and buffers.

The region allocator is used to allocate vertex indices within a vertex domain’s multiple buffers. (“Buffer” refers to any abstract buffer presented by pyglet.graphics.vertexbuffer.

The allocator will at times request more space from the buffers. The current policy is to double the buffer size when there is not enough room to fulfil an allocation. The buffer is never resized smaller.

The allocator maintains references to free space only; it is the caller’s responsibility to maintain the allocated regions.

exception AllocatorMemoryException

The buffer is not large enough to fulfil an allocation.

Raised by Allocator methods when the operation failed due to lack of buffer space. The buffer should be increased to at least requested_capacity and then the operation retried (guaranteed to pass second time).

__init__(requested_capacity)
class Allocator

Buffer space allocation implementation.

__init__(capacity)

Create an allocator for a buffer of the specified capacity.

Parameters:
capacityint

Maximum size of the buffer.

alloc(size)

Allocate memory in the buffer.

Raises AllocatorMemoryException if the allocation cannot be fulfilled.

Parameters:
sizeint

Size of region to allocate.

Return type:

int

Returns:

Starting index of the allocated region.

dealloc(start, size)

Free a region of the buffer.

Parameters:
startint

Starting index of the region.

sizeint

Size of the region.

get_allocated_regions()

Get a list of (aggregate) allocated regions.

The result of this method is (starts, sizes), where starts is a list of starting indices of the regions and sizes their corresponding lengths.

Return type:

(list, list)

get_fragmentation()

Return fraction of free space that is not expandable.

Return type:

float

get_fragmented_free_size()

Returns the amount of space unused, not including the final free block.

Return type:

int

get_free_size()

Return the amount of space unused.

Return type:

int

get_usage()

Return fraction of capacity currently allocated.

Return type:

float

realloc(start, size, new_size)

Reallocate a region of the buffer.

This is more efficient than separate dealloc and alloc calls, as the region can often be resized in-place.

Raises AllocatorMemoryException if the allocation cannot be fulfilled.

Parameters:
startint

Current starting index of the region.

sizeint

Current size of the region.

new_sizeint

New size of the region.

set_capacity(size)

Resize the maximum buffer size.

The capaity cannot be reduced.

Parameters:
sizeint

New maximum size of the buffer.

capacity
sizes
starts