Skip to content

Commit

Permalink
Use elegant Glib allocation routines instead of stdlib. Closes #6.
Browse files Browse the repository at this point in the history
  • Loading branch information
zouppen committed Mar 22, 2014
1 parent 3396ad6 commit 90bf68b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/bitcoin.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,20 @@ 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;
}

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;
}

Expand Down
6 changes: 2 additions & 4 deletions src/incoming_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 90bf68b

Please sign in to comment.