buffer.c 8.3 KB

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