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 extern(C): 34 /*#include "attributes.h" 35 #include "error.h" 36 #include "avutil.h"*/ 37 38 /** 39 * @addtogroup lavu_mem 40 * @{ 41 */ 42 43 44 /*#if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C) 45 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v 46 #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v 47 #elif defined(__TI_COMPILER_VERSION__) 48 #define DECLARE_ALIGNED(n,t,v) \ 49 AV_PRAGMA(DATA_ALIGN(v,n)) \ 50 t __attribute__((aligned(n))) v 51 #define DECLARE_ASM_CONST(n,t,v) \ 52 AV_PRAGMA(DATA_ALIGN(v,n)) \ 53 static const t __attribute__((aligned(n))) v 54 #elif defined(__GNUC__) 55 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v 56 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v 57 #elif defined(_MSC_VER) 58 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v 59 #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v 60 #else 61 #define DECLARE_ALIGNED(n,t,v) t v 62 #define DECLARE_ASM_CONST(n,t,v) static const t v 63 #endif 64 65 #if AV_GCC_VERSION_AT_LEAST(3,1) 66 #define av_malloc_attrib __attribute__((__malloc__)) 67 #else 68 #define av_malloc_attrib 69 #endif 70 71 #if AV_GCC_VERSION_AT_LEAST(4,3) 72 #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) 73 #else 74 #define av_alloc_size(...) 75 #endif*/ 76 77 /** 78 * Allocate a block of size bytes with alignment suitable for all 79 * memory accesses (including vectors if available on the CPU). 80 * @param size Size in bytes for the memory block to be allocated. 81 * @return Pointer to the allocated block, NULL if the block cannot 82 * be allocated. 83 * @see av_mallocz() 84 */ 85 void *av_malloc(size_t size); 86 87 /** 88 * Helper function to allocate a block of size * nmemb bytes with 89 * using av_malloc() 90 * @param nmemb Number of elements 91 * @param size Size of the single element 92 * @return Pointer to the allocated block, NULL if the block cannot 93 * be allocated. 94 * @see av_malloc() 95 */ 96 static void *av_malloc_array(size_t nmemb, size_t size) 97 { 98 if (size <= 0 || nmemb >= int.max / size) 99 return null; 100 return av_malloc(nmemb * size); 101 } 102 103 /** 104 * Allocate or reallocate a block of memory. 105 * If ptr is NULL and size > 0, allocate a new block. If 106 * size is zero, free the memory block pointed to by ptr. 107 * @param ptr Pointer to a memory block already allocated with 108 * av_malloc(z)() or av_realloc() or NULL. 109 * @param size Size in bytes for the memory block to be allocated or 110 * reallocated. 111 * @return Pointer to a newly reallocated block or NULL if the block 112 * cannot be reallocated or the function is used to free the memory block. 113 * @see av_fast_realloc() 114 */ 115 void *av_realloc(void *ptr, size_t size); 116 117 /** 118 * Allocate or reallocate a block of memory. 119 * This function does the same thing as av_realloc, except: 120 * - It takes two arguments and checks the result of the multiplication for 121 * integer overflow. 122 * - It frees the input block in case of failure, thus avoiding the memory 123 * leak with the classic "buf = realloc(buf); if (!buf) return -1;". 124 */ 125 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize); 126 127 /** 128 * Free a memory block which has been allocated with av_malloc(z)() or 129 * av_realloc(). 130 * @param ptr Pointer to the memory block which should be freed. 131 * @note ptr = NULL is explicitly allowed. 132 * @note It is recommended that you use av_freep() instead. 133 * @see av_freep() 134 */ 135 void av_free(void *ptr); 136 137 /** 138 * Allocate a block of size bytes with alignment suitable for all 139 * memory accesses (including vectors if available on the CPU) and 140 * zero all the bytes of the block. 141 * @param size Size in bytes for the memory block to be allocated. 142 * @return Pointer to the allocated block, NULL if it cannot be allocated. 143 * @see av_malloc() 144 */ 145 void *av_mallocz(size_t size); 146 147 /** 148 * Allocate a block of nmemb * size bytes with alignment suitable for all 149 * memory accesses (including vectors if available on the CPU) and 150 * zero all the bytes of the block. 151 * The allocation will fail if nmemb * size is greater than or equal 152 * to INT_MAX. 153 * @param nmemb 154 * @param size 155 * @return Pointer to the allocated block, NULL if it cannot be allocated. 156 */ 157 void *av_calloc(size_t nmemb, size_t size); 158 159 /** 160 * Helper function to allocate a block of size * nmemb bytes with 161 * using av_mallocz() 162 * @param nmemb Number of elements 163 * @param size Size of the single element 164 * @return Pointer to the allocated block, NULL if the block cannot 165 * be allocated. 166 * @see av_mallocz() 167 * @see av_malloc_array() 168 */ 169 static void *av_mallocz_array(size_t nmemb, size_t size) 170 { 171 if (size <= 0 || nmemb >= int.max / size) 172 return null; 173 return av_mallocz(nmemb * size); 174 } 175 176 /** 177 * Duplicate the string s. 178 * @param s string to be duplicated 179 * @return Pointer to a newly allocated string containing a 180 * copy of s or NULL if the string cannot be allocated. 181 */ 182 char *av_strdup(const char *s); 183 184 /** 185 * Free a memory block which has been allocated with av_malloc(z)() or 186 * av_realloc() and set the pointer pointing to it to NULL. 187 * @param ptr Pointer to the pointer to the memory block which should 188 * be freed. 189 * @see av_free() 190 */ 191 void av_freep(void *ptr); 192 193 /** 194 * Add an element to a dynamic array. 195 * 196 * @param tab_ptr Pointer to the array. 197 * @param nb_ptr Pointer to the number of elements in the array. 198 * @param elem Element to be added. 199 */ 200 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem); 201 202 /** 203 * Multiply two size_t values checking for overflow. 204 * @return 0 if success, AVERROR(EINVAL) if overflow. 205 */ 206 static int av_size_mult(size_t a, size_t b, size_t *r) 207 { 208 size_t t = a * b; 209 /* Hack inspired from glibc: only try the division if nelem and elsize 210 * are both greater than sqrt(SIZE_MAX). */ 211 if ((a | b) >= (cast(size_t)1 << size_t.sizeof * 4) && a && t / a != b) 212 return AVERROR!(EINVAL); 213 *r = t; 214 return 0; 215 } 216 217 /** 218 * Set the maximum size that may me allocated in one block. 219 */ 220 void av_max_alloc(size_t max); 221 222 /** 223 * @brief deliberately overlapping memcpy implementation 224 * @param dst destination buffer 225 * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0 226 * @param cnt number of bytes to copy, must be >= 0 227 * 228 * cnt > back is valid, this will copy the bytes we just copied, 229 * thus creating a repeating pattern with a period length of back. 230 */ 231 void av_memcpy_backptr(uint8_t *dst, int back, int cnt); 232 233 /** 234 * @} 235 */ 236 237 //#endif /* AVUTIL_MEM_H */