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.

388 lines
9.0KB

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