This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.c
211 lines (186 loc) · 4.06 KB
/
ring.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
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
/*
* rocks/ring.c
*
* A ring buffer.
*
* Copyright (C) 2001 Victor Zandy
* See COPYING for distribution terms.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
/* TODO:
Use unsigned long.
Name routines consistently
*/
#include "ring.h"
#include "log.h"
#include "rs.h" /* rs_xwrite, rs_xread */
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
struct _ring{
char *buf; /* ring buffer */
unsigned size; /* capacity of ring */
unsigned nbytes; /* number of bytes in the ring */
char *hd; /* pointer to next byte to be removed */
unsigned seq; /* sequence number of hd byte */
};
ring_t
rs_new_ring(unsigned size)
{
ring_t ring;
ring = malloc(sizeof(struct _ring));
if (! ring)
return NULL;
ring->buf = malloc(size);
if (! ring->buf) {
free(ring);
return NULL;
}
ring->size = size;
ring->nbytes = 0;
ring->hd = ring->buf;
ring->seq = 0;
return ring;
}
int
rs_ring_save(ring_t ring, int fd)
{
if (0 > rs_xwrite(fd, ring, sizeof(*ring)))
return -1;
if (0 > rs_xwrite(fd, ring->buf, ring->size))
return -1;
return 0;
}
ring_t
rs_ring_restore(int fd)
{
char *buf;
struct _ring r;
ring_t ring;
if (0 > rs_xread(fd, &r, sizeof(r), 0))
return NULL;
ring = rs_new_ring(r.size);
if (!ring)
return NULL;
buf = ring->buf;
*ring = r;
ring->buf = buf;
ring->hd = buf + (r.hd - r.buf);
if (0 > rs_xread(fd, ring->buf, ring->size, 0))
return NULL;
return ring;
}
void
rs_free_ring(ring_t ring)
{
if (!ring)
return;
if (ring->buf)
free(ring->buf);
free(ring);
}
/* FIXME: Must this be so complicated? */
void
rs_push_ring(ring_t ring, void *p, unsigned nbytes)
{
unsigned hd, tl; /* ring offsets */
unsigned chunk1, chunk2; /* chunk sizes */
int nonempty; /* data in the ring when we started? */
unsigned x;
if (nbytes == 0)
return;
if (nbytes > ring->size) {
p += nbytes - ring->size;
nbytes = ring->size;
}
nonempty = ring->nbytes;
hd = ring->hd - ring->buf;
tl = (hd + ring->nbytes) % ring->size;
chunk1 = MIN(ring->size - tl, nbytes); /* from tl to end of buffer */
chunk2 = nbytes - chunk1; /* from start of buffer to last byte */
/* Append data after the tail, wrapping to start of buffer if
necessary. */
memcpy(ring->buf + tl, p, chunk1);
if (chunk2)
memcpy(ring->buf, p + chunk1, chunk2);
ring->nbytes = MIN(ring->size, ring->nbytes + nbytes);
/* Handle wrap past the head */
x = (tl + nbytes) % ring->size; /* pointer to end of new data */
if (hd == tl && nonempty) {
/* Full buffer */
ring->hd = ring->buf + ((hd + nbytes) % ring->size);
ring->seq += nbytes;
} else if (hd < tl && x > hd && x < tl) {
ring->hd = ring->buf + ((x + 1) % ring->size);
ring->seq += x - hd;
} else if (hd > tl && (x > hd || x < tl)) {
ring->hd = ring->buf + ((x + 1) % ring->size);
if (x > hd)
ring->seq += x - hd;
else
ring->seq += ring->size - (hd - x);
}
}
/* Advance hd until its sequence number is equal to NEWSEQ. */
void
rs_set_ring_seq(ring_t ring, unsigned newseq)
{
unsigned hd;
unsigned x;
if (ring->seq == newseq)
return;
assert(newseq >= ring->seq);
assert(newseq <= ring->seq + ring->nbytes);
x = newseq - ring->seq;
hd = ring->hd - ring->buf;
hd = (hd + x) % ring->size;
ring->hd = ring->buf + hd;
ring->seq = newseq;
ring->nbytes -= x;
}
void
rs_pop_ring(ring_t ring, unsigned n)
{
rs_set_ring_seq(ring, n+ring->seq);
}
unsigned
rs_ring_size(ring_t ring)
{
return ring->size;
}
unsigned
rs_ring_nbytes(ring_t ring)
{
return ring->nbytes;
}
unsigned
rs_ring_seq(ring_t ring)
{
return ring->seq;
}
void *
rs_ring_data(ring_t ring)
{
if (! rs_ring_grow(ring, 0))
return NULL;
/* grow rotates bytes to beginning of data */
return ring->buf;
}
void *
rs_ring_grow(ring_t ring, unsigned growth)
{
char *p;
unsigned len;
p = malloc(ring->size + growth);
if (! p)
return NULL;
len = ring->size - (ring->hd - ring->buf);
memcpy(p, ring->hd, len);
memcpy(p + len, ring->buf, ring->size - len);
free(ring->buf);
ring->hd = ring->buf = p;
ring->size += growth;
return ring->buf;
}