summaryrefslogtreecommitdiffstats
path: root/src/memblockq.c
blob: 9a601c3ae8ae2f32e93dfd074591af9db204f0d2 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#include "memblockq.h"

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

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

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

    bq->measure_delay = 0;
    bq->delay = 0;
    
    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);

    if (bq->measure_delay)
        gettimeofday(&q->stamp, NULL);
    else
        timerclear(&q->stamp);

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

static uint32_t age(struct timeval *tv) {
    assert(tv);
    struct timeval now;
    uint32_t r;

    if (tv->tv_sec == 0)
        return 0;

    gettimeofday(&now, NULL);
    
    r = (now.tv_sec-tv->tv_sec) * 1000000;

    if (now.tv_usec >= tv->tv_usec)
        r += now.tv_usec - tv->tv_usec;
    else
        r -= tv->tv_usec - now.tv_usec;

    return r;
}

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;

        if (bq->measure_delay)
            bq->delay = age(&bq->blocks->stamp);
        
        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;
}

uint32_t memblockq_get_delay(struct memblockq *bq) {
    assert(bq);
    return bq->delay;
}

uint32_t memblockq_get_length(struct memblockq *bq) {
    assert(bq);
    return bq->total_length;
}

uint32_t memblockq_missing_to(struct memblockq *bq, size_t qlen) {
    assert(bq && qlen);

    if (bq->total_length >= qlen)
        return 0;

    return qlen - bq->total_length;
}