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.

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