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