GNUnet  0.11.x
Macros | Functions
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 1165 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 1175 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)); \
})
#define GNUNET_malloc(size)
Wrapper around malloc.
#define SIZE_MAX
Definition: platform.h:207
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

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 1274 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 1288 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 1301 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 1313 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 1323 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 1333 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 1346 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 1359 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 1373 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 1387 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 1398 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:67

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 1436 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)
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...

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 1457 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
lenthe number of elements in the existing vector (number of elements to copy over), will be updated with the new array size

Definition at line 1489 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(), compute_policy(), compute_probabilities(), create_response(), dfa_state_create(), display_bar(), do_accept(), do_resume(), do_shutdown(), estate2s(), GCC_2s(), GCCH_2s(), GCPP_2s(), GCT_2s(), get_class(), get_type(), GNUNET_a2s(), GNUNET_CONFIGURATION_get_data(), GNUNET_DISK_directory_scan(), GNUNET_DISK_file_backup(), GNUNET_FRAGMENT_print_ack(), GNUNET_GNSRECORD_pkey_to_zkey(), GNUNET_log_from_nocheck(), GNUNET_NAT_mini_map_stop(), GNUNET_NETWORK_test_port_free(), GNUNET_STRINGS_byte_size_fancy(), GNUNET_STRINGS_pp2s(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TUN_ipv4toregexsearch(), GNUNET_TUN_ipv6toregexsearch(), GSC_2s(), GSC_CLIENTS_deliver_message(), GST_plugins_a2s(), GST_validation_handle_ping(), handle_estimate(), handle_p2p_estimate(), mac_to_string(), make_keywords(), make_reverse_hostname(), new_sub(), port_to_regex(), print_channel_destination(), process_get(), process_refresh_output(), read_blacklist_configuration(), regex_found_handler(), run(), run_upnpc_r(), sb_printf1(), sb_printf2(), sb_printf3(), sb_wrap(), send_ibf(), start_process(), stats_iterator(), store_peer_presistently_iterator(), store_prefix_file_name(), stun_msg2str(), tcp_plugin_address_to_string(), tokenized_cb(), udp_address_to_string(), unix_plugin_address_to_string(), update_config(), wlan_plugin_address_to_string(), 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(), access_handler_callback(), add_address_to_uri(), address_to_regex(), append_port(), associate_shared_service(), automaton_merge_states(), backward_resolution(), build_authz_response(), build_redirect(), callback_scan_for_members(), callback_scan_for_operations(), callback_scan_for_sessions(), client_connect(), client_connect_get(), client_receive_mst_cb(), client_send_cb(), 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(), display_record(), dns_value_to_string(), do_directory_scan(), do_error(), do_redirect_error(), do_userinfo_error(), ds_put_cont(), dump_pc(), encode_cont(), expand_tree(), finish_reserve(), forward_resolution(), GAS_plugin_init(), gather_uri_data(), generate_userinfo_json(), get_download_sync_filename(), get_ego_filename(), get_handle_data_subdir(), get_host_filename(), get_my_cnf_path(), get_room_data_subdir(), get_serialization_file_name(), get_serialization_file_name_in_dir(), get_state_file(), get_store_operation_type(), get_update_information_directory(), gns_value_to_string(), GNUNET_ABD_delegate_to_string(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_ATS_solvers_load_quotas(), GNUNET_ATS_solvers_solver_start(), GNUNET_BIO_flush(), GNUNET_BIO_read(), GNUNET_BIO_read_meta_data(), GNUNET_BIO_read_string(), GNUNET_BIO_write(), GNUNET_BIO_write_meta_data(), GNUNET_CONFIGURATION_config_tool_run(), GNUNET_CONFIGURATION_default(), GNUNET_CONFIGURATION_default_filename(), GNUNET_CONFIGURATION_load(), GNUNET_CONFIGURATION_serialize(), GNUNET_DATACACHE_create(), GNUNET_FRIENDS_write(), GNUNET_FS_data_reader_file_(), GNUNET_FS_download_start_task_(), GNUNET_FS_meta_data_suggest_filename(), GNUNET_FS_publish_main_(), GNUNET_FS_uri_ksk_add_keyword(), GNUNET_FS_uri_ksk_create_from_args(), GNUNET_GNS_lookup_with_tld(), GNUNET_HELLO_compose_uri(), 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_connect_with_cfg(), GNUNET_REST_handle_request(), GNUNET_SERVICE_run_(), GNUNET_STRINGS_filename_expand(), GNUNET_TESTBED_controller_start(), GNUNET_TESTBED_is_host_habitable(), GNUNET_TESTING_configuration_create_(), GNUNET_TESTING_peer_configure(), GNUNET_TESTING_reserve_port(), GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_get_config_name(), GNUNET_TUN_ipv4policy2regex(), GNUNET_TUN_ipv6policy2regex(), GPI_plugins_load(), GST_plugins_load(), GST_stats_init(), handle_backchannel_encapsulation(), handle_dv_box(), handle_dv_learn(), handle_gns_cname_result(), handle_index_start_failed(), handle_manage_peer_service(), handle_overlay_connect(), handle_peer_create(), handle_peer_reconfigure(), header_iterator(), hostkeys_load(), http_client_plugin_send(), http_common_address_from_socket(), http_common_plugin_address_to_string(), http_common_plugin_dnsresult_to_address(), http_common_socket_from_address(), http_server_plugin_send(), iface_proc(), 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_rooms(), iterate_save_session(), LEGACY_SERVICE_run(), libgnunet_plugin_ats_proportional_init(), libgnunet_plugin_rest_gns_init(), libgnunet_plugin_rest_identity_init(), libgnunet_plugin_rest_namestore_init(), libgnunet_plugin_rest_openid_connect_init(), libgnunet_plugin_rest_pabc_init(), libgnunet_plugin_rest_peerinfo_init(), libgnunet_plugin_rest_reclaim_init(), list_callback(), load_episode(), load_episodes(), load_handle_configuration(), load_keys(), load_member(), load_member_next_sessions(), load_member_session(), load_member_session_next(), load_member_store(), load_message_state(), load_message_store(), load_op_add_address(), load_op_del_address(), load_op_start_request(), load_op_start_set_preference(), load_op_start_set_property(), load_op_stop_request(), load_op_stop_set_preference(), load_op_stop_set_property(), load_plugin(), load_quota(), load_room(), logger_run(), login_cont(), main(), mktemp_name(), multirecord_option(), namestore_flat_store_records(), nat_address_cb(), netint_proc(), 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(), occ_cache_get_handle_ats_occ_cb(), occ_cache_get_handle_core_cb(), occ_hello_sent_cb(), OIDC_generate_id_token(), oidc_ticket_issue_cb(), open_static_page(), overlay_connect_get_config(), p1_transport_connect_cache_callback(), p2_controller_connect_cb(), p2_transport_connect(), p2_transport_connect_cache_callback(), parse_name(), partial_match(), plugin_init(), policy_filename_cb(), port_to_regex(), print_address(), print_quotas(), process_result_with_request(), profiler_reply_handle(), profiler_reply_handle_info(), read_from_buffer(), read_from_file(), recursive_gns2dns_resolution(), regex_combine(), REGEX_TEST_automaton_save_graph_step(), remove_room_member_session(), resolve_function(), run(), sample_load_task(), save_handle_configuration(), save_member(), save_member_store(), save_message_state(), save_message_store(), save_room(), server_configure_plugin(), server_notify_external_hostname(), server_receive_mst_cb(), server_send_callback(), service_manage_result_cb(), set_cont(), setup_data_transfer(), setup_queue(), sockaddr_to_udpaddr_string(), start_helper(), start_peer_run(), start_process(), start_shared_service_instance(), stat_iterator(), stop_callback(), store_and_free_entries(), 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 526 of file common_allocation.c.

527 {
528  struct GNUNET_MessageHeader *ret;
529  uint16_t msize;
530 
531  msize = ntohs (msg->size);
532  GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
533  ret = GNUNET_malloc (msize);
534  GNUNET_memcpy (ret, msg, msize);
535  return ret;
536 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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 forward_overlay_connect(), get_hello(), got_hello(), GST_cache_add_hello(), handle_barrier_status(), handle_client_mutation(), handle_incoming_msg(), handle_remote_overlay_connect(), hello_callback(), occ_cache_get_handle_core_cb(), publicize_rm(), and send_with_fragmentation().

Here is the caller graph for this function: