From 90bf68b723f380756964597e6318f28d0f4ae055 Mon Sep 17 00:00:00 2001 From: Joel Lehtonen Date: Sat, 22 Mar 2014 11:56:57 +0200 Subject: [PATCH] Use elegant Glib allocation routines instead of stdlib. Closes #6. --- src/bitcoin.c | 7 +++---- src/incoming_node.c | 6 ++---- src/main.c | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/bitcoin.c b/src/bitcoin.c index 52ccc58..54f023a 100644 --- a/src/bitcoin.c +++ b/src/bitcoin.c @@ -110,7 +110,7 @@ struct bitcoin_storage bitcoin_new_storage() // st.inv owns the key. Items in inventory must NOT be removed // before they are dequeued from send queue! struct bitcoin_storage st; - st.inv = g_hash_table_new_full(&dhash_hash,dhash_eq,free,free); + st.inv = g_hash_table_new_full(&dhash_hash,dhash_eq,g_free,g_free); st.send_queue = g_sequence_new(NULL); return st; } @@ -118,13 +118,12 @@ struct bitcoin_storage bitcoin_new_storage() bool bitcoin_inv_insert(struct bitcoin_storage const *st, struct msg *const m) { // Allocate buffer for key and calculate hash of a message - guchar* key = malloc(SHA256_DIGEST_LENGTH); - if (key == NULL) errx(5,"Memory allocation failed"); + guchar* key = g_malloc(SHA256_DIGEST_LENGTH); bitcoin_inv_hash_buf(m, key); // If key is already stored, do not replace old one if (g_hash_table_contains(st->inv,key)) { - free(key); + g_free(key); return false; } diff --git a/src/incoming_node.c b/src/incoming_node.c index fbd8ec9..80af0b4 100644 --- a/src/incoming_node.c +++ b/src/incoming_node.c @@ -27,8 +27,7 @@ void incoming_node_data(const int fd, struct bitcoin_storage *const st) // Reallocate buffer only if it is too small. if (buf_allocated < buf_pos+buf_left) { buf_allocated = buf_pos+buf_left; - buf = realloc(buf,buf_allocated); - if (buf == NULL) errx(5,"Memory allocation failed"); + buf = g_realloc(buf,buf_allocated); } const int got = read(fd,(void*)buf+buf_pos,buf_left); @@ -101,8 +100,7 @@ void incoming_node_data(const int fd, struct bitcoin_storage *const st) case BLOCK: // Free some memory if the buffer is larger than contents if (buf_allocated != buf_pos) { - buf = realloc(buf,buf_pos); - if (buf == NULL) errx(5,"Memory compaction failed"); + buf = g_realloc(buf,buf_pos); } // Upadate height diff --git a/src/main.c b/src/main.c index 00f67c3..7aabff6 100644 --- a/src/main.c +++ b/src/main.c @@ -141,7 +141,7 @@ void serial(const int devfd, struct bitcoin_storage *const st) const int max_buffer_len = 2+2*(1+72+1+m->length); // Reallocate buffer if (buf_allocated < max_buffer_len) { - buf_start = g_renew(guint8,buf_start,max_buffer_len); + buf_start = g_realloc(buf_start,max_buffer_len); buf_allocated = max_buffer_len; }