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(requested_capacity: int) → None

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

Requested capacity failed to allocate.

class Allocator(capacity: int) → None

Buffer space allocation implementation.

Create an allocator for a buffer of the specified maximum capacity size.

alloc(size: int) → int

Allocate memory in the buffer.

Raises AllocatorMemoryException if the allocation cannot be fulfilled.

Parameters:

size (int) – Size of region to allocate.

Return type:

int

Returns:

Starting index of the allocated region.

dealloc(start: int, size: int) → None

Free a region of the buffer.

Parameters:
  • start (int) – Starting index of the region.

  • size (int) – Size of the region.

Return type:

None

get_allocated_regions() → tuple[list, list]

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:

tuple[list, list]

get_fragmentation() → float

Return fraction of free space that is not expandable.

Return type:

float

get_fragmented_free_size() → int

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

Return type:

int

get_free_size() → int

Return the amount of space unused.

Return type:

int

get_usage() → float

Return fraction of capacity currently allocated.

Return type:

float

realloc(start: int, size: int, new_size: int) → int

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:
  • start (int) – Current starting index of the region.

  • size (int) – Current size of the region.

  • new_size (int) – int New size of the region.

Return type:

int

Returns:

Starting index of the re-allocated region.

set_capacity(size: int) → None

Resize the maximum buffer size.

The capacity cannot be reduced.

Return type:

None

capacity
sizes: list[int]
starts: list[int]
class ChunkAllocator(
chunk_size: int,
allocate_chunk: Callable[[int], int],
deallocate_chunk: Callable[[int, int], None],
) → None

Allocate local ranges from non-contiguous owned chunks.

allocate_chunk reserves a region from the backing owner and deallocate_chunk returns a fully unused region to it. Each chunk uses Allocator to track the local free space, so allocations never cross into gaps belonging to another owner.

Create an allocator over chunks owned by another allocator.

Parameters:
  • chunk_size (int) – Minimum size of each requested backing chunk.

  • allocate_chunk (Callable[[int], int]) – Reserves a backing range and returns its absolute start.

  • deallocate_chunk (Callable[[int, int], None]) – Releases a fully unused backing range by start and size.

alloc(count: int) → int
Return type:

int

dealloc(start: int, count: int) → None
Return type:

None

get_allocated_regions() → tuple[list[int], list[int]]
Return type:

tuple[list[int], list[int]]

realloc(start: int, count: int, new_count: int) → int
Return type:

int

property sizes: list[int]
property starts: list[int]
class RangeAllocator → None

Tracks and merges (start, count) buffer subranges for drawing.

Used by rendering buckets to collect multiple vertex list ranges that belong to the same group/state, and merge contiguous ones into larger draw calls.

Note

This preserves insertion order when re-allocating, but sorts by range start when rebuilding, so that the final merged ranges are contiguous for better batched drawing.

Example

>>> allocator = RangeAllocator()
>>> allocator.add(0, 4)
>>> allocator.add(4, 4)
>>> allocator.add(8, 4)
>>> allocator.merged_ranges
[(0, 12)]
>>> allocator.remove(4, 4)
>>> allocator.merged_ranges
[(0, 4), (8, 4)]
>>> allocator.add(4, 4)
>>> allocator.merged_ranges
[(0, 12)]

Initialize an empty range collector.

add(start: int, count: int) → None

Add a new range to be tracked.

Parameters:
  • start (int) – Starting index of the buffer range.

  • count (int) – Number of elements in the range.

Return type:

None

clear() → None
Return type:

None

remove(start: int, count: int) → None

Remove a specific tracked range if it exists.

Parameters:
  • start (int) – Starting index of the buffer range.

  • count (int) – Number of elements in the range.

Return type:

None

is_dirty: bool
property is_empty: bool
property merged_ranges: list[tuple[int, int]]

Return contiguous ranges.

Returns:

A list of (start, count) of draw ranges.

property ranges: list[tuple[int, int]]

Return a list of all un-merged ranges tracked by this allocator.