diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 4 | ||||
-rw-r--r-- | toxcore/TCP_server.c | 2 | ||||
-rw-r--r-- | toxcore/list.c | 80 | ||||
-rw-r--r-- | toxcore/list.h | 14 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 2 | ||||
-rw-r--r-- | toxcore/ping_array.c | 11 |
6 files changed, 59 insertions, 54 deletions
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) | |||
2979 | + sizesubhead + m->name_length // Own nickname. | 2979 | + sizesubhead + m->name_length // Own nickname. |
2980 | + sizesubhead + m->statusmessage_length // status message | 2980 | + sizesubhead + m->statusmessage_length // status message |
2981 | + sizesubhead + 1 // status | 2981 | + sizesubhead + 1 // status |
2982 | + sizesubhead + NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6) //TCP relays | 2982 | + sizesubhead + NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6) // TCP relays |
2983 | + sizesubhead + NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6) //saved path nodes | 2983 | + sizesubhead + NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6) // saved path nodes |
2984 | + sizesubhead; | 2984 | + sizesubhead; |
2985 | } | 2985 | } |
2986 | 2986 | ||
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 { | |||
91 | 91 | ||
92 | uint64_t counter; | 92 | uint64_t counter; |
93 | 93 | ||
94 | BS_LIST accepted_key_list; | 94 | BS_List accepted_key_list; |
95 | }; | 95 | }; |
96 | 96 | ||
97 | const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server) | 97 | 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 @@ | |||
45 | * -some considerations since the array size is never perfect | 45 | * -some considerations since the array size is never perfect |
46 | */ | 46 | */ |
47 | 47 | ||
48 | #define INDEX(i) (~i) | 48 | static int32_t |
49 | list_index(uint32_t i) | ||
50 | { | ||
51 | return ~i; | ||
52 | } | ||
49 | 53 | ||
50 | /* Find data in list | 54 | /* Find data in list |
51 | * | 55 | * |
52 | * return value: | 56 | * return value: |
53 | * >= 0 : index of data in array | 57 | * >= 0 : index of data in array |
54 | * < 0 : no match, returns index (return value is INDEX(index)) where | 58 | * < 0 : no match, returns index (return value is list_index(index)) where |
55 | * the data should be inserted | 59 | * the data should be inserted |
56 | */ | 60 | */ |
57 | static int find(const BS_LIST *list, const uint8_t *data) | 61 | static int find(const BS_List *list, const uint8_t *data) |
58 | { | 62 | { |
59 | //should work well, but could be improved | 63 | // should work well, but could be improved |
60 | if (list->n == 0) { | 64 | if (list->n == 0) { |
61 | return INDEX(0); | 65 | return list_index(0); |
62 | } | 66 | } |
63 | 67 | ||
64 | uint32_t i = list->n / 2; //current position in the array | 68 | uint32_t i = list->n / 2; // current position in the array |
65 | uint32_t delta = i / 2; //how much we move in the array | 69 | uint32_t delta = i / 2; // how much we move in the array |
66 | 70 | ||
67 | if (!delta) { | 71 | if (!delta) { |
68 | delta = 1; | 72 | delta = 1; |
69 | } | 73 | } |
70 | 74 | ||
71 | int d = -1; //used to determine if closest match is found | 75 | int d = -1; // used to determine if closest match is found |
72 | //closest match is found if we move back to where we have already been | 76 | // closest match is found if we move back to where we have already been |
73 | 77 | ||
74 | while (1) { | 78 | while (1) { |
75 | int r = memcmp(data, list->data + list->element_size * i, list->element_size); | 79 | 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) | |||
79 | } | 83 | } |
80 | 84 | ||
81 | if (r > 0) { | 85 | if (r > 0) { |
82 | //data is greater | 86 | // data is greater |
83 | //move down | 87 | // move down |
84 | i += delta; | 88 | i += delta; |
85 | 89 | ||
86 | if (d == 0 || i == list->n) { | 90 | if (d == 0 || i == list->n) { |
87 | //reached bottom of list, or closest match | 91 | // reached bottom of list, or closest match |
88 | return INDEX(i); | 92 | return list_index(i); |
89 | } | 93 | } |
90 | 94 | ||
91 | delta = (delta) / 2; | 95 | delta = (delta) / 2; |
@@ -95,13 +99,13 @@ static int find(const BS_LIST *list, const uint8_t *data) | |||
95 | d = 1; | 99 | d = 1; |
96 | } | 100 | } |
97 | } else { | 101 | } else { |
98 | //data is smaller | 102 | // data is smaller |
99 | if (d == 1 || i == 0) { | 103 | if (d == 1 || i == 0) { |
100 | //reached top or list or closest match | 104 | // reached top or list or closest match |
101 | return INDEX(i); | 105 | return list_index(i); |
102 | } | 106 | } |
103 | 107 | ||
104 | //move up | 108 | // move up |
105 | i -= delta; | 109 | i -= delta; |
106 | 110 | ||
107 | delta = (delta) / 2; | 111 | delta = (delta) / 2; |
@@ -120,7 +124,7 @@ static int find(const BS_LIST *list, const uint8_t *data) | |||
120 | * 1 : success | 124 | * 1 : success |
121 | * 0 : failure | 125 | * 0 : failure |
122 | */ | 126 | */ |
123 | static int resize(BS_LIST *list, uint32_t new_size) | 127 | static int resize(BS_List *list, uint32_t new_size) |
124 | { | 128 | { |
125 | if (new_size == 0) { | 129 | if (new_size == 0) { |
126 | bs_list_free(list); | 130 | bs_list_free(list); |
@@ -147,9 +151,9 @@ static int resize(BS_LIST *list, uint32_t new_size) | |||
147 | } | 151 | } |
148 | 152 | ||
149 | 153 | ||
150 | int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity) | 154 | int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity) |
151 | { | 155 | { |
152 | //set initial values | 156 | // set initial values |
153 | list->n = 0; | 157 | list->n = 0; |
154 | list->element_size = element_size; | 158 | list->element_size = element_size; |
155 | list->capacity = 0; | 159 | list->capacity = 0; |
@@ -167,9 +171,9 @@ int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity | |||
167 | return 1; | 171 | return 1; |
168 | } | 172 | } |
169 | 173 | ||
170 | void bs_list_free(BS_LIST *list) | 174 | void bs_list_free(BS_List *list) |
171 | { | 175 | { |
172 | //free both arrays | 176 | // free both arrays |
173 | free(list->data); | 177 | free(list->data); |
174 | list->data = nullptr; | 178 | list->data = nullptr; |
175 | 179 | ||
@@ -177,11 +181,11 @@ void bs_list_free(BS_LIST *list) | |||
177 | list->ids = nullptr; | 181 | list->ids = nullptr; |
178 | } | 182 | } |
179 | 183 | ||
180 | int bs_list_find(const BS_LIST *list, const uint8_t *data) | 184 | int bs_list_find(const BS_List *list, const uint8_t *data) |
181 | { | 185 | { |
182 | int r = find(list, data); | 186 | int r = find(list, data); |
183 | 187 | ||
184 | //return only -1 and positive values | 188 | // return only -1 and positive values |
185 | if (r < 0) { | 189 | if (r < 0) { |
186 | return -1; | 190 | return -1; |
187 | } | 191 | } |
@@ -189,20 +193,20 @@ int bs_list_find(const BS_LIST *list, const uint8_t *data) | |||
189 | return list->ids[r]; | 193 | return list->ids[r]; |
190 | } | 194 | } |
191 | 195 | ||
192 | int bs_list_add(BS_LIST *list, const uint8_t *data, int id) | 196 | int bs_list_add(BS_List *list, const uint8_t *data, int id) |
193 | { | 197 | { |
194 | //find where the new element should be inserted | 198 | // find where the new element should be inserted |
195 | //see: return value of find() | 199 | // see: return value of find() |
196 | int i = find(list, data); | 200 | int i = find(list, data); |
197 | 201 | ||
198 | if (i >= 0) { | 202 | if (i >= 0) { |
199 | //already in list | 203 | // already in list |
200 | return 0; | 204 | return 0; |
201 | } | 205 | } |
202 | 206 | ||
203 | i = ~i; | 207 | i = ~i; |
204 | 208 | ||
205 | //increase the size of the arrays if needed | 209 | // increase the size of the arrays if needed |
206 | if (list->n == list->capacity) { | 210 | if (list->n == list->capacity) { |
207 | // 1.5 * n + 1 | 211 | // 1.5 * n + 1 |
208 | const uint32_t new_capacity = list->n + list->n / 2 + 1; | 212 | 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) | |||
214 | list->capacity = new_capacity; | 218 | list->capacity = new_capacity; |
215 | } | 219 | } |
216 | 220 | ||
217 | //insert data to element array | 221 | // insert data to element array |
218 | memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, | 222 | memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, |
219 | (list->n - i) * list->element_size); | 223 | (list->n - i) * list->element_size); |
220 | memcpy(list->data + i * list->element_size, data, list->element_size); | 224 | memcpy(list->data + i * list->element_size, data, list->element_size); |
221 | 225 | ||
222 | //insert id to id array | 226 | // insert id to id array |
223 | memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); | 227 | memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); |
224 | list->ids[i] = id; | 228 | list->ids[i] = id; |
225 | 229 | ||
226 | //increase n | 230 | // increase n |
227 | list->n++; | 231 | ++list->n; |
228 | 232 | ||
229 | return 1; | 233 | return 1; |
230 | } | 234 | } |
231 | 235 | ||
232 | int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | 236 | int bs_list_remove(BS_List *list, const uint8_t *data, int id) |
233 | { | 237 | { |
234 | int i = find(list, data); | 238 | int i = find(list, data); |
235 | 239 | ||
@@ -238,11 +242,11 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | |||
238 | } | 242 | } |
239 | 243 | ||
240 | if (list->ids[i] != id) { | 244 | if (list->ids[i] != id) { |
241 | //this should never happen | 245 | // this should never happen |
242 | return 0; | 246 | return 0; |
243 | } | 247 | } |
244 | 248 | ||
245 | //decrease the size of the arrays if needed | 249 | // decrease the size of the arrays if needed |
246 | if (list->n < list->capacity / 2) { | 250 | if (list->n < list->capacity / 2) { |
247 | const uint32_t new_capacity = list->capacity / 2; | 251 | const uint32_t new_capacity = list->capacity / 2; |
248 | 252 | ||
@@ -251,7 +255,7 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | |||
251 | } | 255 | } |
252 | } | 256 | } |
253 | 257 | ||
254 | list->n--; | 258 | --list->n; |
255 | 259 | ||
256 | memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, | 260 | memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, |
257 | (list->n - i) * list->element_size); | 261 | (list->n - i) * list->element_size); |
@@ -260,7 +264,7 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | |||
260 | return 1; | 264 | return 1; |
261 | } | 265 | } |
262 | 266 | ||
263 | int bs_list_trim(BS_LIST *list) | 267 | int bs_list_trim(BS_List *list) |
264 | { | 268 | { |
265 | if (!resize(list, list->n)) { | 269 | if (!resize(list, list->n)) { |
266 | return 0; | 270 | 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 { | |||
34 | uint32_t element_size; //size of the elements | 34 | uint32_t element_size; //size of the elements |
35 | uint8_t *data; //array of elements | 35 | uint8_t *data; //array of elements |
36 | int *ids; //array of element ids | 36 | int *ids; //array of element ids |
37 | } BS_LIST; | 37 | } BS_List; |
38 | 38 | ||
39 | /* Initialize a list, element_size is the size of the elements in the list and | 39 | /* Initialize a list, element_size is the size of the elements in the list and |
40 | * initial_capacity is the number of elements the memory will be initially allocated for | 40 | * initial_capacity is the number of elements the memory will be initially allocated for |
@@ -43,10 +43,10 @@ typedef struct { | |||
43 | * 1 : success | 43 | * 1 : success |
44 | * 0 : failure | 44 | * 0 : failure |
45 | */ | 45 | */ |
46 | int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity); | 46 | int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity); |
47 | 47 | ||
48 | /* Free a list initiated with list_init */ | 48 | /* Free a list initiated with list_init */ |
49 | void bs_list_free(BS_LIST *list); | 49 | void bs_list_free(BS_List *list); |
50 | 50 | ||
51 | /* Retrieve the id of an element in the list | 51 | /* Retrieve the id of an element in the list |
52 | * | 52 | * |
@@ -54,7 +54,7 @@ void bs_list_free(BS_LIST *list); | |||
54 | * >= 0 : id associated with data | 54 | * >= 0 : id associated with data |
55 | * -1 : failure | 55 | * -1 : failure |
56 | */ | 56 | */ |
57 | int bs_list_find(const BS_LIST *list, const uint8_t *data); | 57 | int bs_list_find(const BS_List *list, const uint8_t *data); |
58 | 58 | ||
59 | /* Add an element with associated id to the list | 59 | /* Add an element with associated id to the list |
60 | * | 60 | * |
@@ -62,7 +62,7 @@ int bs_list_find(const BS_LIST *list, const uint8_t *data); | |||
62 | * 1 : success | 62 | * 1 : success |
63 | * 0 : failure (data already in list) | 63 | * 0 : failure (data already in list) |
64 | */ | 64 | */ |
65 | int bs_list_add(BS_LIST *list, const uint8_t *data, int id); | 65 | int bs_list_add(BS_List *list, const uint8_t *data, int id); |
66 | 66 | ||
67 | /* Remove element from the list | 67 | /* Remove element from the list |
68 | * | 68 | * |
@@ -70,7 +70,7 @@ int bs_list_add(BS_LIST *list, const uint8_t *data, int id); | |||
70 | * 1 : success | 70 | * 1 : success |
71 | * 0 : failure (element not found or id does not match) | 71 | * 0 : failure (element not found or id does not match) |
72 | */ | 72 | */ |
73 | int bs_list_remove(BS_LIST *list, const uint8_t *data, int id); | 73 | int bs_list_remove(BS_List *list, const uint8_t *data, int id); |
74 | 74 | ||
75 | /* Removes the memory overhead | 75 | /* Removes the memory overhead |
76 | * | 76 | * |
@@ -78,6 +78,6 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id); | |||
78 | * 1 : success | 78 | * 1 : success |
79 | * 0 : failure | 79 | * 0 : failure |
80 | */ | 80 | */ |
81 | int bs_list_trim(BS_LIST *list); | 81 | int bs_list_trim(BS_List *list); |
82 | 82 | ||
83 | #endif | 83 | #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 { | |||
156 | /* The current optimal sleep time */ | 156 | /* The current optimal sleep time */ |
157 | uint32_t current_sleep_time; | 157 | uint32_t current_sleep_time; |
158 | 158 | ||
159 | BS_LIST ip_port_list; | 159 | BS_List ip_port_list; |
160 | }; | 160 | }; |
161 | 161 | ||
162 | const uint8_t *nc_get_self_public_key(const Net_Crypto *c) | 162 | 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 @@ | |||
34 | #include "util.h" | 34 | #include "util.h" |
35 | 35 | ||
36 | 36 | ||
37 | typedef struct { | 37 | typedef struct Ping_Array_Entry { |
38 | void *data; | 38 | void *data; |
39 | uint32_t length; | 39 | uint32_t length; |
40 | uint64_t time; | 40 | uint64_t time; |
@@ -76,7 +76,8 @@ Ping_Array *ping_array_new(uint32_t size, uint32_t timeout) | |||
76 | return nullptr; | 76 | return nullptr; |
77 | } | 77 | } |
78 | 78 | ||
79 | empty_array->last_deleted = empty_array->last_added = 0; | 79 | empty_array->last_deleted = 0; |
80 | empty_array->last_added = 0; | ||
80 | empty_array->total_size = size; | 81 | empty_array->total_size = size; |
81 | empty_array->timeout = timeout; | 82 | empty_array->timeout = timeout; |
82 | return empty_array; | 83 | return empty_array; |
@@ -86,9 +87,9 @@ static void clear_entry(Ping_Array *array, uint32_t index) | |||
86 | { | 87 | { |
87 | free(array->entries[index].data); | 88 | free(array->entries[index].data); |
88 | array->entries[index].data = nullptr; | 89 | array->entries[index].data = nullptr; |
89 | array->entries[index].length = | 90 | array->entries[index].length = 0; |
90 | array->entries[index].time = | 91 | array->entries[index].time = 0; |
91 | array->entries[index].ping_id = 0; | 92 | array->entries[index].ping_id = 0; |
92 | } | 93 | } |
93 | 94 | ||
94 | /* Free all the allocated memory in a Ping_Array. | 95 | /* Free all the allocated memory in a Ping_Array. |