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