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 f96d0dd
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 56 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
2 changes: 1 addition & 1 deletion toxav/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void ac_iterate(ACSession *ac)
free(temp_audio_buffer);
}

int ac_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg)
int ac_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg)
{
ACSession *ac = (ACSession *)cs;

Expand Down
2 changes: 1 addition & 1 deletion toxav/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t f
toxav_audio_receive_frame_cb *cb, void *cb_data);
void ac_kill(ACSession *ac);
void ac_iterate(ACSession *ac);
int ac_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg);
int ac_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg);
int ac_reconfigure_encoder(ACSession *ac, uint32_t bit_rate, uint32_t sampling_rate, uint8_t channels);

#endif /* C_TOXCORE_TOXAV_AUDIO_H */
2 changes: 1 addition & 1 deletion toxav/rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ static struct RTPHeader rtp_default_header(const RTPSession *session, uint32_t l
header.ma = 0;
header.pt = session->payload_type % 128;
header.sequnum = session->sequnum;
Mono_Time *mt = toxav_get_av_mono_time(session->toxav);
const Mono_Time *mt = toxav_get_av_mono_time(session->toxav);
if (mt != nullptr) {
header.timestamp = current_time_monotonic(mt);
} else {
Expand Down
2 changes: 1 addition & 1 deletion toxav/rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct RTPWorkBufferList {

#define DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT 10

typedef int rtp_m_cb(Mono_Time *mono_time, void *cs, struct RTPMessage *msg);
typedef int rtp_m_cb(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg);

/**
* RTP control session.
Expand Down
5 changes: 2 additions & 3 deletions toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "toxav.h"

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -21,7 +20,7 @@
#include "../toxcore/network.h"
#include "../toxcore/tox.h"
#include "../toxcore/tox_private.h"
#include "../toxcore/tox_struct.h"
#include "../toxcore/tox_struct.h" // IWYU pragma: keep
#include "../toxcore/util.h"

// TODO(zoff99): don't hardcode this, let the application choose it
Expand Down Expand Up @@ -342,7 +341,7 @@ uint32_t toxav_iteration_interval(const ToxAV *av)
* @param frame_time the duration of the current frame in ms
* @param start_time the timestamp when decoding of this frame started
*/
static void calc_interval(ToxAV *av, DecodeTimeStats *stats, int32_t frame_time, uint64_t start_time)
static void calc_interval(const ToxAV *av, DecodeTimeStats *stats, int32_t frame_time, uint64_t start_time)
{
stats->interval = frame_time < stats->average ? 0 : (frame_time - stats->average);
stats->total += current_time_monotonic(av->toxav_mono_time) - start_time;
Expand Down
4 changes: 1 addition & 3 deletions toxav/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
#include <stdlib.h>
#include <string.h>

#include "msi.h"
#include "ring_buffer.h"
#include "rtp.h"

#include "../toxcore/ccompat.h"
#include "../toxcore/logger.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/network.h"

/**
* Soft deadline the decoder should attempt to meet, in "us" (microseconds).
Expand Down Expand Up @@ -346,7 +344,7 @@ void vc_iterate(VCSession *vc)
}
}

int vc_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg)
int vc_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg)
{
VCSession *vc = (VCSession *)cs;

Expand Down
2 changes: 1 addition & 1 deletion toxav/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ VCSession *vc_new(const Logger *log, Mono_Time *mono_time, ToxAV *av, uint32_t f
toxav_video_receive_frame_cb *cb, void *cb_data);
void vc_kill(VCSession *vc);
void vc_iterate(VCSession *vc);
int vc_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg);
int vc_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg);
int vc_reconfigure_encoder(VCSession *vc, uint32_t bit_rate, uint16_t width, uint16_t height, int16_t kf_max_dist);

#endif /* C_TOXCORE_TOXAV_VIDEO_H */
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
Loading

0 comments on commit f96d0dd

Please sign in to comment.