From e092ecd1244a1ce4b0e1d92120f403628dfc41e9 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 13 Jan 2025 16:18:12 +0000 Subject: [PATCH] cleanup: Use tox memory allocator in some more places. --- auto_tests/announce_test.c | 2 +- auto_tests/forwarding_test.c | 2 +- other/DHT_bootstrap.c | 6 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 6 +- other/event_tooling/generate_event_c.cpp | 2 +- toxcore/BUILD.bazel | 7 +++ toxcore/LAN_discovery.c | 55 ++++++++++++------- toxcore/LAN_discovery.h | 3 +- toxcore/Messenger.c | 10 ++-- toxcore/TCP_server.c | 2 +- toxcore/announce.c | 13 ++--- toxcore/crypto_core.c | 1 - toxcore/crypto_core_test.cc | 1 - toxcore/events/conference_connected.c | 1 + toxcore/events/conference_invite.c | 1 + toxcore/events/conference_message.c | 1 + toxcore/events/conference_peer_list_changed.c | 1 + toxcore/events/conference_peer_name.c | 1 + toxcore/events/conference_title.c | 1 + toxcore/events/dht_get_nodes_response.c | 1 + toxcore/events/events_alloc.h | 3 - toxcore/events/file_chunk_request.c | 1 + toxcore/events/file_recv.c | 1 + toxcore/events/file_recv_chunk.c | 1 + toxcore/events/file_recv_control.c | 1 + toxcore/events/friend_connection_status.c | 1 + toxcore/events/friend_lossless_packet.c | 1 + toxcore/events/friend_lossy_packet.c | 1 + toxcore/events/friend_message.c | 1 + toxcore/events/friend_name.c | 1 + toxcore/events/friend_read_receipt.c | 1 + toxcore/events/friend_request.c | 1 + toxcore/events/friend_status.c | 1 + toxcore/events/friend_status_message.c | 1 + toxcore/events/friend_typing.c | 1 + toxcore/events/group_custom_packet.c | 1 + toxcore/events/group_custom_private_packet.c | 1 + toxcore/events/group_invite.c | 1 + toxcore/events/group_join_fail.c | 1 + toxcore/events/group_message.c | 1 + toxcore/events/group_moderation.c | 1 + toxcore/events/group_password.c | 1 + toxcore/events/group_peer_exit.c | 1 + toxcore/events/group_peer_join.c | 1 + toxcore/events/group_peer_limit.c | 1 + toxcore/events/group_peer_name.c | 1 + toxcore/events/group_peer_status.c | 1 + toxcore/events/group_privacy_state.c | 1 + toxcore/events/group_private_message.c | 1 + toxcore/events/group_self_join.c | 1 + toxcore/events/group_topic.c | 1 + toxcore/events/group_topic_lock.c | 1 + toxcore/events/group_voice_state.c | 1 + toxcore/events/self_connection_status.c | 1 + toxcore/forwarding.c | 10 ++-- toxcore/forwarding.h | 3 +- toxcore/friend_connection.c | 18 +++--- toxcore/friend_connection.h | 4 +- toxcore/friend_requests.c | 26 +++++++-- toxcore/friend_requests.h | 4 +- toxcore/group_announce.c | 27 ++++++--- toxcore/group_announce.h | 8 ++- toxcore/group_announce_fuzz_test.cc | 5 +- toxcore/group_announce_test.cc | 16 +++--- toxcore/group_chats.c | 2 +- toxcore/group_onion_announce.c | 4 +- toxcore/list.c | 14 +++-- toxcore/list.h | 5 +- toxcore/list_test.cc | 11 +++- toxcore/net_crypto.c | 2 +- toxcore/onion_announce.c | 2 +- toxcore/onion_announce.h | 2 +- toxcore/tox_dispatch.c | 2 +- toxcore/util.c | 6 +- toxcore/util.h | 9 ++- 75 files changed, 222 insertions(+), 111 deletions(-) diff --git a/auto_tests/announce_test.c b/auto_tests/announce_test.c index ef93095b1b..7e387c2739 100644 --- a/auto_tests/announce_test.c +++ b/auto_tests/announce_test.c @@ -66,7 +66,7 @@ static void test_store_data(void) ck_assert(net != nullptr); DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true); ck_assert(dht != nullptr); - Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht); + Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht); ck_assert(forwarding != nullptr); Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding); ck_assert(announce != nullptr); diff --git a/auto_tests/forwarding_test.c b/auto_tests/forwarding_test.c index 3ba85ed151..a1b8d6d248 100644 --- a/auto_tests/forwarding_test.c +++ b/auto_tests/forwarding_test.c @@ -129,7 +129,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp, const TCP_Proxy_Info inf = {{{{0}}}}; subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf); - subtox->forwarding = new_forwarding(subtox->log, rng, subtox->mono_time, subtox->dht); + subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht); ck_assert(subtox->forwarding != nullptr); subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding); diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index cd7ea4df8b..629ec60c7a 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -159,8 +159,8 @@ int main(int argc, char *argv[]) const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); DHT *dht = new_dht(logger, mem, rng, ns, mono_time, new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr), true, true); Onion *onion = new_onion(logger, mem, mono_time, rng, dht); - Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht); - GC_Announces_List *gc_announces_list = new_gca_list(); + Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht); + GC_Announces_List *gc_announces_list = new_gca_list(mem); Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht); #ifdef DHT_NODE_EXTRA_PACKETS @@ -245,7 +245,7 @@ int main(int argc, char *argv[]) bool is_waiting_for_dht_connection = true; uint64_t last_lan_discovery = 0; - const Broadcast_Info *broadcast = lan_discovery_init(ns); + const Broadcast_Info *broadcast = lan_discovery_init(mem, ns); while (true) { mono_time_update(mono_time); diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index ad4dbec1ae..09a54df5d1 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -348,7 +348,7 @@ int main(int argc, char *argv[]) return 1; } - Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht); + Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht); if (forwarding == nullptr) { log_write(LOG_LEVEL_ERROR, "Couldn't initialize forwarding. Exiting.\n"); @@ -377,7 +377,7 @@ int main(int argc, char *argv[]) return 1; } - GC_Announces_List *group_announce = new_gca_list(); + GC_Announces_List *group_announce = new_gca_list(mem); if (group_announce == nullptr) { log_write(LOG_LEVEL_ERROR, "Couldn't initialize group announces. Exiting.\n"); @@ -563,7 +563,7 @@ int main(int argc, char *argv[]) Broadcast_Info *broadcast = nullptr; if (enable_lan_discovery) { - broadcast = lan_discovery_init(ns); + broadcast = lan_discovery_init(mem, ns); log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n"); } diff --git a/other/event_tooling/generate_event_c.cpp b/other/event_tooling/generate_event_c.cpp index 9a7fb92060..51aeca15fa 100644 --- a/other/event_tooling/generate_event_c.cpp +++ b/other/event_tooling/generate_event_c.cpp @@ -178,6 +178,7 @@ void generate_event_impl(const std::string& event_name, const std::vector - #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) // The mingw32/64 Windows library warns about including winsock2.h after // windows.h even though with the above it's a valid thing to do. So, to make @@ -41,11 +39,14 @@ #include "attributes.h" #include "ccompat.h" #include "crypto_core.h" +#include "mem.h" #include "network.h" #define MAX_INTERFACES 16 struct Broadcast_Info { + const Memory *mem; + uint32_t count; IP ips[MAX_INTERFACES]; }; @@ -53,29 +54,31 @@ struct Broadcast_Info { #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; } - IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); + broadcast->mem = mem; + + IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, sizeof(IP_ADAPTER_INFO)); if (adapter_info == nullptr) { - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO); if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) { - free(adapter_info); - IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len); + mem_delete(mem, adapter_info); + IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, out_buf_len); if (new_adapter_info == nullptr) { - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } @@ -113,7 +116,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) } if (adapter_info != nullptr) { - free(adapter_info); + mem_delete(mem, adapter_info); } return broadcast; @@ -122,14 +125,16 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) #elif !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && (defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)) non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; } + broadcast->mem = mem; + /* Not sure how many platforms this will run on, * so it's wrapped in `__linux__` for now. * Definitely won't work like this on Windows... @@ -137,7 +142,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, 0); if (!sock_valid(sock)) { - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } @@ -150,7 +155,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) if (ioctl(net_socket_to_native(sock), SIOCGIFCONF, &ifc) < 0) { kill_sock(ns, sock); - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } @@ -197,9 +202,17 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) #else // TODO(irungentoo): Other platforms? non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) { - return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); + + if (broadcast == nullptr) { + return nullptr; + } + + broadcast->mem = mem; + + return broadcast; } #endif /* platforms */ @@ -375,12 +388,16 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc return res; } -Broadcast_Info *lan_discovery_init(const Network *ns) +Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns) { - return fetch_broadcast_info(ns); + return fetch_broadcast_info(mem, ns); } void lan_discovery_kill(Broadcast_Info *broadcast) { - free(broadcast); + if (broadcast == nullptr) { + return; + } + + mem_delete(broadcast->mem, broadcast); } diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 414f18b1c7..c18ce9e562 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -10,6 +10,7 @@ #define C_TOXCORE_TOXCORE_LAN_DISCOVERY_H #include "attributes.h" +#include "mem.h" #include "network.h" /** @@ -32,7 +33,7 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc * Discovers broadcast devices and IP addresses. */ non_null() -Broadcast_Info *lan_discovery_init(const Network *ns); +Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns); /** * Free all resources associated with the broadcast info. diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 0943f4c9dd..788147b5f0 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2515,7 +2515,7 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend return false; } - if (gca_add_announce(m->mono_time, m->group_announce, &announce) == nullptr) { + if (gca_add_announce(m->mem, m->mono_time, m->group_announce, &announce) == nullptr) { onion_friend_set_gc_data(onion_friend, nullptr, 0); return false; } @@ -3475,7 +3475,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * m->rng = rng; m->ns = ns; - m->fr = friendreq_new(); + m->fr = friendreq_new(mem); if (m->fr == nullptr) { mem_delete(mem, m); @@ -3544,7 +3544,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } - m->group_announce = new_gca_list(); + m->group_announce = new_gca_list(m->mem); if (m->group_announce == nullptr) { LOGGER_WARNING(m->log, "DHT group chats initialisation failed"); @@ -3559,7 +3559,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * } if (options->dht_announcements_enabled) { - m->forwarding = new_forwarding(m->log, m->rng, m->mono_time, m->dht); + m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht); if (m->forwarding != nullptr) { m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding); } else { @@ -3574,7 +3574,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht); m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto); if (m->onion_c != nullptr) { - m->fr_c = new_friend_connections(m->log, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled); + m->fr_c = new_friend_connections(m->log, m->mem, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled); } if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) || diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 9ef298899f..186431b1df 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -1040,7 +1040,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random memcpy(temp->secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); crypto_derive_public_key(temp->public_key, temp->secret_key); - bs_list_init(&temp->accepted_key_list, CRYPTO_PUBLIC_KEY_SIZE, 8, memcmp); + bs_list_init(&temp->accepted_key_list, mem, CRYPTO_PUBLIC_KEY_SIZE, 8, memcmp); return temp; } diff --git a/toxcore/announce.c b/toxcore/announce.c index 060e8be422..dc34ca05e3 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -9,7 +9,6 @@ #include "announce.h" #include -#include #include #include "DHT.h" @@ -239,9 +238,9 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key if (length > 0) { assert(data != nullptr); - free(entry->data); + mem_delete(announce->mem, entry->data); - uint8_t *entry_data = (uint8_t *)malloc(length); + uint8_t *entry_data = (uint8_t *)mem_balloc(announce->mem, length); if (entry_data == nullptr) { entry->data = nullptr; // TODO(iphydf): Is this necessary? @@ -651,7 +650,7 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran return nullptr; } - Announcements *announce = (Announcements *)calloc(1, sizeof(Announcements)); + Announcements *announce = (Announcements *)mem_alloc(mem, sizeof(Announcements)); if (announce == nullptr) { return nullptr; @@ -669,7 +668,7 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran new_hmac_key(announce->rng, announce->hmac_key); announce->shared_keys = shared_key_cache_new(log, mono_time, mem, announce->secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); if (announce->shared_keys == nullptr) { - free(announce); + mem_delete(announce->mem, announce); return nullptr; } @@ -700,8 +699,8 @@ void kill_announcements(Announcements *announce) shared_key_cache_free(announce->shared_keys); for (uint32_t i = 0; i < ANNOUNCE_BUCKETS * ANNOUNCE_BUCKET_SIZE; ++i) { - free(announce->entries[i].data); + mem_delete(announce->mem, announce->entries[i].data); } - free(announce); + mem_delete(announce->mem, announce); } diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 44d16555fd..8a6aa38fba 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -6,7 +6,6 @@ #include "crypto_core.h" #include -#include #include #include diff --git a/toxcore/crypto_core_test.cc b/toxcore/crypto_core_test.cc index 6203afbce5..b18f0c1871 100644 --- a/toxcore/crypto_core_test.cc +++ b/toxcore/crypto_core_test.cc @@ -8,7 +8,6 @@ #include "crypto_core_test_util.hh" #include "mem_test_util.hh" -#include "util.h" namespace { diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index 622a530397..d19f0191a2 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index de417c900b..04206e768f 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index fea2151fb8..7cef201a35 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index e800b2bce7..68744208d6 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index adc31521c5..36de6ba9c2 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 399370143d..cd92ba1651 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/dht_get_nodes_response.c b/toxcore/events/dht_get_nodes_response.c index 712b1ab6fc..918b97d1b7 100644 --- a/toxcore/events/dht_get_nodes_response.c +++ b/toxcore/events/dht_get_nodes_response.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_private.h" diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index 86ae6f09a6..7477559993 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -6,11 +6,8 @@ #define C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H #include "../attributes.h" -#include "../bin_pack.h" -#include "../bin_unpack.h" #include "../mem.h" #include "../tox.h" -#include "../tox_event.h" #include "../tox_events.h" #include "../tox_private.h" diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 5779345630..d59b7a5ba7 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index cd6d477a60..6ba2fdecfd 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 0153aa010a..cb4487e23c 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index 6a4c451fb3..72de182e72 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 8503f03023..d57980ddff 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index 441b779034..098c5ab006 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index 317071bb2a..016fa5d25c 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 5115521c2e..cedc3b588c 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index e1075d3561..9f8de0ce31 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 51db0ba257..4ba426716e 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index d208e41b48..631c236bac 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_private.h" diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index c5a66bd3bd..209eed8bc5 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index 2c0bff8543..2c6d0fb02f 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index 9f710acd4a..cb2cfac94d 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_custom_packet.c b/toxcore/events/group_custom_packet.c index 22716055b9..337d601f04 100644 --- a/toxcore/events/group_custom_packet.c +++ b/toxcore/events/group_custom_packet.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_custom_private_packet.c b/toxcore/events/group_custom_private_packet.c index 7fb2a3581f..9408a8dc68 100644 --- a/toxcore/events/group_custom_private_packet.c +++ b/toxcore/events/group_custom_private_packet.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_invite.c b/toxcore/events/group_invite.c index 4faa7f8d8c..3385b89ad5 100644 --- a/toxcore/events/group_invite.c +++ b/toxcore/events/group_invite.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_join_fail.c b/toxcore/events/group_join_fail.c index 85879077a1..8fdefb77e4 100644 --- a/toxcore/events/group_join_fail.c +++ b/toxcore/events/group_join_fail.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_message.c b/toxcore/events/group_message.c index 9957e4bea9..dfb2bc961a 100644 --- a/toxcore/events/group_message.c +++ b/toxcore/events/group_message.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_moderation.c b/toxcore/events/group_moderation.c index ca67cfa44c..9e2b87e6c2 100644 --- a/toxcore/events/group_moderation.c +++ b/toxcore/events/group_moderation.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_password.c b/toxcore/events/group_password.c index e61017846f..33b64a0a8b 100644 --- a/toxcore/events/group_password.c +++ b/toxcore/events/group_password.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_peer_exit.c b/toxcore/events/group_peer_exit.c index babb5d1514..a09f96b7d0 100644 --- a/toxcore/events/group_peer_exit.c +++ b/toxcore/events/group_peer_exit.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_peer_join.c b/toxcore/events/group_peer_join.c index 838ee2b5e0..d450564429 100644 --- a/toxcore/events/group_peer_join.c +++ b/toxcore/events/group_peer_join.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_peer_limit.c b/toxcore/events/group_peer_limit.c index 62991dd54c..65c44e7a19 100644 --- a/toxcore/events/group_peer_limit.c +++ b/toxcore/events/group_peer_limit.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_peer_name.c b/toxcore/events/group_peer_name.c index 4a7d499d90..d653ff5531 100644 --- a/toxcore/events/group_peer_name.c +++ b/toxcore/events/group_peer_name.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_peer_status.c b/toxcore/events/group_peer_status.c index ff827bfb18..3dc56df5ff 100644 --- a/toxcore/events/group_peer_status.c +++ b/toxcore/events/group_peer_status.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_privacy_state.c b/toxcore/events/group_privacy_state.c index 8efbff6636..fde9e1d79c 100644 --- a/toxcore/events/group_privacy_state.c +++ b/toxcore/events/group_privacy_state.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_private_message.c b/toxcore/events/group_private_message.c index 533016bdfe..24dd1db670 100644 --- a/toxcore/events/group_private_message.c +++ b/toxcore/events/group_private_message.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_self_join.c b/toxcore/events/group_self_join.c index 162ed683ee..07038632aa 100644 --- a/toxcore/events/group_self_join.c +++ b/toxcore/events/group_self_join.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_topic.c b/toxcore/events/group_topic.c index ed6e8738c6..f9d1a7b953 100644 --- a/toxcore/events/group_topic.c +++ b/toxcore/events/group_topic.c @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/toxcore/events/group_topic_lock.c b/toxcore/events/group_topic_lock.c index c0e1154a0c..a21acea293 100644 --- a/toxcore/events/group_topic_lock.c +++ b/toxcore/events/group_topic_lock.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_voice_state.c b/toxcore/events/group_voice_state.c index ce1dbeb885..ebc78dca21 100644 --- a/toxcore/events/group_voice_state.c +++ b/toxcore/events/group_voice_state.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index d6129391aa..f66daff544 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/toxcore/forwarding.c b/toxcore/forwarding.c index c0480cb815..6d608b5e7b 100644 --- a/toxcore/forwarding.c +++ b/toxcore/forwarding.c @@ -5,7 +5,6 @@ #include "forwarding.h" #include -#include #include #include "DHT.h" @@ -13,12 +12,14 @@ #include "ccompat.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" #include "timed_auth.h" struct Forwarding { const Logger *log; + const Memory *mem; const Random *rng; DHT *dht; const Mono_Time *mono_time; @@ -357,19 +358,20 @@ void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *functi forwarding->forward_reply_callback_object = object; } -Forwarding *new_forwarding(const Logger *log, const Random *rng, const Mono_Time *mono_time, DHT *dht) +Forwarding *new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) { if (log == nullptr || mono_time == nullptr || dht == nullptr) { return nullptr; } - Forwarding *forwarding = (Forwarding *)calloc(1, sizeof(Forwarding)); + Forwarding *forwarding = (Forwarding *)mem_alloc(mem, sizeof(Forwarding)); if (forwarding == nullptr) { return nullptr; } forwarding->log = log; + forwarding->mem = mem; forwarding->rng = rng; forwarding->mono_time = mono_time; forwarding->dht = dht; @@ -396,5 +398,5 @@ void kill_forwarding(Forwarding *forwarding) crypto_memzero(forwarding->hmac_key, CRYPTO_HMAC_KEY_SIZE); - free(forwarding); + mem_delete(forwarding->mem, forwarding); } diff --git a/toxcore/forwarding.h b/toxcore/forwarding.h index a2dfa31aa4..b4431f57af 100644 --- a/toxcore/forwarding.h +++ b/toxcore/forwarding.h @@ -9,6 +9,7 @@ #include "attributes.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" @@ -116,7 +117,7 @@ non_null(1) nullable(2, 3) void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *function, void *object); non_null() -Forwarding *new_forwarding(const Logger *log, const Random *rng, const Mono_Time *mono_time, DHT *dht); +Forwarding *new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht); nullable(1) void kill_forwarding(Forwarding *forwarding); diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 4a0ddd8ad4..78c6d4acdc 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -8,7 +8,6 @@ */ #include "friend_connection.h" -#include #include #include "DHT.h" @@ -18,6 +17,7 @@ #include "ccompat.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "net_crypto.h" #include "network.h" @@ -69,6 +69,7 @@ static const Friend_Conn empty_friend_conn = {0}; struct Friend_Connections { const Mono_Time *mono_time; + const Memory *mem; const Logger *logger; Net_Crypto *net_crypto; DHT *dht; @@ -126,12 +127,12 @@ non_null() static bool realloc_friendconns(Friend_Connections *fr_c, uint32_t num) { if (num == 0) { - free(fr_c->conns); + mem_delete(fr_c->mem, fr_c->conns); fr_c->conns = nullptr; return true; } - Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn)); + Friend_Conn *newgroup_cons = (Friend_Conn *)mem_vrealloc(fr_c->mem, fr_c->conns, num, sizeof(Friend_Conn)); if (newgroup_cons == nullptr) { return false; @@ -912,14 +913,14 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 /** Create new friend_connections instance. */ Friend_Connections *new_friend_connections( - const Logger *logger, const Mono_Time *mono_time, const Network *ns, + const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Network *ns, Onion_Client *onion_c, bool local_discovery_enabled) { if (onion_c == nullptr) { return nullptr; } - Friend_Connections *const temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); + Friend_Connections *const temp = (Friend_Connections *)mem_alloc(mem, sizeof(Friend_Connections)); if (temp == nullptr) { return nullptr; @@ -928,7 +929,7 @@ Friend_Connections *new_friend_connections( temp->local_discovery_enabled = local_discovery_enabled; if (temp->local_discovery_enabled) { - temp->broadcast = lan_discovery_init(ns); + temp->broadcast = lan_discovery_init(mem, ns); if (temp->broadcast == nullptr) { LOGGER_ERROR(logger, "could not initialise LAN discovery"); @@ -937,6 +938,7 @@ Friend_Connections *new_friend_connections( } temp->mono_time = mono_time; + temp->mem = mem; temp->logger = logger; temp->dht = onion_get_dht(onion_c); temp->net_crypto = onion_get_net_crypto(onion_c); @@ -1038,9 +1040,9 @@ void kill_friend_connections(Friend_Connections *fr_c) // there might be allocated NONE connections if (fr_c->conns != nullptr) { - free(fr_c->conns); + mem_delete(fr_c->mem, fr_c->conns); } lan_discovery_kill(fr_c->broadcast); - free(fr_c); + mem_delete(fr_c->mem, fr_c); } diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 81aef8a863..8c3c84f1c5 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -12,9 +12,9 @@ #include #include "DHT.h" -#include "LAN_discovery.h" #include "attributes.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "net_crypto.h" #include "network.h" @@ -161,7 +161,7 @@ void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_req /** Create new friend_connections instance. */ non_null() Friend_Connections *new_friend_connections( - const Logger *logger, const Mono_Time *mono_time, const Network *ns, + const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Network *ns, Onion_Client *onion_c, bool local_discovery_enabled); /** main friend_connections loop. */ diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 240a75e6e4..e28ce7df85 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -8,18 +8,22 @@ */ #include "friend_requests.h" -#include #include #include "attributes.h" #include "ccompat.h" #include "crypto_core.h" #include "friend_connection.h" +#include "mem.h" #include "network.h" #include "onion.h" #include "onion_announce.h" #include "onion_client.h" +static_assert(ONION_CLIENT_MAX_DATA_SIZE <= MAX_DATA_REQUEST_SIZE, "ONION_CLIENT_MAX_DATA_SIZE is too big"); +static_assert(MAX_DATA_REQUEST_SIZE <= ONION_MAX_DATA_SIZE, "MAX_DATA_REQUEST_SIZE is too big"); +static_assert(SIZE_IPPORT <= ONION_SEND_BASE, "IP_Port does not fit in the onion packet"); + /** * NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. * TODO(irungentoo): Make this better (This will most likely tie in with the way we will handle spam). @@ -32,6 +36,8 @@ struct Received_Requests { }; struct Friend_Requests { + const Memory *mem; + uint32_t nospam; fr_friend_request_cb *handle_friendrequest; uint8_t handle_friendrequest_isset; @@ -164,12 +170,24 @@ void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c) set_friend_request_callback(fr_c, &friendreq_handlepacket, fr); } -Friend_Requests *friendreq_new(void) +Friend_Requests *friendreq_new(const Memory *mem) { - return (Friend_Requests *)calloc(1, sizeof(Friend_Requests)); + Friend_Requests *fr = (Friend_Requests *)mem_alloc(mem, sizeof(Friend_Requests)); + + if (fr == nullptr) { + return nullptr; + } + + fr->mem = mem; + + return fr; } void friendreq_kill(Friend_Requests *fr) { - free(fr); + if (fr == nullptr) { + return; + } + + mem_delete(fr->mem, fr); } diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index 2c3bb310cc..c7bae81fb8 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h @@ -13,6 +13,7 @@ #include "attributes.h" #include "friend_connection.h" +#include "mem.h" // TODO(Jfreegman): This should be the maximum size that an onion client can handle. #define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - 100) @@ -50,7 +51,8 @@ void set_filter_function(Friend_Requests *fr, filter_function_cb *function, void non_null() void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c); -Friend_Requests *friendreq_new(void); +non_null() +Friend_Requests *friendreq_new(const Memory *mem); nullable(1) void friendreq_kill(Friend_Requests *fr); diff --git a/toxcore/group_announce.c b/toxcore/group_announce.c index ee083198c1..caa70193aa 100644 --- a/toxcore/group_announce.c +++ b/toxcore/group_announce.c @@ -5,7 +5,6 @@ #include "group_announce.h" -#include #include #include "DHT.h" @@ -13,6 +12,7 @@ #include "ccompat.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" @@ -36,7 +36,7 @@ static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces announces->next_announce->prev_announce = announces->prev_announce; } - free(announces); + mem_delete(gc_announces_list->mem, announces); } /** @@ -343,10 +343,11 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l non_null() static GC_Announces *gca_new_announces( + const Memory *mem, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { - GC_Announces *announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); + GC_Announces *announces = (GC_Announces *)mem_alloc(mem, sizeof(GC_Announces)); if (announces == nullptr) { return nullptr; @@ -366,7 +367,7 @@ static GC_Announces *gca_new_announces( return announces; } -GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, +GC_Peer_Announce *gca_add_announce(const Memory *mem, const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { if (gc_announces_list == nullptr || public_announce == nullptr) { @@ -377,7 +378,7 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List // No entry for this chat_id exists so we create one if (announces == nullptr) { - announces = gca_new_announces(gc_announces_list, public_announce); + announces = gca_new_announces(mem, gc_announces_list, public_announce); if (announces == nullptr) { return nullptr; @@ -410,9 +411,17 @@ bool gca_is_valid_announce(const GC_Announce *announce) return announce->tcp_relays_count > 0 || announce->ip_port_is_set; } -GC_Announces_List *new_gca_list(void) +GC_Announces_List *new_gca_list(const Memory *mem) { - return (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); + GC_Announces_List *announces_list = (GC_Announces_List *)mem_alloc(mem, sizeof(GC_Announces_List)); + + if (announces_list == nullptr) { + return nullptr; + } + + announces_list->mem = mem; + + return announces_list; } void kill_gca(GC_Announces_List *announces_list) @@ -425,11 +434,11 @@ void kill_gca(GC_Announces_List *announces_list) while (root != nullptr) { GC_Announces *next = root->next_announce; - free(root); + mem_delete(announces_list->mem, root); root = next; } - free(announces_list); + mem_delete(announces_list->mem, announces_list); } /* How long we save a peer's announce before we consider it stale and remove it. */ diff --git a/toxcore/group_announce.h b/toxcore/group_announce.h index 72f2cfc1b8..2e043a069a 100644 --- a/toxcore/group_announce.h +++ b/toxcore/group_announce.h @@ -16,6 +16,7 @@ #include "attributes.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" @@ -79,6 +80,8 @@ struct GC_Announces { /* A list of all announces. */ struct GC_Announces_List { + const Memory *mem; + GC_Announces *root_announces; uint64_t last_timeout_check; }; @@ -87,7 +90,8 @@ struct GC_Announces_List { * * The caller is responsible for freeing the memory with `kill_gca`. */ -GC_Announces_List *new_gca_list(void); +non_null() +GC_Announces_List *new_gca_list(const Memory *mem); /** @brief Frees all dynamically allocated memory associated with `announces_list`. */ nullable(1) @@ -135,7 +139,7 @@ int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *g * @retval null on failure. */ non_null() -GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, +GC_Peer_Announce *gca_add_announce(const Memory *mem, const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce); /** @brief Packs an announce into a data buffer. diff --git a/toxcore/group_announce_fuzz_test.cc b/toxcore/group_announce_fuzz_test.cc index 9f5655ac15..154e486d76 100644 --- a/toxcore/group_announce_fuzz_test.cc +++ b/toxcore/group_announce_fuzz_test.cc @@ -60,7 +60,8 @@ void TestDoGca(Fuzz_Data &input) mem, [](void *user_data) { return *static_cast(user_data); }, &clock), [mem](Mono_Time *ptr) { mono_time_free(mem, ptr); }); assert(mono_time != nullptr); - std::unique_ptr gca(new_gca_list(), kill_gca); + std::unique_ptr gca( + new_gca_list(mem), kill_gca); assert(gca != nullptr); while (!input.empty()) { @@ -72,7 +73,7 @@ void TestDoGca(Fuzz_Data &input) CONSUME_OR_RETURN(const uint8_t *data, input, length); GC_Public_Announce public_announce; if (gca_unpack_public_announce(logger.get(), data, length, &public_announce) != -1) { - gca_add_announce(mono_time.get(), gca.get(), &public_announce); + gca_add_announce(mem, mono_time.get(), gca.get(), &public_announce); } break; } diff --git a/toxcore/group_announce_test.cc b/toxcore/group_announce_test.cc index f1027f3437..1b37fd3344 100644 --- a/toxcore/group_announce_test.cc +++ b/toxcore/group_announce_test.cc @@ -27,7 +27,7 @@ struct Announces : ::testing::Test { mono_time_set_current_time_callback( mono_time_, [](void *user_data) { return *static_cast(user_data); }, &clock_); - gca_ = new_gca_list(); + gca_ = new_gca_list(mem_); ASSERT_NE(gca_, nullptr); } @@ -54,10 +54,10 @@ TEST_F(Announces, CanBeCreatedAndDeleted) { GC_Public_Announce ann{}; ann.chat_public_key[0] = 0x88; - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann), nullptr); #ifndef _DEBUG - ASSERT_EQ(gca_add_announce(mono_time_, gca_, nullptr), nullptr); - ASSERT_EQ(gca_add_announce(mono_time_, nullptr, &ann), nullptr); + ASSERT_EQ(gca_add_announce(mem_, mono_time_, gca_, nullptr), nullptr); + ASSERT_EQ(gca_add_announce(mem_, mono_time_, nullptr, &ann), nullptr); #endif } @@ -67,7 +67,7 @@ TEST_F(Announces, AnnouncesCanTimeOut) ASSERT_EQ(gca_->root_announces, nullptr); GC_Public_Announce ann{}; ann.chat_public_key[0] = 0xae; - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann), nullptr); ASSERT_NE(gca_->root_announces, nullptr); ASSERT_EQ(gca_->root_announces->chat_id[0], 0xae); @@ -95,9 +95,9 @@ TEST_F(Announces, AnnouncesGetAndCleanup) ann2.chat_public_key[0] = 0x92; ann2.base_announce.peer_public_key[0] = 0x7c; - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann1), nullptr); - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann1), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann2), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann2), nullptr); uint8_t empty_pk[ENC_PUBLIC_KEY_SIZE] = {0}; diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index a9cab40449..6b0271644f 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -4116,7 +4116,7 @@ int gc_founder_set_password(GC_Chat *chat, const uint8_t *password, uint16_t pas } const uint16_t oldlen = chat->shared_state.password_length; - uint8_t *oldpasswd = memdup(chat->shared_state.password, oldlen); + uint8_t *oldpasswd = memdup(chat->mem, chat->shared_state.password, oldlen); if (oldpasswd == nullptr && oldlen > 0) { return -4; diff --git a/toxcore/group_onion_announce.c b/toxcore/group_onion_announce.c index e079d79a66..5a19935c50 100644 --- a/toxcore/group_onion_announce.c +++ b/toxcore/group_onion_announce.c @@ -29,7 +29,7 @@ static_assert(GCA_ANNOUNCE_MAX_SIZE <= ONION_MAX_EXTRA_DATA_SIZE, static pack_extra_data_cb pack_group_announces; non_null() -static int pack_group_announces(void *object, const Logger *logger, const Mono_Time *mono_time, +static int pack_group_announces(void *object, const Logger *logger, const Memory *mem, const Mono_Time *mono_time, uint8_t num_nodes, uint8_t *plain, uint16_t plain_size, uint8_t *response, uint16_t response_size, uint16_t offset) { @@ -42,7 +42,7 @@ static int pack_group_announces(void *object, const Logger *logger, const Mono_T return -1; } - const GC_Peer_Announce *new_announce = gca_add_announce(mono_time, gc_announces_list, &public_announce); + const GC_Peer_Announce *new_announce = gca_add_announce(mem, mono_time, gc_announces_list, &public_announce); if (new_announce == nullptr) { LOGGER_ERROR(logger, "Failed to add group announce"); diff --git a/toxcore/list.c b/toxcore/list.c index 33242a2371..fd83825c2e 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -12,11 +12,11 @@ #include #include -#include #include #include "attributes.h" #include "ccompat.h" +#include "mem.h" /** * Basically, the elements in the list are placed in order so that they can be searched for easily @@ -115,7 +115,7 @@ static bool resize(BS_List *list, uint32_t new_size) return true; } - uint8_t *data = (uint8_t *)realloc(list->data, list->element_size * new_size); + uint8_t *data = (uint8_t *)mem_vrealloc(list->mem, list->data, list->element_size, new_size); if (data == nullptr) { return false; @@ -123,7 +123,7 @@ static bool resize(BS_List *list, uint32_t new_size) list->data = data; - int *ids = (int *)realloc(list->ids, new_size * sizeof(int)); + int *ids = (int *)mem_vrealloc(list->mem, list->ids, new_size, sizeof(int)); if (ids == nullptr) { return false; @@ -134,8 +134,10 @@ static bool resize(BS_List *list, uint32_t new_size) return true; } -int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback) +int bs_list_init(BS_List *list, const Memory *mem, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback) { + list->mem = mem; + // set initial values list->n = 0; list->element_size = element_size; @@ -162,10 +164,10 @@ void bs_list_free(BS_List *list) } // free both arrays - free(list->data); + mem_delete(list->mem, list->data); list->data = nullptr; - free(list->ids); + mem_delete(list->mem, list->ids); list->ids = nullptr; } diff --git a/toxcore/list.h b/toxcore/list.h index 7c607f75c4..ae0ea32746 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -16,6 +16,7 @@ #include #include "attributes.h" +#include "mem.h" #ifdef __cplusplus extern "C" { @@ -24,6 +25,8 @@ extern "C" { typedef int bs_list_cmp_cb(const void *a, const void *b, size_t size); typedef struct BS_List { + const Memory *mem; + uint32_t n; // number of elements uint32_t capacity; // number of elements memory is allocated for uint32_t element_size; // size of the elements @@ -41,7 +44,7 @@ typedef struct BS_List { * @retval 0 failure */ non_null() -int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback); +int bs_list_init(BS_List *list, const Memory *mem, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback); /** Free a list initiated with list_init */ nullable(1) diff --git a/toxcore/list_test.cc b/toxcore/list_test.cc index 0d8f646c10..794c5e2337 100644 --- a/toxcore/list_test.cc +++ b/toxcore/list_test.cc @@ -2,26 +2,31 @@ #include +#include "mem.h" + namespace { TEST(List, CreateAndDestroyWithNonZeroSize) { + const Memory *mem = os_memory(); BS_List list; - bs_list_init(&list, sizeof(int), 10, memcmp); + bs_list_init(&list, mem, sizeof(int), 10, memcmp); bs_list_free(&list); } TEST(List, CreateAndDestroyWithZeroSize) { + const Memory *mem = os_memory(); BS_List list; - bs_list_init(&list, sizeof(int), 0, memcmp); + bs_list_init(&list, mem, sizeof(int), 0, memcmp); bs_list_free(&list); } TEST(List, DeleteFromEmptyList) { + const Memory *mem = os_memory(); BS_List list; - bs_list_init(&list, sizeof(int), 0, memcmp); + bs_list_init(&list, mem, sizeof(int), 0, memcmp); const uint8_t data[sizeof(int)] = {0}; bs_list_remove(&list, data, 0); bs_list_free(&list); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 5c12e58b93..0ed1ed3f2c 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -3026,7 +3026,7 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); - bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8, ipport_cmp_handler); + bs_list_init(&temp->ip_port_list, mem, sizeof(IP_Port), 8, ipport_cmp_handler); return temp; } diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 44797a3707..f30e6c7207 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -574,7 +574,7 @@ static int handle_announce_request_common( const int extra_size = pack_extra_data_callback == nullptr ? 0 : pack_extra_data_callback(onion_a->extra_data_object, - onion_a->log, onion_a->mono_time, num_nodes, + onion_a->log, onion_a->mem, onion_a->mono_time, num_nodes, plain + ONION_MINIMAL_SIZE, length - ANNOUNCE_REQUEST_MIN_SIZE_RECV, response, response_size, offset); diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h index 7e7d731700..d8834395c0 100644 --- a/toxcore/onion_announce.h +++ b/toxcore/onion_announce.h @@ -129,7 +129,7 @@ int send_data_request( const uint8_t *public_key, const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length); -typedef int pack_extra_data_cb(void *object, const Logger *logger, const Mono_Time *mono_time, +typedef int pack_extra_data_cb(void *object, const Logger *logger, const Memory *mem, const Mono_Time *mono_time, uint8_t num_nodes, uint8_t *plain, uint16_t plain_size, uint8_t *response, uint16_t response_size, uint16_t offset); diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index e3cb9afce9..dde768e89b 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -4,12 +4,12 @@ #include "tox_dispatch.h" +#include #include #include "attributes.h" #include "ccompat.h" #include "events/events_alloc.h" // IWYU pragma: keep -#include "tox.h" #include "tox_event.h" #include "tox_events.h" diff --git a/toxcore/util.c b/toxcore/util.c index d5c240a750..455e513cef 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -13,10 +13,8 @@ #include "util.h" -#include #include -#include "attributes.h" #include "ccompat.h" #include "mem.h" @@ -82,13 +80,13 @@ bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t b_size) return a_size == b_size && memcmp(a, b, a_size) == 0; } -uint8_t *memdup(const uint8_t *data, size_t data_size) +uint8_t *memdup(const Memory *mem, const uint8_t *data, size_t data_size) { if (data == nullptr || data_size == 0) { return nullptr; } - uint8_t *copy = (uint8_t *)malloc(data_size); + uint8_t *copy = (uint8_t *)mem_balloc(mem, data_size); if (copy != nullptr) { memcpy(copy, data, data_size); diff --git a/toxcore/util.h b/toxcore/util.h index b3382ae624..9be721318b 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -46,7 +46,7 @@ non_null() bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t * * @return nullptr on allocation failure or if the input data was nullptr or data_size was 0. */ -nullable(1) uint8_t *memdup(const uint8_t *data, size_t data_size); +non_null(1) nullable(2) uint8_t *memdup(const Memory *mem, const uint8_t *data, size_t data_size); /** * @brief Set all bytes in `data` to 0. @@ -79,7 +79,12 @@ uint16_t min_u16(uint16_t a, uint16_t b); uint32_t min_u32(uint32_t a, uint32_t b); uint64_t min_u64(uint64_t a, uint64_t b); -// Comparison function: return -1 if ab. +/** + * Comparison function: + * @retval -1 if `a < b` + * @retval 0 if `a == b` + * @retval 1 if `a > b` + */ int cmp_uint(uint64_t a, uint64_t b); /** @brief Returns a 32-bit hash of key of size len */