Skip to content

Commit

Permalink
cleanup: Use tox memory allocator in some more places.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 14, 2025
1 parent 3cfe41c commit e092ecd
Show file tree
Hide file tree
Showing 75 changed files with 222 additions and 111 deletions.
2 changes: 1 addition & 1 deletion auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_event.h"
#include "../tox_events.h")";
if (need_tox_unpack_h) {
f << R"(
Expand Down Expand Up @@ -843,4 +844,3 @@ int main(int argc, char** argv) {

return 0;
}

7 changes: 7 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ cc_library(
deps = [
":attributes",
":ccompat",
":mem",
],
)

Expand All @@ -279,6 +280,7 @@ cc_test(
srcs = ["list_test.cc"],
deps = [
":list",
":mem",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down Expand Up @@ -452,6 +454,7 @@ cc_library(
":attributes",
":ccompat",
":crypto_core",
":mem",
":network",
":util",
"@psocket",
Expand Down Expand Up @@ -577,6 +580,7 @@ cc_library(
":ccompat",
":crypto_core",
":logger",
":mem",
":mono_time",
":network",
":timed_auth",
Expand Down Expand Up @@ -787,6 +791,7 @@ cc_library(
":ccompat",
":crypto_core",
":logger",
":mem",
":mono_time",
":network",
":util",
Expand Down Expand Up @@ -888,6 +893,7 @@ cc_library(
":ccompat",
":crypto_core",
":logger",
":mem",
":mono_time",
":net_crypto",
":network",
Expand All @@ -912,6 +918,7 @@ cc_library(
":ccompat",
":crypto_core",
":friend_connection",
":mem",
":network",
":onion",
":onion_announce",
Expand Down
55 changes: 36 additions & 19 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
#include "LAN_discovery.h"

#include <stdlib.h>

#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
Expand Down Expand Up @@ -41,41 +39,46 @@
#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];
};

#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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -122,22 +125,24 @@ 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...
*/
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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define C_TOXCORE_TOXCORE_LAN_DISCOVERY_H

#include "attributes.h"
#include "mem.h"
#include "network.h"

/**
Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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 {
Expand All @@ -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)) ||
Expand Down
2 changes: 1 addition & 1 deletion toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit e092ecd

Please sign in to comment.