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.

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