Back to main page

# Buffers

Every file that is saved or loaded in the framework will pass through a Buffer. Buffers can not refer to each other in cycles and are automatically reference counted and deleted, so that you don't have to worry about memory leaks unless something holding a buffer creates a cycle of handles. They store a fixed size allocation of memory padded and aligned with DSR_MAXIMUM_ALIGNMENT bytes to work well with the largest SIMD vectors without false sharing of cache lines between threads.

## Construction

The default constructor creates an empty handle, so dsr::Buffer() can be treated as null and checked using the buffer_exists function. Returning an empty buffer handle is common when something went wrong with an operation.

To create a buffer that actually stores something, call buffer_create with the number of bytes to contain as the only argument. The memory always start initialized to zero, which prevents random bugs.

If you create a buffer of size zero, it will allocate the head but not the data. Trying to clone an empty buffer will just return the same handle without cloning, because empty buffers are immutable.

## Read and write data access

Trying to get the pointer of a non-existing or zero length Buffer will safely return a null pointer, no matter if you use buffer_getSafeData<type>(buffer, "Buffer name") or buffer_dangerous_getUnsafeData(buffer). You access the data by getting a SafePointer, which can later be sliced into smaller parts. Sometimes you can't use the SafePointer because an operating system wants a regular pointer.