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.

683 lines
22KB

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