buffer.c 9.0 KB

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