-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.c
76 lines (69 loc) · 1.48 KB
/
buffer.c
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
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include "buffer.h"
#include "debug.h"
Buffer
buffer_init(size_t size)
{
Buffer b;
DBUG_ENTER("buffer_init");
assert((b = malloc(sizeof(struct buffer_tag) + size)));
b->len = 0;
b->avail = size;
DBUG_RETURN(b);
}
void
buffer_free(Buffer *pb)
{
DBUG_ENTER("buffer_free");
assert(pb);
if (*pb) free(*pb);
*pb = 0;
DBUG_VOID_RETURN;
}
ssize_t
buffer_read(Buffer b, int d)
{
ssize_t n, total;
DBUG_ENTER("buffer_read");
total = 0;
n = -1;
DBUG_PRINT("buffer", ("reading %d: %p:%u", d, &b->data[b->len], b->avail));
if (b->avail > 0 && (n = read(d, &b->data[b->len], b->avail)) > 0) {
DBUG_PRINT("io", (" read %4d", n));
b->len += n;
b->avail -= n;
total += n;
}
DBUG_PRINT("buffer", ("total %4d", total));
DBUG_RETURN(total ? total : n);
}
ssize_t
buffer_write(Buffer b, int d)
{
ssize_t n, total;
DBUG_ENTER("buffer_write");
total = 0;
n = -1;
DBUG_PRINT("buffer", ("writing %d: %p:%u", d, b->data, b->len));
if (b->len > 0 && (n = write(d, b->data, b->len)) > 0) {
DBUG_PRINT("io", ("wrote %4d", n));
buffer_consumed(b, n);
total += n;
}
DBUG_PRINT("buffer", ("total %4d", total));
DBUG_RETURN(total ? total : n);
}
size_t
buffer_consumed(Buffer b, size_t n)
{
DBUG_ENTER("buffer_consumed");
assert(0 < n && n <= b->len);
memmove(b->data, &b->data[n], b->len - n);
b->len -= n;
b->avail += n;
DBUG_RETURN(n);
}