11 #define MIN(a, b) ((a)>(b)?(b):(a))
12 #define MAX(a, b) ((a)>(b)?(a):(b))
15 size_t rpos; /* read position */
16 size_t wpos; /* write position */
17 size_t alen; /* allocated size */
18 size_t blen; /* current content size */
22 char readchar(ringbuffer* b) {
24 return b->buffer[(b->rpos++) % b->alen];
27 void writechar(ringbuffer* b, char c) {
29 b->buffer[(b->wpos++) % b->alen] = c;
32 /* make sure position counters stay within the allocation */
33 void modpos(ringbuffer* b) {
34 b->rpos = b->rpos % b->alen;
35 b->wpos = b->wpos % b->alen;
38 int find(ringbuffer* b, const char* s, int l) {
42 if(b->rpos == b->wpos) { /* empty */
46 for(i = 0; i <= b->blen - l; i++) {
47 if(b->buffer[(b->rpos + i) % b->alen] == *s) {
50 for(j = 1; j < l; j++)
51 if(b->buffer[(b->rpos + i + j) % b->alen] != s[j]) {
65 int rb_find(lua_State* L) {
67 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
68 const char* s = luaL_checklstring(L, 2, &l);
72 lua_pushinteger(L, m);
80 int rb_read(lua_State* L) {
81 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
82 int r = luaL_checkinteger(L, 2);
83 int peek = lua_toboolean(L, 3);
90 if((b->rpos + r) > b->alen) {
91 lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos);
92 lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos));
95 lua_pushlstring(L, &b->buffer[b->rpos], r);
108 int rb_readuntil(lua_State* L) {
110 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
111 const char* s = luaL_checklstring(L, 2, &l);
116 lua_pushinteger(L, m);
123 int rb_write(lua_State* L) {
125 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
126 const char* s = luaL_checklstring(L, 2, &l);
128 /* Does `l` bytes fit? */
129 if((l + b->blen) > b->alen) {
141 lua_pushinteger(L, w);
146 int rb_tostring(lua_State* L) {
147 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
148 lua_pushfstring(L, "ringbuffer: %p->%p %d/%d", b, b->buffer, b->blen, b->alen);
152 int rb_length(lua_State* L) {
153 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
154 lua_pushinteger(L, b->blen);
158 int rb_size(lua_State* L) {
159 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
160 lua_pushinteger(L, b->alen);
164 int rb_free(lua_State* L) {
165 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
166 lua_pushinteger(L, b->alen - b->blen);
170 int rb_new(lua_State* L) {
171 size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE));
172 ringbuffer* b = lua_newuserdata(L, sizeof(ringbuffer));
177 b->buffer = malloc(size);
179 if(b->buffer == NULL) {
183 luaL_getmetatable(L, "ringbuffer_mt");
184 lua_setmetatable(L, -2);
189 int rb_gc(lua_State* L) {
190 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
192 if(b->buffer != NULL) {
199 int luaopen_util_ringbuffer(lua_State* L) {
200 if(luaL_newmetatable(L, "ringbuffer_mt")) {
201 lua_pushcfunction(L, rb_tostring);
202 lua_setfield(L, -2, "__tostring");
203 lua_pushcfunction(L, rb_length);
204 lua_setfield(L, -2, "__len");
205 lua_pushcfunction(L, rb_gc);
206 lua_setfield(L, -2, "__gc");
208 lua_newtable(L); /* __index */
210 lua_pushcfunction(L, rb_find);
211 lua_setfield(L, -2, "find");
212 lua_pushcfunction(L, rb_read);
213 lua_setfield(L, -2, "read");
214 lua_pushcfunction(L, rb_readuntil);
215 lua_setfield(L, -2, "readuntil");
216 lua_pushcfunction(L, rb_write);
217 lua_setfield(L, -2, "write");
218 lua_pushcfunction(L, rb_size);
219 lua_setfield(L, -2, "size");
220 lua_pushcfunction(L, rb_length);
221 lua_setfield(L, -2, "length");
222 lua_pushcfunction(L, rb_free);
223 lua_setfield(L, -2, "free");
225 lua_setfield(L, -2, "__index");
229 lua_pushcfunction(L, rb_new);
230 lua_setfield(L, -2, "new");