mem.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * @ingroup lavu_mem
  23. * Memory handling functions
  24. */
  25. #ifndef AVUTIL_MEM_H
  26. #define AVUTIL_MEM_H
  27. #include <limits.h>
  28. #include <stdint.h>
  29. #include "attributes.h"
  30. #include "avutil.h"
  31. #include "version.h"
  32. /**
  33. * @addtogroup lavu_mem
  34. * Utilities for manipulating memory.
  35. *
  36. * FFmpeg has several applications of memory that are not required of a typical
  37. * program. For example, the computing-heavy components like video decoding and
  38. * encoding can be sped up significantly through the use of aligned memory.
  39. *
  40. * However, for each of FFmpeg's applications of memory, there might not be a
  41. * recognized or standardized API for that specific use. Memory alignment, for
  42. * instance, varies wildly depending on operating systems, architectures, and
  43. * compilers. Hence, this component of @ref libavutil is created to make
  44. * dealing with memory consistently possible on all platforms.
  45. *
  46. * @{
  47. */
  48. #if FF_API_DECLARE_ALIGNED
  49. /**
  50. *
  51. * @defgroup lavu_mem_macros Alignment Macros
  52. * Helper macros for declaring aligned variables.
  53. * @{
  54. */
  55. /**
  56. * @def DECLARE_ALIGNED(n,t,v)
  57. * Declare a variable that is aligned in memory.
  58. *
  59. * @code{.c}
  60. * DECLARE_ALIGNED(16, uint16_t, aligned_int) = 42;
  61. * DECLARE_ALIGNED(32, uint8_t, aligned_array)[128];
  62. *
  63. * // The default-alignment equivalent would be
  64. * uint16_t aligned_int = 42;
  65. * uint8_t aligned_array[128];
  66. * @endcode
  67. *
  68. * @param n Minimum alignment in bytes
  69. * @param t Type of the variable (or array element)
  70. * @param v Name of the variable
  71. */
  72. /**
  73. * @def DECLARE_ASM_ALIGNED(n,t,v)
  74. * Declare an aligned variable appropriate for use in inline assembly code.
  75. *
  76. * @code{.c}
  77. * DECLARE_ASM_ALIGNED(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
  78. * @endcode
  79. *
  80. * @param n Minimum alignment in bytes
  81. * @param t Type of the variable (or array element)
  82. * @param v Name of the variable
  83. */
  84. /**
  85. * @def DECLARE_ASM_CONST(n,t,v)
  86. * Declare a static constant aligned variable appropriate for use in inline
  87. * assembly code.
  88. *
  89. * @code{.c}
  90. * DECLARE_ASM_CONST(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
  91. * @endcode
  92. *
  93. * @param n Minimum alignment in bytes
  94. * @param t Type of the variable (or array element)
  95. * @param v Name of the variable
  96. */
  97. #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
  98. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  99. #define DECLARE_ASM_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  100. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  101. #elif defined(__DJGPP__)
  102. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (FFMIN(n, 16)))) v
  103. #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
  104. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
  105. #elif defined(__GNUC__) || defined(__clang__)
  106. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  107. #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (n))) v
  108. #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
  109. #elif defined(_MSC_VER)
  110. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  111. #define DECLARE_ASM_ALIGNED(n,t,v) __declspec(align(n)) t v
  112. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  113. #else
  114. #define DECLARE_ALIGNED(n,t,v) t v
  115. #define DECLARE_ASM_ALIGNED(n,t,v) t v
  116. #define DECLARE_ASM_CONST(n,t,v) static const t v
  117. #endif
  118. /**
  119. * @}
  120. */
  121. #endif
  122. /**
  123. * @defgroup lavu_mem_attrs Function Attributes
  124. * Function attributes applicable to memory handling functions.
  125. *
  126. * These function attributes can help compilers emit more useful warnings, or
  127. * generate better code.
  128. * @{
  129. */
  130. /**
  131. * @def av_malloc_attrib
  132. * Function attribute denoting a malloc-like function.
  133. *
  134. * @see <a href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-g_t_0040code_007bmalloc_007d-function-attribute-3251">Function attribute `malloc` in GCC's documentation</a>
  135. */
  136. #if AV_GCC_VERSION_AT_LEAST(3,1)
  137. #define av_malloc_attrib __attribute__((__malloc__))
  138. #else
  139. #define av_malloc_attrib
  140. #endif
  141. /**
  142. * @def av_alloc_size(...)
  143. * Function attribute used on a function that allocates memory, whose size is
  144. * given by the specified parameter(s).
  145. *
  146. * @code{.c}
  147. * void *av_malloc(size_t size) av_alloc_size(1);
  148. * void *av_calloc(size_t nmemb, size_t size) av_alloc_size(1, 2);
  149. * @endcode
  150. *
  151. * @param ... One or two parameter indexes, separated by a comma
  152. *
  153. * @see <a href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-g_t_0040code_007balloc_005fsize_007d-function-attribute-3220">Function attribute `alloc_size` in GCC's documentation</a>
  154. */
  155. #if AV_GCC_VERSION_AT_LEAST(4,3)
  156. #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
  157. #else
  158. #define av_alloc_size(...)
  159. #endif
  160. /**
  161. * @}
  162. */
  163. /**
  164. * @defgroup lavu_mem_funcs Heap Management
  165. * Functions responsible for allocating, freeing, and copying memory.
  166. *
  167. * All memory allocation functions have a built-in upper limit of `INT_MAX`
  168. * bytes. This may be changed with av_max_alloc(), although exercise extreme
  169. * caution when doing so.
  170. *
  171. * @{
  172. */
  173. /**
  174. * Allocate a memory block with alignment suitable for all memory accesses
  175. * (including vectors if available on the CPU).
  176. *
  177. * @param size Size in bytes for the memory block to be allocated
  178. * @return Pointer to the allocated block, or `NULL` if the block cannot
  179. * be allocated
  180. * @see av_mallocz()
  181. */
  182. void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  183. /**
  184. * Allocate a memory block with alignment suitable for all memory accesses
  185. * (including vectors if available on the CPU) and zero all the bytes of the
  186. * block.
  187. *
  188. * @param size Size in bytes for the memory block to be allocated
  189. * @return Pointer to the allocated block, or `NULL` if it cannot be allocated
  190. * @see av_malloc()
  191. */
  192. void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  193. /**
  194. * Allocate a memory block for an array with av_malloc().
  195. *
  196. * The allocated memory will have size `size * nmemb` bytes.
  197. *
  198. * @param nmemb Number of element
  199. * @param size Size of a single element
  200. * @return Pointer to the allocated block, or `NULL` if the block cannot
  201. * be allocated
  202. * @see av_malloc()
  203. */
  204. av_alloc_size(1, 2) void *av_malloc_array(size_t nmemb, size_t size);
  205. /**
  206. * Allocate a memory block for an array with av_mallocz().
  207. *
  208. * The allocated memory will have size `size * nmemb` bytes.
  209. *
  210. * @param nmemb Number of elements
  211. * @param size Size of the single element
  212. * @return Pointer to the allocated block, or `NULL` if the block cannot
  213. * be allocated
  214. *
  215. * @see av_mallocz()
  216. * @see av_malloc_array()
  217. */
  218. void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib av_alloc_size(1, 2);
  219. #if FF_API_AV_MALLOCZ_ARRAY
  220. /**
  221. * @deprecated use av_calloc()
  222. */
  223. attribute_deprecated
  224. void *av_mallocz_array(size_t nmemb, size_t size) av_malloc_attrib av_alloc_size(1, 2);
  225. #endif
  226. /**
  227. * Allocate, reallocate, or free a block of memory.
  228. *
  229. * If `ptr` is `NULL` and `size` > 0, allocate a new block. Otherwise, expand or
  230. * shrink that block of memory according to `size`.
  231. *
  232. * @param ptr Pointer to a memory block already allocated with
  233. * av_realloc() or `NULL`
  234. * @param size Size in bytes of the memory block to be allocated or
  235. * reallocated
  236. *
  237. * @return Pointer to a newly-reallocated block or `NULL` if the block
  238. * cannot be reallocated
  239. *
  240. * @warning Unlike av_malloc(), the returned pointer is not guaranteed to be
  241. * correctly aligned. The returned pointer must be freed after even
  242. * if size is zero.
  243. * @see av_fast_realloc()
  244. * @see av_reallocp()
  245. */
  246. void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  247. /**
  248. * Allocate, reallocate, or free a block of memory through a pointer to a
  249. * pointer.
  250. *
  251. * If `*ptr` is `NULL` and `size` > 0, allocate a new block. If `size` is
  252. * zero, free the memory block pointed to by `*ptr`. Otherwise, expand or
  253. * shrink that block of memory according to `size`.
  254. *
  255. * @param[in,out] ptr Pointer to a pointer to a memory block already allocated
  256. * with av_realloc(), or a pointer to `NULL`. The pointer
  257. * is updated on success, or freed on failure.
  258. * @param[in] size Size in bytes for the memory block to be allocated or
  259. * reallocated
  260. *
  261. * @return Zero on success, an AVERROR error code on failure
  262. *
  263. * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be
  264. * correctly aligned.
  265. */
  266. av_warn_unused_result
  267. int av_reallocp(void *ptr, size_t size);
  268. /**
  269. * Allocate, reallocate, or free a block of memory.
  270. *
  271. * This function does the same thing as av_realloc(), except:
  272. * - It takes two size arguments and allocates `nelem * elsize` bytes,
  273. * after checking the result of the multiplication for integer overflow.
  274. * - It frees the input block in case of failure, thus avoiding the memory
  275. * leak with the classic
  276. * @code{.c}
  277. * buf = realloc(buf);
  278. * if (!buf)
  279. * return -1;
  280. * @endcode
  281. * pattern.
  282. */
  283. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
  284. /**
  285. * Allocate, reallocate, or free an array.
  286. *
  287. * If `ptr` is `NULL` and `nmemb` > 0, allocate a new block.
  288. *
  289. * @param ptr Pointer to a memory block already allocated with
  290. * av_realloc() or `NULL`
  291. * @param nmemb Number of elements in the array
  292. * @param size Size of the single element of the array
  293. *
  294. * @return Pointer to a newly-reallocated block or NULL if the block
  295. * cannot be reallocated
  296. *
  297. * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be
  298. * correctly aligned. The returned pointer must be freed after even if
  299. * nmemb is zero.
  300. * @see av_reallocp_array()
  301. */
  302. av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
  303. /**
  304. * Allocate, reallocate an array through a pointer to a pointer.
  305. *
  306. * If `*ptr` is `NULL` and `nmemb` > 0, allocate a new block.
  307. *
  308. * @param[in,out] ptr Pointer to a pointer to a memory block already
  309. * allocated with av_realloc(), or a pointer to `NULL`.
  310. * The pointer is updated on success, or freed on failure.
  311. * @param[in] nmemb Number of elements
  312. * @param[in] size Size of the single element
  313. *
  314. * @return Zero on success, an AVERROR error code on failure
  315. *
  316. * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be
  317. * correctly aligned. *ptr must be freed after even if nmemb is zero.
  318. */
  319. int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
  320. /**
  321. * Reallocate the given buffer if it is not large enough, otherwise do nothing.
  322. *
  323. * If the given buffer is `NULL`, then a new uninitialized buffer is allocated.
  324. *
  325. * If the given buffer is not large enough, and reallocation fails, `NULL` is
  326. * returned and `*size` is set to 0, but the original buffer is not changed or
  327. * freed.
  328. *
  329. * A typical use pattern follows:
  330. *
  331. * @code{.c}
  332. * uint8_t *buf = ...;
  333. * uint8_t *new_buf = av_fast_realloc(buf, &current_size, size_needed);
  334. * if (!new_buf) {
  335. * // Allocation failed; clean up original buffer
  336. * av_freep(&buf);
  337. * return AVERROR(ENOMEM);
  338. * }
  339. * @endcode
  340. *
  341. * @param[in,out] ptr Already allocated buffer, or `NULL`
  342. * @param[in,out] size Pointer to the size of buffer `ptr`. `*size` is
  343. * updated to the new allocated size, in particular 0
  344. * in case of failure.
  345. * @param[in] min_size Desired minimal size of buffer `ptr`
  346. * @return `ptr` if the buffer is large enough, a pointer to newly reallocated
  347. * buffer if the buffer was not large enough, or `NULL` in case of
  348. * error
  349. * @see av_realloc()
  350. * @see av_fast_malloc()
  351. */
  352. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
  353. /**
  354. * Allocate a buffer, reusing the given one if large enough.
  355. *
  356. * Contrary to av_fast_realloc(), the current buffer contents might not be
  357. * preserved and on error the old buffer is freed, thus no special handling to
  358. * avoid memleaks is necessary.
  359. *
  360. * `*ptr` is allowed to be `NULL`, in which case allocation always happens if
  361. * `size_needed` is greater than 0.
  362. *
  363. * @code{.c}
  364. * uint8_t *buf = ...;
  365. * av_fast_malloc(&buf, &current_size, size_needed);
  366. * if (!buf) {
  367. * // Allocation failed; buf already freed
  368. * return AVERROR(ENOMEM);
  369. * }
  370. * @endcode
  371. *
  372. * @param[in,out] ptr Pointer to pointer to an already allocated buffer.
  373. * `*ptr` will be overwritten with pointer to new
  374. * buffer on success or `NULL` on failure
  375. * @param[in,out] size Pointer to the size of buffer `*ptr`. `*size` is
  376. * updated to the new allocated size, in particular 0
  377. * in case of failure.
  378. * @param[in] min_size Desired minimal size of buffer `*ptr`
  379. * @see av_realloc()
  380. * @see av_fast_mallocz()
  381. */
  382. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
  383. /**
  384. * Allocate and clear a buffer, reusing the given one if large enough.
  385. *
  386. * Like av_fast_malloc(), but all newly allocated space is initially cleared.
  387. * Reused buffer is not cleared.
  388. *
  389. * `*ptr` is allowed to be `NULL`, in which case allocation always happens if
  390. * `size_needed` is greater than 0.
  391. *
  392. * @param[in,out] ptr Pointer to pointer to an already allocated buffer.
  393. * `*ptr` will be overwritten with pointer to new
  394. * buffer on success or `NULL` on failure
  395. * @param[in,out] size Pointer to the size of buffer `*ptr`. `*size` is
  396. * updated to the new allocated size, in particular 0
  397. * in case of failure.
  398. * @param[in] min_size Desired minimal size of buffer `*ptr`
  399. * @see av_fast_malloc()
  400. */
  401. void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size);
  402. /**
  403. * Free a memory block which has been allocated with a function of av_malloc()
  404. * or av_realloc() family.
  405. *
  406. * @param ptr Pointer to the memory block which should be freed.
  407. *
  408. * @note `ptr = NULL` is explicitly allowed.
  409. * @note It is recommended that you use av_freep() instead, to prevent leaving
  410. * behind dangling pointers.
  411. * @see av_freep()
  412. */
  413. void av_free(void *ptr);
  414. /**
  415. * Free a memory block which has been allocated with a function of av_malloc()
  416. * or av_realloc() family, and set the pointer pointing to it to `NULL`.
  417. *
  418. * @code{.c}
  419. * uint8_t *buf = av_malloc(16);
  420. * av_free(buf);
  421. * // buf now contains a dangling pointer to freed memory, and accidental
  422. * // dereference of buf will result in a use-after-free, which may be a
  423. * // security risk.
  424. *
  425. * uint8_t *buf = av_malloc(16);
  426. * av_freep(&buf);
  427. * // buf is now NULL, and accidental dereference will only result in a
  428. * // NULL-pointer dereference.
  429. * @endcode
  430. *
  431. * @param ptr Pointer to the pointer to the memory block which should be freed
  432. * @note `*ptr = NULL` is safe and leads to no action.
  433. * @see av_free()
  434. */
  435. void av_freep(void *ptr);
  436. /**
  437. * Duplicate a string.
  438. *
  439. * @param s String to be duplicated
  440. * @return Pointer to a newly-allocated string containing a
  441. * copy of `s` or `NULL` if the string cannot be allocated
  442. * @see av_strndup()
  443. */
  444. char *av_strdup(const char *s) av_malloc_attrib;
  445. /**
  446. * Duplicate a substring of a string.
  447. *
  448. * @param s String to be duplicated
  449. * @param len Maximum length of the resulting string (not counting the
  450. * terminating byte)
  451. * @return Pointer to a newly-allocated string containing a
  452. * substring of `s` or `NULL` if the string cannot be allocated
  453. */
  454. char *av_strndup(const char *s, size_t len) av_malloc_attrib;
  455. /**
  456. * Duplicate a buffer with av_malloc().
  457. *
  458. * @param p Buffer to be duplicated
  459. * @param size Size in bytes of the buffer copied
  460. * @return Pointer to a newly allocated buffer containing a
  461. * copy of `p` or `NULL` if the buffer cannot be allocated
  462. */
  463. void *av_memdup(const void *p, size_t size);
  464. /**
  465. * Overlapping memcpy() implementation.
  466. *
  467. * @param dst Destination buffer
  468. * @param back Number of bytes back to start copying (i.e. the initial size of
  469. * the overlapping window); must be > 0
  470. * @param cnt Number of bytes to copy; must be >= 0
  471. *
  472. * @note `cnt > back` is valid, this will copy the bytes we just copied,
  473. * thus creating a repeating pattern with a period length of `back`.
  474. */
  475. void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  476. /**
  477. * @}
  478. */
  479. /**
  480. * @defgroup lavu_mem_dynarray Dynamic Array
  481. *
  482. * Utilities to make an array grow when needed.
  483. *
  484. * Sometimes, the programmer would want to have an array that can grow when
  485. * needed. The libavutil dynamic array utilities fill that need.
  486. *
  487. * libavutil supports two systems of appending elements onto a dynamically
  488. * allocated array, the first one storing the pointer to the value in the
  489. * array, and the second storing the value directly. In both systems, the
  490. * caller is responsible for maintaining a variable containing the length of
  491. * the array, as well as freeing of the array after use.
  492. *
  493. * The first system stores pointers to values in a block of dynamically
  494. * allocated memory. Since only pointers are stored, the function does not need
  495. * to know the size of the type. Both av_dynarray_add() and
  496. * av_dynarray_add_nofree() implement this system.
  497. *
  498. * @code
  499. * type **array = NULL; //< an array of pointers to values
  500. * int nb = 0; //< a variable to keep track of the length of the array
  501. *
  502. * type to_be_added = ...;
  503. * type to_be_added2 = ...;
  504. *
  505. * av_dynarray_add(&array, &nb, &to_be_added);
  506. * if (nb == 0)
  507. * return AVERROR(ENOMEM);
  508. *
  509. * av_dynarray_add(&array, &nb, &to_be_added2);
  510. * if (nb == 0)
  511. * return AVERROR(ENOMEM);
  512. *
  513. * // Now:
  514. * // nb == 2
  515. * // &to_be_added == array[0]
  516. * // &to_be_added2 == array[1]
  517. *
  518. * av_freep(&array);
  519. * @endcode
  520. *
  521. * The second system stores the value directly in a block of memory. As a
  522. * result, the function has to know the size of the type. av_dynarray2_add()
  523. * implements this mechanism.
  524. *
  525. * @code
  526. * type *array = NULL; //< an array of values
  527. * int nb = 0; //< a variable to keep track of the length of the array
  528. *
  529. * type to_be_added = ...;
  530. * type to_be_added2 = ...;
  531. *
  532. * type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array), NULL);
  533. * if (!addr)
  534. * return AVERROR(ENOMEM);
  535. * memcpy(addr, &to_be_added, sizeof(to_be_added));
  536. *
  537. * // Shortcut of the above.
  538. * type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array),
  539. * (const void *)&to_be_added2);
  540. * if (!addr)
  541. * return AVERROR(ENOMEM);
  542. *
  543. * // Now:
  544. * // nb == 2
  545. * // to_be_added == array[0]
  546. * // to_be_added2 == array[1]
  547. *
  548. * av_freep(&array);
  549. * @endcode
  550. *
  551. * @{
  552. */
  553. /**
  554. * Add the pointer to an element to a dynamic array.
  555. *
  556. * The array to grow is supposed to be an array of pointers to
  557. * structures, and the element to add must be a pointer to an already
  558. * allocated structure.
  559. *
  560. * The array is reallocated when its size reaches powers of 2.
  561. * Therefore, the amortized cost of adding an element is constant.
  562. *
  563. * In case of success, the pointer to the array is updated in order to
  564. * point to the new grown array, and the number pointed to by `nb_ptr`
  565. * is incremented.
  566. * In case of failure, the array is freed, `*tab_ptr` is set to `NULL` and
  567. * `*nb_ptr` is set to 0.
  568. *
  569. * @param[in,out] tab_ptr Pointer to the array to grow
  570. * @param[in,out] nb_ptr Pointer to the number of elements in the array
  571. * @param[in] elem Element to add
  572. * @see av_dynarray_add_nofree(), av_dynarray2_add()
  573. */
  574. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
  575. /**
  576. * Add an element to a dynamic array.
  577. *
  578. * Function has the same functionality as av_dynarray_add(),
  579. * but it doesn't free memory on fails. It returns error code
  580. * instead and leave current buffer untouched.
  581. *
  582. * @return >=0 on success, negative otherwise
  583. * @see av_dynarray_add(), av_dynarray2_add()
  584. */
  585. av_warn_unused_result
  586. int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem);
  587. /**
  588. * Add an element of size `elem_size` to a dynamic array.
  589. *
  590. * The array is reallocated when its number of elements reaches powers of 2.
  591. * Therefore, the amortized cost of adding an element is constant.
  592. *
  593. * In case of success, the pointer to the array is updated in order to
  594. * point to the new grown array, and the number pointed to by `nb_ptr`
  595. * is incremented.
  596. * In case of failure, the array is freed, `*tab_ptr` is set to `NULL` and
  597. * `*nb_ptr` is set to 0.
  598. *
  599. * @param[in,out] tab_ptr Pointer to the array to grow
  600. * @param[in,out] nb_ptr Pointer to the number of elements in the array
  601. * @param[in] elem_size Size in bytes of an element in the array
  602. * @param[in] elem_data Pointer to the data of the element to add. If
  603. * `NULL`, the space of the newly added element is
  604. * allocated but left uninitialized.
  605. *
  606. * @return Pointer to the data of the element to copy in the newly allocated
  607. * space
  608. * @see av_dynarray_add(), av_dynarray_add_nofree()
  609. */
  610. void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
  611. const uint8_t *elem_data);
  612. /**
  613. * @}
  614. */
  615. /**
  616. * @defgroup lavu_mem_misc Miscellaneous Functions
  617. *
  618. * Other functions related to memory allocation.
  619. *
  620. * @{
  621. */
  622. /**
  623. * Multiply two `size_t` values checking for overflow.
  624. *
  625. * @param[in] a,b Operands of multiplication
  626. * @param[out] r Pointer to the result of the operation
  627. * @return 0 on success, AVERROR(EINVAL) on overflow
  628. */
  629. int av_size_mult(size_t a, size_t b, size_t *r);
  630. /**
  631. * Set the maximum size that may be allocated in one block.
  632. *
  633. * The value specified with this function is effective for all libavutil's @ref
  634. * lavu_mem_funcs "heap management functions."
  635. *
  636. * By default, the max value is defined as `INT_MAX`.
  637. *
  638. * @param max Value to be set as the new maximum size
  639. *
  640. * @warning Exercise extreme caution when using this function. Don't touch
  641. * this if you do not understand the full consequence of doing so.
  642. */
  643. void av_max_alloc(size_t max);
  644. /**
  645. * @}
  646. * @}
  647. */
  648. #endif /* AVUTIL_MEM_H */