Skip to content

Commit

Permalink
Minor cleanups: dead stores and avoiding complex macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 24, 2018
1 parent b2a2a0b commit d2a6d3e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
43 changes: 25 additions & 18 deletions toxcore/TCP_connection.c
Expand Up @@ -72,13 +72,27 @@ const uint8_t *tcp_connections_public_key(const TCP_Connections *tcp_c)
* return -1 if realloc fails.
* return 0 if it succeeds.
*/
#define realloc_tox_array(array, element_type, num, temp_pointer) \
(num \
? (temp_pointer = (element_type *)realloc( \
array, \
(num) * sizeof(element_type)), \
temp_pointer ? (array = temp_pointer, 0) : -1) \
: (free(array), array = nullptr, 0))
#define MAKE_REALLOC(T) \
static int realloc_##T(T **array, size_t num) \
{ \
if (!num) { \
*array = nullptr; \
return 0; \
} \
\
T *temp_pointer = (T *)realloc(*array, num * sizeof(T)); \
\
if (!temp_pointer) { \
return -1; \
} \
\
*array = temp_pointer; \
\
return 0; \
}

MAKE_REALLOC(TCP_Connection_to)
MAKE_REALLOC(TCP_con)


/* return 1 if the connections_number is not valid.
Expand Down Expand Up @@ -138,10 +152,7 @@ static int create_connection(TCP_Connections *tcp_c)

int id = -1;

TCP_Connection_to *temp_pointer;

if (realloc_tox_array(tcp_c->connections, TCP_Connection_to, tcp_c->connections_length + 1,
temp_pointer) == 0) {
if (realloc_TCP_Connection_to(&tcp_c->connections, tcp_c->connections_length + 1) == 0) {
id = tcp_c->connections_length;
++tcp_c->connections_length;
memset(&tcp_c->connections[id], 0, sizeof(TCP_Connection_to));
Expand All @@ -167,9 +178,7 @@ static int create_tcp_connection(TCP_Connections *tcp_c)

int id = -1;

TCP_con *temp_pointer;

if (realloc_tox_array(tcp_c->tcp_connections, TCP_con, tcp_c->tcp_connections_length + 1, temp_pointer) == 0) {
if (realloc_TCP_con(&tcp_c->tcp_connections, tcp_c->tcp_connections_length + 1) == 0) {
id = tcp_c->tcp_connections_length;
++tcp_c->tcp_connections_length;
memset(&tcp_c->tcp_connections[id], 0, sizeof(TCP_con));
Expand Down Expand Up @@ -200,8 +209,7 @@ static int wipe_connection(TCP_Connections *tcp_c, int connections_number)

if (tcp_c->connections_length != i) {
tcp_c->connections_length = i;
TCP_Connection_to *temp_pointer;
realloc_tox_array(tcp_c->connections, TCP_Connection_to, tcp_c->connections_length, temp_pointer);
realloc_TCP_Connection_to(&tcp_c->connections, tcp_c->connections_length);
}

return 0;
Expand Down Expand Up @@ -229,8 +237,7 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe

if (tcp_c->tcp_connections_length != i) {
tcp_c->tcp_connections_length = i;
TCP_con *temp_pointer;
realloc_tox_array(tcp_c->tcp_connections, TCP_con, tcp_c->tcp_connections_length, temp_pointer);
realloc_TCP_con(&tcp_c->tcp_connections, tcp_c->tcp_connections_length);
}

return 0;
Expand Down
28 changes: 16 additions & 12 deletions toxcore/network.c
Expand Up @@ -1484,16 +1484,18 @@ size_t net_pack_u16(uint8_t *bytes, uint16_t v)

size_t net_pack_u32(uint8_t *bytes, uint32_t v)
{
bytes += net_pack_u16(bytes, (v >> 16) & 0xffff);
bytes += net_pack_u16(bytes, v & 0xffff);
return sizeof(v);
uint8_t *p = bytes;
p += net_pack_u16(p, (v >> 16) & 0xffff);
p += net_pack_u16(p, v & 0xffff);
return p - bytes;
}

size_t net_pack_u64(uint8_t *bytes, uint64_t v)
{
bytes += net_pack_u32(bytes, (v >> 32) & 0xffffffff);
bytes += net_pack_u32(bytes, v & 0xffffffff);
return sizeof(v);
uint8_t *p = bytes;
p += net_pack_u32(p, (v >> 32) & 0xffffffff);
p += net_pack_u32(p, v & 0xffffffff);
return p - bytes;
}

size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
Expand All @@ -1506,18 +1508,20 @@ size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)

size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v)
{
const uint8_t *p = bytes;
uint16_t lo, hi;
bytes += net_unpack_u16(bytes, &hi);
bytes += net_unpack_u16(bytes, &lo);
p += net_unpack_u16(p, &hi);
p += net_unpack_u16(p, &lo);
*v = ((uint32_t)hi << 16) | lo;
return sizeof(*v);
return p - bytes;
}

size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
{
const uint8_t *p = bytes;
uint32_t lo, hi;
bytes += net_unpack_u32(bytes, &hi);
bytes += net_unpack_u32(bytes, &lo);
p += net_unpack_u32(p, &hi);
p += net_unpack_u32(p, &lo);
*v = ((uint64_t)hi << 32) | lo;
return sizeof(*v);
return p - bytes;
}
2 changes: 1 addition & 1 deletion toxcore/onion_announce.c
Expand Up @@ -390,7 +390,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t *
uint8_t ping_id2[ONION_PING_ID_SIZE];
generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet_public_key, source, ping_id2);

int index = -1;
int index;

uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE;

Expand Down
2 changes: 1 addition & 1 deletion toxcore/onion_client.c
Expand Up @@ -845,7 +845,7 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
}

VLA(uint8_t, plain, 1 + ONION_PING_ID_SIZE + len_nodes);
int len = -1;
int len;

if (num == 0) {
len = decrypt_data(public_key, nc_get_self_secret_key(onion_c->c),
Expand Down

0 comments on commit d2a6d3e

Please sign in to comment.