Reduced number of realloc calls bs_list does

This commit is contained in:
Maxim Biro 2014-06-20 19:34:13 -04:00
parent 760333b907
commit a32d270330
4 changed files with 93 additions and 30 deletions

View File

@ -915,7 +915,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, uint16_t
memcpy(temp->public_key, public_key, crypto_box_PUBLICKEYBYTES); memcpy(temp->public_key, public_key, crypto_box_PUBLICKEYBYTES);
memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES); memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES);
bs_list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES); bs_list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES, 8);
return temp; return temp;
} }

View File

@ -63,7 +63,7 @@ static int find(const BS_LIST *list, const void *data)
//closest match is found if we move back to where we have already been //closest match is found if we move back to where we have already been
while (1) { while (1) {
int r = memcmp(data, list->data + list->size * i, list->size); int r = memcmp(data, list->data + list->element_size * i, list->element_size);
if (r == 0) { if (r == 0) {
return i; return i;
@ -105,14 +105,52 @@ static int find(const BS_LIST *list, const void *data)
} }
} }
/* Resized the list list
*
* return value:
* 1 : success
* 0 : failure
*/
static int resize(BS_LIST *list, uint32_t new_size)
{
void *p;
void bs_list_init(BS_LIST *list, uint32_t element_size) p = realloc(list->data, list->element_size * new_size);
if (!p) {
return 0;
} else {
list->data = p;
}
p = realloc(list->ids, sizeof(int) * new_size);
if (!p) {
return 0;
} else {
list->ids = p;
}
return 1;
}
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->n = 0;
list->size = element_size; list->element_size = element_size;
list->data = NULL; if (initial_capacity == 0) {
list->ids = NULL; list->data = NULL;
list->ids = NULL;
} else {
if (!resize(list, initial_capacity)) {
return 0;
}
}
list->capacity = initial_capacity;
return 1;
} }
void bs_list_free(BS_LIST *list) void bs_list_free(BS_LIST *list)
@ -147,28 +185,19 @@ int bs_list_add(BS_LIST *list, const void *data, int id)
i = ~i; i = ~i;
//increase the size of the arrays by one //increase the size of the arrays if needed
void *p; if (list->n == list->capacity) {
// 1.5 * n + 1
p = realloc(list->data, list->size * (list->n + 1)); const uint32_t new_capacity = list->n + list->n/2 + 1;
if (!resize(list, new_capacity)) {
if (!p) { return 0;
return 0; }
} else { list->capacity = new_capacity;
list->data = p;
}
p = realloc(list->ids, sizeof(int) * (list->n + 1));
if (!p) {
return 0;
} else {
list->ids = p;
} }
//insert data to element array //insert data to element array
memmove(list->data + (i + 1) * list->size, list->data + i * list->size, (list->n - i) * list->size); 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->size, data, list->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)); 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)
return 0; return 0;
} }
//decrease the size of the arrays if needed
if (list->n < list->capacity/2) {
const uint32_t new_capacity = list->capacity/2;
if (!resize(list, new_capacity)) {
return 0;
}
list->capacity = new_capacity;
}
list->n--; list->n--;
memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size); memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, (list->n - i) * list->element_size);
memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int)); memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int));
return 1; return 1;
} }
int bs_list_trim(BS_LIST *list)
{
if (!resize(list, list->n)) {
return 0;
}
list->capacity = list->n;
return 1;
}

View File

@ -32,13 +32,20 @@
typedef struct { typedef struct {
uint32_t n; //number of elements uint32_t n; //number of elements
uint32_t size; //size of the elements uint32_t capacity; //number of elements memory is allocated for
uint32_t element_size; //size of the elements
void *data; //array of elements void *data; //array of elements
int *ids; //array of element ids int *ids; //array of element ids
} BS_LIST; } BS_LIST;
/* Initialize a list, element_size is the size of the elements in the list */ /* Initialize a list, element_size is the size of the elements in the list and
void bs_list_init(BS_LIST *list, uint32_t element_size); * initial_capacity is the number of elements the memory will be initially allocated for
*
* return value:
* 1 : success
* 0 : failure
*/
int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity);
/* Free a list initiated with list_init */ /* Free a list initiated with list_init */
void bs_list_free(BS_LIST *list); void bs_list_free(BS_LIST *list);
@ -67,4 +74,12 @@ int bs_list_add(BS_LIST *list, const void *data, int id);
*/ */
int bs_list_remove(BS_LIST *list, const void *data, int id); int bs_list_remove(BS_LIST *list, const void *data, int id);
/* Removes the memory overhead
*
* return value:
* 1 : success
* 0 : failure
*/
int bs_list_trim(BS_LIST *list);
#endif #endif

View File

@ -2507,7 +2507,7 @@ Net_Crypto *new_net_crypto(DHT *dht)
networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp);
networking_registerhandler(dht->net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); networking_registerhandler(dht->net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp);
bs_list_init(&temp->ip_port_list, sizeof(IP_Port)); bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8);
return temp; return temp;
} }