GNUnet 0.22.2
Memory management
Collaboration diagram for Memory management:

Macros

#define GNUNET_MAX_MALLOC_CHECKED   (1024 * 1024 * 40)
 Maximum allocation with GNUNET_malloc macro. More...
 
#define GNUNET_new(type)   (type *) GNUNET_malloc (sizeof(type))
 Allocate a struct or union of the given type. More...
 
#define GNUNET_new_array(n, type)
 Allocate a size n array with structs or unions of the given type. More...
 
#define GNUNET_malloc(size)   GNUNET_xmalloc_ (size, __FILE__, __LINE__)
 Wrapper around malloc. More...
 
#define GNUNET_memdup(buf, size)   GNUNET_xmemdup_ (buf, size, __FILE__, __LINE__)
 Allocate and initialize a block of memory. More...
 
#define GNUNET_malloc_large(size)    GNUNET_xmalloc_unchecked_ (size, __FILE__, __LINE__)
 Wrapper around malloc. More...
 
#define GNUNET_realloc(ptr, size)    GNUNET_xrealloc_ (ptr, size, __FILE__, __LINE__)
 Wrapper around realloc. More...
 
#define GNUNET_free_nz(ptr)   GNUNET_xfree_ (ptr, __FILE__, __LINE__)
 Wrapper around free. More...
 
#define GNUNET_free(ptr)
 Wrapper around free. More...
 
#define GNUNET_strdup(a)   GNUNET_xstrdup_ (a, __FILE__, __LINE__)
 Wrapper around GNUNET_xstrdup_. More...
 
#define GNUNET_strndup(a, length)    GNUNET_xstrndup_ (a, length, __FILE__, __LINE__)
 Wrapper around GNUNET_xstrndup_. More...
 
#define GNUNET_array_grow(arr, size, tsize)
 Grow a well-typed (!) array. More...
 
#define GNUNET_array_append(arr, len, element)
 Append an element to an array (growing the array by one). More...
 
#define GNUNET_array_concatenate(arr1, len1, arr2, len2)
 Append arr2 to arr1 (growing arr1 as needed). More...
 

Functions

int GNUNET_snprintf (char *buf, size_t size, const char *format,...) __attribute__((format(printf
 Like snprintf, just aborts if the buffer is of insufficient size. More...
 
int int GNUNET_asprintf (char **buf, const char *format,...) __attribute__((format(printf
 Like asprintf, just portable. More...
 
struct GNUNET_MessageHeaderGNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
 Create a copy of the given message. More...
 

Detailed Description

Macro Definition Documentation

◆ GNUNET_MAX_MALLOC_CHECKED

#define GNUNET_MAX_MALLOC_CHECKED   (1024 * 1024 * 40)

Maximum allocation with GNUNET_malloc macro.

Definition at line 1226 of file gnunet_common.h.

◆ GNUNET_new

#define GNUNET_new (   type)    (type *) GNUNET_malloc (sizeof(type))

Allocate a struct or union of the given type.

Wrapper around GNUNET_malloc that returns a pointer to the newly created object of the correct type.

Parameters
typename of the struct or union, i.e. pass 'struct Foo'.

Definition at line 1236 of file gnunet_common.h.

◆ GNUNET_new_array

#define GNUNET_new_array (   n,
  type 
)
Value:
({ \
GNUNET_assert (SIZE_MAX / sizeof (type) >= n); \
(type *) GNUNET_malloc ((n) * sizeof(type)); \
})
static uint32_t type
Type string converted to DNS type value.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define SIZE_MAX
Definition: platform.h:208

Allocate a size n array with structs or unions of the given type.

Wrapper around GNUNET_malloc that returns a pointer to the newly created objects of the correct type.

Parameters
nnumber of elements in the array
typename of the struct or union, i.e. pass 'struct Foo'.

Definition at line 1336 of file gnunet_common.h.

◆ GNUNET_malloc

#define GNUNET_malloc (   size)    GNUNET_xmalloc_ (size, __FILE__, __LINE__)

Wrapper around malloc.

Allocates size bytes of memory. The memory will be zero'ed out.

Parameters
sizethe number of bytes to allocate, must be smaller than 40 MB.
Returns
pointer to size bytes of memory, never NULL (!)

Definition at line 1351 of file gnunet_common.h.

◆ GNUNET_memdup

#define GNUNET_memdup (   buf,
  size 
)    GNUNET_xmemdup_ (buf, size, __FILE__, __LINE__)

Allocate and initialize a block of memory.

Parameters
bufdata to initialize the block with
sizethe number of bytes in buf (and size of the allocation)
Returns
pointer to size bytes of memory, never NULL (!)

Definition at line 1361 of file gnunet_common.h.

◆ GNUNET_malloc_large

#define GNUNET_malloc_large (   size)     GNUNET_xmalloc_unchecked_ (size, __FILE__, __LINE__)

Wrapper around malloc.

Allocates size bytes of memory. The memory will be zero'ed out.

Parameters
sizethe number of bytes to allocate
Returns
pointer to size bytes of memory, NULL if we do not have enough memory

Definition at line 1371 of file gnunet_common.h.

◆ GNUNET_realloc

#define GNUNET_realloc (   ptr,
  size 
)     GNUNET_xrealloc_ (ptr, size, __FILE__, __LINE__)

Wrapper around realloc.

Reallocates size bytes of memory. The content of the intersection of the new and old size will be unchanged.

Parameters
ptrthe pointer to reallocate
sizethe number of bytes to reallocate
Returns
pointer to size bytes of memory

Definition at line 1384 of file gnunet_common.h.

◆ GNUNET_free_nz

#define GNUNET_free_nz (   ptr)    GNUNET_xfree_ (ptr, __FILE__, __LINE__)

Wrapper around free.

Frees the memory referred to by ptr. Note that it is generally better to free memory that was allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free_nz.

Parameters
ptrlocation where to free the memory. ptr must have been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or GNUNET_array_grow earlier. NULL is allowed.

Definition at line 1397 of file gnunet_common.h.

◆ GNUNET_free

#define GNUNET_free (   ptr)
Value:
do { \
GNUNET_xfree_ (ptr, __FILE__, __LINE__); \
ptr = NULL; \
} while (0)

Wrapper around free.

Frees the memory referred to by ptr and sets ptr to NULL. Note that it is generally better to free memory that was allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free.

ptr will be set to NULL. Use GNUNET_free_nz() if ptr is not an L-value.

Parameters
ptrlocation where to free the memory. ptr must have been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or GNUNET_array_grow earlier. NULL is allowed.

Definition at line 1411 of file gnunet_common.h.

◆ GNUNET_strdup

#define GNUNET_strdup (   a)    GNUNET_xstrdup_ (a, __FILE__, __LINE__)

Wrapper around GNUNET_xstrdup_.

Makes a copy of the zero-terminated string pointed to by a.

Parameters
apointer to a zero-terminated string
Returns
a copy of the string including zero-termination

Definition at line 1425 of file gnunet_common.h.

◆ GNUNET_strndup

#define GNUNET_strndup (   a,
  length 
)     GNUNET_xstrndup_ (a, length, __FILE__, __LINE__)

Wrapper around GNUNET_xstrndup_.

Makes a partial copy of the string pointed to by a.

Parameters
apointer to a string
lengthof the string to duplicate
Returns
a partial copy of the string including zero-termination

Definition at line 1437 of file gnunet_common.h.

◆ GNUNET_array_grow

#define GNUNET_array_grow (   arr,
  size,
  tsize 
)
Value:
GNUNET_xgrow_ ((void **) &(arr), \
sizeof((arr)[0]), \
&size, \
tsize, \
__FILE__, \
__LINE__)
void GNUNET_xgrow_(void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber)
Grow an array, the new elements are zeroed out.
static unsigned int size
Size of the "table".
Definition: peer.c:68

Grow a well-typed (!) array.

This is a convenience method to grow a vector arr of size size to the new (target) size tsize.

Example (simple, well-typed stack):

static struct foo * myVector = NULL;
static int myVecLen = 0;

static void push(struct foo * elem) {
  GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
  GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
}

static void pop(struct foo * elem) {
  if (myVecLen == 0) die();
  GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
  GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
}
Parameters
arrbase-pointer of the vector, may be NULL if size is 0; will be updated to reflect the new address. The TYPE of arr is important since size is the number of elements and not the size in bytes
sizethe number of elements in the existing vector (number of elements to copy over), will be updated with the new array size
tsizethe target size for the resulting vector, use 0 to free the vector (then, arr will be NULL afterwards).

Definition at line 1475 of file gnunet_common.h.

◆ GNUNET_array_append

#define GNUNET_array_append (   arr,
  len,
  element 
)
Value:
do \
{ \
GNUNET_assert ((len) + 1 > (len)); \
GNUNET_array_grow (arr, len, len + 1); \
(arr) [len - 1] = element; \
} while (0)

Append an element to an array (growing the array by one).

Parameters
arrbase-pointer of the vector, may be NULL if len is 0; will be updated to reflect the new address. The TYPE of arr is important since size is the number of elements and not the size in bytes
lenthe number of elements in the existing vector (number of elements to copy over), will be updated with the new array length
elementthe element that will be appended to the array

Definition at line 1496 of file gnunet_common.h.

◆ GNUNET_array_concatenate

#define GNUNET_array_concatenate (   arr1,
  len1,
  arr2,
  len2 
)
Value:
do \
{ \
const typeof (*arr2) * _a1 = (arr1); \
const typeof (*arr1) * _a2 = (arr2); \
GNUNET_assert ((len1) + (len2) >= (len1)); \
GNUNET_assert (SIZE_MAX / sizeof (*_a1) >= ((len1) + (len2))); \
GNUNET_array_grow (arr1, len1, (len1) + (len2)); \
memcpy (&(arr1) [(len1) - (len2)], _a2, (len2) * sizeof (*arr1)); \
} while (0)

Append arr2 to arr1 (growing arr1 as needed).

The arr2 array is left unchanged. Naturally this function performs a shallow copy. Both arrays must have the same type for their elements.

Parameters
arr1base-pointer of the vector, may be NULL if len is 0; will be updated to reflect the new address. The TYPE of arr is important since size is the number of elements and not the size in bytes
len1the number of elements in the existing vector (number of elements to copy over), will be updated with the new array size
arr2base-pointer a second array to concatenate, may be NULL if len2 is 0; will be updated to reflect the new address. The TYPE of arr is important since size is the number of elements and not the size in bytes
len2the number of elements in the existing vector (number of elements to copy over), will be updated with the new array size

Definition at line 1528 of file gnunet_common.h.

Function Documentation

◆ GNUNET_snprintf()

int GNUNET_snprintf ( char *  buf,
size_t  size,
const char *  format,
  ... 
)

Like snprintf, just aborts if the buffer is of insufficient size.

Parameters
bufpointer to buffer that is written to
sizenumber of bytes in buf
formatformat strings
...data for format string
Returns
number of bytes written to buf or negative value on error

Referenced by auth_key_to_string(), check_patch_applied(), compute_policy(), create_response(), dfa_state_create(), display_bar(), do_accept(), do_reject(), do_resume(), do_shutdown(), estate2s(), GCC_2s(), GCCH_2s(), GCPP_2s(), GCT_2s(), get_class(), get_type(), GN_request_connection_reversal(), GNUNET_a2s(), GNUNET_CONFIGURATION_set_value_number(), GNUNET_DHT_pp2s(), GNUNET_DISK_directory_scan(), GNUNET_DISK_file_backup(), GNUNET_DNSPARSER_builder_add_uri(), GNUNET_GNSRECORD_pkey_to_zkey(), GNUNET_log_from_nocheck(), GNUNET_NAT_mini_map_stop(), GNUNET_NETWORK_test_port_free(), GNUNET_PQ_run_sql(), GNUNET_REGEX_ipv4toregexsearch(), GNUNET_REGEX_ipv6toregexsearch(), GNUNET_STRINGS_byte_size_fancy(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_relative2s(), GSC_2s(), GSC_CLIENTS_deliver_message(), handle_p2p_estimate(), make_keywords(), make_reverse_hostname(), netjail_start_run(), new_sub(), port_to_regex(), print_channel_destination(), process_get(), process_refresh_output(), regex_found_handler(), run(), run_upnpc_r(), sb_printf1(), sb_printf2(), sb_printf3(), sb_wrap(), send_ibf(), start_helper(), start_process(), stats_iterator(), store_peer_presistently_iterator(), store_prefix_file_name(), stream_send_data(), stun_msg2str(), submit_post_request(), tokenized_cb(), update_config(), and work().

◆ GNUNET_asprintf()

int int GNUNET_asprintf ( char **  buf,
const char *  format,
  ... 
)

Like asprintf, just portable.

Parameters
bufset to a buffer of sufficient size (allocated, caller must free)
formatformat string (see printf, fprintf, etc.)
...data for format string
Returns
number of bytes in "*buf" excluding 0-termination

Referenced by abd_value_to_string(), add_search_string(), address_to_regex(), automaton_merge_states(), automaton_save_graph_step(), backward_resolution(), build_authz_response(), build_redirect(), callback_scan_for_members(), callback_scan_for_sessions(), code_redirect(), compute_policy(), con_val_iter(), connect_peers_run(), construct_task_graph(), construct_task_graph_gradecast(), conversation_value_to_string(), create_channel_to_destination(), create_response(), create_source(), create_string_array(), create_subnets(), create_unique_cfgs(), curl_check_hdr(), dfa_add_multi_strides_helper(), dfa_compress_paths_helper(), dfa_state_create(), DID_ego_to_did(), DID_key_convert_gnunet_to_multibase_base64(), DID_pkey_to_did(), DID_pkey_to_did_document(), display_record(), dns_value_to_string(), do_error(), do_redirect_error(), do_userinfo_error(), ds_put_cont(), ego_sign_data_cb(), encapsulate_for_dv(), encode_cont(), expand_tree(), finish_reserve(), forward_resolution(), gather_uri_data(), generate_userinfo_json(), get_download_sync_filename(), get_ego_filename(), get_oidc_jwk_path(), get_room_data_subdir(), get_serialization_file_name(), get_serialization_file_name_in_dir(), get_srv_handle_data_subdir(), get_state_file(), get_update_information_directory(), gns_value_to_string(), GNUNET_ABD_delegate_to_string(), GNUNET_BIO_flush(), GNUNET_BIO_read(), GNUNET_BIO_read_string(), GNUNET_BIO_write(), GNUNET_CONFIGURATION_config_tool_run(), GNUNET_CONFIGURATION_default_filename(), GNUNET_CONFIGURATION_load(), GNUNET_CONFIGURATION_serialize(), GNUNET_DATACACHE_create(), GNUNET_DISK_fn_write(), GNUNET_DNSPARSER_parse_uri(), GNUNET_FS_data_reader_file_(), GNUNET_FS_download_start_task_(), GNUNET_FS_meta_data_suggest_filename(), GNUNET_FS_publish_main_(), GNUNET_FS_read_meta_data(), GNUNET_FS_uri_ksk_add_keyword(), GNUNET_FS_uri_ksk_create_from_args(), GNUNET_get_udp_socket(), GNUNET_GNS_lookup_with_tld(), GNUNET_HELLO_builder_to_url2(), GNUNET_HELLO_parser_from_url(), GNUNET_HELLO_parser_to_url(), GNUNET_HELLO_sign_address(), GNUNET_HOSTLIST_server_start(), GNUNET_log_setup(), GNUNET_OS_check_helper_binary(), GNUNET_OS_get_libexec_binary_path(), GNUNET_OS_get_suid_binary_path(), GNUNET_OS_installation_get_path(), GNUNET_PQ_exec_sql(), GNUNET_PROGRAM_conf_and_options(), GNUNET_REGEX_ipv4policy2regex(), GNUNET_REGEX_ipv6policy2regex(), GNUNET_REST_handle_request(), GNUNET_SERVICE_run_(), GNUNET_STRINGS_filename_expand(), GNUNET_TESTBED_cmd_system_create(), GNUNET_TESTBED_configuration_create(), GNUNET_TESTBED_reserve_port(), GNUNET_TESTING_get_address(), GNUNET_TESTING_get_topo_from_string_(), GNUNET_TRANSPORT_TESTING_get_config_name(), gnunet_try_connect(), handle_backchannel_encapsulation(), handle_dv_box(), handle_dv_learn(), handle_gns_redirect_result(), handle_index_start_failed(), handle_inline(), header_iterator(), hello_for_client_cb(), hello_for_incoming_cb(), insert_non_mandatory_keyword(), ipv4_to_regex(), ipv6_to_regex(), iterate_address_and_compare_cb(), iterate_address_start_burst(), iterate_initial_edge(), iterate_load_next_member_sessions(), iterate_load_next_session(), iterate_save_members(), iterate_save_operations(), iterate_save_session(), libgnunet_plugin_rest_pabc_init(), list_callback(), load_member(), load_member_next_sessions(), load_member_session(), load_member_session_next(), load_member_store(), load_message_state(), load_message_store(), load_operation_store(), load_plugin(), load_srv_room(), login_cont(), main(), messenger_value_to_string(), mktemp_name(), namestore_flat_store_records(), nat_address_cb(), netjail_start_run(), new_sub(), nfa_state_create(), nibble_to_regex(), notify_connect(), notify_disconnect(), ns_lookup_result_cb(), num_to_regex(), OIDC_access_token_new(), OIDC_generate_id_token_hmac(), OIDC_generate_id_token_rsa(), OIDC_parse_authz_code(), oidc_ticket_issue_cb(), open_library(), open_static_resource(), parse_name(), partial_match(), port_to_regex(), process_result_with_request(), read_from_buffer(), read_from_file(), read_send(), recursive_gns2dns_resolution(), regex_combine(), remove_room_member_session(), resolve_function(), REST_gns_init(), REST_identity_init(), REST_namestore_init(), REST_openid_init(), REST_reclaim_init(), run(), save_member(), save_member_session(), save_member_store(), save_message_state(), save_message_store(), save_operation_store(), save_srv_room(), set_cont(), setup_data_transfer(), setup_queue(), sockaddr_to_udpaddr_string(), start_arm_service(), start_helper(), start_peer_run(), start_process(), stop_callback(), store_and_free_entries(), store_pi(), term_callback(), translate_dot_plus(), trigger_recursive_download(), try_handle_plaintext(), try_ifconfig(), try_ip(), try_match_block(), update_config(), update_config_sections(), update_hostlist(), uri_chk_to_string(), uri_loc_to_string(), uri_sks_to_string(), url_iterator(), write_benchmark_data(), and write_to_file().

◆ GNUNET_copy_message()

struct GNUNET_MessageHeader * GNUNET_copy_message ( const struct GNUNET_MessageHeader msg)

Create a copy of the given message.

Parameters
msgmessage to copy
Returns
duplicate of the message

Definition at line 360 of file common_allocation.c.

361{
363 uint16_t msize;
364
365 msize = ntohs (msg->size);
366 GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
367 ret = GNUNET_malloc (msize);
368 GNUNET_memcpy (ret, msg, msize);
369 return ret;
370}
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static int ret
Final status code.
Definition: gnunet-arm.c:93
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Header for all communications.

References GNUNET_assert, GNUNET_malloc, GNUNET_memcpy, msg, ret, and GNUNET_MessageHeader::size.

Referenced by handle_client_mutation(), handle_incoming_msg(), and publicize_rm().

Here is the caller graph for this function: