Skip to content

Commit a32d270

Browse files
committed
Reduced number of realloc calls bs_list does
1 parent 760333b commit a32d270

File tree

4 files changed

+93
-30
lines changed

4 files changed

+93
-30
lines changed

toxcore/TCP_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, uint16_t
915915
memcpy(temp->public_key, public_key, crypto_box_PUBLICKEYBYTES);
916916
memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES);
917917

918-
bs_list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES);
918+
bs_list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES, 8);
919919

920920
return temp;
921921
}

toxcore/list.c

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int find(const BS_LIST *list, const void *data)
6363
//closest match is found if we move back to where we have already been
6464

6565
while (1) {
66-
int r = memcmp(data, list->data + list->size * i, list->size);
66+
int r = memcmp(data, list->data + list->element_size * i, list->element_size);
6767

6868
if (r == 0) {
6969
return i;
@@ -105,14 +105,52 @@ static int find(const BS_LIST *list, const void *data)
105105
}
106106
}
107107

108+
/* Resized the list list
109+
*
110+
* return value:
111+
* 1 : success
112+
* 0 : failure
113+
*/
114+
static int resize(BS_LIST *list, uint32_t new_size)
115+
{
116+
void *p;
117+
118+
p = realloc(list->data, list->element_size * new_size);
119+
120+
if (!p) {
121+
return 0;
122+
} else {
123+
list->data = p;
124+
}
125+
126+
p = realloc(list->ids, sizeof(int) * new_size);
108127

109-
void bs_list_init(BS_LIST *list, uint32_t element_size)
128+
if (!p) {
129+
return 0;
130+
} else {
131+
list->ids = p;
132+
}
133+
134+
return 1;
135+
}
136+
137+
138+
int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity)
110139
{
111140
//set initial values
112141
list->n = 0;
113-
list->size = element_size;
114-
list->data = NULL;
115-
list->ids = NULL;
142+
list->element_size = element_size;
143+
if (initial_capacity == 0) {
144+
list->data = NULL;
145+
list->ids = NULL;
146+
} else {
147+
if (!resize(list, initial_capacity)) {
148+
return 0;
149+
}
150+
}
151+
list->capacity = initial_capacity;
152+
153+
return 1;
116154
}
117155

118156
void bs_list_free(BS_LIST *list)
@@ -147,28 +185,19 @@ int bs_list_add(BS_LIST *list, const void *data, int id)
147185

148186
i = ~i;
149187

150-
//increase the size of the arrays by one
151-
void *p;
152-
153-
p = realloc(list->data, list->size * (list->n + 1));
154-
155-
if (!p) {
156-
return 0;
157-
} else {
158-
list->data = p;
159-
}
160-
161-
p = realloc(list->ids, sizeof(int) * (list->n + 1));
162-
163-
if (!p) {
164-
return 0;
165-
} else {
166-
list->ids = p;
188+
//increase the size of the arrays if needed
189+
if (list->n == list->capacity) {
190+
// 1.5 * n + 1
191+
const uint32_t new_capacity = list->n + list->n/2 + 1;
192+
if (!resize(list, new_capacity)) {
193+
return 0;
194+
}
195+
list->capacity = new_capacity;
167196
}
168197

169198
//insert data to element array
170-
memmove(list->data + (i + 1) * list->size, list->data + i * list->size, (list->n - i) * list->size);
171-
memcpy(list->data + i * list->size, data, list->size);
199+
memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, (list->n - i) * list->element_size);
200+
memcpy(list->data + i * list->element_size, data, list->element_size);
172201

173202
//insert id to id array
174203
memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int));
@@ -193,10 +222,29 @@ int bs_list_remove(BS_LIST *list, const void *data, int id)
193222
return 0;
194223
}
195224

225+
//decrease the size of the arrays if needed
226+
if (list->n < list->capacity/2) {
227+
const uint32_t new_capacity = list->capacity/2;
228+
if (!resize(list, new_capacity)) {
229+
return 0;
230+
}
231+
list->capacity = new_capacity;
232+
}
233+
196234
list->n--;
197235

198-
memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size);
236+
memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, (list->n - i) * list->element_size);
199237
memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int));
200238

201239
return 1;
202240
}
241+
242+
int bs_list_trim(BS_LIST *list)
243+
{
244+
if (!resize(list, list->n)) {
245+
return 0;
246+
}
247+
248+
list->capacity = list->n;
249+
return 1;
250+
}

toxcore/list.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@
3232

3333
typedef struct {
3434
uint32_t n; //number of elements
35-
uint32_t size; //size of the elements
35+
uint32_t capacity; //number of elements memory is allocated for
36+
uint32_t element_size; //size of the elements
3637
void *data; //array of elements
3738
int *ids; //array of element ids
3839
} BS_LIST;
3940

40-
/* Initialize a list, element_size is the size of the elements in the list */
41-
void bs_list_init(BS_LIST *list, uint32_t element_size);
41+
/* Initialize a list, element_size is the size of the elements in the list and
42+
* initial_capacity is the number of elements the memory will be initially allocated for
43+
*
44+
* return value:
45+
* 1 : success
46+
* 0 : failure
47+
*/
48+
int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity);
4249

4350
/* Free a list initiated with list_init */
4451
void bs_list_free(BS_LIST *list);
@@ -67,4 +74,12 @@ int bs_list_add(BS_LIST *list, const void *data, int id);
6774
*/
6875
int bs_list_remove(BS_LIST *list, const void *data, int id);
6976

77+
/* Removes the memory overhead
78+
*
79+
* return value:
80+
* 1 : success
81+
* 0 : failure
82+
*/
83+
int bs_list_trim(BS_LIST *list);
84+
7085
#endif

toxcore/net_crypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,7 @@ Net_Crypto *new_net_crypto(DHT *dht)
25072507
networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp);
25082508
networking_registerhandler(dht->net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp);
25092509

2510-
bs_list_init(&temp->ip_port_list, sizeof(IP_Port));
2510+
bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8);
25112511
return temp;
25122512
}
25132513

0 commit comments

Comments
 (0)