summaryrefslogtreecommitdiffstats
path: root/src/memblockq.c
blob: b25adc65496a6e4371b9e4b0e1ec2132ba855eba (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#include "memblockq.h"

struct memblock_list {
    struct memblock_list *next;
    struct memchunk chunk;
};

struct memblockq {
    struct memblock_list *blocks, *blocks_tail;
    unsigned n_blocks;
    size_t total_length;
    size_t maxlength;
    size_t base;
    size_t prebuf;
};

struct memblockq* memblockq_new(size_t maxlength, size_t base, size_t prebuf) {
    struct memblockq* bq;
    assert(maxlength && base);
    
    bq = malloc(sizeof(struct memblockq));
    assert(bq);
    bq->blocks = bq->blocks_tail = 0;
    bq->n_blocks = 0;
    bq->total_length = 0;
    bq->base = base;
    bq->maxlength = ((maxlength+base-1)/base)*base;
    bq->prebuf = prebuf == (size_t) -1 ? bq->maxlength/2 : prebuf;
    
    if (bq->prebuf > bq->maxlength)
        bq->prebuf = bq->maxlength;
    
    assert(bq->maxlength >= base);
    return bq;
}

void memblockq_free(struct memblockq* bq) {
    struct memblock_list *l;
    assert(bq);

    while ((l = bq->blocks)) {
        bq->blocks = l->next;
        memblock_unref(l->chunk.memblock);
        free(l);
    }
    
    free(bq);
}

void memblockq_push(struct memblockq* bq, struct memchunk *chunk, size_t delta) {
    struct memblock_list *q;
    assert(bq && chunk && chunk->memblock && chunk->length);

    q = malloc(sizeof(struct memblock_list));
    assert(q);

    q->chunk = *chunk;
    memblock_ref(q->chunk.memblock);
    assert(q->chunk.index+q->chunk.length <= q->chunk.memblock->length);
    q->next = NULL;
    
    if (bq->blocks_tail)
        bq->blocks_tail->next = q;
    else
        bq->blocks = q;
    
    bq->blocks_tail = q;

    bq->n_blocks++;
    bq->total_length += chunk->length;

    memblockq_shorten(bq, bq->maxlength);
}

int memblockq_peek(struct memblockq* bq, struct memchunk *chunk) {
    assert(bq && chunk);

    if (!bq->blocks || bq->total_length < bq->prebuf)
        return -1;

    bq->prebuf = 0;

    *chunk = bq->blocks->chunk;
    memblock_ref(chunk->memblock);
    return 0;
}

int memblockq_pop(struct memblockq* bq, struct memchunk *chunk) {
    struct memblock_list *q;
    
    assert(bq && chunk);

    if (!bq->blocks || bq->total_length < bq->prebuf)
        return -1;

    bq->prebuf = 0;

    q = bq->blocks;
    bq->blocks = bq->blocks->next;

    *chunk = q->chunk;

    bq->n_blocks--;
    bq->total_length -= chunk->length;

    free(q);
    return 0;
}

void memblockq_drop(struct memblockq *bq, size_t length) {
    assert(bq);

    while (length > 0) {
        size_t l = length;
        assert(bq->blocks && bq->total_length >= length);
        
        if (l > bq->blocks->chunk.length)
            l = bq->blocks->chunk.length;
    
        bq->blocks->chunk.index += l;
        bq->blocks->chunk.length -= l;
        bq->total_length -= l;
        
        if (bq->blocks->chunk.length == 0) {
            struct memblock_list *q;
            
            q = bq->blocks;
            bq->blocks = bq->blocks->next;
            if (bq->blocks == NULL)
                bq->blocks_tail = NULL;
            memblock_unref(q->chunk.memblock);
            free(q);
            
            bq->n_blocks--;
        }

        length -= l;
    }
}

void memblockq_shorten(struct memblockq *bq, size_t length) {
    size_t l;
    assert(bq);

    if (bq->total_length <= length)
        return;

    fprintf(stderr, "Warning! memblockq_shorten()\n");
    
    l = bq->total_length - length;
    l /= bq->base;
    l *= bq->base;

    memblockq_drop(bq, l);
}


void memblockq_empty(struct memblockq *bq) {
    assert(bq);
    memblockq_shorten(bq, 0);
}

int memblockq_is_readable(struct memblockq *bq) {
    assert(bq);

    return bq->total_length >= bq->prebuf;
}

int memblockq_is_writable(struct memblockq *bq, size_t length) {
    assert(bq);

    assert(length <= bq->maxlength);
    return bq->total_length + length <= bq->maxlength;
}