Commit Graph

42 Commits

Author SHA1 Message Date
Andreas Rheinhardt d576b37fa7 avutil/buffer: Never poison returned buffers
Poisoning returned buffers is based around the implicit assumption
that the contents of said buffers are transient. Yet this is not true
for the buffer pools used by the various hardware contexts which store
important state in there that needs to be preserved.
Furthermore, the current code is also based on the assumption
that the complete buffer pointed to by AVBuffer->data coincides with
AVBufferRef->data; yet an implementation might store some data of its
own before the actual user-visible data (accessible via AVBufferRef)
which would be broken by the current code.

(This is of course yet more proof that the AVBuffer API is not the right
tool for the hardware contexts.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-08-10 18:49:35 +02:00
Andreas Rheinhardt 4e0da7d311 avutil/buffer: Avoid allocation of AVBuffer when using buffer pool
Do this by putting an AVBuffer structure into BufferPoolEntry and
reuse it for all subsequent uses of said BufferPoolEntry.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-18 23:16:49 +02:00
James Almer ccfdef79b1 avutil/buffer: constify some function parameters
Reviewed-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-09-17 13:28:09 -03:00
Andreas Rheinhardt ef6a9e5e31 avutil/buffer: Switch AVBuffer API to size_t
Announced in 14040a1d91.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 10:43:13 -03:00
James Almer 14040a1d91 avutil/buffer: change public function and struct size parameter types to size_t
Signed-off-by: James Almer <jamrial@gmail.com>
2021-03-10 20:26:36 -03:00
James Almer 45a2902976 avutil/buffer: free all pooled buffers immediately after uninitializing the pool
No buffer will be fetched from the pool after it's uninitialized, so there's
no benefit from waiting until every single buffer has been returned to it
before freeing them all.
This should free some memory in certain scenarios, which can be beneficial in
low memory systems.

Based on a patch by Jonas Karlman.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-02-24 10:45:30 -03:00
Anton Khirnov 68918d3b7f lavu/buffer: add a convenience function for replacing buffers
A common pattern e.g. in libavcodec is replacing/updating buffer
references: unref old one, ref new one. This function allows simplifying
such code and avoiding unnecessary refs+unrefs if the references are
already equivalent.
2020-09-28 11:33:35 +02:00
Nicolas George 2b71cd3e0b lavu/buffer: forward av_buffer_realloc() error code.
Fix CID 1457235.
2020-08-21 11:44:30 +02:00
James Almer b6c8444e23 avutil/buffer: separate public and internal flags inside AVBuffers
It's better to not mix user provided flags and internal flags set by
AVBufferRef helper functions.

Signed-off-by: James Almer <jamrial@gmail.com>
2020-06-05 10:07:05 -03:00
James Almer ec39c2276a avutil/buffer: use the default allocator if none is provided to av_buffer_pool_init2()
Signed-off-by: James Almer <jamrial@gmail.com>
2020-06-05 10:07:04 -03:00
Marton Balint 16685114d5 avutil/buffer: add av_buffer_pool_buffer_get_opaque
In order to access the original opaque parameter of a buffer in the buffer
pool. (The buffer pool implementation overrides the normal opaque parameter but
also saves it so it is accessible).

v2: add assertion check before dereferencing the BufferPoolEntry.

Signed-off-by: Marton Balint <cus@passwd.hu>
2019-12-26 00:47:24 +01:00
Zhao Zhili 71fd72070c avutil/buffer: use appropriate atomic operations
No functional changes. ref/unref vs add/sub is symmetrical.

Signed-off-by: James Almer <jamrial@gmail.com>
2019-12-05 20:53:52 -03:00
Clément Bœsch 443e969293 Merge commit '27079a426c9d3db918b158976e44b9b143d78e1c'
* commit '27079a426c9d3db918b158976e44b9b143d78e1c':
  buffer: convert to stdatomic

Merged-by: Clément Bœsch <u@pkh.me>
2017-03-22 17:46:01 +01:00
Clément Bœsch 67d8eabdbb lavu/buffer: drop USE_ATOMICS
USE_ATOMICS is only set if there is no thread implementation enabled, in
which case you can't expect any lock mechanism from FFmpeg.

This is also conflicting with the incoming use of stdatomic.
2017-03-22 17:40:03 +01:00
Clément Bœsch 5e4a572699 Merge commit '24a362569bff1d4161742fffaca80a4a4428be8a'
* commit '24a362569bff1d4161742fffaca80a4a4428be8a':
  buffer: fix av_buffer_realloc() when the data is offset wrt buffer start

Merged-by: Clément Bœsch <u@pkh.me>
2017-03-16 21:42:45 +01:00
Anton Khirnov 27079a426c buffer: convert to stdatomic 2016-10-02 18:58:04 +02:00
Anton Khirnov 24a362569b buffer: fix av_buffer_realloc() when the data is offset wrt buffer start
In such a case behave as if the buffer was not reallocatable -- allocate a
new one and copy the data (preserving just the part described by the
reference passed to av_buffer_realloc).

CC: libav-stable@libav.org
Reported-By: wm4 <nfxjfg@googlemail.com>
2016-08-03 11:07:15 +02:00
Derek Buitenhuis 26abd5149e Merge commit '721a4efc0545548a241080b53ab480e34f366240'
* commit '721a4efc0545548a241080b53ab480e34f366240':
  buffer: add support for pools using caller data in allocation

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
2016-02-17 16:07:16 +00:00
Anton Khirnov 721a4efc05 buffer: add support for pools using caller data in allocation
This should allow using more complex allocators than simple malloc
wrappers.
2016-02-14 21:24:39 +01:00
Michael Niedermayer 26d81b5703 avutil/buffer: Avoid moving the AVBufferRef to a new place in memory in av_buffer_make_writable()
This allows making a AVBufferRef writable without the need to
update all pointers to it

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2015-03-12 02:15:28 +01:00
Michael Niedermayer 35fad1e9c9 avutil/buffer: Avoid moving the AVBufferRef to a new place in memory in av_buffer_realloc()
This allows reallocating AVBufferRefs without the need to update
all pointers to it

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2015-03-12 02:15:28 +01:00
Michael Niedermayer b4f5da2651 avutil/buffer: factor buffer_replace() out
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2015-01-23 04:45:05 +01:00
Michael Niedermayer 890d8f44fd avutil/buffer: add #if USE_ATOMICS around atomics specific functions to ensure sync types arent mixed by mistake
Fixes CID1257011

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-12-06 20:47:41 +01:00
Michael Niedermayer c299b6fd08 avutil/buffer: Move USE_ATOMICS to thread.h to avoid it becoming out of sync with it
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-11-29 12:26:22 +01:00
Michael Niedermayer 6db8cd8f37 Merge commit 'fbd6c97f9ca858140df16dd07200ea0d4bdc1a83'
* commit 'fbd6c97f9ca858140df16dd07200ea0d4bdc1a83':
  lavu: fix memory leaks by using a mutex instead of atomics

Conflicts:
	libavutil/buffer.c

The atomics code is left in place as a fallback for synchronization in the
absence of p/w32 threads. Our ABI did not requires applications to
only use threads (and matching ones) to what libavutil was build with
Our code also was not affected by the leak this change fixes, though
no question the atomics based implementation is not pretty at all.
First and foremost the code must work, being pretty comes after that.

If this causes problems, for example when libavutil is used by multiple
applications each using a different kind of threading system then the
default possibly has to be changed to the uglier atomics.

See: cea3a63ba3
Merged-by: Michael Niedermayer <michaelni@gmx.at>
2014-11-27 23:42:16 +01:00
wm4 fbd6c97f9c lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).

This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.

Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.

Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-27 13:36:00 +01:00
Lukasz Marek bba7b6fc41 Revert "lavu/buffer: add release function"
This reverts commit 3144440004.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-03-06 03:23:40 +01:00
Lukasz Marek 3144440004 lavu/buffer: add release function
new function allows to unref buffer and obtain its data.

Signed-off-by: Lukasz Marek <lukasz.m.luki@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-02-25 01:38:52 +01:00
Stefano Sabatini 84be806982 lavu: define FF_MEMORY_POISON and use it
Allow single-place definition of constant used to fill poisoned memory.
2013-05-13 13:52:26 +02:00
Michael Niedermayer dc92464aee avutil: add av_buffer_get_ref_count()
This function is quite usefull for debuging

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-30 20:28:00 +01:00
Xidorn Quan c81d2fa96d avutil/buffer: add get_opaque
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-29 17:31:16 +01:00
Michael Niedermayer 8e944891ce avutil/buffer: remove redundant memory poisoning
Found-by: ubitux
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-24 02:21:42 +01:00
Michael Niedermayer 058c002932 avutil/buffer: support memory poisoning
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-24 01:44:48 +01:00
Michael Niedermayer f1c3d8b344 avutil/add_to_pool: remove unused assgnment
Fixed CID991859
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-19 13:35:10 +01:00
Michael Niedermayer cea3a63ba3 avutil/buffer: Fix race in pool.
This race will always happen sooner or later in a multi-threaded
environment and it will over time lead to OOM.
This fix works by spinning, there are other ways by which this
can be fixed, like simply detecting the issue after it happened
and freeing the over-allocated memory or simply using a mutex.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-18 19:19:22 +01:00
Michael Niedermayer c603f22683 avutil/get_pool: remove dead operations whichs result is never used.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-17 18:40:15 +01:00
Michael Niedermayer e3be7b1159 avutil/get_pool: Remove redundant initial atomic operation
602->442 dezicycles

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-17 18:40:15 +01:00
Clément Bœsch 80661e0f55 buffer: use the atomic get instead of the add and fetch variant. 2013-03-12 12:12:49 +01:00
Michael Niedermayer 532f31a695 Merge commit '1cec0624d0e6f48590283a57169b58b9fe8449d3'
* commit '1cec0624d0e6f48590283a57169b58b9fe8449d3':
  AVBuffer: add a new API for buffer pools

Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-08 16:06:20 +01:00
Michael Niedermayer 36099df521 Merge commit '8e401dbe90cc77b1f3067a917d9fa48cefa3fcdb'
* commit '8e401dbe90cc77b1f3067a917d9fa48cefa3fcdb':
  lavu: add a new API for reference-counted data buffers.

Conflicts:
	libavutil/Makefile

Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-08 16:01:00 +01:00
Anton Khirnov 1cec0624d0 AVBuffer: add a new API for buffer pools 2013-03-08 07:33:28 +01:00
Anton Khirnov 8e401dbe90 lavu: add a new API for reference-counted data buffers. 2013-03-08 07:33:03 +01:00