aboutsummaryrefslogtreecommitdiff
path: root/lib/cbit/cqueue.c
blob: aebc26569a286b576b433ce673cb4eccc17dd163 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "circle.h"

void cqueue_realloc_internal(struct cqueue_internal *ci,
                             size_t new_capacity_bytes) {
    char *storage = (char *) malloc(new_capacity_bytes);
    char *new_write_ptr;
    if (c->write_ptr >= c->read_ptr) {
        size_t diff = c->write_ptr - c->read_ptr;
        memcpy(storage, c->read_ptr, diff);
        new_write_ptr = storage + diff;
    } else {
        size_t diff1 = c->end - c->read_ptr;
        size_t diff2 = c->write_ptr - c->start;
        memcpy(storage, c->read_ptr, diff1);
        char *tmp = storage + diff1;
        memcpy(tmp, c->start, diff2);
        new_write_ptr = tmp + diff2;
    }
    c->start = storage;
    c->end = storage + new_capacity_bytes;
    c->read_ptr = storage;
    c->write_ptr = new_write_ptr;
}