-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.h
More file actions
112 lines (89 loc) · 1.84 KB
/
buffer.h
File metadata and controls
112 lines (89 loc) · 1.84 KB
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
#ifndef BUFFER_H
#define BUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_BS 1024
struct buffer {
char *data;
size_t len;
size_t wr_pos;
size_t rd_pos;
};
static inline char * buffer_data(struct buffer *b)
{
return &b->data[b->rd_pos];
}
static inline size_t buffer_len(struct buffer *b)
{
return b->wr_pos - b->rd_pos;
}
static inline int buffer_resize(struct buffer *b, size_t newlen)
{
char *d = (char *)realloc(b->data, newlen);
if (d == NULL)
return -1;
b->data = d;
b->len = newlen;
return 0;
}
static inline int buffer_alloc(struct buffer *b, size_t reqlen)
{
int rc = 0;
if (b->wr_pos + reqlen >= b->len)
rc = buffer_resize(b, b->len + reqlen + BUF_BS);
return rc;
}
static inline void buffer_append(struct buffer *b, char *data, size_t len)
{
if (!buffer_alloc(b, len)) {
memcpy(&b->data[b->wr_pos], data, len);
b->wr_pos += len;
}
}
static inline void buffer_consume(struct buffer *b, size_t bytes)
{
b->rd_pos += bytes;
}
static inline void buffer_dump(struct buffer *b)
{
fprintf(stderr, "data %p len %zu rd %zu wr %zu\n",
b->data, b->len, b->rd_pos, b->wr_pos);
}
static inline int buffer_flush(struct buffer *b)
{
const size_t newlen = b->len - b->rd_pos;
const size_t newpos = b->wr_pos - b->rd_pos;
if (b->rd_pos == 0)
return 0;
if (newpos) {
char *new_data = (char *)malloc(newlen);
if (new_data == NULL)
return -1;
memcpy(new_data, &b->data[b->rd_pos], newpos);
free(b->data);
b->data = new_data;
} else {
b->data = (char *)realloc(b->data, newlen);
}
b->len = newlen;
b->wr_pos = newpos;
b->rd_pos = 0;
return 0;
}
static inline void buffer_init(struct buffer *b)
{
memset(b, 0, sizeof(*b));
}
static inline void buffer_free(struct buffer *b)
{
free(b->data);
buffer_init(b);
}
#ifdef __cplusplus
}
#endif
#endif