Memory Management

Macros

#define DECLARE_ALIGNED(n, t, v)   t v
 
#define DECLARE_ASM_CONST(n, t, v)   static const t v
 
#define av_malloc_attrib
 
#define av_alloc_size(...)
 

Functions

void * av_malloc (size_t size) av_malloc_attrib 1(1)
 Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if available on the CPU). More...
 
void * av_malloc_array (size_t nmemb, size_t size)
 
void * av_realloc (void *ptr, size_t size) 1(2)
 Allocate or reallocate a block of memory. More...
 
int av_reallocp (void *ptr, size_t size)
 Allocate or reallocate a block of memory. More...
 
void * av_realloc_array (void *ptr, size_t nmemb, size_t size)
 
int av_reallocp_array (void *ptr, size_t nmemb, size_t size)
 
void av_free (void *ptr)
 Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). More...
 
void * av_mallocz (size_t size) av_malloc_attrib 1(1)
 Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if available on the CPU) and zero all the bytes of the block. More...
 
void * av_mallocz_array (size_t nmemb, size_t size)
 
char * av_strdup (const char *s) av_malloc_attrib
 Duplicate the string s. More...
 
char * av_strndup (const char *s, size_t len) av_malloc_attrib
 Duplicate a substring of the string s. More...
 
void av_freep (void *ptr)
 Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer pointing to it to NULL. More...
 
void av_memcpy_backptr (uint8_t *dst, int back, int cnt)
 deliberately overlapping memcpy implementation More...
 
void * av_fast_realloc (void *ptr, unsigned int *size, size_t min_size)
 Reallocate the given block if it is not large enough, otherwise do nothing. More...
 
void av_fast_malloc (void *ptr, unsigned int *size, size_t min_size)
 Allocate a buffer, reusing the given one if large enough. More...
 

Detailed Description

Macro Definition Documentation

◆ DECLARE_ALIGNED

#define DECLARE_ALIGNED (   n,
  t,
 
)    t v

Definition at line 51 of file mem.h.

◆ DECLARE_ASM_CONST

#define DECLARE_ASM_CONST (   n,
  t,
 
)    static const t v

Definition at line 52 of file mem.h.

◆ av_malloc_attrib

#define av_malloc_attrib

Definition at line 58 of file mem.h.

◆ av_alloc_size

#define av_alloc_size (   ...)

Definition at line 64 of file mem.h.

Function Documentation

◆ av_malloc()

void* av_malloc ( size_t  size)

Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if available on the CPU).

Parameters
sizeSize in bytes for the memory block to be allocated.
Returns
Pointer to the allocated block, NULL if the block cannot be allocated.
See also
av_mallocz()
Examples:
decode_audio.c.

Referenced by decode().

◆ av_malloc_array()

void* av_malloc_array ( size_t  nmemb,
size_t  size 
)

◆ av_realloc()

void* av_realloc ( void *  ptr,
size_t  size 
)

Allocate or reallocate a block of memory.

If ptr is NULL and size > 0, allocate a new block. If size is zero, free the memory block pointed to by ptr.

Parameters
ptrPointer to a memory block already allocated with av_realloc() or NULL.
sizeSize in bytes of the memory block to be allocated or reallocated.
Returns
Pointer to a newly-reallocated block or NULL if the block cannot be reallocated or the function is used to free the memory block.
Warning
Pointers originating from the av_malloc() family of functions must not be passed to av_realloc(). The former can be implemented using memalign() (or other functions), and there is no guarantee that pointers from such functions can be passed to realloc() at all. The situation is undefined according to POSIX and may crash with some libc implementations.
See also
av_fast_realloc()

◆ av_reallocp()

int av_reallocp ( void *  ptr,
size_t  size 
)

Allocate or reallocate a block of memory.

If *ptr is NULL and size > 0, allocate a new block. If size is zero, free the memory block pointed to by ptr.

Parameters
ptrPointer to a pointer to a memory block already allocated with av_realloc(), or pointer to a pointer to NULL. The pointer is updated on success, or freed on failure.
sizeSize in bytes for the memory block to be allocated or reallocated
Returns
Zero on success, an AVERROR error code on failure.
Warning
Pointers originating from the av_malloc() family of functions must not be passed to av_reallocp(). The former can be implemented using memalign() (or other functions), and there is no guarantee that pointers from such functions can be passed to realloc() at all. The situation is undefined according to POSIX and may crash with some libc implementations.

◆ av_realloc_array()

void* av_realloc_array ( void *  ptr,
size_t  nmemb,
size_t  size 
)

◆ av_reallocp_array()

int av_reallocp_array ( void *  ptr,
size_t  nmemb,
size_t  size 
)

◆ av_free()

void av_free ( void *  ptr)

Free a memory block which has been allocated with av_malloc(z)() or av_realloc().

Parameters
ptrPointer to the memory block which should be freed.
Note
ptr = NULL is explicitly allowed.
It is recommended that you use av_freep() instead.
See also
av_freep()
Examples:
decode_audio.c.

Referenced by main().

◆ av_mallocz()

void* av_mallocz ( size_t  size)

Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if available on the CPU) and zero all the bytes of the block.

Parameters
sizeSize in bytes for the memory block to be allocated.
Returns
Pointer to the allocated block, NULL if it cannot be allocated.
See also
av_malloc()

◆ av_mallocz_array()

void* av_mallocz_array ( size_t  nmemb,
size_t  size 
)

◆ av_strdup()

char* av_strdup ( const char *  s)

Duplicate the string s.

Parameters
sstring to be duplicated
Returns
Pointer to a newly-allocated string containing a copy of s or NULL if the string cannot be allocated.

◆ av_strndup()

char* av_strndup ( const char *  s,
size_t  len 
)

Duplicate a substring of the string s.

Parameters
sstring to be duplicated
lenthe maximum length of the resulting string (not counting the terminating byte).
Returns
Pointer to a newly-allocated string containing a copy of s or NULL if the string cannot be allocated.

◆ av_freep()

void av_freep ( void *  ptr)

Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer pointing to it to NULL.

Parameters
ptrPointer to the pointer to the memory block which should be freed.
See also
av_free()
Examples:
decode_audio.c, filter_audio.c, and transcode_aac.c.

Referenced by decode(), init_converted_samples(), main(), and read_decode_convert_and_store().

◆ av_memcpy_backptr()

void av_memcpy_backptr ( uint8_t *  dst,
int  back,
int  cnt 
)

deliberately overlapping memcpy implementation

Parameters
dstdestination buffer
backhow many bytes back we start (the initial size of the overlapping window)
cntnumber of bytes to copy, must be >= 0

cnt > back is valid, this will copy the bytes we just copied, thus creating a repeating pattern with a period length of back.

◆ av_fast_realloc()

void* av_fast_realloc ( void *  ptr,
unsigned int *  size,
size_t  min_size 
)

Reallocate the given block if it is not large enough, otherwise do nothing.

See also
av_realloc

◆ av_fast_malloc()

void av_fast_malloc ( void *  ptr,
unsigned int *  size,
size_t  min_size 
)

Allocate a buffer, reusing the given one if large enough.

Contrary to av_fast_realloc the current buffer contents might not be preserved and on error the old buffer is freed, thus no special handling to avoid memleaks is necessary.

Parameters
ptrpointer to pointer to already allocated buffer, overwritten with pointer to new buffer
sizesize of the buffer *ptr points to
min_sizeminimum size of *ptr buffer after returning, *ptr will be NULL and *size 0 if an error occurred.