Skip to content

Commit

Permalink
cleanup: Enforce for loop consistency.
Browse files Browse the repository at this point in the history
All for-loops must have an init-decl, a condition, and an increment
statement. Any loop that doesn't have one of these should be a while
loop (only 2 of these exist in toxav, none in toxcore).
  • Loading branch information
iphydf committed Dec 9, 2021
1 parent 044a930 commit 4e145ea
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 29 deletions.
7 changes: 4 additions & 3 deletions toxav/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ void ac_iterate(ACSession *ac)
struct JitterBuffer *const j_buf = (struct JitterBuffer *)ac->j_buf;

int rc = 0;
struct RTPMessage *msg = jbuf_read(j_buf, &rc);

for (; msg != nullptr || rc == 2; msg = jbuf_read(j_buf, &rc)) {
for (struct RTPMessage *msg = jbuf_read(j_buf, &rc); msg != nullptr || rc == 2; msg = jbuf_read(j_buf, &rc)) {
pthread_mutex_unlock(ac->queue_mutex);

if (rc == 2) {
Expand Down Expand Up @@ -297,11 +296,13 @@ static struct JitterBuffer *jbuf_new(uint32_t capacity)
}
static void jbuf_clear(struct JitterBuffer *q)
{
for (; q->bottom != q->top; ++q->bottom) {
while (q->bottom != q->top) {
if (q->queue[q->bottom % q->size]) {
free(q->queue[q->bottom % q->size]);
q->queue[q->bottom % q->size] = nullptr;
}

++q->bottom;
}
}
static void jbuf_free(struct JitterBuffer *q)
Expand Down
4 changes: 3 additions & 1 deletion toxav/groupav.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ static Group_JitterBuffer *create_queue(unsigned int capacity)

static void clear_queue(Group_JitterBuffer *q)
{
for (; q->bottom != q->top; ++q->bottom) {
while (q->bottom != q->top) {
if (q->queue[q->bottom % q->size]) {
free(q->queue[q->bottom % q->size]);
q->queue[q->bottom % q->size] = nullptr;
}

++q->bottom;
}
}

Expand Down
4 changes: 1 addition & 3 deletions toxav/msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,7 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number)
session->calls = tmp;

/* Set fields in between to null */
uint32_t i = session->calls_tail + 1;

for (; i < friend_number; ++i) {
for (uint32_t i = session->calls_tail + 1; i < friend_number; ++i) {
session->calls[i] = nullptr;
}

Expand Down
4 changes: 2 additions & 2 deletions toxav/ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ uint16_t rb_size(const RingBuffer *b)

uint16_t rb_data(const RingBuffer *b, void **dest)
{
uint16_t i = 0;
uint16_t i;

for (; i < rb_size(b); ++i) {
for (i = 0; i < rb_size(b); ++i) {
dest[i] = b->data[(b->start + i) % b->size];
}

Expand Down
4 changes: 1 addition & 3 deletions toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ void toxav_iterate(ToxAV *av)
uint64_t start = current_time_monotonic(av->toxav_mono_time);
int32_t rc = 500;

ToxAVCall *i = av->calls[av->calls_head];

for (; i; i = i->next) {
for (ToxAVCall *i = av->calls[av->calls_head]; i; i = i->next) {
if (i->active) {
pthread_mutex_lock(i->toxav_call_mutex);
pthread_mutex_unlock(av->mutex);
Expand Down
6 changes: 2 additions & 4 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,9 @@ void increment_nonce(uint8_t *nonce)
* that loop bounds and their potential underflow or overflow
* are independent of user-controlled input (you may have heard of the Heartbleed bug).
*/
uint32_t i = crypto_box_NONCEBYTES;
uint_fast16_t carry = 1U;

for (; i != 0; --i) {
for (uint32_t i = crypto_box_NONCEBYTES; i != 0; --i) {
carry += (uint_fast16_t)nonce[i - 1];
nonce[i - 1] = (uint8_t)carry;
carry >>= 8;
Expand All @@ -260,10 +259,9 @@ void increment_nonce_number(uint8_t *nonce, uint32_t increment)
num_as_nonce[crypto_box_NONCEBYTES - 2] = increment >> 8;
num_as_nonce[crypto_box_NONCEBYTES - 1] = increment;

uint32_t i = crypto_box_NONCEBYTES;
uint_fast16_t carry = 0U;

for (; i != 0; --i) {
for (uint32_t i = crypto_box_NONCEBYTES; i != 0; --i) {
carry += (uint_fast16_t)nonce[i - 1] + (uint_fast16_t)num_as_nonce[i - 1];
nonce[i - 1] = (uint8_t)carry;
carry >>= 8;
Expand Down
30 changes: 17 additions & 13 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -3073,28 +3073,23 @@ static bool groupchat_freeze_timedout(Group_Chats *g_c, uint32_t groupnumber, vo
/* Push non-empty slots to start. */
static void squash_connections(Group_c *g)
{
uint16_t i = 0;
uint16_t num_connected = 0;

for (uint16_t j = 0; j < MAX_GROUP_CONNECTIONS; ++j) {
if (g->connections[j].type != GROUPCHAT_CONNECTION_NONE) {
g->connections[i] = g->connections[j];
++i;
for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type != GROUPCHAT_CONNECTION_NONE) {
g->connections[num_connected] = g->connections[i];
++num_connected;
}
}

for (; i < MAX_GROUP_CONNECTIONS; ++i) {
for (uint16_t i = num_connected; i < MAX_GROUP_CONNECTIONS; ++i) {
g->connections[i].type = GROUPCHAT_CONNECTION_NONE;
}
}

#define MIN_EMPTY_CONNECTIONS (1 + MAX_GROUP_CONNECTIONS / 10)

/* Remove old connections as necessary to ensure we have space for new
* connections. This invalidates connections array indices (which is
* why we do this periodically rather than on adding a connection).
*/
static void clean_connections(Group_Chats *g_c, Group_c *g)
{
static uint16_t empty_connection_count(Group_c *g) {
uint16_t to_clear = MIN_EMPTY_CONNECTIONS;

for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
Expand All @@ -3107,7 +3102,16 @@ static void clean_connections(Group_Chats *g_c, Group_c *g)
}
}

for (; to_clear > 0; --to_clear) {
return to_clear;
}

/* Remove old connections as necessary to ensure we have space for new
* connections. This invalidates connections array indices (which is
* why we do this periodically rather than on adding a connection).
*/
static void clean_connections(Group_Chats *g_c, Group_c *g)
{
for (uint16_t to_clear = empty_connection_count(g); to_clear > 0; --to_clear) {
// Remove a connection. Prefer non-closest connections, and given
// that prefer non-online connections, and given that prefer earlier
// slots.
Expand Down

0 comments on commit 4e145ea

Please sign in to comment.