FFmpeg
mem.h
Go to the documentation of this file.
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 
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 "error.h"
35 #include "avutil.h"
36 
103 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
104  #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
105  #define DECLARE_ASM_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
106  #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
107 #elif defined(__DJGPP__)
108  #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (FFMIN(n, 16)))) v
109  #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
110  #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
111 #elif defined(__GNUC__) || defined(__clang__)
112  #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
113  #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (n))) v
114  #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
115 #elif defined(_MSC_VER)
116  #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
117  #define DECLARE_ASM_ALIGNED(n,t,v) __declspec(align(n)) t v
118  #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
119 #else
120  #define DECLARE_ALIGNED(n,t,v) t v
121  #define DECLARE_ASM_ALIGNED(n,t,v) t v
122  #define DECLARE_ASM_CONST(n,t,v) static const t v
123 #endif
124 
145 #if AV_GCC_VERSION_AT_LEAST(3,1)
146  #define av_malloc_attrib __attribute__((__malloc__))
147 #else
148  #define av_malloc_attrib
149 #endif
150 
166 #if AV_GCC_VERSION_AT_LEAST(4,3)
167  #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
168 #else
169  #define av_alloc_size(...)
170 #endif
171 
196 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
197 
207 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
208 
220 av_alloc_size(1, 2) void *av_malloc_array(size_t nmemb, size_t size);
221 
235 av_alloc_size(1, 2) void *av_mallocz_array(size_t nmemb, size_t size);
236 
242 void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
243 
264 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
265 
285 av_warn_unused_result
286 int av_reallocp(void *ptr, size_t size);
287 
303 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
304 
323 av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
324 
342 av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
343 
376 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
377 
407 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
408 
427 void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size);
428 
440 void av_free(void *ptr);
441 
463 void av_freep(void *ptr);
464 
473 char *av_strdup(const char *s) av_malloc_attrib;
474 
484 char *av_strndup(const char *s, size_t len) av_malloc_attrib;
485 
494 void *av_memdup(const void *p, size_t size);
495 
507 void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
508 
609 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
610 
621 av_warn_unused_result
622 int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem);
623 
647 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
648  const uint8_t *elem_data);
649 
669 static inline int av_size_mult(size_t a, size_t b, size_t *r)
670 {
671  size_t t = a * b;
672  /* Hack inspired from glibc: don't try the division if nelem and elsize
673  * are both less than sqrt(SIZE_MAX). */
674  if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
675  return AVERROR(EINVAL);
676  *r = t;
677  return 0;
678 }
679 
693 void av_max_alloc(size_t max);
694 
700 #endif /* AVUTIL_MEM_H */
void * av_realloc_f(void *ptr, size_t nelem, size_t elsize)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:148
void av_max_alloc(size_t max)
Set the maximum size that may be allocated in one block.
Definition: mem.c:73
#define av_malloc_attrib
Function attribute denoting a malloc-like function.
Definition: mem.h:148
Convenience header that includes libavutil&#39;s core.
Macro definitions for various function/variable attributes.
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:414
av_warn_unused_result int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:294
void * av_malloc(size_t size) av_malloc_attrib av_alloc_size(1)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:77
error code definitions
void av_freep(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family...
Definition: mem.c:227
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:283
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:488
void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
Allocate and clear a buffer, reusing the given one if large enough.
Definition: mem.c:493
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:464
av_warn_unused_result int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
char * av_strdup(const char *s) av_malloc_attrib
Duplicate a string.
Definition: mem.c:251
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition: mem.c:322
void * av_realloc(void *ptr, size_t size) av_alloc_size(2)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:135
void av_free(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family...
Definition: mem.c:218
#define av_alloc_size(...)
Function attribute used on a function that allocates memory, whose size is given by the specified par...
Definition: mem.h:169
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
Definition: mem.c:308
void * av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
char * av_strndup(const char *s, size_t len) av_malloc_attrib
Duplicate a substring of a string.
Definition: mem.c:263