Skip to content

Commit e092ecd

Browse files
committed
cleanup: Use tox memory allocator in some more places.
1 parent 3cfe41c commit e092ecd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+222
-111
lines changed

auto_tests/announce_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void test_store_data(void)
6666
ck_assert(net != nullptr);
6767
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
6868
ck_assert(dht != nullptr);
69-
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);
69+
Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht);
7070
ck_assert(forwarding != nullptr);
7171
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding);
7272
ck_assert(announce != nullptr);

auto_tests/forwarding_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
129129
const TCP_Proxy_Info inf = {{{{0}}}};
130130
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf);
131131

132-
subtox->forwarding = new_forwarding(subtox->log, rng, subtox->mono_time, subtox->dht);
132+
subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht);
133133
ck_assert(subtox->forwarding != nullptr);
134134

135135
subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding);

other/DHT_bootstrap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ int main(int argc, char *argv[])
159159
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
160160
DHT *dht = new_dht(logger, mem, rng, ns, mono_time, new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr), true, true);
161161
Onion *onion = new_onion(logger, mem, mono_time, rng, dht);
162-
Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht);
163-
GC_Announces_List *gc_announces_list = new_gca_list();
162+
Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht);
163+
GC_Announces_List *gc_announces_list = new_gca_list(mem);
164164
Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht);
165165

166166
#ifdef DHT_NODE_EXTRA_PACKETS
@@ -245,7 +245,7 @@ int main(int argc, char *argv[])
245245
bool is_waiting_for_dht_connection = true;
246246

247247
uint64_t last_lan_discovery = 0;
248-
const Broadcast_Info *broadcast = lan_discovery_init(ns);
248+
const Broadcast_Info *broadcast = lan_discovery_init(mem, ns);
249249

250250
while (true) {
251251
mono_time_update(mono_time);

other/bootstrap_daemon/src/tox-bootstrapd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ int main(int argc, char *argv[])
348348
return 1;
349349
}
350350

351-
Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht);
351+
Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht);
352352

353353
if (forwarding == nullptr) {
354354
log_write(LOG_LEVEL_ERROR, "Couldn't initialize forwarding. Exiting.\n");
@@ -377,7 +377,7 @@ int main(int argc, char *argv[])
377377
return 1;
378378
}
379379

380-
GC_Announces_List *group_announce = new_gca_list();
380+
GC_Announces_List *group_announce = new_gca_list(mem);
381381

382382
if (group_announce == nullptr) {
383383
log_write(LOG_LEVEL_ERROR, "Couldn't initialize group announces. Exiting.\n");
@@ -563,7 +563,7 @@ int main(int argc, char *argv[])
563563
Broadcast_Info *broadcast = nullptr;
564564

565565
if (enable_lan_discovery) {
566-
broadcast = lan_discovery_init(ns);
566+
broadcast = lan_discovery_init(mem, ns);
567567
log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n");
568568
}
569569

other/event_tooling/generate_event_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
178178
#include "../ccompat.h"
179179
#include "../mem.h"
180180
#include "../tox.h"
181+
#include "../tox_event.h"
181182
#include "../tox_events.h")";
182183
if (need_tox_unpack_h) {
183184
f << R"(
@@ -843,4 +844,3 @@ int main(int argc, char** argv) {
843844

844845
return 0;
845846
}
846-

toxcore/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ cc_library(
270270
deps = [
271271
":attributes",
272272
":ccompat",
273+
":mem",
273274
],
274275
)
275276

@@ -279,6 +280,7 @@ cc_test(
279280
srcs = ["list_test.cc"],
280281
deps = [
281282
":list",
283+
":mem",
282284
"@com_google_googletest//:gtest",
283285
"@com_google_googletest//:gtest_main",
284286
],
@@ -452,6 +454,7 @@ cc_library(
452454
":attributes",
453455
":ccompat",
454456
":crypto_core",
457+
":mem",
455458
":network",
456459
":util",
457460
"@psocket",
@@ -577,6 +580,7 @@ cc_library(
577580
":ccompat",
578581
":crypto_core",
579582
":logger",
583+
":mem",
580584
":mono_time",
581585
":network",
582586
":timed_auth",
@@ -787,6 +791,7 @@ cc_library(
787791
":ccompat",
788792
":crypto_core",
789793
":logger",
794+
":mem",
790795
":mono_time",
791796
":network",
792797
":util",
@@ -888,6 +893,7 @@ cc_library(
888893
":ccompat",
889894
":crypto_core",
890895
":logger",
896+
":mem",
891897
":mono_time",
892898
":net_crypto",
893899
":network",
@@ -912,6 +918,7 @@ cc_library(
912918
":ccompat",
913919
":crypto_core",
914920
":friend_connection",
921+
":mem",
915922
":network",
916923
":onion",
917924
":onion_announce",

toxcore/LAN_discovery.c

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*/
99
#include "LAN_discovery.h"
1010

11-
#include <stdlib.h>
12-
1311
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
1412
// The mingw32/64 Windows library warns about including winsock2.h after
1513
// windows.h even though with the above it's a valid thing to do. So, to make
@@ -41,41 +39,46 @@
4139
#include "attributes.h"
4240
#include "ccompat.h"
4341
#include "crypto_core.h"
42+
#include "mem.h"
4443
#include "network.h"
4544

4645
#define MAX_INTERFACES 16
4746

4847
struct Broadcast_Info {
48+
const Memory *mem;
49+
4950
uint32_t count;
5051
IP ips[MAX_INTERFACES];
5152
};
5253

5354
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
5455

5556
non_null()
56-
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
57+
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
5758
{
58-
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
59+
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
5960

6061
if (broadcast == nullptr) {
6162
return nullptr;
6263
}
6364

64-
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
65+
broadcast->mem = mem;
66+
67+
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, sizeof(IP_ADAPTER_INFO));
6568

6669
if (adapter_info == nullptr) {
67-
free(broadcast);
70+
mem_delete(mem, broadcast);
6871
return nullptr;
6972
}
7073

7174
unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO);
7275

7376
if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
74-
free(adapter_info);
75-
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len);
77+
mem_delete(mem, adapter_info);
78+
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, out_buf_len);
7679

7780
if (new_adapter_info == nullptr) {
78-
free(broadcast);
81+
mem_delete(mem, broadcast);
7982
return nullptr;
8083
}
8184

@@ -113,7 +116,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
113116
}
114117

115118
if (adapter_info != nullptr) {
116-
free(adapter_info);
119+
mem_delete(mem, adapter_info);
117120
}
118121

119122
return broadcast;
@@ -122,22 +125,24 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
122125
#elif !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && (defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__))
123126

124127
non_null()
125-
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
128+
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
126129
{
127-
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
130+
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
128131

129132
if (broadcast == nullptr) {
130133
return nullptr;
131134
}
132135

136+
broadcast->mem = mem;
137+
133138
/* Not sure how many platforms this will run on,
134139
* so it's wrapped in `__linux__` for now.
135140
* Definitely won't work like this on Windows...
136141
*/
137142
const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, 0);
138143

139144
if (!sock_valid(sock)) {
140-
free(broadcast);
145+
mem_delete(mem, broadcast);
141146
return nullptr;
142147
}
143148

@@ -150,7 +155,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
150155

151156
if (ioctl(net_socket_to_native(sock), SIOCGIFCONF, &ifc) < 0) {
152157
kill_sock(ns, sock);
153-
free(broadcast);
158+
mem_delete(mem, broadcast);
154159
return nullptr;
155160
}
156161

@@ -197,9 +202,17 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
197202
#else // TODO(irungentoo): Other platforms?
198203

199204
non_null()
200-
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
205+
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
201206
{
202-
return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
207+
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
208+
209+
if (broadcast == nullptr) {
210+
return nullptr;
211+
}
212+
213+
broadcast->mem = mem;
214+
215+
return broadcast;
203216
}
204217

205218
#endif /* platforms */
@@ -375,12 +388,16 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
375388
return res;
376389
}
377390

378-
Broadcast_Info *lan_discovery_init(const Network *ns)
391+
Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns)
379392
{
380-
return fetch_broadcast_info(ns);
393+
return fetch_broadcast_info(mem, ns);
381394
}
382395

383396
void lan_discovery_kill(Broadcast_Info *broadcast)
384397
{
385-
free(broadcast);
398+
if (broadcast == nullptr) {
399+
return;
400+
}
401+
402+
mem_delete(broadcast->mem, broadcast);
386403
}

toxcore/LAN_discovery.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define C_TOXCORE_TOXCORE_LAN_DISCOVERY_H
1111

1212
#include "attributes.h"
13+
#include "mem.h"
1314
#include "network.h"
1415

1516
/**
@@ -32,7 +33,7 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
3233
* Discovers broadcast devices and IP addresses.
3334
*/
3435
non_null()
35-
Broadcast_Info *lan_discovery_init(const Network *ns);
36+
Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns);
3637

3738
/**
3839
* Free all resources associated with the broadcast info.

toxcore/Messenger.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,7 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
25152515
return false;
25162516
}
25172517

2518-
if (gca_add_announce(m->mono_time, m->group_announce, &announce) == nullptr) {
2518+
if (gca_add_announce(m->mem, m->mono_time, m->group_announce, &announce) == nullptr) {
25192519
onion_friend_set_gc_data(onion_friend, nullptr, 0);
25202520
return false;
25212521
}
@@ -3475,7 +3475,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
34753475
m->rng = rng;
34763476
m->ns = ns;
34773477

3478-
m->fr = friendreq_new();
3478+
m->fr = friendreq_new(mem);
34793479

34803480
if (m->fr == nullptr) {
34813481
mem_delete(mem, m);
@@ -3544,7 +3544,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
35443544
return nullptr;
35453545
}
35463546

3547-
m->group_announce = new_gca_list();
3547+
m->group_announce = new_gca_list(m->mem);
35483548

35493549
if (m->group_announce == nullptr) {
35503550
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 *
35593559
}
35603560

35613561
if (options->dht_announcements_enabled) {
3562-
m->forwarding = new_forwarding(m->log, m->rng, m->mono_time, m->dht);
3562+
m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht);
35633563
if (m->forwarding != nullptr) {
35643564
m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding);
35653565
} else {
@@ -3574,7 +3574,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
35743574
m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht);
35753575
m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto);
35763576
if (m->onion_c != nullptr) {
3577-
m->fr_c = new_friend_connections(m->log, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled);
3577+
m->fr_c = new_friend_connections(m->log, m->mem, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled);
35783578
}
35793579

35803580
if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) ||

toxcore/TCP_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
10401040
memcpy(temp->secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE);
10411041
crypto_derive_public_key(temp->public_key, temp->secret_key);
10421042

1043-
bs_list_init(&temp->accepted_key_list, CRYPTO_PUBLIC_KEY_SIZE, 8, memcmp);
1043+
bs_list_init(&temp->accepted_key_list, mem, CRYPTO_PUBLIC_KEY_SIZE, 8, memcmp);
10441044

10451045
return temp;
10461046
}

0 commit comments

Comments
 (0)