Skip to content

Commit

Permalink
cleanup: Some random cleanups, mostly related to mem.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 15, 2025
1 parent 5cca245 commit a7bc4f5
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion auto_tests/proxy_test.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* Tests that we can send messages to friends.
*/

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#include "auto_test_support.h"

Expand Down
4 changes: 2 additions & 2 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ void getaddress(const Messenger *m, uint8_t *address)
}

non_null()
static bool send_online_packet(Messenger *m, int friendcon_id)
static bool send_online_packet(const Messenger *m, int friendcon_id)
{
const uint8_t packet[1] = {PACKET_ID_ONLINE};
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
sizeof(packet), false) != -1;
}

non_null()
static bool send_offline_packet(Messenger *m, int friendcon_id)
static bool send_offline_packet(const Messenger *m, int friendcon_id)
{
const uint8_t packet[1] = {PACKET_ID_OFFLINE};
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
Expand Down
14 changes: 7 additions & 7 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ static bool is_groupnumber_valid(const Group_Chats *g_c, uint32_t groupnumber)

/** @brief Set the size of the groupchat list to num.
*
* @retval false if realloc fails.
* @retval false if mem_vrealloc fails.
* @retval true if it succeeds.
*/
non_null()
Expand Down Expand Up @@ -1067,7 +1067,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index,
return false;
}

Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->m->mem, g->frozen, g->numfrozen + 1, sizeof(Group_Peer));
Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->frozen, g->numfrozen + 1, sizeof(Group_Peer));

if (temp == nullptr) {
return false;
Expand All @@ -1085,7 +1085,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index,

++g->numfrozen;

delete_old_frozen(g, g_c->m->mem);
delete_old_frozen(g, g_c->mem);

return true;
}
Expand Down Expand Up @@ -1572,7 +1572,7 @@ int group_set_max_frozen(const Group_Chats *g_c, uint32_t groupnumber, uint32_t
}

g->maxfrozen = maxfrozen;
delete_old_frozen(g, g_c->m->mem);
delete_old_frozen(g, g_c->mem);
return 0;
}

Expand Down Expand Up @@ -3805,19 +3805,19 @@ bool conferences_load_state_section(Group_Chats *g_c, const uint8_t *data, uint3
}

/** Create new groupchat instance. */
Group_Chats *new_groupchats(const Mono_Time *mono_time, Messenger *m)
Group_Chats *new_groupchats(const Mono_Time *mono_time, const Memory *mem, Messenger *m)
{
if (m == nullptr) {
return nullptr;
}

Group_Chats *temp = (Group_Chats *)mem_alloc(m->mem, sizeof(Group_Chats));
Group_Chats *temp = (Group_Chats *)mem_alloc(mem, sizeof(Group_Chats));

if (temp == nullptr) {
return nullptr;
}

temp->mem = m->mem;
temp->mem = mem;
temp->mono_time = mono_time;
temp->m = m;
temp->fr_c = m->fr_c;
Expand Down
3 changes: 2 additions & 1 deletion toxcore/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Messenger.h"
#include "attributes.h"
#include "crypto_core.h"
#include "mem.h"
#include "mono_time.h"
#include "state.h"

Expand Down Expand Up @@ -390,7 +391,7 @@ bool conferences_load_state_section(

/** Create new groupchat instance. */
non_null()
Group_Chats *new_groupchats(const Mono_Time *mono_time, Messenger *m);
Group_Chats *new_groupchats(const Mono_Time *mono_time, const Memory *mem, Messenger *m);

/** main groupchats loop. */
non_null(1) nullable(2)
Expand Down
6 changes: 3 additions & 3 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,7 @@ static bool send_gc_peer_exchange(const GC_Chat *chat, GC_Connection *gconn)
* Return -5 if supplied group password is invalid.
* Return -6 if we fail to add the peer to the peer list.
* Return -7 if peer's role cannot be validated.
* Return -8 if malloc fails.
* Return -8 if memory allocation fails.
*/
non_null(1, 2, 4) nullable(6)
static int handle_gc_peer_info_response(const GC_Session *c, GC_Chat *chat, uint32_t peer_number,
Expand Down Expand Up @@ -5534,7 +5534,7 @@ static int unwrap_group_handshake_packet(const Logger *log, const Memory *mem, c
*
* Return length of encrypted packet on success.
* Return -1 if packet size is invalid.
* Return -2 on malloc failure.
* Return -2 on memory allocation failure.
* Return -3 if encryption fails.
*/
non_null()
Expand Down Expand Up @@ -6825,7 +6825,7 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key)
GC_Peer *tmp_group = (GC_Peer *)mem_vrealloc(chat->mem, chat->group, chat->numpeers + 1, sizeof(GC_Peer));

if (tmp_group == nullptr) {
LOGGER_ERROR(chat->log, "Failed to allocate memory for group realloc");
LOGGER_ERROR(chat->log, "Failed to allocate memory for group mem_vrealloc");

if (tcp_connection_num != -1) {
kill_tcp_connection_to(chat->tcp_conn, tcp_connection_num);
Expand Down
1 change: 0 additions & 1 deletion toxcore/group_moderation.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <assert.h>


#include <string.h>
#include <time.h>

Expand Down
8 changes: 4 additions & 4 deletions toxcore/group_onion_announce.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ void gca_onion_init(GC_Announces_List *group_announce, Onion_Announce *onion_a);

non_null()
int create_gca_announce_request(
const Memory *mem, const Random *rng, uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id,
const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id,
const uint8_t *client_id, const uint8_t *data_public_key, uint64_t sendback_data,
const uint8_t *gc_data, uint16_t gc_data_length);
const Memory *mem, const Random *rng, uint8_t *packet, uint16_t max_packet_length,
const uint8_t *dest_client_id, const uint8_t *public_key, const uint8_t *secret_key,
const uint8_t *ping_id, const uint8_t *client_id, const uint8_t *data_public_key,
uint64_t sendback_data, const uint8_t *gc_data, uint16_t gc_data_length);

#endif /* C_TOXCORE_TOXCORE_GROUP_ONION_ANNOUNCE_H */
2 changes: 1 addition & 1 deletion toxcore/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static bool resize(BS_List *list, uint32_t new_size)
return true;
}

uint8_t *data = (uint8_t *)mem_vrealloc(list->mem, list->data, list->element_size, new_size);
uint8_t *data = (uint8_t *)mem_brealloc(list->mem, list->data, new_size * list->element_size);

if (data == nullptr) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions toxcore/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
#include "logger.h"

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -52,6 +53,7 @@ void logger_kill(Logger *log)

void logger_callback_log(Logger *log, logger_cb *function, void *context, void *userdata)
{
assert(log != nullptr);
log->callback = function;
log->context = context;
log->userdata = userdata;
Expand Down
6 changes: 6 additions & 0 deletions toxcore/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ void *mem_balloc(const Memory *mem, uint32_t size)
return ptr;
}

void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size)
{
void *const new_ptr = mem->funcs->realloc(mem->obj, ptr, size);
return new_ptr;
}

void *mem_alloc(const Memory *mem, uint32_t size)
{
void *const ptr = mem->funcs->calloc(mem->obj, 1, size);
Expand Down
8 changes: 8 additions & 0 deletions toxcore/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const Memory *os_memory(void);
*/
non_null() void *mem_balloc(const Memory *mem, uint32_t size);

/**
* @brief Resize an array of a given size for built-in types.
*
* If used for a type other than byte-sized types, `size` needs to be manually
* multiplied by the element size.
*/
non_null(1) nullable(2) void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size);

/**
* @brief Allocate a single object.
*
Expand Down
2 changes: 1 addition & 1 deletion toxcore/mono_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time,
* The starting point is unspecified and in particular is likely not comparable
* to the return value of `mono_time_get_ms()`.
*/
uint64_t current_time_monotonic(Mono_Time *mono_time)
uint64_t current_time_monotonic(const Mono_Time *mono_time)
{
return mono_time->current_time_callback(mono_time->user_data);
}
2 changes: 1 addition & 1 deletion toxcore/mono_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64
* to the return value of `mono_time_get_ms()`.
*/
non_null()
uint64_t current_time_monotonic(Mono_Time *mono_time);
uint64_t current_time_monotonic(const Mono_Time *mono_time);

/**
* Override implementation of `current_time_monotonic()` (for tests).
Expand Down
33 changes: 17 additions & 16 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
#include "net_crypto.h"

#include <pthread.h>
#include <string.h>

#include "DHT.h"
Expand Down Expand Up @@ -673,7 +674,7 @@ static IP_Port return_ip_port_connection(const Net_Crypto *c, int crypt_connecti
* @retval 0 on success.
*/
non_null()
static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
static int send_packet_to(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
{
// TODO(irungentoo): TCP, etc...
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
Expand Down Expand Up @@ -981,7 +982,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets
* @return number of requested packets on success.
*/
non_null()
static int handle_request_packet(const Memory *mem, Mono_Time *mono_time, Packets_Array *send_array,
static int handle_request_packet(const Memory *mem, const Mono_Time *mono_time, Packets_Array *send_array,
const uint8_t *data, uint16_t length,
uint64_t *latest_send_time, uint64_t rtt_time)
{
Expand Down Expand Up @@ -1064,7 +1065,7 @@ static int handle_request_packet(const Memory *mem, Mono_Time *mono_time, Packet
* @retval 0 on success.
*/
non_null()
static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
static int send_data_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
{
const uint16_t max_length = MAX_CRYPTO_PACKET_SIZE - (1 + sizeof(uint16_t) + CRYPTO_MAC_SIZE);

Expand Down Expand Up @@ -1102,7 +1103,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_
* @retval 0 on success.
*/
non_null()
static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num,
static int send_data_packet_helper(const Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num,
const uint8_t *data, uint16_t length)
{
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) {
Expand All @@ -1124,7 +1125,7 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3
}

non_null()
static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
static int reset_max_speed_reached(const Net_Crypto *c, int crypt_connection_id)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

Expand Down Expand Up @@ -1159,7 +1160,7 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
* @return positive packet number if data was put into the queue.
*/
non_null()
static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
static int64_t send_lossless_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
bool congestion_control)
{
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) {
Expand Down Expand Up @@ -1275,7 +1276,7 @@ static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint
* @retval 0 on success.
*/
non_null()
static int send_request_packet(Net_Crypto *c, int crypt_connection_id)
static int send_request_packet(const Net_Crypto *c, int crypt_connection_id)
{
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

Expand All @@ -1300,7 +1301,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id)
* @return number of packets sent on success.
*/
non_null()
static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32_t max_num)
static int send_requested_packets(const Net_Crypto *c, int crypt_connection_id, uint32_t max_num)
{
if (max_num == 0) {
return -1;
Expand Down Expand Up @@ -1414,7 +1415,7 @@ static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id)
* @retval 0 on success.
*/
non_null()
static int send_temp_packet(Net_Crypto *c, int crypt_connection_id)
static int send_temp_packet(const Net_Crypto *c, int crypt_connection_id)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

Expand Down Expand Up @@ -1442,7 +1443,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id)
* @retval 0 on success.
*/
non_null()
static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie,
static int create_send_handshake(const Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie,
const uint8_t *dht_public_key)
{
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
Expand Down Expand Up @@ -1472,7 +1473,7 @@ static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, const u
* @retval 0 on success.
*/
non_null()
static int send_kill_packet(Net_Crypto *c, int crypt_connection_id)
static int send_kill_packet(const Net_Crypto *c, int crypt_connection_id)
{
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

Expand Down Expand Up @@ -1647,7 +1648,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
}

non_null()
static int handle_packet_cookie_response(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length)
static int handle_packet_cookie_response(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

Expand Down Expand Up @@ -1679,7 +1680,7 @@ static int handle_packet_cookie_response(Net_Crypto *c, int crypt_connection_id,
}

non_null(1, 3) nullable(5)
static int handle_packet_crypto_hs(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
static int handle_packet_crypto_hs(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
void *userdata)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
Expand Down Expand Up @@ -2757,7 +2758,7 @@ static void send_crypto_packets(Net_Crypto *c)
* @retval 1 if max speed was reached for this connection (no more data can be physically through the pipe).
* @retval 0 if it wasn't reached.
*/
bool max_speed_reached(Net_Crypto *c, int crypt_connection_id)
bool max_speed_reached(const Net_Crypto *c, int crypt_connection_id)
{
return reset_max_speed_reached(c, crypt_connection_id) != 0;
}
Expand Down Expand Up @@ -2792,7 +2793,7 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti
*
* congestion_control: should congestion control apply to this packet?
*/
int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
bool congestion_control)
{
if (length == 0) {
Expand Down Expand Up @@ -2878,7 +2879,7 @@ int cryptpacket_received(const Net_Crypto *c, int crypt_connection_id, uint32_t
*
* The first byte of data must be in the PACKET_ID_RANGE_LOSSY.
*/
int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
{
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) {
return -1;
Expand Down
Loading

0 comments on commit a7bc4f5

Please sign in to comment.