Path: blob/master/dep/ffmpeg/include/libavcodec/refstruct.h
5196 views
/*1* This file is part of FFmpeg.2*3* FFmpeg is free software; you can redistribute it and/or4* modify it under the terms of the GNU Lesser General Public5* License as published by the Free Software Foundation; either6* version 2.1 of the License, or (at your option) any later version.7*8* FFmpeg is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11* Lesser General Public License for more details.12*13* You should have received a copy of the GNU Lesser General Public14* License along with FFmpeg; if not, write to the Free Software15* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA16*/1718#ifndef AVUTIL_REFSTRUCT_H19#define AVUTIL_REFSTRUCT_H2021#include <stddef.h>2223/**24* RefStruct is an API for creating reference-counted objects25* with minimal overhead. The API is designed for objects,26* not buffers like the AVBuffer API. The main differences27* to the AVBuffer API are as follows:28*29* - It uses void* instead of uint8_t* as its base type due to30* its focus on objects.31* - There are no equivalents of AVBuffer and AVBufferRef.32* E.g. there is no way to get the usable size of the object:33* The user is supposed to know what is at the other end of34* the pointer. It also avoids one level of indirection.35* - Custom allocators are not supported. This allows to simplify36* the implementation and reduce the amount of allocations.37* - It also has the advantage that the user's free callback need38* only free the resources owned by the object, but not the39* object itself.40* - Because referencing (and replacing) an object managed by the41* RefStruct API does not involve allocations, they can not fail42* and therefore need not be checked.43*44* @note Referencing and unreferencing the buffers is thread-safe and thus45* may be done from multiple threads simultaneously without any need for46* additional locking.47*/4849/**50* This union is used for all opaque parameters in this API to spare the user51* to cast const away in case the opaque to use is const-qualified.52*53* The functions provided by this API with an AVRefStructOpaque come in pairs54* named foo_c and foo. The foo function accepts void* as opaque and is just55* a wrapper around the foo_c function; "_c" means "(potentially) const".56*/57typedef union {58void *nc;59const void *c;60} AVRefStructOpaque;6162/**63* If this flag is set in av_refstruct_alloc_ext_c(), the object will not64* be initially zeroed.65*/66#define AV_REFSTRUCT_FLAG_NO_ZEROING (1 << 0)6768/**69* Allocate a refcounted object of usable size `size` managed via70* the RefStruct API.71*72* By default (in the absence of flags to the contrary),73* the returned object is initially zeroed.74*75* @param size Desired usable size of the returned object.76* @param flags A bitwise combination of AV_REFSTRUCT_FLAG_* flags.77* @param opaque A pointer that will be passed to the free_cb callback.78* @param free_cb A callback for freeing this object's content79* when its reference count reaches zero;80* it must not free the object itself.81* @return A pointer to an object of the desired size or NULL on failure.82*/83void *av_refstruct_alloc_ext_c(size_t size, unsigned flags, AVRefStructOpaque opaque,84void (*free_cb)(AVRefStructOpaque opaque, void *obj));8586/**87* A wrapper around av_refstruct_alloc_ext_c() for the common case88* of a non-const qualified opaque.89*90* @see av_refstruct_alloc_ext_c()91*/92static inline93void *av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque,94void (*free_cb)(AVRefStructOpaque opaque, void *obj))95{96return av_refstruct_alloc_ext_c(size, flags, (AVRefStructOpaque){.nc = opaque},97free_cb);98}99100/**101* Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)102*/103static inline104void *av_refstruct_allocz(size_t size)105{106return av_refstruct_alloc_ext(size, 0, NULL, NULL);107}108109/**110* Decrement the reference count of the underlying object and automatically111* free the object if there are no more references to it.112*113* `*objp == NULL` is legal and a no-op.114*115* @param objp Pointer to a pointer that is either NULL or points to an object116* managed via this API. `*objp` is set to NULL on return.117*/118void av_refstruct_unref(void *objp);119120/**121* Create a new reference to an object managed via this API,122* i.e. increment the reference count of the underlying object123* and return obj.124* @return a pointer equal to obj.125*/126void *av_refstruct_ref(void *obj);127128/**129* Analog of av_refstruct_ref(), but for constant objects.130* @see av_refstruct_ref()131*/132const void *av_refstruct_ref_c(const void *obj);133134/**135* Ensure `*dstp` refers to the same object as src.136*137* If `*dstp` is already equal to src, do nothing. Otherwise unreference `*dstp`138* and replace it with a new reference to src in case `src != NULL` (this139* involves incrementing the reference count of src's underlying object) or140* with NULL otherwise.141*142* @param dstp Pointer to a pointer that is either NULL or points to an object143* managed via this API.144* @param src A pointer to an object managed via this API or NULL.145*/146void av_refstruct_replace(void *dstp, const void *src);147148/**149* Check whether the reference count of an object managed150* via this API is 1.151*152* @param obj A pointer to an object managed via this API.153* @return 1 if the reference count of obj is 1; 0 otherwise.154*/155int av_refstruct_exclusive(const void *obj);156157/**158* AVRefStructPool is an API for a thread-safe pool of objects managed159* via the RefStruct API.160*161* Frequently allocating and freeing large or complicated objects may be slow162* and wasteful. This API is meant to solve this in cases when the caller163* needs a set of interchangeable objects.164*165* At the beginning, the user must call allocate the pool via166* av_refstruct_pool_alloc() or its analogue av_refstruct_pool_alloc_ext().167* Then whenever an object is needed, call av_refstruct_pool_get() to168* get a new or reused object from the pool. This new object works in all169* aspects the same way as the ones created by av_refstruct_alloc_ext().170* However, when the last reference to this object is unreferenced, it is171* (optionally) reset and returned to the pool instead of being freed and172* will be reused for subsequent av_refstruct_pool_get() calls.173*174* When the caller is done with the pool and no longer needs to create any new175* objects, av_refstruct_pool_uninit() must be called to mark the pool as176* freeable. Then entries returned to the pool will then be freed.177* Once all the entries are freed, the pool will automatically be freed.178*179* Allocating and releasing objects with this API is thread-safe as long as180* the user-supplied callbacks (if provided) are thread-safe.181*/182183/**184* The buffer pool. This structure is opaque and not meant to be accessed185* directly. It is allocated with the allocators below and freed with186* av_refstruct_pool_uninit().187*/188typedef struct AVRefStructPool AVRefStructPool;189190/**191* If this flag is not set, every object in the pool will be zeroed before192* the init callback is called or before it is turned over to the user193* for the first time if no init callback has been provided.194*/195#define AV_REFSTRUCT_POOL_FLAG_NO_ZEROING AV_REFSTRUCT_FLAG_NO_ZEROING196/**197* If this flag is set and both init_cb and reset_cb callbacks are provided,198* then reset_cb will be called if init_cb fails.199* The object passed to reset_cb will be in the state left by init_cb.200*/201#define AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR (1 << 16)202/**203* If this flag is set and both init_cb and free_entry_cb callbacks are204* provided, then free_cb will be called if init_cb fails.205*206* It will be called after reset_cb in case reset_cb and the207* AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR flag are also set.208*209* The object passed to free_cb will be in the state left by210* the callbacks applied earlier (init_cb potentially followed by reset_cb).211*/212#define AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR (1 << 17)213/**214* If this flag is set, the entries will be zeroed before215* being returned to the user (after the init or reset callbacks216* have been called (if provided)). Furthermore, to avoid zeroing twice217* it also makes the pool behave as if the AV_REFSTRUCT_POOL_FLAG_NO_ZEROING218* flag had been provided.219*/220#define AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME (1 << 18)221222/**223* Equivalent to av_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL)224*/225AVRefStructPool *av_refstruct_pool_alloc(size_t size, unsigned flags);226227/**228* Allocate an AVRefStructPool, potentially using complex callbacks.229*230* @param size size of the entries of the pool231* @param flags a bitwise combination of AV_REFSTRUCT_POOL_FLAG_* flags232* @param opaque A pointer that will be passed to the callbacks below.233* @param init A callback that will be called directly after a new entry234* has been allocated. obj has already been zeroed unless235* the AV_REFSTRUCT_POOL_FLAG_NO_ZEROING flag is in use.236* @param reset A callback that will be called after an entry has been237* returned to the pool and before it is reused.238* @param free_entry A callback that will be called when an entry is freed239* after the pool has been marked as to be uninitialized.240* @param free A callback that will be called when the pool itself is241* freed (after the last entry has been returned and freed).242*/243AVRefStructPool *av_refstruct_pool_alloc_ext_c(size_t size, unsigned flags,244AVRefStructOpaque opaque,245int (*init_cb)(AVRefStructOpaque opaque, void *obj),246void (*reset_cb)(AVRefStructOpaque opaque, void *obj),247void (*free_entry_cb)(AVRefStructOpaque opaque, void *obj),248void (*free_cb)(AVRefStructOpaque opaque));249250/**251* A wrapper around av_refstruct_pool_alloc_ext_c() for the common case252* of a non-const qualified opaque.253*254* @see av_refstruct_pool_alloc_ext_c()255*/256static inline257AVRefStructPool *av_refstruct_pool_alloc_ext(size_t size, unsigned flags,258void *opaque,259int (*init_cb)(AVRefStructOpaque opaque, void *obj),260void (*reset_cb)(AVRefStructOpaque opaque, void *obj),261void (*free_entry_cb)(AVRefStructOpaque opaque, void *obj),262void (*free_cb)(AVRefStructOpaque opaque))263{264return av_refstruct_pool_alloc_ext_c(size, flags, (AVRefStructOpaque){.nc = opaque},265init_cb, reset_cb, free_entry_cb, free_cb);266}267268/**269* Get an object from the pool, reusing an old one from the pool when270* available.271*272* Every call to this function must happen before av_refstruct_pool_uninit().273* Otherwise undefined behaviour may occur.274*275* @param pool the pool from which to get the object276* @return a reference to the object on success, NULL on error.277*/278void *av_refstruct_pool_get(AVRefStructPool *pool);279280/**281* Mark the pool as being available for freeing. It will actually be freed282* only once all the allocated buffers associated with the pool are released.283* Thus it is safe to call this function while some of the allocated buffers284* are still in use.285*286* It is illegal to try to get a new entry after this function has been called.287*288* @param poolp pointer to a pointer to either NULL or a pool to be freed.289* `*poolp` will be set to NULL.290*/291static inline void av_refstruct_pool_uninit(AVRefStructPool **poolp)292{293av_refstruct_unref(poolp);294}295296#endif /* AVUTIL_REFSTRUCT_H */297298299