Memory cleanup

This commit is contained in:
John Jones 2017-04-17 11:57:37 -05:00
parent f3de55999f
commit caf02463c6
8 changed files with 42 additions and 3 deletions

View file

@ -7,6 +7,7 @@
int socket_open4();
int socket_bind4(int s, uint32_t ip, uint16_t port);
int socket_bind4_reuse(int s, uint32_t ip, uint16_t port);
int socket_read_select4(int socket_fd, int num_seconds);
int socket_accept4(int s, uint32_t *ip, uint16_t *port);
int socket_local4(int s, uint32_t *ip, uint16_t *port);
int socket_connect4(int s, uint32_t ip, uint16_t port);

View file

@ -26,6 +26,8 @@ void libp2p_logger_init();
*/
int libp2p_logger_initialized();
int libp2p_logger_free();
/**
* Log a message to the console
* @param area the class it is coming from

View file

@ -41,6 +41,19 @@ int socket_bind4_reuse(int s, uint32_t ip, uint16_t port)
return socket_bind4(s, ip, port);
}
int socket_read_select4(int socket_fd, int num_seconds) {
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(socket_fd, &rfds);
tv.tv_sec = num_seconds;
tv.tv_usec = 0;
return select(socket_fd +1, &rfds, NULL, NULL, &tv);
}
/* Accept a connection in a socket and return ip and port of
* remote connection at pointers passed as parameters.
*/

View file

@ -34,6 +34,7 @@ struct Libp2pPeer* libp2p_peer_new_from_multiaddress(const struct MultiAddress*
out->id_size = strlen(id) + 1;
out->id = malloc(out->id_size);
strcpy(out->id, id);
free(id);
}
out->addr_head = libp2p_utils_linked_list_new();
out->addr_head->item = multiaddress_copy(in);
@ -100,6 +101,10 @@ void libp2p_peer_free(struct Libp2pPeer* in) {
if (in != NULL) {
if (in->id != NULL)
free(in->id);
if (in->connection != NULL) {
libp2p_net_multistream_stream_free(in->connection);
in->connection = NULL;
}
// free the memory in the linked list
struct Libp2pLinkedList* current = in->addr_head;
while (current != NULL) {

View file

@ -33,7 +33,8 @@ void libp2p_message_free(struct Libp2pMessage* in) {
struct Libp2pLinkedList* next = NULL;
while (current != NULL) {
next = current->next;
libp2p_peer_free(current->item);
struct Libp2pPeer* peer = (struct Libp2pPeer*)current->item;
libp2p_peer_free(peer);
current->item = NULL;
libp2p_utils_linked_list_free(current);
current = next;
@ -43,7 +44,8 @@ void libp2p_message_free(struct Libp2pMessage* in) {
current = in->provider_peer_head;
while (current != NULL) {
next = current->next;
libp2p_peer_free(current->item);
struct Libp2pPeer* peer = (struct Libp2pPeer*)current->item;
libp2p_peer_free(peer);
current->item = NULL;
libp2p_utils_linked_list_free(current);
current = next;

View file

@ -184,7 +184,10 @@ int libp2p_routing_dht_handle_add_provider(struct SessionContext* session, struc
char new_string[255];
multiaddress_get_ip_address(session->default_stream->address, &ip);
int port = multiaddress_get_ip_port(peer_ma);
sprintf(new_string, "/ip4/%s/tcp/%d/ipfs/%s", ip, port, peer->id);
char* peer_id = multiaddress_get_peer_id(peer_ma);
sprintf(new_string, "/ip4/%s/tcp/%d/ipfs/%s", ip, port, peer_id);
free(ip);
free(peer_id);
struct MultiAddress* new_ma = multiaddress_new_from_string(new_string);
if (new_ma == NULL)
goto exit;
@ -359,5 +362,9 @@ int libp2p_routing_dht_handle_message(struct SessionContext* session, struct Pee
free(buffer);
if (result_buffer != NULL)
free(result_buffer);
/* JMJ Debugging
if (message != NULL)
libp2p_message_free(message);
*/
return retVal;
}

View file

@ -31,6 +31,14 @@ int libp2p_logger_initialized() {
return 1;
}
int libp2p_logger_free() {
for(int i = 0; i < logger_classes->total; i++) {
free(libp2p_utils_vector_get(logger_classes, i));
}
libp2p_utils_vector_free(logger_classes);
return 1;
}
/***
* Add a class to watch for logging messages
* @param str the class name to watch

View file

@ -76,4 +76,5 @@ void libp2p_utils_vector_delete(struct Libp2pVector *v, int index)
void libp2p_utils_vector_free(struct Libp2pVector *v)
{
free(v->items);
free(v);
}