summaryrefslogtreecommitdiff
path: root/src/memchunk.c
blob: faee4508687c03d36d90f0219ce8f76d29b0e668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "memchunk.h"

void memchunk_make_writable(struct memchunk *c) {
    struct memblock *n;
    assert(c && c->memblock && c->memblock->ref >= 1);

    if (c->memblock->ref == 1)
        return;
    
    n = memblock_new(c->length);
    assert(n);
    memcpy(n->data, c->memblock->data+c->index, c->length);
    memblock_unref(c->memblock);
    c->memblock = n;
    c->index = 0;
}


struct mcalign {
    size_t base;
    struct memchunk chunk;
    uint8_t *buffer;
    size_t buffer_fill;
};

struct mcalign *mcalign_new(size_t base) {
    struct mcalign *m;
    assert(base);

    m = malloc(sizeof(struct mcalign));
    assert(m);
    m->base = base;
    m->chunk.memblock = NULL;
    m->chunk.length = m->chunk.index = 0;
    m->buffer = NULL;
    m->buffer_fill = 0;
    return m;
}

void mcalign_free(struct mcalign *m) {
    assert(m);

    free(m->buffer);
    
    if (m->chunk.memblock)
        memblock_unref(m->chunk.memblock);
    
    free(m);
}

void mcalign_push(struct mcalign *m, const struct memchunk *c) {
    assert(m && c && !m->chunk.memblock && c->memblock && c->length);

    m->chunk = *c;
    memblock_ref(m->chunk.memblock);
}

int mcalign_pop(struct mcalign *m, struct memchunk *c) {
    assert(m && c && m->base > m->buffer_fill);
    int ret;

    if (!m->chunk.memblock)
        return -1;

    if (m->buffer_fill) {
        size_t l = m->base - m->buffer_fill;
        if (l > m->chunk.length)
            l = m->chunk.length;
        assert(m->buffer && l);

        memcpy(m->buffer + m->buffer_fill, m->chunk.memblock->data + m->chunk.index, l);
        m->buffer_fill += l;
        m->chunk.index += l;
        m->chunk.length -= l;

        if (m->chunk.length == 0) {
            m->chunk.length = m->chunk.index = 0;
            memblock_unref(m->chunk.memblock);
            m->chunk.memblock = NULL;
        }

        assert(m->buffer_fill <= m->base);
        if (m->buffer_fill == m->base) {
            c->memblock = memblock_new_dynamic(m->buffer, m->base);
            assert(c->memblock);
            c->index = 0;
            c->length = m->base;
            m->buffer = NULL;
            m->buffer_fill = 0;

            return 0;
        }

        return -1;
    }

    m->buffer_fill = m->chunk.length % m->base;

    if (m->buffer_fill) {
        assert(!m->buffer);
        m->buffer = malloc(m->base);
        assert(m->buffer);
        m->chunk.length -= m->buffer_fill;
        memcpy(m->buffer, m->chunk.memblock->data + m->chunk.index + m->chunk.length, m->buffer_fill);
    }

    if (m->chunk.length) {
        *c = m->chunk;
        memblock_ref(c->memblock);
        ret = 0;
    } else
        ret = -1;
    
    m->chunk.length = m->chunk.index = 0;
    memblock_unref(m->chunk.memblock);
    m->chunk.memblock = NULL;

    return ret;
}