Loading...
Searching...
No Matches
refstruct.h File Reference
`#include <stddef.h>`

Go to the source code of this file.

## Data Structures

union | AVRefStructOpaque
| RefStruct is an API for creating reference-counted objects with minimal overhead. More...

## Macros

#define | AV_REFSTRUCT_FLAG_NO_ZEROING (1 << 0)
| If this flag is set in av_refstruct_alloc_ext_c(), the object will not be initially zeroed.
#define | AV_REFSTRUCT_POOL_FLAG_NO_ZEROING AV_REFSTRUCT_FLAG_NO_ZEROING
| If this flag is not set, every object in the pool will be zeroed before the init callback is called or before it is turned over to the user for the first time if no init callback has been provided.
#define | AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR (1 << 16)
| If this flag is set and both init_cb and reset_cb callbacks are provided, then reset_cb will be called if init_cb fails.
#define | AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR (1 << 17)
| If this flag is set and both init_cb and free_entry_cb callbacks are provided, then free_cb will be called if init_cb fails.
#define | AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME (1 << 18)
| If this flag is set, the entries will be zeroed before being returned to the user (after the init or reset callbacks have been called (if provided)).

## Typedefs

typedef struct AVRefStructPool | AVRefStructPool
| AVRefStructPool is an API for a thread-safe pool of objects managed via the RefStruct API.

## Functions

void * | av_refstruct_alloc_ext_c (size_t size, unsigned flags, AVRefStructOpaque opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
| Allocate a refcounted object of usable size size managed via the RefStruct API.
static void * | av_refstruct_alloc_ext (size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
| A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
static void * | av_refstruct_allocz (size_t size)
| Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL).
void | av_refstruct_unref (void *objp)
| Decrement the reference count of the underlying object and automatically free the object if there are no more references to it.
void * | av_refstruct_ref (void *obj)
| Create a new reference to an object managed via this API, i.e.
const void * | av_refstruct_ref_c (const void *obj)
| Analog of av_refstruct_ref(), but for constant objects.
void | av_refstruct_replace (void *dstp, const void *src)
| Ensure *dstp refers to the same object as src.
int | av_refstruct_exclusive (const void *obj)
| Check whether the reference count of an object managed via this API is 1.
AVRefStructPool * | av_refstruct_pool_alloc (size_t size, unsigned flags)
| Equivalent to av_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL).
AVRefStructPool * | av_refstruct_pool_alloc_ext_c (size_t size, unsigned flags, AVRefStructOpaque opaque, int(*init_cb)(AVRefStructOpaque opaque, void *obj), void(*reset_cb)(AVRefStructOpaque opaque, void *obj), void(*free_entry_cb)(AVRefStructOpaque opaque, void *obj), void(*free_cb)(AVRefStructOpaque opaque))
| Allocate an AVRefStructPool, potentially using complex callbacks.
static AVRefStructPool * | av_refstruct_pool_alloc_ext (size_t size, unsigned flags, void *opaque, int(*init_cb)(AVRefStructOpaque opaque, void *obj), void(*reset_cb)(AVRefStructOpaque opaque, void *obj), void(*free_entry_cb)(AVRefStructOpaque opaque, void *obj), void(*free_cb)(AVRefStructOpaque opaque))
| A wrapper around av_refstruct_pool_alloc_ext_c() for the common case of a non-const qualified opaque.
void * | av_refstruct_pool_get (AVRefStructPool *pool)
| Get an object from the pool, reusing an old one from the pool when available.
static void | av_refstruct_pool_uninit (AVRefStructPool **poolp)
| Mark the pool as being available for freeing.

## Macro Definition Documentation

## AV_REFSTRUCT_FLAG_NO_ZEROING

#define AV_REFSTRUCT_FLAG_NO_ZEROING (1 << 0)

If this flag is set in av_refstruct_alloc_ext_c(), the object will not be initially zeroed.

Definition at line 67 of file refstruct.h.

## AV_REFSTRUCT_POOL_FLAG_NO_ZEROING

#define AV_REFSTRUCT_POOL_FLAG_NO_ZEROING AV_REFSTRUCT_FLAG_NO_ZEROING

If this flag is not set, every object in the pool will be zeroed before the init callback is called or before it is turned over to the user for the first time if no init callback has been provided.

Definition at line 196 of file refstruct.h.

## AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR

#define AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR (1 << 16)

If this flag is set and both init_cb and reset_cb callbacks are provided, then reset_cb will be called if init_cb fails.

The object passed to reset_cb will be in the state left by init_cb.

Definition at line 202 of file refstruct.h.

## AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR

#define AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR (1 << 17)

If this flag is set and both init_cb and free_entry_cb callbacks are provided, then free_cb will be called if init_cb fails.

It will be called after reset_cb in case reset_cb and the AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR flag are also set.

The object passed to free_cb will be in the state left by the callbacks applied earlier (init_cb potentially followed by reset_cb).

Definition at line 213 of file refstruct.h.

## AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME

#define AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME (1 << 18)

If this flag is set, the entries will be zeroed before being returned to the user (after the init or reset callbacks have been called (if provided)).

Furthermore, to avoid zeroing twice it also makes the pool behave as if the AV_REFSTRUCT_POOL_FLAG_NO_ZEROING flag had been provided.

Definition at line 221 of file refstruct.h.

## Typedef Documentation

## AVRefStructPool

typedef struct AVRefStructPool AVRefStructPool

AVRefStructPool is an API for a thread-safe pool of objects managed via the RefStruct API.

Frequently allocating and freeing large or complicated objects may be slow and wasteful. This API is meant to solve this in cases when the caller needs a set of interchangeable objects.

At the beginning, the user must call allocate the pool via av_refstruct_pool_alloc() or its analogue av_refstruct_pool_alloc_ext(). Then whenever an object is needed, call av_refstruct_pool_get() to get a new or reused object from the pool. This new object works in all aspects the same way as the ones created by av_refstruct_alloc_ext(). However, when the last reference to this object is unreferenced, it is (optionally) reset and returned to the pool instead of being freed and will be reused for subsequent av_refstruct_pool_get() calls.

When the caller is done with the pool and no longer needs to create any new objects, av_refstruct_pool_uninit() must be called to mark the pool as freeable. Then entries returned to the pool will then be freed. Once all the entries are freed, the pool will automatically be freed.

Allocating and releasing objects with this API is thread-safe as long as the user-supplied callbacks (if provided) are thread-safe. The buffer pool. This structure is opaque and not meant to be accessed directly. It is allocated with the allocators below and freed with av_refstruct_pool_uninit().

Definition at line 189 of file refstruct.h.

## Function Documentation

## av_refstruct_alloc_ext_c()

void * av_refstruct_alloc_ext_c | ( | size_t | size,
|  | unsigned | flags,
|  | AVRefStructOpaque | opaque,
|  | void(* | free_cb )(AVRefStructOpaque opaque, void *obj) )

Allocate a refcounted object of usable size size managed via the RefStruct API.

By default (in the absence of flags to the contrary), the returned object is initially zeroed.

Parameters

  size | Desired usable size of the returned object.
  flags | A bitwise combination of AV_REFSTRUCT_FLAG_* flags.
  opaque | A pointer that will be passed to the free_cb callback.
  free_cb | A callback for freeing this object's content when its reference count reaches zero; it must not free the object itself.

Returns
  A pointer to an object of the desired size or NULL on failure.

Referenced by av_refstruct_alloc_ext().

## av_refstruct_alloc_ext()

void * av_refstruct_alloc_ext | ( | size_t | size,
|  | unsigned | flags,
|  | void * | opaque,
|  | void(* | free_cb )(AVRefStructOpaque opaque, void *obj) )

inline static

A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.

See also
  av_refstruct_alloc_ext_c()

Definition at line 94 of file refstruct.h.

Referenced by av_refstruct_allocz().

## av_refstruct_allocz()

void * av_refstruct_allocz | ( | size_t | size | ) |

inline static

Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL).

Definition at line 105 of file refstruct.h.

## av_refstruct_unref()

void av_refstruct_unref | ( | void * | objp | ) |

Decrement the reference count of the underlying object and automatically free the object if there are no more references to it.

*objp == NULL is legal and a no-op.

Parameters

  objp | Pointer to a pointer that is either NULL or points to an object managed via this API. *objp is set to NULL on return.

Referenced by av_refstruct_pool_uninit().

## av_refstruct_ref()

void * av_refstruct_ref | ( | void * | obj | ) |

Create a new reference to an object managed via this API, i.e.

increment the reference count of the underlying object and return obj.

Returns
  a pointer equal to obj.

## av_refstruct_ref_c()

const void * av_refstruct_ref_c | ( | const void * | obj | ) |

Analog of av_refstruct_ref(), but for constant objects.

See also
  av_refstruct_ref()

## av_refstruct_replace()

void av_refstruct_replace | ( | void * | dstp,
|  | const void * | src )

Ensure *dstp refers to the same object as src.

If *dstp is already equal to src, do nothing. Otherwise unreference *dstp and replace it with a new reference to src in case src != NULL (this involves incrementing the reference count of src's underlying object) or with NULL otherwise.

Parameters

  dstp | Pointer to a pointer that is either NULL or points to an object managed via this API.
  src | A pointer to an object managed via this API or NULL.

## av_refstruct_exclusive()

int av_refstruct_exclusive | ( | const void * | obj | ) |

Check whether the reference count of an object managed via this API is 1.

Parameters

  obj | A pointer to an object managed via this API.

Returns
  1 if the reference count of obj is 1; 0 otherwise.

## av_refstruct_pool_alloc()

AVRefStructPool * av_refstruct_pool_alloc | ( | size_t | size,
|  | unsigned | flags )

Equivalent to av_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL).

## av_refstruct_pool_alloc_ext_c()

AVRefStructPool * av_refstruct_pool_alloc_ext_c | ( | size_t | size,
|  | unsigned | flags,
|  | AVRefStructOpaque | opaque,
|  | int(* | init_cb )(AVRefStructOpaque opaque, void *obj),
|  | void(* | reset_cb )(AVRefStructOpaque opaque, void *obj),
|  | void(* | free_entry_cb )(AVRefStructOpaque opaque, void *obj),
|  | void(* | free_cb )(AVRefStructOpaque opaque) )

Allocate an AVRefStructPool, potentially using complex callbacks.

Parameters

  size | size of the entries of the pool
  flags | a bitwise combination of AV_REFSTRUCT_POOL_FLAG_* flags
  opaque | A pointer that will be passed to the callbacks below.
  init | A callback that will be called directly after a new entry has been allocated. obj has already been zeroed unless the AV_REFSTRUCT_POOL_FLAG_NO_ZEROING flag is in use.
  reset | A callback that will be called after an entry has been returned to the pool and before it is reused.
  free_entry | A callback that will be called when an entry is freed after the pool has been marked as to be uninitialized.
  free | A callback that will be called when the pool itself is freed (after the last entry has been returned and freed).

Referenced by av_refstruct_pool_alloc_ext().

## av_refstruct_pool_alloc_ext()

AVRefStructPool * av_refstruct_pool_alloc_ext | ( | size_t | size,
|  | unsigned | flags,
|  | void * | opaque,
|  | int(* | init_cb )(AVRefStructOpaque opaque, void *obj),
|  | void(* | reset_cb )(AVRefStructOpaque opaque, void *obj),
|  | void(* | free_entry_cb )(AVRefStructOpaque opaque, void *obj),
|  | void(* | free_cb )(AVRefStructOpaque opaque) )

inline static

A wrapper around av_refstruct_pool_alloc_ext_c() for the common case of a non-const qualified opaque.

See also
  av_refstruct_pool_alloc_ext_c()

Definition at line 258 of file refstruct.h.

## av_refstruct_pool_get()

void * av_refstruct_pool_get | ( | AVRefStructPool * | pool | ) |

Get an object from the pool, reusing an old one from the pool when available.

Every call to this function must happen before av_refstruct_pool_uninit(). Otherwise undefined behaviour may occur.

Parameters

  pool | the pool from which to get the object

Returns
  a reference to the object on success, NULL on error.

## av_refstruct_pool_uninit()

void av_refstruct_pool_uninit | ( | AVRefStructPool ** | poolp | ) |

inline static

Mark the pool as being available for freeing.

It will actually be freed only once all the allocated buffers associated with the pool are released. Thus it is safe to call this function while some of the allocated buffers are still in use.

It is illegal to try to get a new entry after this function has been called.

Parameters

  poolp | pointer to a pointer to either NULL or a pool to be freed. *poolp will be set to NULL.

Definition at line 292 of file refstruct.h.