-
Notifications
You must be signed in to change notification settings - Fork 9
/
buf.h
136 lines (109 loc) · 2.48 KB
/
buf.h
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
#ifndef __BUF_H__
#define __BUF_H__
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/uio.h>
/* TODO: add a sbuf_ctx_t and cbuf_ctx_t for allocator */
/* FIXME: cbuf is not well tested do not use */
/* should reset sbuf when off == len to save memory */
typedef struct sbuf { /* stream buffer */
int off; /* increase by write */
int len; /* increase by read */
int max; /* off < len <= max */
char *buf;
} sbuf_t;
typedef struct cbuf { /* chained buffer */
int off; /* not byte */
int len; /* not byte */
int max; /* off < len <= max */
sbuf_t *buf;
} cbuf_t;
int
sbuf_alloc(sbuf_t *sbuf, int max);
void
sbuf_release(sbuf_t *sbuf);
/*
* sbuf->off = 0;
* sbuf->len = 0;
* keep sbuf->max and sbuf->buf
*/
void
sbuf_reset(sbuf_t *sbuf);
char *
sbuf_detach(sbuf_t *sbuf);
/*
* free sbuf->buf
* sbuf->buf = buf;
* sbuf->max = len;
*/
void
sbuf_attach(sbuf_t *sbuf, char *buf, int len);
/*
* make buf->max >= buf->len + len
*/
int
sbuf_extend(sbuf_t *sbuf, int len);
int
sbuf_prepend(sbuf_t *sbuf, void *buf, int len);
int
sbuf_append(sbuf_t *sbuf, void *buf, int len);
/*
* send until empty the sbuf or will block or full
* return > 0 success
* return <= 0 fail
*/
ssize_t
sbuf_send(const int fd, sbuf_t *buf, ssize_t *snd);
/*
* recv until full the sbuf or will block
* return > 0 success
* return <= 0 fail
*/
ssize_t
sbuf_recv(const int fd, sbuf_t *buf, ssize_t *rcv);
/* TODO: add sbuf interface to cbuf */
int
cbuf_alloc(cbuf_t *cbuf, int max);
void
cbuf_release(cbuf_t *cbuf);
/* bytes offset */
long
cbuf_get_off(cbuf_t *cbuf);
void
cbuf_set_off(cbuf_t *cbuf, long off);
/* bytes length */
long
cbuf_get_len(cbuf_t *cbuf);
void
cbuf_set_len(cbuf_t *cbuf, long off);
/*
* bytes max
* and there is no set_max
*/
long
cbuf_get_max(cbuf_t *cbuf);
int
cbuf_extend(cbuf_t *cbuf, int len);
/*
* If type is 0, the offset is from start (0 -> len).
* If type is 1, the offset is from current location (off -> len).
* If type is 2, the offset is from end location (len -> max).
*/
void
cbuf_iovec(cbuf_t *cbuf, struct iovec *iov, int iovcnt, int type);
/*
* send until empty the cbuf or will block or full
* return > 0 success
* return <= 0 fail
*/
ssize_t
cbuf_send(const int fd, cbuf_t *buf, ssize_t *snd);
/*
* recv until full the cbuf or will block
* return > 0 success
* return <= 0 fail
*/
ssize_t
cbuf_recv(const int fd, cbuf_t *buf, ssize_t *rcv);
#endif /* __BUF_H__ */