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 module ffmpeg.libavutil.mem; 21 /** 22 * @file 23 * memory handling functions 24 */ 25 26 /*#ifndef AVUTIL_MEM_H 27 #define AVUTIL_MEM_H 28 29 #include <limits.h>*/ 30 import std.stdint; 31 import core.stdc.errno; 32 import ffmpeg.libavutil.error; 33 /*#include "attributes.h" 34 #include "error.h" 35 #include "avutil.h"*/ 36 37 @nogc nothrow extern(C): 38 39 /** 40 * @addtogroup lavu_mem 41 * @{ 42 */ 43 44 45 /*#if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C) 46 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v 47 #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v 48 #elif defined(__TI_COMPILER_VERSION__) 49 #define DECLARE_ALIGNED(n,t,v) \ 50 AV_PRAGMA(DATA_ALIGN(v,n)) \ 51 t __attribute__((aligned(n))) v 52 #define DECLARE_ASM_CONST(n,t,v) \ 53 AV_PRAGMA(DATA_ALIGN(v,n)) \ 54 static const t __attribute__((aligned(n))) v 55 #elif defined(__GNUC__) 56 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v 57 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v 58 #elif defined(_MSC_VER) 59 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v 60 #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v 61 #else 62 #define DECLARE_ALIGNED(n,t,v) t v 63 #define DECLARE_ASM_CONST(n,t,v) static const t v 64 #endif 65 66 #if AV_GCC_VERSION_AT_LEAST(3,1) 67 #define av_malloc_attrib __attribute__((__malloc__)) 68 #else 69 #define av_malloc_attrib 70 #endif 71 72 #if AV_GCC_VERSION_AT_LEAST(4,3) 73 #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) 74 #else 75 #define av_alloc_size(...) 76 #endif*/ 77 78 /** 79 * Allocate a block of size bytes with alignment suitable for all 80 * memory accesses (including vectors if available on the CPU). 81 * @param size Size in bytes for the memory block to be allocated. 82 * @return Pointer to the allocated block, NULL if the block cannot 83 * be allocated. 84 * @see av_mallocz() 85 */ 86 void *av_malloc(size_t size); 87 88 /** 89 * Helper function to allocate a block of size * nmemb bytes with 90 * using av_malloc() 91 * @param nmemb Number of elements 92 * @param size Size of the single element 93 * @return Pointer to the allocated block, NULL if the block cannot 94 * be allocated. 95 * @see av_malloc() 96 */ 97 static void *av_malloc_array(size_t nmemb, size_t size) 98 { 99 if (size <= 0 || nmemb >= int.max / size) 100 return null; 101 return av_malloc(nmemb * size); 102 } 103 104 /** 105 * Allocate or reallocate a block of memory. 106 * If ptr is NULL and size > 0, allocate a new block. If 107 * size is zero, free the memory block pointed to by ptr. 108 * @param ptr Pointer to a memory block already allocated with 109 * av_malloc(z)() or av_realloc() or NULL. 110 * @param size Size in bytes for the memory block to be allocated or 111 * reallocated. 112 * @return Pointer to a newly reallocated block or NULL if the block 113 * cannot be reallocated or the function is used to free the memory block. 114 * @see av_fast_realloc() 115 */ 116 void *av_realloc(void *ptr, size_t size); 117 118 /** 119 * Allocate or reallocate a block of memory. 120 * This function does the same thing as av_realloc, except: 121 * - It takes two arguments and checks the result of the multiplication for 122 * integer overflow. 123 * - It frees the input block in case of failure, thus avoiding the memory 124 * leak with the classic "buf = realloc(buf); if (!buf) return -1;". 125 */ 126 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize); 127 128 /** 129 * Allocate or reallocate a block of memory. 130 * If *ptr is NULL and size > 0, allocate a new block. If 131 * size is zero, free the memory block pointed to by ptr. 132 * @param ptr Pointer to a pointer to a memory block already allocated 133 * with av_realloc(), or pointer to a pointer to NULL. 134 * The pointer is updated on success, or freed on failure. 135 * @param size Size in bytes for the memory block to be allocated or 136 * reallocated 137 * @return Zero on success, an AVERROR error code on failure. 138 * @warning Pointers originating from the av_malloc() family of functions must 139 * not be passed to av_reallocp(). The former can be implemented using 140 * memalign() (or other functions), and there is no guarantee that 141 * pointers from such functions can be passed to realloc() at all. 142 * The situation is undefined according to POSIX and may crash with 143 * some libc implementations. 144 */ 145 //av_warn_unused_result 146 int av_reallocp(void *ptr, size_t size); 147 148 /** 149 * Allocate or reallocate an array. 150 * If ptr is NULL and nmemb > 0, allocate a new block. If 151 * nmemb is zero, free the memory block pointed to by ptr. 152 * @param ptr Pointer to a memory block already allocated with 153 * av_realloc() or NULL. 154 * @param nmemb Number of elements 155 * @param size Size of the single element 156 * @return Pointer to a newly-reallocated block or NULL if the block 157 * cannot be reallocated or the function is used to free the memory block. 158 * @warning Pointers originating from the av_malloc() family of functions must 159 * not be passed to av_realloc(). The former can be implemented using 160 * memalign() (or other functions), and there is no guarantee that 161 * pointers from such functions can be passed to realloc() at all. 162 * The situation is undefined according to POSIX and may crash with 163 * some libc implementations. 164 */ 165 void *av_realloc_array(void *ptr, size_t nmemb, size_t size); 166 167 /** 168 * Allocate or reallocate an array through a pointer to a pointer. 169 * If *ptr is NULL and nmemb > 0, allocate a new block. If 170 * nmemb is zero, free the memory block pointed to by ptr. 171 * @param ptr Pointer to a pointer to a memory block already allocated 172 * with av_realloc(), or pointer to a pointer to NULL. 173 * The pointer is updated on success, or freed on failure. 174 * @param nmemb Number of elements 175 * @param size Size of the single element 176 * @return Zero on success, an AVERROR error code on failure. 177 * @warning Pointers originating from the av_malloc() family of functions must 178 * not be passed to av_realloc(). The former can be implemented using 179 * memalign() (or other functions), and there is no guarantee that 180 * pointers from such functions can be passed to realloc() at all. 181 * The situation is undefined according to POSIX and may crash with 182 * some libc implementations. 183 */ 184 int av_reallocp_array(void *ptr, size_t nmemb, size_t size); 185 186 /** 187 * Free a memory block which has been allocated with av_malloc(z)() or 188 * av_realloc(). 189 * @param ptr Pointer to the memory block which should be freed. 190 * @note ptr = NULL is explicitly allowed. 191 * @note It is recommended that you use av_freep() instead. 192 * @see av_freep() 193 */ 194 void av_free(void *ptr); 195 196 /** 197 * Allocate a block of size bytes with alignment suitable for all 198 * memory accesses (including vectors if available on the CPU) and 199 * zero all the bytes of the block. 200 * @param size Size in bytes for the memory block to be allocated. 201 * @return Pointer to the allocated block, NULL if it cannot be allocated. 202 * @see av_malloc() 203 */ 204 void *av_mallocz(size_t size); 205 206 /** 207 * Allocate a block of nmemb * size bytes with alignment suitable for all 208 * memory accesses (including vectors if available on the CPU) and 209 * zero all the bytes of the block. 210 * The allocation will fail if nmemb * size is greater than or equal 211 * to INT_MAX. 212 * @param nmemb 213 * @param size 214 * @return Pointer to the allocated block, NULL if it cannot be allocated. 215 */ 216 void *av_calloc(size_t nmemb, size_t size); 217 218 /** 219 * Allocate a block of size * nmemb bytes with av_mallocz(). 220 * @param nmemb Number of elements 221 * @param size Size of the single element 222 * @return Pointer to the allocated block, NULL if the block cannot 223 * be allocated. 224 * @see av_mallocz() 225 * @see av_malloc_array() 226 */ 227 static void *av_mallocz_array(size_t nmemb, size_t size) 228 { 229 if (size <= 0 || nmemb >= int.max / size) 230 return null; 231 return av_mallocz(nmemb * size); 232 } 233 234 /** 235 * Duplicate the string s. 236 * @param s string to be duplicated 237 * @return Pointer to a newly allocated string containing a 238 * copy of s or NULL if the string cannot be allocated. 239 */ 240 char *av_strdup(const char *s); 241 242 /** 243 * Duplicate a substring of the string s. 244 * @param s string to be duplicated 245 * @param len the maximum length of the resulting string (not counting the 246 * terminating byte). 247 * @return Pointer to a newly-allocated string containing a 248 * copy of s or NULL if the string cannot be allocated. 249 */ 250 char *av_strndup(const char *s, size_t len); 251 252 /** 253 * Duplicate the buffer p. 254 * @param p buffer to be duplicated 255 * @return Pointer to a newly allocated buffer containing a 256 * copy of p or NULL if the buffer cannot be allocated. 257 */ 258 void *av_memdup(const void *p, size_t size); 259 260 /** 261 * Free a memory block which has been allocated with av_malloc(z)() or 262 * av_realloc() and set the pointer pointing to it to NULL. 263 * @param ptr Pointer to the pointer to the memory block which should 264 * be freed. 265 * @note passing a pointer to a NULL pointer is safe and leads to no action. 266 * @see av_free() 267 */ 268 void av_freep(void *ptr); 269 270 /** 271 * Add an element to a dynamic array. 272 * 273 * The array to grow is supposed to be an array of pointers to 274 * structures, and the element to add must be a pointer to an already 275 * allocated structure. 276 * 277 * The array is reallocated when its size reaches powers of 2. 278 * Therefore, the amortized cost of adding an element is constant. 279 * 280 * In case of success, the pointer to the array is updated in order to 281 * point to the new grown array, and the number pointed to by nb_ptr 282 * is incremented. 283 * In case of failure, the array is freed, *tab_ptr is set to NULL and 284 * *nb_ptr is set to 0. 285 * 286 * @param tab_ptr pointer to the array to grow 287 * @param nb_ptr pointer to the number of elements in the array 288 * @param elem element to add 289 * @see av_dynarray_add_nofree(), av_dynarray2_add() 290 */ 291 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem); 292 293 /** 294 * Add an element to a dynamic array. 295 * 296 * Function has the same functionality as av_dynarray_add(), 297 * but it doesn't free memory on fails. It returns error code 298 * instead and leave current buffer untouched. 299 * 300 * @param tab_ptr pointer to the array to grow 301 * @param nb_ptr pointer to the number of elements in the array 302 * @param elem element to add 303 * @return >=0 on success, negative otherwise. 304 * @see av_dynarray_add(), av_dynarray2_add() 305 */ 306 //av_warn_unused_result 307 int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem); 308 309 /** 310 * Add an element of size elem_size to a dynamic array. 311 * 312 * The array is reallocated when its number of elements reaches powers of 2. 313 * Therefore, the amortized cost of adding an element is constant. 314 * 315 * In case of success, the pointer to the array is updated in order to 316 * point to the new grown array, and the number pointed to by nb_ptr 317 * is incremented. 318 * In case of failure, the array is freed, *tab_ptr is set to NULL and 319 * *nb_ptr is set to 0. 320 * 321 * @param tab_ptr pointer to the array to grow 322 * @param nb_ptr pointer to the number of elements in the array 323 * @param elem_size size in bytes of the elements in the array 324 * @param elem_data pointer to the data of the element to add. If NULL, the space of 325 * the new added element is not filled. 326 * @return pointer to the data of the element to copy in the new allocated space. 327 * If NULL, the new allocated space is left uninitialized." 328 * @see av_dynarray_add(), av_dynarray_add_nofree() 329 */ 330 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, 331 const uint8_t *elem_data); 332 333 /** 334 * Multiply two size_t values checking for overflow. 335 * @return 0 if success, AVERROR(EINVAL) if overflow. 336 */ 337 static int av_size_mult(size_t a, size_t b, size_t *r) 338 { 339 size_t t = a * b; 340 /* Hack inspired from glibc: only try the division if nelem and elsize 341 * are both greater than sqrt(SIZE_MAX). */ 342 if ((a | b) >= (cast(size_t)1 << size_t.sizeof * 4) && a && t / a != b) 343 return AVERROR!(EINVAL); 344 *r = t; 345 return 0; 346 } 347 348 /** 349 * Set the maximum size that may me allocated in one block. 350 */ 351 void av_max_alloc(size_t max); 352 353 /** 354 * deliberately overlapping memcpy implementation 355 * @param dst destination buffer 356 * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0 357 * @param cnt number of bytes to copy, must be >= 0 358 * 359 * cnt > back is valid, this will copy the bytes we just copied, 360 * thus creating a repeating pattern with a period length of back. 361 */ 362 void av_memcpy_backptr(uint8_t *dst, int back, int cnt); 363 364 /** 365 * Reallocate the given block if it is not large enough, otherwise do nothing. 366 * 367 * @see av_realloc 368 */ 369 void *av_fast_realloc(void *ptr, uint *size, size_t min_size); 370 371 /** 372 * Allocate a buffer, reusing the given one if large enough. 373 * 374 * Contrary to av_fast_realloc the current buffer contents might not be 375 * preserved and on error the old buffer is freed, thus no special 376 * handling to avoid memleaks is necessary. 377 * 378 * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer 379 * @param size size of the buffer *ptr points to 380 * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and 381 * *size 0 if an error occurred. 382 */ 383 void av_fast_malloc(void *ptr, uint *size, size_t min_size); 384 385 /** 386 * Allocate a buffer, reusing the given one if large enough. 387 * 388 * All newly allocated space is initially cleared 389 * Contrary to av_fast_realloc the current buffer contents might not be 390 * preserved and on error the old buffer is freed, thus no special 391 * handling to avoid memleaks is necessary. 392 * 393 * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer 394 * @param size size of the buffer *ptr points to 395 * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and 396 * *size 0 if an error occurred. 397 */ 398 void av_fast_mallocz(void *ptr, uint *size, size_t min_size); 399 400 /** 401 * @} 402 */ 403 404 //#endif /* AVUTIL_MEM_H */