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
210 ff_mutex_destroy(&pool->mutex);210 ff_mutex_destroy(&pool->mutex);
211 if (pool->free_cb)211 if (pool->free_cb)
212 pool->free_cb(pool->opaque);212 pool->free_cb(pool->opaque);
213 av_free(pool);213 av_free(get_refcount(pool));
214}214}
215 215
216static void pool_free_entry(FFRefStructPool *pool, RefCount *ref)216static void pool_free_entry(FFRefStructPool *pool, RefCount *ref)
@@ -301,13 +301,22 @@ void *ff_refstruct_pool_get(FFRefStructPool *pool) · 84 unmodified lines
301 return ret;301 return ret;
302}302}
303 303
304void 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 */
309static void pool_unref(void *ref)
305{310{
306 FFRefStructPool *pool = *poolp;311 FFRefStructPool *pool = get_userdata(ref);
307 RefCount *entry;312 if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
313 pool_free(pool);
314}
308 315
309 if (!pool)316static void refstruct_pool_uninit(FFRefStructOpaque unused, void *obj)
310 return;317{
318 FFRefStructPool *pool = obj;
319 RefCount *entry;
311 320
312 ff_mutex_lock(&pool->mutex);321 ff_mutex_lock(&pool->mutex);
313 ff_assert(!pool->uninited);322 ff_assert(!pool->uninited);
@@ -321,11 +330,6 @@ void ff_refstruct_pool_uninit(FFRefStructPool **poolp) · 7 unmodified lines
321 pool_free_entry(pool, entry);330 pool_free_entry(pool, entry);
322 entry = next;331 entry = next;
323 }332 }
324
325 if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
326 pool_free(pool);
327
328 *poolp = NULL;
329}333}
330 334
331FFRefStructPool *ff_refstruct_pool_alloc(size_t size, unsigned flags)335FFRefStructPool *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
340 void (*free_entry_cb)(FFRefStructOpaque opaque, void *obj),344 void (*free_entry_cb)(FFRefStructOpaque opaque, void *obj),
341 void (*free_cb)(FFRefStructOpaque opaque))345 void (*free_cb)(FFRefStructOpaque opaque))
342{346{
343 FFRefStructPool *pool = av_mallocz(sizeof(*pool));347 FFRefStructPool *pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL,
348 refstruct_pool_uninit);
344 int err;349 int err;
345 350
346 if (!pool)351 if (!pool)
347 return NULL;352 return NULL;
353 get_refcount(pool)->free = pool_unref;
348 354
349 pool->size = size;355 pool->size = size;
350 pool->opaque = opaque;356 pool->opaque = opaque;
@@ -371,7 +377,9 @@ FFRefStructPool *ff_refstruct_pool_alloc_ext_c(size_t size, unsigned flags, · 20 unmodified lines
371 377
372 err = ff_mutex_init(&pool->mutex, NULL);378 err = ff_mutex_init(&pool->mutex, NULL);
373 if (err) {379 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));
375 return NULL;383 return NULL;
376 }384 }
377 return pool;385 return pool;
libavcodec/refstruct.h+4−1
@@ -289,6 +289,9 @@ void *ff_refstruct_pool_get(FFRefStructPool *pool); · 288 unmodified lines
289 * @param poolp pointer to a pointer to either NULL or a pool to be freed.289 * @param poolp pointer to a pointer to either NULL or a pool to be freed.
290 * `*poolp` will be set to NULL.290 * `*poolp` will be set to NULL.
291 */291 */
292void ff_refstruct_pool_uninit(FFRefStructPool **poolp);292static inline void ff_refstruct_pool_uninit(FFRefStructPool **poolp)
293{
294 ff_refstruct_unref(poolp);
295}
293 296
294#endif /* AVCODEC_REFSTRUCT_H */297#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