You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
8.2KB

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