ContiguousBlockAllocator:
Filter:
Classes | Control

ContiguousBlockAllocator : Object

for better handling of dynamic allocation
Source: Engine.sc

Description

The default allocator used in servers to allocate bus numbers and buffer numbers. Compared to its predecessor, PowerOfTwoAllocator, it can reserve a block of numbers at the beginning of its range, and it can offset its entire range of numbers to support multiple clientIDs.

Class Methods

ContiguousBlockAllocator.new(size, pos: 0, addrOffset: 0)

Create a new allocator with size slots. You may block off the first pos slots (the server's audioBusAllocator does this to reserve the hardware input and output buses).

Inherited class methods

Instance Methods

Public interface

.alloc(n: 1)

Return the starting index of a free block that is n slots wide. The default is 1 slot.

Note that the returned address is not guaranteed to be the lowest possible address that can satisfy the requested size. It should be adjacent to a previously allocated block, however (to minimize fragmentation of the address space).

.free(address)

Free a previously allocated block starting at address.

.reserve(address, size: 1, warn: true)

Mark a specific range of addresses as used so that the alloc method will not return any addresses within that range.

.findAvailable(n)

Given an integer width of a desired block, find and return a ContiguousBlock object whose start is the beginning address of the block and whose size is the width. This method only queries the allocator; it does not change the state. If you obtain an address using findAvailable, there is no guarantee that a later call will not return the same address. So, in general, use -alloc to request an address. (alloc calls findAvailable and then reserve.)

This method could be considered "half-private": It may be useful for queries, but in general, you can get everything you need from -alloc, -free and -reserve.

Status and debugging

You may query these state variables, but it is not recommended to change them outside of the public interface.

.debug(text)

Post internal state of allocator for debugging.

.size

The number of id numbers it can allocate.

.pos

The allocator's offset for a reserved block (e.g. for hardware input and output buses).

.addrOffset

The offset of the allocator's address range, which is used to accomodate multiple clientIDs.

.top

The address of the last empty block.

Inherited instance methods

Examples

In the last line of the second example, findAvailable may locate any of indices 1, 5, 9, 13, or 17. (21 would not be used until a suitable block could not be found in the other empty spaces.) If you were to allocate a single address, then the empty block of three would always be reduced to an empty block of two (instead of dividing the empty space in half and having two empty single-address blocks).

It may be surprising that the algorithm does not favor lower-index blocks, but if two addresses A and B have been previously used and freed, there is no inherent reason to prefer A if A < B.