FFmpeg
refstruct.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVCODEC_REFSTRUCT_H
20 #define AVCODEC_REFSTRUCT_H
21 
22 #include <stddef.h>
23 
24 /**
25  * RefStruct is an API for creating reference-counted objects
26  * with minimal overhead. The API is designed for objects,
27  * not buffers like the AVBuffer API. The main differences
28  * to the AVBuffer API are as follows:
29  *
30  * - It uses void* instead of uint8_t* as its base type due to
31  * its focus on objects.
32  * - There are no equivalents of AVBuffer and AVBufferRef.
33  * E.g. there is no way to get the usable size of the object:
34  * The user is supposed to know what is at the other end of
35  * the pointer. It also avoids one level of indirection.
36  * - Custom allocators are not supported. This allows to simplify
37  * the implementation and reduce the amount of allocations.
38  * - It also has the advantage that the user's free callback need
39  * only free the resources owned by the object, but not the
40  * object itself.
41  * - Because referencing (and replacing) an object managed by the
42  * RefStruct API does not involve allocations, they can not fail
43  * and therefore need not be checked.
44  *
45  * @note Referencing and unreferencing the buffers is thread-safe and thus
46  * may be done from multiple threads simultaneously without any need for
47  * additional locking.
48  */
49 
50 /**
51  * This union is used for all opaque parameters in this API to spare the user
52  * to cast const away in case the opaque to use is const-qualified.
53  *
54  * The functions provided by this API with an FFRefStructOpaque come in pairs
55  * named foo_c and foo. The foo function accepts void* as opaque and is just
56  * a wrapper around the foo_c function; "_c" means "(potentially) const".
57  */
58 typedef union {
59  void *nc;
60  const void *c;
62 
63 /**
64  * If this flag is set in ff_refstruct_alloc_ext_c(), the object will not
65  * be initially zeroed.
66  */
67 #define FF_REFSTRUCT_FLAG_NO_ZEROING (1 << 0)
68 
69 /**
70  * Allocate a refcounted object of usable size `size` managed via
71  * the RefStruct API.
72  *
73  * By default (in the absence of flags to the contrary),
74  * the returned object is initially zeroed.
75  *
76  * @param size Desired usable size of the returned object.
77  * @param flags A bitwise combination of FF_REFSTRUCT_FLAG_* flags.
78  * @param opaque A pointer that will be passed to the free_cb callback.
79  * @param free_cb A callback for freeing this object's content
80  * when its reference count reaches zero;
81  * it must not free the object itself.
82  * @return A pointer to an object of the desired size or NULL on failure.
83  */
84 void *ff_refstruct_alloc_ext_c(size_t size, unsigned flags, FFRefStructOpaque opaque,
85  void (*free_cb)(FFRefStructOpaque opaque, void *obj));
86 
87 /**
88  * A wrapper around ff_refstruct_alloc_ext_c() for the common case
89  * of a non-const qualified opaque.
90  *
91  * @see ff_refstruct_alloc_ext_c()
92  */
93 static inline
94 void *ff_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque,
95  void (*free_cb)(FFRefStructOpaque opaque, void *obj))
96 {
97  return ff_refstruct_alloc_ext_c(size, flags, (FFRefStructOpaque){.nc = opaque},
98  free_cb);
99 }
100 
101 /**
102  * Equivalent to ff_refstruct_alloc_ext(size, 0, NULL, NULL)
103  */
104 static inline
106 {
107  return ff_refstruct_alloc_ext(size, 0, NULL, NULL);
108 }
109 
110 /**
111  * Decrement the reference count of the underlying object and automatically
112  * free the object if there are no more references to it.
113  *
114  * `*objp == NULL` is legal and a no-op.
115  *
116  * @param objp Pointer to a pointer that is either NULL or points to an object
117  * managed via this API. `*objp` is set to NULL on return.
118  */
119 void ff_refstruct_unref(void *objp);
120 
121 /**
122  * Create a new reference to an object managed via this API,
123  * i.e. increment the reference count of the underlying object
124  * and return obj.
125  * @return a pointer equal to obj.
126  */
127 void *ff_refstruct_ref(void *obj);
128 
129 /**
130  * Analog of ff_refstruct_ref(), but for constant objects.
131  * @see ff_refstruct_ref()
132  */
133 const void *ff_refstruct_ref_c(const void *obj);
134 
135 /**
136  * Ensure `*dstp` refers to the same object as src.
137  *
138  * If `*dstp` is already equal to src, do nothing. Otherwise unreference `*dstp`
139  * and replace it with a new reference to src in case `src != NULL` (this
140  * involves incrementing the reference count of src's underlying object) or
141  * with NULL otherwise.
142  *
143  * @param dstp Pointer to a pointer that is either NULL or points to an object
144  * managed via this API.
145  * @param src A pointer to an object managed via this API or NULL.
146  */
147 void ff_refstruct_replace(void *dstp, const void *src);
148 
149 /**
150  * Check whether the reference count of an object managed
151  * via this API is 1.
152  *
153  * @param obj A pointer to an object managed via this API.
154  * @return 1 if the reference count of obj is 1; 0 otherwise.
155  */
156 int ff_refstruct_exclusive(const void *obj);
157 
158 #endif /* AVCODEC_REFSTRUCT_H */
ff_refstruct_alloc_ext
static void * ff_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(FFRefStructOpaque opaque, void *obj))
A wrapper around ff_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:94
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
ff_refstruct_exclusive
int ff_refstruct_exclusive(const void *obj)
Check whether the reference count of an object managed via this API is 1.
Definition: refstruct.c:170
NULL
#define NULL
Definition: coverity.c:32
ff_refstruct_allocz
static void * ff_refstruct_allocz(size_t size)
Equivalent to ff_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition: refstruct.h:105
ff_refstruct_ref_c
const void * ff_refstruct_ref_c(const void *obj)
Analog of ff_refstruct_ref(), but for constant objects.
Definition: refstruct.c:145
ff_refstruct_alloc_ext_c
void * ff_refstruct_alloc_ext_c(size_t size, unsigned flags, FFRefStructOpaque opaque, void(*free_cb)(FFRefStructOpaque opaque, void *obj))
Allocate a refcounted object of usable size size managed via the RefStruct API.
Definition: refstruct.c:98
size
int size
Definition: twinvq_data.h:10344
FFRefStructOpaque::nc
void * nc
Definition: refstruct.h:59
ff_refstruct_ref
void * ff_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:136
ff_refstruct_replace
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:156
FFRefStructOpaque::c
const void * c
Definition: refstruct.h:60
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:116