FFmpeg
Loading...
Searching...
No Matches
Heap Management

Functions responsible for allocating, freeing, and copying memory. More...

Functions

void * av_malloc (size_t size) av_malloc_attrib 1(1)
 Allocate a memory block with alignment suitable for all memory accesses (including vectors if available on the CPU).
 
void * av_mallocz (size_t size) av_malloc_attrib 1(1)
 Allocate a memory block with alignment suitable for all memory accesses (including vectors if available on the CPU) and zero all the bytes of the block.
 
void * av_malloc_array (size_t nmemb, size_t size)
 
void * av_calloc (size_t nmemb, size_t size) av_malloc_attrib 1(1
 Allocate a memory block for an array with av_mallocz().
 
void void * av_realloc (void *ptr, size_t size) 1(2)
 Allocate, reallocate, or free a block of memory.
 
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.
 
void * av_realloc_f (void *ptr, size_t nelem, size_t elsize)
 Allocate, reallocate, or free a block of memory.
 
void * av_realloc_array (void *ptr, size_t nmemb, size_t size)
 
int av_reallocp_array (void *ptr, size_t nmemb, size_t size)
 Allocate, reallocate an array through a pointer to a pointer.
 
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.
 
void av_fast_malloc (void *ptr, unsigned int *size, size_t min_size)
 Allocate a buffer, reusing the given one if large enough.
 
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.
 
void av_free (void *ptr)
 Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family.
 
void av_freep (void *ptr)
 Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family, and set the pointer pointing to it to NULL.
 
char * av_strdup (const char *s) av_malloc_attrib
 Duplicate a string.
 
char * av_strndup (const char *s, size_t len) av_malloc_attrib
 Duplicate a substring of a string.
 
void * av_memdup (const void *p, size_t size)
 Duplicate a buffer with av_malloc().
 
void av_memcpy_backptr (uint8_t *dst, int back, int cnt)
 Overlapping memcpy() implementation.
 

Detailed Description

Functions responsible for allocating, freeing, and copying memory.

All memory allocation functions have a built-in upper limit of INT_MAX bytes. This may be changed with av_max_alloc(), although exercise extreme caution when doing so.

Function Documentation

◆ av_malloc()

void * av_malloc ( size_t size)

Allocate a memory block 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, or NULL if the block cannot be allocated
See also
av_mallocz()

Definition at line 98 of file mem.c.

◆ av_mallocz()

void * av_mallocz ( size_t size)

Allocate a memory block 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, or NULL if it cannot be allocated
See also
av_malloc()

Definition at line 256 of file mem.c.

◆ av_malloc_array()

void * av_malloc_array ( size_t nmemb,
size_t size )

Definition at line 209 of file mem.c.

◆ av_calloc()

void * av_calloc ( size_t nmemb,
size_t size )

Allocate a memory block for an array with av_mallocz().

The allocated memory will have size size * nmemb bytes.

Parameters
nmembNumber of elements
sizeSize of the single element
Returns
Pointer to the allocated block, or NULL if the block cannot be allocated
See also
av_mallocz()
av_malloc_array()

◆ av_realloc()

void void * av_realloc ( void * ptr,
size_t size )

Allocate, reallocate, or free a block of memory.

If ptr is NULL and size > 0, allocate a new block. Otherwise, expand or shrink that block of memory according to size.

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
Warning
Unlike av_malloc(), the returned pointer is not guaranteed to be correctly aligned. The returned pointer must be freed after even if size is zero.
See also
av_fast_realloc()
av_reallocp()

Definition at line 155 of file mem.c.

◆ av_reallocp()

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.

If *ptr is NULL and size > 0, allocate a new block. If size is zero, free the memory block pointed to by *ptr. Otherwise, expand or shrink that block of memory according to size.

Parameters
[in,out]ptrPointer to a pointer to a memory block already allocated with av_realloc(), or a pointer to NULL. The pointer is updated on success, or freed on failure.
[in]sizeSize in bytes for the memory block to be allocated or reallocated
Returns
Zero on success, an AVERROR error code on failure
Warning
Unlike av_malloc(), the allocated memory is not guaranteed to be correctly aligned.

Definition at line 188 of file mem.c.

Referenced by aom_init(), append_flv_data(), asfrtp_parse_packet(), auxiliary_info_alloc_size(), av_calloc(), av_probe_input_buffer2(), av_vdpau_bind_context(), avi_read_header(), bmv_read_packet(), concatenate_packet(), decode_frame(), dyn_buf_write(), ff_cbs_h2645_assemble_fragment(), ff_els_decode_unsigned(), ff_opencl_filter_load_program_from_file(), fixup_vorbis_headers(), flashsv_decode_frame(), g2m_load_cursor(), get_tcp_server_response(), h264_extradata_to_annexb(), handle_metadata(), hap_decode(), hevc_extradata_to_annexb(), http_open(), jpg_decode_data(), mediacodec_receive(), mov_cenc_end_packet(), mov_realloc_extradata(), opt_get_array(), parse_icy(), qt_rtp_parse_packet(), rdt_parse_sdp_line(), realloc_buffer(), rtmp_http_write(), rtmp_open(), rtmp_write(), set_segment_filename(), tdsc_decode_frame(), tdsc_decode_tiles(), tdsc_init(), tdsc_load_cursor(), tgv_decode_inter(), theora_header(), tls_handshake_loop(), tls_read(), vorbis_packet(), vpx_init(), and vvc_extradata_to_annexb().

◆ av_realloc_f()

void * av_realloc_f ( void * ptr,
size_t nelem,
size_t elsize )

Allocate, reallocate, or free a block of memory.

This function does the same thing as av_realloc(), except:

  • It takes two size arguments and allocates nelem * elsize bytes, after checking the result of the multiplication for integer overflow.
  • It frees the input block in case of failure, thus avoiding the memory leak with the classic
    buf = realloc(buf);
    if (!buf)
    return -1;
    pattern.

Definition at line 173 of file mem.c.

◆ av_realloc_array()

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

Definition at line 217 of file mem.c.

Referenced by add_param_definition(), add_serial_pair(), add_side_data_from_buf_ext(), amf_encode_init_av1(), append_pad(), apv_add_configuration_entry(), apv_add_frameinfo(), apv_metadata_add_payload(), aribcaption_init(), audio_element_obu(), av_bsf_graph_alloc_filter(), av_calloc(), av_dict_set(), av_fifo_alloc2(), av_fifo_grow2(), av_frame_replace(), av_opt_set_array(), av_program_add_stream_index2(), avfilter_graph_alloc_filter(), avformat_new_stream(), avformat_stream_group_create(), batch_assign(), codec_config_obu(), color_inc(), decode_header(), decode_styl(), decode_unregistered_user_data(), decoded_frames_get_free(), ff_alsa_extend_reorder_buf(), ff_ass_add_rect2(), ff_cbs_sei_list_add(), ff_encode_add_cpb_side_data(), ff_framequeue_add(), ff_iamf_add_audio_element(), ff_iamf_add_mix_presentation(), ff_mov_write_packet(), ff_qsv_enc_init(), ff_rtmp_check_alloc_array(), ff_vaapi_decode_make_param_buffer(), ff_vaapi_decode_make_slice_buffer(), fill_codec_config(), fill_marker_resource(), filter_frame(), flv_queue_extradata(), frame_hash(), get_preset_file(), grow_array(), handle_file(), handle_file(), handler(), init(), jpeg2000_decode_packet(), log_callback(), mix_presentation_obu(), mkv_add_cuepoint(), mov_add_tref_id(), mov_add_tref_id(), mov_add_tref_tag(), mov_add_tref_tag(), mov_read_iinf(), mov_read_iloc(), mov_read_iref_dimg(), mxf_read_partition_pack(), new_stream_video(), ogg_new_stream(), open_virtual_track(), opt_set_array(), packet_side_data_add(), param_parse(), parse_audio_preroll(), parse_imf_asset_map_from_xml_dom(), progress_init(), push_main_audio_sequence(), push_marker_sequence(), rd_get(), realloc_section_array(), reduce_colors(), refine_full(), refine_residual(), sample_queue_push(), slices_realloc(), sq_add_stream(), stream_group_add_stream(), tee_open(), update_block_dimensions(), update_frag_index(), vaapi_av1_decode_slice(), vaapi_encode_make_packed_header(), vaapi_encode_make_param_buffer(), wpp_progress_init(), wv_alloc_frame_context(), and xan_decode_frame().

◆ av_reallocp_array()

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

Allocate, reallocate an array through a pointer to a pointer.

If *ptr is NULL and nmemb > 0, allocate a new block.

Parameters
[in,out]ptrPointer to a pointer to a memory block already allocated with av_realloc(), or a pointer to NULL. The pointer is updated on success, or freed on failure.
[in]nmembNumber of elements
[in]sizeSize of the single element
Returns
Zero on success, an AVERROR error code on failure
Warning
Unlike av_malloc(), the allocated memory is not guaranteed to be correctly aligned. *ptr must be freed after even if nmemb is zero.

Definition at line 225 of file mem.c.

Referenced by add_fragment(), add_fragment(), add_segment(), add_tracked_method(), allocate_buffers(), ass_split_section(), av_calloc(), cuvid_handle_video_sequence(), dshow_cycle_devices(), evcc_array_add_nal_unit(), ff_h2645_extract_rbsp(), ff_h2645_sei_ctx_replace(), ff_hap_set_chunk_count(), gem_decode_frame(), gxf_write_map_packet(), gxf_write_packet(), hvcc_array_add_nal_unit(), hw_device_add(), init_tile_data(), mov_add_tfra_entries(), mov_build_index(), mov_finish_fragment(), mov_read_trex(), mxf_write_packet(), mxf_write_partition(), nut_write_packet(), ogg_restore(), opencl_frames_get_constraints(), parse_adaptation_sets(), parse_strk(), tgv_decode_inter(), tm2_read_stream(), update_dimensions_clear_info(), update_index(), vp56_size_changed(), vvcc_array_add_nal_unit(), and write_tiles().

◆ av_fast_realloc()

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.

If the given buffer is NULL, then a new uninitialized buffer is allocated.

If the given buffer is not large enough, and reallocation fails, NULL is returned and *size is set to 0, but the original buffer is not changed or freed.

A typical use pattern follows:

uint8_t *buf = ...;
uint8_t *new_buf = av_fast_realloc(buf, &current_size, size_needed);
if (!new_buf) {
// Allocation failed; clean up original buffer
av_freep(&buf);
return AVERROR(ENOMEM);
}
#define AVERROR(e)
Definition error.h:45
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:495
#define av_freep(p)
Parameters
[in,out]ptrAlready allocated buffer, or NULL
[in,out]sizePointer to the size of buffer ptr. *size is updated to the new allocated size, in particular 0 in case of failure.
[in]min_sizeDesired minimal size of buffer ptr
Returns
ptr if the buffer is large enough, a pointer to newly reallocated buffer if the buffer was not large enough, or NULL in case of error
See also
av_realloc()
av_fast_malloc()

Definition at line 495 of file mem.c.

Referenced by add_index_entry(), add_program(), add_tts_entry(), av_calloc(), av_exif_set_entry(), caf_write_packet(), config_output(), decode_frame(), dirac_combine_frame(), draw_items(), dxv_decode(), dxva2_av1_decode_slice(), evc_frame_merge_filter(), exif_parse_ifd_list(), ff_add_index_entry(), ff_af_queue_add(), ff_av1_packet_split(), ff_combine_frame(), ff_h2645_packet_split(), ff_mms_asf_header_parser(), ff_nvdec_simple_decode_slice(), ff_range_add(), ff_subtitles_queue_insert(), ff_tx_init_subtx(), ff_vdpau_add_buffer(), ff_videotoolbox_buffer_append(), ff_videotoolbox_buffer_copy(), ff_vk_decode_add_slice(), filter_frame(), filter_frame(), flac_fifo_read_wrap(), get_stats(), get_stats(), h264_extradata_to_annexb(), h264_mp4toannexb_save_ps(), init(), init(), init(), intercept_id3(), libx265_encode_frame(), lzf_realloc(), mov_merge_tts_data(), mov_parse_auxiliary_info(), mov_read_ctts(), mov_read_pssh(), mov_read_saio(), mov_read_senc(), mov_read_stts(), mov_read_trun(), mov_text_style_start(), mxg_update_cache(), nal_parse_units(), nvdec_av1_decode_slice(), nvdec_h264_decode_slice(), nvdec_hevc_decode_slice(), nvdec_vc1_decode_slice(), open_track_file_resource(), parse_string(), prepare_sei_data_array(), push_main_audio_sequence(), push_main_image_2d_sequence(), queue_frames(), queue_frames(), read_frame(), setup_frame(), shorten_decode_frame(), spdif_header_eac3(), update_frag_index(), and vc1_decode_frame().

◆ 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.

*ptr is allowed to be NULL, in which case allocation always happens if size_needed is greater than 0.

uint8_t *buf = ...;
av_fast_malloc(&buf, &current_size, size_needed);
if (!buf) {
// Allocation failed; buf already freed
return AVERROR(ENOMEM);
}
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:555
Parameters
[in,out]ptrPointer to pointer to an already allocated buffer. *ptr will be overwritten with pointer to new buffer on success or NULL on failure
[in,out]sizePointer to the size of buffer *ptr. *size is updated to the new allocated size, in particular 0 in case of failure.
[in]min_sizeDesired minimal size of buffer *ptr
See also
av_realloc()
av_fast_mallocz()

Definition at line 555 of file mem.c.

Referenced by alloc_scratch_buffers(), allocate_buffers(), ape_decode_frame(), audio_decode_frame(), av_calloc(), av_fast_padded_mallocz(), bl16_decode_8(), chs_alloc_lsb_band_data(), chs_alloc_msb_band_data(), chs_assemble_freq_bands(), codec_reinit(), crypto_write(), decode_frame(), decode_frame(), decode_frame(), decode_remap(), detect_clips(), do_interpolation(), ff_dca_core_filter_fixed(), ff_h274_hash_verify(), fic_decode_frame(), filter_frame(), filter_frame_float(), find_motion(), gif_decode_frame(), gif_read_image(), id3v2_parse(), interpolation(), iterm2_write_packet(), ljpeg_decode_rgb_scan(), magy_decode_frame(), opus_decode_subpacket(), parse_navi_table(), parse_utf(), pmp_packet(), rscc_decode_frame(), scale_internal(), spdif_header_dts4(), spdif_write_packet(), tak_decode_frame(), truemotion1_decode_header(), truemotion1_decode_init(), update_block_dimensions(), update_init_section(), update_init_section(), update_md5_sum(), and verify_md5().

◆ av_fast_mallocz()

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.

Like av_fast_malloc(), but all newly allocated space is initially cleared. Reused buffer is not cleared.

*ptr is allowed to be NULL, in which case allocation always happens if size_needed is greater than 0.

Parameters
[in,out]ptrPointer to pointer to an already allocated buffer. *ptr will be overwritten with pointer to new buffer on success or NULL on failure
[in,out]sizePointer to the size of buffer *ptr. *size is updated to the new allocated size, in particular 0 in case of failure.
[in]min_sizeDesired minimal size of buffer *ptr
See also
av_fast_malloc()

Definition at line 560 of file mem.c.

Referenced by alloc_sample_buffer(), alloc_sample_buffer(), alloc_scratch_buffers(), alloc_x96_sample_buffer(), av_calloc(), av_exif_clone_ifd(), av_fast_padded_malloc(), decode_frame(), decode_frame(), g2m_init_buffers(), and op_pass_setup().

◆ av_free()

void av_free ( void * ptr)

Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family.

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, to prevent leaving behind dangling pointers.
See also
av_freep()

Definition at line 238 of file mem.c.

◆ av_freep()

void av_freep ( void * ptr)

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

uint8_t *buf = av_malloc(16);
av_free(buf);
// buf now contains a dangling pointer to freed memory, and accidental
// dereference of buf will result in a use-after-free, which may be a
// security risk.
uint8_t *buf = av_malloc(16);
av_freep(&buf);
// buf is now NULL, and accidental dereference will only result in a
// NULL-pointer dereference.
#define av_malloc(s)
Definition ops_static.c:52
#define av_free(p)
Parameters
ptrPointer to the pointer to the memory block which should be freed
Note
*ptr = NULL is safe and leads to no action.
See also
av_free()

Definition at line 247 of file mem.c.

◆ av_strdup()

char * av_strdup ( const char * s)

Duplicate a string.

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
See also
av_strndup()

Definition at line 272 of file mem.c.

◆ av_strndup()

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

Duplicate a substring of a string.

Parameters
sString to be duplicated
lenMaximum length of the resulting string (not counting the terminating byte)
Returns
Pointer to a newly-allocated string containing a substring of s or NULL if the string cannot be allocated

Definition at line 284 of file mem.c.

Referenced by av_calloc(), get_extension(), hw_device_init_from_string(), id3v2_check_write_lang_descr_tag(), match_stream_specifier(), mpjpeg_get_boundary(), parse_adaptation_sets(), parse_cookie(), process_line(), stream_specifier_parse(), and vgs_parse_statement().

◆ av_memdup()

◆ av_memcpy_backptr()

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

Overlapping memcpy() implementation.

Parameters
dstDestination buffer
backNumber of bytes back to start copying (i.e. the initial size of the overlapping window); must be > 0
cntNumber of bytes to copy; must be >= 0
Note
cnt > back is valid, this will copy the bytes we just copied, thus creating a repeating pattern with a period length of back.

Definition at line 445 of file mem.c.

Referenced by av_calloc(), color_frame(), copy_backptr(), decode_dsw1(), decode_frame(), decode_frame(), decode_tsw1(), ff_lzf_uncompress(), fill_frame(), generate_missing_ref(), generate_missing_ref(), memset_bytes(), mszh_decomp(), picmemset(), qsv_fill_border(), qsvenc_fill_padding_area(), realloc_alpha_uv(), residual_interp(), synth_block_fcb_acb(), unpack(), xan_unpack(), and xan_unpack().