buffer.c 9.8 KB

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