Macros | Functions
malloc.h File Reference
#include <stddef.h>
#include <stdlib.h>
#include "lib/cc/compat_compiler.h"

Go to the source code of this file.


#define tor_free(p)
#define tor_malloc(size)   tor_malloc_(size)
#define tor_malloc_zero(size)   tor_malloc_zero_(size)
#define tor_calloc(nmemb, size)   tor_calloc_(nmemb, size)
#define tor_realloc(ptr, size)   tor_realloc_(ptr, size)
#define tor_reallocarray(ptr, sz1, sz2)   tor_reallocarray_((ptr), (sz1), (sz2))
#define tor_strdup(s)   tor_strdup_(s)
#define tor_strndup(s, n)   tor_strndup_(s, n)
#define tor_memdup(s, n)   tor_memdup_(s, n)
#define tor_memdup_nulterm(s, n)   tor_memdup_nulterm_(s, n)
#define raw_malloc   malloc
#define raw_realloc   realloc
#define raw_free   free
#define raw_strdup   strdup
#define FREE_AND_NULL(typename, freefn, var)


void * tor_malloc_ (size_t size) ATTR_MALLOC
void * tor_malloc_zero_ (size_t size) ATTR_MALLOC
void * tor_calloc_ (size_t nmemb, size_t size) ATTR_MALLOC
void * tor_realloc_ (void *ptr, size_t size)
void * tor_reallocarray_ (void *ptr, size_t size1, size_t size2)
char * tor_strdup_ (const char *s) ATTR_MALLOC
char * tor_strndup_ (const char *s, size_t n) ATTR_MALLOC
void * tor_memdup_ (const void *mem, size_t len) ATTR_MALLOC
void * tor_memdup_nulterm_ (const void *mem, size_t len) ATTR_MALLOC
void tor_free_ (void *mem)

Detailed Description

Headers for util_malloc.c.

Definition in file malloc.h.

Macro Definition Documentation


#define FREE_AND_NULL (   typename,
do { \
/* only evaluate (var) once. */ \
typename **tmp__free__ptr ## freefn = &(var); \
freefn(*tmp__free__ptr ## freefn); \
(*tmp__free__ptr ## freefn) = NULL; \
} while (0)

Definition at line 80 of file malloc.h.

◆ tor_free

#define tor_free (   p)
raw_free(p); \
(p)=NULL; \

Release memory allocated by tor_malloc, tor_realloc, tor_strdup, etc. Unlike the free() function, the tor_free() macro sets the pointer value to NULL after freeing it.

This is a macro. If you need a function pointer to release memory from tor_malloc(), use tor_free_().

Note that this macro takes the address of the pointer it is going to free and clear. If that pointer is stored with a nonstandard alignment (eg because of a "packed" pragma) it is not correct to use tor_free().

Definition at line 52 of file malloc.h.

Referenced by addr_policy_free_(), address_ttl_free_(), addressmap_ent_free_(), addressmap_register(), addressmap_virtaddress_ent_free_(), addressmap_virtaddress_remove(), authority_cert_free_(), bitarray_free_(), bridge_free_(), bridge_line_free_(), bto_chan_clear_map(), bto_gid_clear_map(), buf_add_vprintf(), buf_free_(), bw_array_free_(), cached_dir_decref(), circpad_circuit_machineinfo_free_idx(), circpad_machine_setup_tokens(), circpad_machines_free(), circuit_build_times_free_timeouts(), circuit_clear_testing_cell_stats(), circuit_describe_status_for_controller(), circuit_guard_state_free_(), circuit_log_path(), circuitmux_free_(), clear_cached_dir(), clear_geoip_db(), client_dns_set_reverse_addressmap(), config_clear(), config_free_lines_(), connection_free_all(), control_event_logmsg(), countbytes_close_fn(), cpath_ref_decref(), cpuworker_cancel_circ_handshake(), crypto_dh_free_(), crypto_openssl_free_all(), crypto_pk_free_(), crypto_pk_write_private_key_to_filename(), crypto_xof_free_(), ddmap_entry_free_(), destroy_cell_queue_clear(), dir_server_free_(), dirserv_expire_measured_bw_cache(), document_signature_free_(), dump_desc_fifo_cleanup(), ed25519_keypair_free_(), ed25519_pubkey_read_from_file(), ed25519_seckey_read_from_file(), entry_guard_free_(), entry_guard_restriction_free_(), esc_router_info(), escaped(), ewma_free_circ_data(), ext_or_cmd_free_(), ext_orport_free_all(), extrainfo_free_(), fast_handshake_state_free_(), format_cell_stats(), fp_pair_map_free_(), fp_pair_map_iter_next_rmv(), fp_pair_map_remove(), free_cached_resolve_(), free_execve_args(), free_or_history(), geoip_load_file(), geoip_reset_dirreq_stats(), geoip_stats_free_all(), get_bindaddr_for_server_proxy(), get_configured_bridge_by_routerinfo(), get_total_system_memory_impl(), get_transport_bindaddr(), get_transport_options_for_server_proxy(), getinfo_helper_downloads(), guard_selection_free_(), half_edge_free_(), hs_clean_last_hid_serv_requests(), hs_purge_hid_serv_from_last_hid_serv_requests(), hs_token_free_(), inform_pending_connections(), interface_address6_list_free_(), keypin_clear(), kvline_encode(), list_getinfo_options(), load_torrc_from_disk(), log_free_(), make_consensus_method_list(), memarea_chunk_free_unchecked(), memarea_drop_all_(), microdesc_free_all(), microdesc_reset_outdated_dirservers_list(), microdesc_wipe_body(), microdescs_add_to_cache(), MOCK_IMPL(), networkstatus_check_required_protocols(), networkstatus_vote_free_(), node_free_(), nodefamily_free_(), ntor_handshake_state_free_(), ntor_key_map_free_helper(), packed_cell_free_unchecked(), pending_log_message_free_(), port_cfg_free_(), process_environment_free_(), process_free_(), process_reset_environment(), process_vprintf(), proto_entry_free_(), pt_get_extra_info_descriptor_string(), pt_prepare_proxy_list_for_config_read(), queued_event_free_(), rend_cache_failure_intro_entry_free_(), rend_cache_store_v2_desc_as_dir(), rend_data_free_(), rend_encoded_v2_service_descriptor_free_(), rend_parse_service_authorization(), rend_service_authorization_free_(), rend_service_free_(), rend_service_free_intro_(), rend_service_port_config_free_(), rep_hist_exit_stats_term(), rep_hist_remove_predicted_ports(), rep_hist_reset_buffer_stats(), rep_hist_update_bwhist_state_section(), router_reset_warnings(), routerinfo_free_(), routerlist_reset_warnings(), routerset_free_(), routerset_union(), routerstatus_free_(), short_policy_free_(), signed_descriptor_free_(), signed_descriptor_reset(), smartlist_string_remove(), spooled_resource_free_(), storage_dir_free_(), storage_dir_get_usage(), storage_dir_read(), storage_dir_rescan(), tor_addr_port_parse(), tor_cert_free_(), tor_cleanup(), tor_cond_free_(), tor_free_(), tor_lzma_compress_free_(), tor_munmap_anonymous(), tor_mutex_free_(), tor_passwd_free_(), tor_pthread_helper_fn(), tor_tls_free_(), tor_tls_set_logged_address(), tor_x509_cert_free_(), tor_zlib_compress_free_(), tor_zstd_compress_free_(), transport_free_(), validate_data_directories(), var_cell_free_(), vote_routerstatus_free_(), voting_schedule_free_(), workqueue_entry_free_(), and write_short_policy().

Function Documentation

◆ tor_calloc_()

void* tor_calloc_ ( size_t  nmemb,
size_t  size 

Allocate a chunk of nmemb*size bytes of memory, fill the memory with zero bytes, and return a pointer to the result. Log and terminate the process on error. (Same as calloc(nmemb,size), but never returns NULL.) The second argument (size) should preferably be non-zero and a compile-time constant.

Definition at line 107 of file malloc.c.

◆ tor_free_()

void tor_free_ ( void *  mem)

Helper for places that need to take a function pointer to the right spelling of "free()".

Definition at line 227 of file malloc.c.

References tor_free.

Referenced by free_execve_args(), smartlist_uniq_digests(), smartlist_uniq_digests256(), smartlist_uniq_strings(), sort_version_list(), and tor_run_main().

◆ tor_malloc_()

void* tor_malloc_ ( size_t  size)

Allocate a chunk of size bytes of memory, and return a pointer to result. On error, log and terminate the process. (Same as malloc(size), but never returns NULL.)

Definition at line 32 of file malloc.c.

Referenced by tor_malloc_zero_(), and tor_run_main().

◆ tor_malloc_zero_()

void* tor_malloc_zero_ ( size_t  size)

Allocate a chunk of size bytes of memory, fill the memory with zero bytes, and return a pointer to the result. Log and terminate the process on error. (Same as calloc(size,1), but never returns NULL.)

Definition at line 63 of file malloc.c.

References tor_malloc_().

◆ tor_memdup_()

void* tor_memdup_ ( const void *  mem,
size_t  len 

Allocate a chunk of len bytes, with the same contents as the len bytes starting at mem.

Definition at line 200 of file malloc.c.

◆ tor_memdup_nulterm_()

void* tor_memdup_nulterm_ ( const void *  mem,
size_t  len 

As tor_memdup(), but add an extra 0 byte at the end of the resulting memory.

Definition at line 213 of file malloc.c.

◆ tor_realloc_()

void* tor_realloc_ ( void *  ptr,
size_t  size 

Change the size of the memory block pointed to by ptr to size bytes long; return the new memory block. On error, log and terminate. (Like realloc(ptr,size), but never returns NULL.)

Definition at line 118 of file malloc.c.

Referenced by tor_run_main().

◆ tor_reallocarray_()

void* tor_reallocarray_ ( void *  ptr,
size_t  sz1,
size_t  sz2 

Try to realloc ptr so that it takes up sz1 * sz2 bytes. Check for overflow. Unlike other allocation functions, return NULL on overflow.

Definition at line 146 of file malloc.c.

◆ tor_strdup_()

char* tor_strdup_ ( const char *  s)

Return a newly allocated copy of the NUL-terminated string s. On error, log and terminate. (Like strdup(s), but never returns NULL.)

Definition at line 160 of file malloc.c.

◆ tor_strndup_()

char* tor_strndup_ ( const char *  s,
size_t  n 

Allocate and return a new string containing the first n characters of s. If s is longer than n characters, only the first n are copied. The result is always NUL-terminated. (Like strndup(s,n), but never returns NULL.)

Definition at line 182 of file malloc.c.