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.

700 lines
23KB

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