From 2b49f803959cf47f81d1b49032f7c6f30ccc37f7 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 7 Jul 2018 21:40:17 +0000 Subject: Rename `BS_LIST` to `BS_List` to follow the naming conventions. `BS_LIST` would be a constant. `BS_List` is a type name. --- auto_tests/monolith_test.cc | 2 +- toxcore/Messenger.c | 4 +-- toxcore/TCP_server.c | 2 +- toxcore/list.c | 80 ++++++++++++++++++++++++--------------------- toxcore/list.h | 14 ++++---- toxcore/net_crypto.c | 2 +- toxcore/ping_array.c | 11 ++++--- 7 files changed, 60 insertions(+), 55 deletions(-) diff --git a/auto_tests/monolith_test.cc b/auto_tests/monolith_test.cc index 45d55958..7511decf 100644 --- a/auto_tests/monolith_test.cc +++ b/auto_tests/monolith_test.cc @@ -184,7 +184,7 @@ int main(int argc, char *argv[]) { CHECK_SIZE(Group_Chats, 2120); CHECK_SIZE(Group_Peer, 480); // toxcore/list - CHECK_SIZE(BS_LIST, 32); + CHECK_SIZE(BS_List, 32); // toxcore/logger CHECK_SIZE(Logger, 24); // toxcore/Messenger diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index b8509706..9014d56d 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2979,8 +2979,8 @@ uint32_t messenger_size(const Messenger *m) + sizesubhead + m->name_length // Own nickname. + sizesubhead + m->statusmessage_length // status message + sizesubhead + 1 // status - + sizesubhead + NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6) //TCP relays - + sizesubhead + NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6) //saved path nodes + + sizesubhead + NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6) // TCP relays + + sizesubhead + NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6) // saved path nodes + sizesubhead; } diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 65dc81c3..b31fab72 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -91,7 +91,7 @@ struct TCP_Server { uint64_t counter; - BS_LIST accepted_key_list; + BS_List accepted_key_list; }; const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server) diff --git a/toxcore/list.c b/toxcore/list.c index 7b65e184..4846ce25 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -45,31 +45,35 @@ * -some considerations since the array size is never perfect */ -#define INDEX(i) (~i) +static int32_t +list_index(uint32_t i) +{ + return ~i; +} /* Find data in list * * return value: * >= 0 : index of data in array - * < 0 : no match, returns index (return value is INDEX(index)) where + * < 0 : no match, returns index (return value is list_index(index)) where * the data should be inserted */ -static int find(const BS_LIST *list, const uint8_t *data) +static int find(const BS_List *list, const uint8_t *data) { - //should work well, but could be improved + // should work well, but could be improved if (list->n == 0) { - return INDEX(0); + return list_index(0); } - uint32_t i = list->n / 2; //current position in the array - uint32_t delta = i / 2; //how much we move in the array + uint32_t i = list->n / 2; // current position in the array + uint32_t delta = i / 2; // how much we move in the array if (!delta) { delta = 1; } - int d = -1; //used to determine if closest match is found - //closest match is found if we move back to where we have already been + int d = -1; // used to determine if closest match is found + // closest match is found if we move back to where we have already been while (1) { int r = memcmp(data, list->data + list->element_size * i, list->element_size); @@ -79,13 +83,13 @@ static int find(const BS_LIST *list, const uint8_t *data) } if (r > 0) { - //data is greater - //move down + // data is greater + // move down i += delta; if (d == 0 || i == list->n) { - //reached bottom of list, or closest match - return INDEX(i); + // reached bottom of list, or closest match + return list_index(i); } delta = (delta) / 2; @@ -95,13 +99,13 @@ static int find(const BS_LIST *list, const uint8_t *data) d = 1; } } else { - //data is smaller + // data is smaller if (d == 1 || i == 0) { - //reached top or list or closest match - return INDEX(i); + // reached top or list or closest match + return list_index(i); } - //move up + // move up i -= delta; delta = (delta) / 2; @@ -120,7 +124,7 @@ static int find(const BS_LIST *list, const uint8_t *data) * 1 : success * 0 : failure */ -static int resize(BS_LIST *list, uint32_t new_size) +static int resize(BS_List *list, uint32_t new_size) { if (new_size == 0) { bs_list_free(list); @@ -147,9 +151,9 @@ static int resize(BS_LIST *list, uint32_t new_size) } -int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity) +int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity) { - //set initial values + // set initial values list->n = 0; list->element_size = element_size; list->capacity = 0; @@ -167,9 +171,9 @@ int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity return 1; } -void bs_list_free(BS_LIST *list) +void bs_list_free(BS_List *list) { - //free both arrays + // free both arrays free(list->data); list->data = nullptr; @@ -177,11 +181,11 @@ void bs_list_free(BS_LIST *list) list->ids = nullptr; } -int bs_list_find(const BS_LIST *list, const uint8_t *data) +int bs_list_find(const BS_List *list, const uint8_t *data) { int r = find(list, data); - //return only -1 and positive values + // return only -1 and positive values if (r < 0) { return -1; } @@ -189,20 +193,20 @@ int bs_list_find(const BS_LIST *list, const uint8_t *data) return list->ids[r]; } -int bs_list_add(BS_LIST *list, const uint8_t *data, int id) +int bs_list_add(BS_List *list, const uint8_t *data, int id) { - //find where the new element should be inserted - //see: return value of find() + // find where the new element should be inserted + // see: return value of find() int i = find(list, data); if (i >= 0) { - //already in list + // already in list return 0; } i = ~i; - //increase the size of the arrays if needed + // increase the size of the arrays if needed if (list->n == list->capacity) { // 1.5 * n + 1 const uint32_t new_capacity = list->n + list->n / 2 + 1; @@ -214,22 +218,22 @@ int bs_list_add(BS_LIST *list, const uint8_t *data, int id) list->capacity = new_capacity; } - //insert data to element array + // insert data to element array memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, (list->n - i) * list->element_size); memcpy(list->data + i * list->element_size, data, list->element_size); - //insert id to id array + // insert id to id array memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); list->ids[i] = id; - //increase n - list->n++; + // increase n + ++list->n; return 1; } -int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) +int bs_list_remove(BS_List *list, const uint8_t *data, int id) { int i = find(list, data); @@ -238,11 +242,11 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) } if (list->ids[i] != id) { - //this should never happen + // this should never happen return 0; } - //decrease the size of the arrays if needed + // decrease the size of the arrays if needed if (list->n < list->capacity / 2) { const uint32_t new_capacity = list->capacity / 2; @@ -251,7 +255,7 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) } } - list->n--; + --list->n; memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, (list->n - i) * list->element_size); @@ -260,7 +264,7 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) return 1; } -int bs_list_trim(BS_LIST *list) +int bs_list_trim(BS_List *list) { if (!resize(list, list->n)) { return 0; diff --git a/toxcore/list.h b/toxcore/list.h index 9ad8f44d..8c6ddbf3 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -34,7 +34,7 @@ typedef struct { uint32_t element_size; //size of the elements uint8_t *data; //array of elements int *ids; //array of element ids -} BS_LIST; +} BS_List; /* Initialize a list, element_size is the size of the elements in the list and * initial_capacity is the number of elements the memory will be initially allocated for @@ -43,10 +43,10 @@ typedef struct { * 1 : success * 0 : failure */ -int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity); +int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity); /* Free a list initiated with list_init */ -void bs_list_free(BS_LIST *list); +void bs_list_free(BS_List *list); /* Retrieve the id of an element in the list * @@ -54,7 +54,7 @@ void bs_list_free(BS_LIST *list); * >= 0 : id associated with data * -1 : failure */ -int bs_list_find(const BS_LIST *list, const uint8_t *data); +int bs_list_find(const BS_List *list, const uint8_t *data); /* Add an element with associated id to the list * @@ -62,7 +62,7 @@ int bs_list_find(const BS_LIST *list, const uint8_t *data); * 1 : success * 0 : failure (data already in list) */ -int bs_list_add(BS_LIST *list, const uint8_t *data, int id); +int bs_list_add(BS_List *list, const uint8_t *data, int id); /* Remove element from the list * @@ -70,7 +70,7 @@ int bs_list_add(BS_LIST *list, const uint8_t *data, int id); * 1 : success * 0 : failure (element not found or id does not match) */ -int bs_list_remove(BS_LIST *list, const uint8_t *data, int id); +int bs_list_remove(BS_List *list, const uint8_t *data, int id); /* Removes the memory overhead * @@ -78,6 +78,6 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id); * 1 : success * 0 : failure */ -int bs_list_trim(BS_LIST *list); +int bs_list_trim(BS_List *list); #endif diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index bfdbbe30..bae5ee19 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -156,7 +156,7 @@ struct Net_Crypto { /* The current optimal sleep time */ uint32_t current_sleep_time; - BS_LIST ip_port_list; + BS_List ip_port_list; }; const uint8_t *nc_get_self_public_key(const Net_Crypto *c) diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index bcab9fe2..ded3ed3a 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c @@ -34,7 +34,7 @@ #include "util.h" -typedef struct { +typedef struct Ping_Array_Entry { void *data; uint32_t length; uint64_t time; @@ -76,7 +76,8 @@ Ping_Array *ping_array_new(uint32_t size, uint32_t timeout) return nullptr; } - empty_array->last_deleted = empty_array->last_added = 0; + empty_array->last_deleted = 0; + empty_array->last_added = 0; empty_array->total_size = size; empty_array->timeout = timeout; return empty_array; @@ -86,9 +87,9 @@ static void clear_entry(Ping_Array *array, uint32_t index) { free(array->entries[index].data); array->entries[index].data = nullptr; - array->entries[index].length = - array->entries[index].time = - array->entries[index].ping_id = 0; + array->entries[index].length = 0; + array->entries[index].time = 0; + array->entries[index].ping_id = 0; } /* Free all the allocated memory in a Ping_Array. -- cgit v1.2.3