Skip to content

Commit

Permalink
Style fixes in TCP code; remove MIN and PAIR from util.h.
Browse files Browse the repository at this point in the history
* Moved PAIR to toxav, where it's used (but really this should die).
* Replace most MIN calls with typed `min_*` calls. Didn't replace the
  ones where the desired semantics are unclear. Moved the MIN macro to
  the one place where it's still used.
* Avoid assignments in `while` loops. Instead, factored out the loop body
  into a separate `bool`-returning function.
* Use named types for callbacks (`_cb` types).
* Avoid assignments in `if` conditions.
* Removed `MAKE_REALLOC` and expanded its two calls. We can't have
  templates in C, and this fake templating is ugly and hard to analyse
  and debug (it expands on a single line).
* Moved epoll system include to the .c file, out of the .h file.
* Avoid assignments in expressions (`a = b = c;`).
* Avoid multiple declarators per struct member declaration.
* Fix naming inconsistencies.
* Replace `net_to_host` macro with function.
  • Loading branch information
iphydf committed Jul 12, 2018
1 parent cbda010 commit 5c58458
Show file tree
Hide file tree
Showing 18 changed files with 542 additions and 459 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ if(BUILD_TOXAV)
toxav/groupav.h
toxav/msi.c
toxav/msi.h
toxav/pair.h
toxav/ring_buffer.c
toxav/ring_buffer.h
toxav/rtp.c
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/file_transfer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ static void file_transfer_test(void)
printf("after %u iterations: %.2fMiB done\n", (unsigned int)i + 1, (double)size_recv / 1024 / 1024);
}

c_sleep(MIN(tox1_interval, MIN(tox2_interval, tox3_interval)));
c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval)));
}

ck_assert_msg(file_sending_done, "file sending did not complete after %u iterations: sendf_ok:%u file_recv:%u "
Expand Down Expand Up @@ -348,7 +348,7 @@ static void file_transfer_test(void)
uint32_t tox2_interval = tox_iteration_interval(tox2);
uint32_t tox3_interval = tox_iteration_interval(tox3);

c_sleep(MIN(tox1_interval, MIN(tox2_interval, tox3_interval)));
c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval)));
}

printf("Starting file 0 transfer test.\n");
Expand Down Expand Up @@ -397,7 +397,7 @@ static void file_transfer_test(void)
uint32_t tox2_interval = tox_iteration_interval(tox2);
uint32_t tox3_interval = tox_iteration_interval(tox3);

c_sleep(MIN(tox1_interval, MIN(tox2_interval, tox3_interval)));
c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval)));
}

printf("file_transfer_test succeeded, took %llu seconds\n", time(nullptr) - cur_time);
Expand Down
7 changes: 7 additions & 0 deletions toxav/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ cc_test(
],
)

cc_library(
name = "pair",
hdrs = ["pair.h"],
)

cc_library(
name = "audio",
srcs = ["audio.c"],
hdrs = ["audio.h"],
deps = [
":pair",
":public",
":rtp",
"//c-toxcore/toxcore:network",
Expand All @@ -78,6 +84,7 @@ cc_library(
],
deps = [
":audio",
":pair",
":public",
"//c-toxcore/toxcore:network",
"@libvpx",
Expand Down
1 change: 1 addition & 0 deletions toxav/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ libtoxav_la_SOURCES = ../toxav/rtp.h \
../toxav/video.c \
../toxav/bwcontroller.h \
../toxav/bwcontroller.c \
../toxav/pair.h \
../toxav/ring_buffer.h \
../toxav/ring_buffer.c \
../toxav/toxav.h \
Expand Down
1 change: 1 addition & 0 deletions toxav/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "../toxcore/logger.h"
#include "../toxcore/util.h"
#include "pair.h"

#include <opus.h>
#include <pthread.h>
Expand Down
6 changes: 6 additions & 0 deletions toxav/pair.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef C_TOXCORE_TOXAV_PAIR_H
#define C_TOXCORE_TOXAV_PAIR_H

#define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; }

#endif // C_TOXCORE_TOXAV_PAIR_H
4 changes: 4 additions & 0 deletions toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ void toxav_iterate(ToxAV *av)
ac_iterate(i->audio.second);
vc_iterate(i->video.second);

// TODO(iphydf): Find out what MIN-semantics are desired here and
// use a min_* function from util.h.
#define MIN(a,b) (((a)<(b))?(a):(b))

if (i->msi_call->self_capabilities & msi_CapRAudio &&
i->msi_call->peer_capabilities & msi_CapSAudio) {
rc = MIN(i->audio.second->lp_frame_duration, rc);
Expand Down
1 change: 1 addition & 0 deletions toxav/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "../toxcore/logger.h"
#include "../toxcore/util.h"
#include "pair.h"

#include <vpx/vpx_decoder.h>
#include <vpx/vpx_encoder.h>
Expand Down
9 changes: 6 additions & 3 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@ int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf,
return -1;
}

int msglen = MIN(maxlen, m->friendlist[friendnumber].statusmessage_length);
// TODO(iphydf): This should be uint16_t and min_u16. If maxlen exceeds
// uint16_t's range, it won't affect the result.
uint32_t msglen = min_u32(maxlen, m->friendlist[friendnumber].statusmessage_length);

memcpy(buf, m->friendlist[friendnumber].statusmessage, msglen);
memset(buf + msglen, 0, maxlen - msglen);
Expand Down Expand Up @@ -2855,9 +2857,10 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
memcpy(temp.real_pk, m->friendlist[i].real_pk, CRYPTO_PUBLIC_KEY_SIZE);

if (temp.status < 3) {
// TODO(iphydf): Use uint16_t and min_u16 here.
const size_t friendrequest_length =
MIN(m->friendlist[i].info_size,
MIN(SAVED_FRIEND_REQUEST_SIZE, MAX_FRIEND_REQUEST_DATA_SIZE));
min_u32(m->friendlist[i].info_size,
min_u32(SAVED_FRIEND_REQUEST_SIZE, MAX_FRIEND_REQUEST_DATA_SIZE));
memcpy(temp.info, m->friendlist[i].info, friendrequest_length);

temp.info_size = net_htons(m->friendlist[i].info_size);
Expand Down
Loading

0 comments on commit 5c58458

Please sign in to comment.