reallocBuffer

Used throughout dplug:dsp to avoid reliance on GC. Important: Size 0 is special-case to free the slice. This works a bit like alignedRealloc except with slices as input. You MUST use consistent alignement thoughout the lifetime of this buffer.

nothrow @nogc
void
reallocBuffer
(
T
)
(
ref T[] buffer
,
size_t length
,
int alignment = 1
)

Parameters

buffer T[]

Existing allocated buffer. Can be null. Input slice length is not considered.

length size_t

Desired slice length.

alignment int

Alignement if the slice has allocation requirements, 1 else. Must match for deallocation.

Examples

import std.stdio;

struct MyDSP
{
nothrow @nogc:

    void initialize(int maxFrames)
    {
        // mybuf points to maxFrames frames
        mybuf.reallocBuffer(maxFrames);
    }

    ~this()
    {
        // If you don't free the buffer, it will leak.
        mybuf.reallocBuffer(0);
    }

private:
    float[] mybuf;
}

Important: Don't call that often in a real-time thread without amortization (keep a capacity). If you need to allocate from nextBuffer, do prefer the use of Vec which doesn't shrink, and will reuse the allocation. This, instead, will shrink the allocation which makes it more expensive.

Meta