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.

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