History

avcodec/refstruct: Allow to share pools

To do this, make FFRefStructPool itself refcounted according to the RefStruct API.

Andreas Rheinhardt committed 4 years ago

Commit 0c44f631 parent 92abc722 files · +25 −14Browse files

Unified · Split

2 files
  1. libavcodec/refstruct.c +21 −13
  2. libavcodec/refstruct.h +4 −1
libavcodec/refstruct.c+21−13
@@ -210,7 +210,7 @@ static void pool_free(FFRefStructPool *pool) · 209 unmodified lines
210210 ff_mutex_destroy(&pool->mutex);
211211 if (pool->free_cb)
212212 pool->free_cb(pool->opaque);
213− av_free(pool);
213+ av_free(get_refcount(pool));
214214 }
215215
216216 static void pool_free_entry(FFRefStructPool *pool, RefCount *ref)
@@ -301,13 +301,22 @@ void *ff_refstruct_pool_get(FFRefStructPool *pool) · 84 unmodified lines
301301 return ret;
302302 }
303303
304−void ff_refstruct_pool_uninit(FFRefStructPool **poolp)
304+/**
305+ * Hint: The content of pool_unref() and refstruct_pool_uninit()
306+ * could currently be merged; they are only separate functions
307+ * in case we would ever introduce weak references.
308+ */
309+static void pool_unref(void *ref)
305310 {
306− FFRefStructPool *pool = *poolp;
307− RefCount *entry;
311+ FFRefStructPool *pool = get_userdata(ref);
312+ if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
313+ pool_free(pool);
314+}
308315
309− if (!pool)
310− return;
316+static void refstruct_pool_uninit(FFRefStructOpaque unused, void *obj)
317+{
318+ FFRefStructPool *pool = obj;
319+ RefCount *entry;
311320
312321 ff_mutex_lock(&pool->mutex);
313322 ff_assert(!pool->uninited);
@@ -321,11 +330,6 @@ void ff_refstruct_pool_uninit(FFRefStructPool **poolp) · 7 unmodified lines
321330 pool_free_entry(pool, entry);
322331 entry = next;
323332 }
324−
325− if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
326− pool_free(pool);
327−
328− *poolp = NULL;
329333 }
330334
331335 FFRefStructPool *ff_refstruct_pool_alloc(size_t size, unsigned flags)
@@ -340,11 +344,13 @@ FFRefStructPool *ff_refstruct_pool_alloc_ext_c(size_t size, unsigned flags, · 8 unmodified lines
340344 void (*free_entry_cb)(FFRefStructOpaque opaque, void *obj),
341345 void (*free_cb)(FFRefStructOpaque opaque))
342346 {
343− FFRefStructPool *pool = av_mallocz(sizeof(*pool));
347+ FFRefStructPool *pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL,
348+ refstruct_pool_uninit);
344349 int err;
345350
346351 if (!pool)
347352 return NULL;
353+ get_refcount(pool)->free = pool_unref;
348354
349355 pool->size = size;
350356 pool->opaque = opaque;
@@ -371,7 +377,9 @@ FFRefStructPool *ff_refstruct_pool_alloc_ext_c(size_t size, unsigned flags, · 20 unmodified lines
371377
372378 err = ff_mutex_init(&pool->mutex, NULL);
373379 if (err) {
374− av_free(pool);
380+ // Don't call ff_refstruct_uninit() on pool, as it hasn't been properly
381+ // set up and is just a POD right now.
382+ av_free(get_refcount(pool));
375383 return NULL;
376384 }
377385 return pool;
libavcodec/refstruct.h+4−1
@@ -289,6 +289,9 @@ void *ff_refstruct_pool_get(FFRefStructPool *pool); · 288 unmodified lines
289289 * @param poolp pointer to a pointer to either NULL or a pool to be freed.
290290 * `*poolp` will be set to NULL.
291291 */
292−void ff_refstruct_pool_uninit(FFRefStructPool **poolp);
292+static inline void ff_refstruct_pool_uninit(FFRefStructPool **poolp)
293+{
294+ ff_refstruct_unref(poolp);
295+}
293296
294297 #endif /* AVCODEC_REFSTRUCT_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t