Blender V4.3
buffer.c File Reference
#include <string.h>
#include "MEM_guardedalloc.h"
#include "BLI_buffer.h"
#include "BLI_utildefines.h"
#include "BLI_strict_flags.h"

Go to the source code of this file.

Functions

static void * buffer_alloc (const BLI_Buffer *buffer, const size_t len)
 
static void * buffer_realloc (BLI_Buffer *buffer, const size_t len)
 
void BLI_buffer_resize (BLI_Buffer *buffer, const size_t new_count)
 
void BLI_buffer_reinit (BLI_Buffer *buffer, const size_t new_count)
 
void _bli_buffer_append_array (BLI_Buffer *buffer, void *new_data, size_t count)
 
void _bli_buffer_free (BLI_Buffer *buffer)
 

Detailed Description

Primitive generic buffer library.

  • Automatically grow as needed. (currently never shrinks).
  • Can be passed between functions.
  • Supports using stack memory by default, falling back to heap as needed.

Usage examples:

BLI_buffer_declare_static(int, my_int_array, BLI_BUFFER_NOP, 32);
BLI_buffer_append(my_int_array, int, 42);
BLI_assert(my_int_array.count == 1);
BLI_assert(BLI_buffer_at(my_int_array, int, 0) == 42);
BLI_buffer_free(&my_int_array);
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_buffer_append(buffer_, type_, val_)
Definition BLI_buffer.h:52
#define BLI_buffer_at(buffer_, type_, index_)
Definition BLI_buffer.h:38
@ BLI_BUFFER_NOP
Definition BLI_buffer.h:23
#define BLI_buffer_declare_static(type_, name_, flag_, static_count_)
Definition BLI_buffer.h:27
#define BLI_buffer_free(name_)
Definition BLI_buffer.h:96
Note
this more or less fills same purpose as #BLI_array, but supports resizing the array outside of the function it was declared in.

Definition in file buffer.c.

Function Documentation

◆ _bli_buffer_append_array()

void _bli_buffer_append_array ( BLI_Buffer * buffer,
void * new_data,
size_t count )

Append an array of elements.

Callers use BLI_buffer_append_array.

Definition at line 100 of file buffer.c.

References BLI_buffer_resize(), BLI_Buffer::count, count, BLI_Buffer::data, and BLI_Buffer::elem_size.

◆ _bli_buffer_free()

void _bli_buffer_free ( BLI_Buffer * buffer)

Does not free the buffer structure itself.

Callers use BLI_buffer_free.

Definition at line 109 of file buffer.c.

References BLI_BUFFER_USE_STATIC, BLI_Buffer::data, BLI_Buffer::flag, and MEM_freeN().

◆ BLI_buffer_reinit()

void BLI_buffer_reinit ( BLI_Buffer * buffer,
size_t new_count )

Ensure size, throwing away old data, respecting #BLI_BUFFER_USE_CALLOC.

Similar to BLI_buffer_resize, but use when the existing data can be:

  • Ignored (malloc'd).
  • Cleared (when #BLI_BUFFER_USE_CALLOC is set).

Definition at line 77 of file buffer.c.

References BLI_Buffer::alloc_count, BLI_BUFFER_USE_STATIC, buffer_alloc(), BLI_Buffer::count, BLI_Buffer::data, BLI_Buffer::flag, MEM_freeN(), and UNLIKELY.

◆ BLI_buffer_resize()

void BLI_buffer_resize ( BLI_Buffer * buffer,
size_t new_count )
Note
Never decreases the amount of memory allocated.

Definition at line 51 of file buffer.c.

References BLI_Buffer::alloc_count, BLI_BUFFER_USE_STATIC, buffer_alloc(), buffer_realloc(), BLI_Buffer::count, BLI_Buffer::data, BLI_Buffer::elem_size, BLI_Buffer::flag, and UNLIKELY.

Referenced by _bli_buffer_append_array().

◆ buffer_alloc()

static void * buffer_alloc ( const BLI_Buffer * buffer,
const size_t len )
static

◆ buffer_realloc()

static void * buffer_realloc ( BLI_Buffer * buffer,
const size_t len )
static

Definition at line 46 of file buffer.c.

References BLI_Buffer::data, BLI_Buffer::elem_size, len, and MEM_reallocN_id.

Referenced by BLI_buffer_resize().