GNUnet 0.21.0
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_new_array_2d(n, m, type)    (type **) GNUNET_xnew_array_2d_ (n, m, sizeof(type), __FILE__, __LINE__)
 Allocate a size n times m array with structs or unions of the given type. More...
 
#define GNUNET_new_array_3d(n, m, o, type)    (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof(type), __FILE__, __LINE__)
 Allocate a size n times m times o 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 1198 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 1208 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 1307 of file gnunet_common.h.

◆ GNUNET_new_array_2d

#define GNUNET_new_array_2d (   n,
  m,
  type 
)     (type **) GNUNET_xnew_array_2d_ (n, m, sizeof(type), __FILE__, __LINE__)

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

Parameters
nsize of the first dimension
msize of the second dimension
typename of the struct or union, i.e. pass 'struct Foo'.

Definition at line 1321 of file gnunet_common.h.

◆ GNUNET_new_array_3d

#define GNUNET_new_array_3d (   n,
  m,
  o,
  type 
)     (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof(type), __FILE__, __LINE__)

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

Parameters
nsize of the first dimension
msize of the second dimension
osize of the third dimension
typename of the struct or union, i.e. pass 'struct Foo'.

Definition at line 1334 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 1346 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 1356 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 1366 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 1379 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 1392 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 1406 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 1420 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 1431 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 1469 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 1490 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 1522 of file gnunet_common.h.

Function Documentation

◆ GNUNET_snprintf()

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

◆ 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(), add_to_builder(), address_to_regex(), associate_shared_service(), automaton_merge_states(), 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_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(), exec_bash_script_run(), 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(), 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_GNS_lookup_with_tld(), GNUNET_HELLO_builder_from_url(), GNUNET_HELLO_builder_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_REST_handle_request(), GNUNET_SERVICE_main(), GNUNET_SERVICE_run_(), GNUNET_STRINGS_filename_expand(), GNUNET_TESTING_configuration_create_(), GNUNET_TESTING_get_address(), GNUNET_TESTING_peer_configure(), GNUNET_TESTING_reserve_port(), GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_get_config_name(), gnunet_try_connect(), GNUNET_TUN_ipv4policy2regex(), GNUNET_TUN_ipv6policy2regex(), 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(), hostkeys_load(), insert_non_mandatory_keyword(), ipv4_to_regex(), ipv6_to_regex(), 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(), netjail_stop_run(), new_sub(), nfa_state_create(), nibble_to_regex(), notify_connect(), notify_disconnect(), ns_lookup_result_cb(), num_to_regex(), OIDC_generate_id_token_hmac(), OIDC_generate_id_token_rsa(), oidc_ticket_issue_cb(), open_static_resource(), parse_name(), partial_match(), plugin_init(), plugins_load(), port_to_regex(), process_result_with_request(), read_from_buffer(), read_from_file(), recursive_gns2dns_resolution(), regex_combine(), REGEX_TEST_automaton_save_graph_step(), 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(), start_shared_service_instance(), stop_callback(), store_and_free_entries(), store_pi(), term_callback(), tokenizer_cb(), 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 470 of file common_allocation.c.

471{
473 uint16_t msize;
474
475 msize = ntohs (msg->size);
476 GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
477 ret = GNUNET_malloc (msize);
478 GNUNET_memcpy (ret, msg, msize);
479 return ret;
480}
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static int ret
Final status code.
Definition: gnunet-arm.c:94
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.

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: