diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2016-05-17 15:54:16 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2016-05-17 15:54:49 -0400 |
commit | 120fc56d436a4b8791fc82389192a6ca2efdece5 (patch) | |
tree | af659109b5ad31431410e54f9fb1aa9234ebff28 /bftw.c | |
parent | 4f7ced52c09ce4e59bf74989d71991a7da168fac (diff) | |
download | bfs-120fc56d436a4b8791fc82389192a6ca2efdece5.tar.xz |
bftw: Use realloc() to grow the dirqueue.
Diffstat (limited to 'bftw.c')
-rw-r--r-- | bftw.c | 24 |
1 files changed, 11 insertions, 13 deletions
@@ -422,27 +422,25 @@ static int dirqueue_push(struct dirqueue *queue, struct dircache_entry *entry) { if (back == queue->front) { size_t old_size = queue->mask + 1; - struct dircache_entry **old_entries = queue->entries; - size_t new_size = 2*old_size; - struct dircache_entry **new_entries = malloc(new_size*sizeof(struct dircache_entry *)); - if (!new_entries) { + + struct dircache_entry **entries = realloc(queue->entries, new_size*sizeof(struct dircache_entry *)); + if (!entries) { return -1; } - size_t mid = old_size - back; - memcpy(new_entries, old_entries + back, mid*sizeof(struct dircache_entry *)); - memcpy(new_entries + mid, old_entries, back*sizeof(struct dircache_entry *)); - free(old_entries); + size_t old_front = queue->front; + size_t new_front = old_size + old_front; + size_t tail_size = old_size - old_front; - queue->entries = new_entries; + memcpy(entries + new_front, entries + old_front, tail_size*sizeof(struct dircache_entry *)); + + queue->entries = entries; queue->mask = new_size - 1; - queue->front = 0; - queue->back = old_size; - } else { - queue->back = back; + queue->front = new_front; } + queue->back = back; return 0; } |