buffer.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. #include <stdint.h>
  19. #include <string.h>
  20. #include "atomic.h"
  21. #include "buffer_internal.h"
  22. #include "common.h"
  23. #include "mem.h"
  24. #include "thread.h"
  25. //#define USE_ATOMICS !(HAVE_PTHREADS || HAVE_W32THREADS)
  26. #define USE_ATOMICS 1 // can be changed to the above once it received more testing in master
  27. AVBufferRef *av_buffer_create(uint8_t *data, int size,
  28. void (*free)(void *opaque, uint8_t *data),
  29. void *opaque, int flags)
  30. {
  31. AVBufferRef *ref = NULL;
  32. AVBuffer *buf = NULL;
  33. buf = av_mallocz(sizeof(*buf));
  34. if (!buf)
  35. return NULL;
  36. buf->data = data;
  37. buf->size = size;
  38. buf->free = free ? free : av_buffer_default_free;
  39. buf->opaque = opaque;
  40. buf->refcount = 1;
  41. if (flags & AV_BUFFER_FLAG_READONLY)
  42. buf->flags |= BUFFER_FLAG_READONLY;
  43. ref = av_mallocz(sizeof(*ref));
  44. if (!ref) {
  45. av_freep(&buf);
  46. return NULL;
  47. }
  48. ref->buffer = buf;
  49. ref->data = data;
  50. ref->size = size;
  51. return ref;
  52. }
  53. void av_buffer_default_free(void *opaque, uint8_t *data)
  54. {
  55. av_free(data);
  56. }
  57. AVBufferRef *av_buffer_alloc(int size)
  58. {
  59. AVBufferRef *ret = NULL;
  60. uint8_t *data = NULL;
  61. data = av_malloc(size);
  62. if (!data)
  63. return NULL;
  64. ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
  65. if (!ret)
  66. av_freep(&data);
  67. return ret;
  68. }
  69. AVBufferRef *av_buffer_allocz(int size)
  70. {
  71. AVBufferRef *ret = av_buffer_alloc(size);
  72. if (!ret)
  73. return NULL;
  74. memset(ret->data, 0, size);
  75. return ret;
  76. }
  77. AVBufferRef *av_buffer_ref(AVBufferRef *buf)
  78. {
  79. AVBufferRef *ret = av_mallocz(sizeof(*ret));
  80. if (!ret)
  81. return NULL;
  82. *ret = *buf;
  83. avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
  84. return ret;
  85. }
  86. void av_buffer_unref(AVBufferRef **buf)
  87. {
  88. AVBuffer *b;
  89. if (!buf || !*buf)
  90. return;
  91. b = (*buf)->buffer;
  92. av_freep(buf);
  93. if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
  94. b->free(b->opaque, b->data);
  95. av_freep(&b);
  96. }
  97. }
  98. int av_buffer_is_writable(const AVBufferRef *buf)
  99. {
  100. if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
  101. return 0;
  102. return avpriv_atomic_int_get(&buf->buffer->refcount) == 1;
  103. }
  104. void *av_buffer_get_opaque(const AVBufferRef *buf)
  105. {
  106. return buf->buffer->opaque;
  107. }
  108. int av_buffer_get_ref_count(const AVBufferRef *buf)
  109. {
  110. return buf->buffer->refcount;
  111. }
  112. int av_buffer_make_writable(AVBufferRef **pbuf)
  113. {
  114. AVBufferRef *newbuf, *buf = *pbuf;
  115. if (av_buffer_is_writable(buf))
  116. return 0;
  117. newbuf = av_buffer_alloc(buf->size);
  118. if (!newbuf)
  119. return AVERROR(ENOMEM);
  120. memcpy(newbuf->data, buf->data, buf->size);
  121. av_buffer_unref(pbuf);
  122. *pbuf = newbuf;
  123. return 0;
  124. }
  125. int av_buffer_realloc(AVBufferRef **pbuf, int size)
  126. {
  127. AVBufferRef *buf = *pbuf;
  128. uint8_t *tmp;
  129. if (!buf) {
  130. /* allocate a new buffer with av_realloc(), so it will be reallocatable
  131. * later */
  132. uint8_t *data = av_realloc(NULL, size);
  133. if (!data)
  134. return AVERROR(ENOMEM);
  135. buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
  136. if (!buf) {
  137. av_freep(&data);
  138. return AVERROR(ENOMEM);
  139. }
  140. buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
  141. *pbuf = buf;
  142. return 0;
  143. } else if (buf->size == size)
  144. return 0;
  145. if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
  146. !av_buffer_is_writable(buf)) {
  147. /* cannot realloc, allocate a new reallocable buffer and copy data */
  148. AVBufferRef *new = NULL;
  149. av_buffer_realloc(&new, size);
  150. if (!new)
  151. return AVERROR(ENOMEM);
  152. memcpy(new->data, buf->data, FFMIN(size, buf->size));
  153. av_buffer_unref(pbuf);
  154. *pbuf = new;
  155. return 0;
  156. }
  157. tmp = av_realloc(buf->buffer->data, size);
  158. if (!tmp)
  159. return AVERROR(ENOMEM);
  160. buf->buffer->data = buf->data = tmp;
  161. buf->buffer->size = buf->size = size;
  162. return 0;
  163. }
  164. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
  165. {
  166. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  167. if (!pool)
  168. return NULL;
  169. ff_mutex_init(&pool->mutex, NULL);
  170. pool->size = size;
  171. pool->alloc = alloc ? alloc : av_buffer_alloc;
  172. avpriv_atomic_int_set(&pool->refcount, 1);
  173. return pool;
  174. }
  175. /*
  176. * This function gets called when the pool has been uninited and
  177. * all the buffers returned to it.
  178. */
  179. static void buffer_pool_free(AVBufferPool *pool)
  180. {
  181. while (pool->pool) {
  182. BufferPoolEntry *buf = pool->pool;
  183. pool->pool = buf->next;
  184. buf->free(buf->opaque, buf->data);
  185. av_freep(&buf);
  186. }
  187. ff_mutex_destroy(&pool->mutex);
  188. av_freep(&pool);
  189. }
  190. void av_buffer_pool_uninit(AVBufferPool **ppool)
  191. {
  192. AVBufferPool *pool;
  193. if (!ppool || !*ppool)
  194. return;
  195. pool = *ppool;
  196. *ppool = NULL;
  197. if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
  198. buffer_pool_free(pool);
  199. }
  200. /* remove the whole buffer list from the pool and return it */
  201. static BufferPoolEntry *get_pool(AVBufferPool *pool)
  202. {
  203. BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL;
  204. while (cur != last) {
  205. last = cur;
  206. cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL);
  207. if (!cur)
  208. return NULL;
  209. }
  210. return cur;
  211. }
  212. static void add_to_pool(BufferPoolEntry *buf)
  213. {
  214. AVBufferPool *pool;
  215. BufferPoolEntry *cur, *end = buf;
  216. if (!buf)
  217. return;
  218. pool = buf->pool;
  219. while (end->next)
  220. end = end->next;
  221. while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) {
  222. /* pool is not empty, retrieve it and append it to our list */
  223. cur = get_pool(pool);
  224. end->next = cur;
  225. while (end->next)
  226. end = end->next;
  227. }
  228. }
  229. static void pool_release_buffer(void *opaque, uint8_t *data)
  230. {
  231. BufferPoolEntry *buf = opaque;
  232. AVBufferPool *pool = buf->pool;
  233. if(CONFIG_MEMORY_POISONING)
  234. memset(buf->data, FF_MEMORY_POISON, pool->size);
  235. #if USE_ATOMICS
  236. add_to_pool(buf);
  237. #else
  238. ff_mutex_lock(&pool->mutex);
  239. buf->next = pool->pool;
  240. pool->pool = buf;
  241. ff_mutex_unlock(&pool->mutex);
  242. #endif
  243. if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
  244. buffer_pool_free(pool);
  245. }
  246. /* allocate a new buffer and override its free() callback so that
  247. * it is returned to the pool on free */
  248. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  249. {
  250. BufferPoolEntry *buf;
  251. AVBufferRef *ret;
  252. ret = pool->alloc(pool->size);
  253. if (!ret)
  254. return NULL;
  255. buf = av_mallocz(sizeof(*buf));
  256. if (!buf) {
  257. av_buffer_unref(&ret);
  258. return NULL;
  259. }
  260. buf->data = ret->buffer->data;
  261. buf->opaque = ret->buffer->opaque;
  262. buf->free = ret->buffer->free;
  263. buf->pool = pool;
  264. ret->buffer->opaque = buf;
  265. ret->buffer->free = pool_release_buffer;
  266. #if USE_ATOMICS
  267. avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
  268. avpriv_atomic_int_add_and_fetch(&pool->nb_allocated, 1);
  269. #endif
  270. return ret;
  271. }
  272. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  273. {
  274. AVBufferRef *ret;
  275. BufferPoolEntry *buf;
  276. #if USE_ATOMICS
  277. /* check whether the pool is empty */
  278. buf = get_pool(pool);
  279. if (!buf && pool->refcount <= pool->nb_allocated) {
  280. av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n");
  281. while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated))
  282. buf = get_pool(pool);
  283. }
  284. if (!buf)
  285. return pool_alloc_buffer(pool);
  286. /* keep the first entry, return the rest of the list to the pool */
  287. add_to_pool(buf->next);
  288. buf->next = NULL;
  289. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  290. buf, 0);
  291. if (!ret) {
  292. add_to_pool(buf);
  293. return NULL;
  294. }
  295. #else
  296. ff_mutex_lock(&pool->mutex);
  297. buf = pool->pool;
  298. if (buf) {
  299. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  300. buf, 0);
  301. if (ret) {
  302. pool->pool = buf->next;
  303. buf->next = NULL;
  304. }
  305. } else {
  306. ret = pool_alloc_buffer(pool);
  307. }
  308. ff_mutex_unlock(&pool->mutex);
  309. #endif
  310. if (ret)
  311. avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
  312. return ret;
  313. }