Software /
code /
prosody
Annotate
util-src/ringbuffer.c @ 7672:27f5db07bec9
mod_carbons: Fix inverted logic (Thanks Ge0rg)}
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 23 Sep 2016 16:26:09 +0200 |
parent | 7117:2b4432cc9c29 |
child | 7818:54669df178c2 |
rev | line source |
---|---|
7117
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 #include <stdlib.h> |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 #include <unistd.h> |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 #include <string.h> |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 #include <stdio.h> |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 #include <lua.h> |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 #include <lauxlib.h> |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 #define MIN(a, b) ((a)>(b)?(b):(a)) |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 #define MAX(a, b) ((a)>(b)?(a):(b)) |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 typedef struct { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 size_t rpos; /* read position */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 size_t wpos; /* write position */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 size_t alen; /* allocated size */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 size_t blen; /* current content size */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 char* buffer; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 } ringbuffer; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 char readchar(ringbuffer* b) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 b->blen--; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 return b->buffer[(b->rpos++) % b->alen]; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 void writechar(ringbuffer* b, char c) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 b->blen++; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 b->buffer[(b->wpos++) % b->alen] = c; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 /* make sure position counters stay within the allocation */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 void modpos(ringbuffer* b) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 b->rpos = b->rpos % b->alen; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 b->wpos = b->wpos % b->alen; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 int find(ringbuffer* b, const char* s, int l) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 size_t i, j; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 int m; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 if(b->rpos == b->wpos) { /* empty */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 return 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 for(i = 0; i <= b->blen - l; i++) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 if(b->buffer[(b->rpos + i) % b->alen] == *s) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 m = 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 for(j = 1; j < l; j++) |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 if(b->buffer[(b->rpos + i + j) % b->alen] != s[j]) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 m = 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 break; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 if(m) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 return i + l; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 return 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 int rb_find(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 size_t l, m; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 const char* s = luaL_checklstring(L, 2, &l); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 m = find(b, s, l); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 if(m > 0) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 lua_pushinteger(L, m); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 return 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 int rb_read(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 int r = luaL_checkinteger(L, 2); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 int peek = lua_toboolean(L, 3); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 if(r > b->blen) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 lua_pushnil(L); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 if((b->rpos + r) > b->alen) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos)); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 lua_concat(L, 2); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 } else { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 lua_pushlstring(L, &b->buffer[b->rpos], r); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 if(!peek) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 b->blen -= r; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 b->rpos += r; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 modpos(b); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 int rb_readuntil(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 size_t l, m; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 const char* s = luaL_checklstring(L, 2, &l); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 m = find(b, s, l); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 if(m > 0) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 lua_settop(L, 1); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 lua_pushinteger(L, m); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 return rb_read(L); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 return 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 int rb_write(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 size_t l, w = 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 const char* s = luaL_checklstring(L, 2, &l); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 /* Does `l` bytes fit? */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 if((l + b->blen) > b->alen) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 lua_pushnil(L); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 while(l-- > 0) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 writechar(b, *s++); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 w++; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 modpos(b); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 lua_pushinteger(L, w); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 int rb_tostring(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 lua_pushfstring(L, "ringbuffer: %p->%p %d/%d", b, b->buffer, b->blen, b->alen); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 int rb_length(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 lua_pushinteger(L, b->blen); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 int rb_size(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 lua_pushinteger(L, b->alen); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 int rb_free(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 lua_pushinteger(L, b->alen - b->blen); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 int rb_new(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE)); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 ringbuffer* b = lua_newuserdata(L, sizeof(ringbuffer)); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 b->rpos = 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 b->wpos = 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 b->alen = size; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 b->blen = 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 b->buffer = malloc(size); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 if(b->buffer == NULL) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 return 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 luaL_getmetatable(L, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 lua_setmetatable(L, -2); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 int rb_gc(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 if(b->buffer != NULL) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 free(b->buffer); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 return 0; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 int luaopen_util_ringbuffer(lua_State* L) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 if(luaL_newmetatable(L, "ringbuffer_mt")) { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 lua_pushcfunction(L, rb_tostring); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 lua_setfield(L, -2, "__tostring"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 lua_pushcfunction(L, rb_length); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 lua_setfield(L, -2, "__len"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 lua_pushcfunction(L, rb_gc); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 lua_setfield(L, -2, "__gc"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 lua_newtable(L); /* __index */ |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 { |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 lua_pushcfunction(L, rb_find); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 lua_setfield(L, -2, "find"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 lua_pushcfunction(L, rb_read); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 lua_setfield(L, -2, "read"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 lua_pushcfunction(L, rb_readuntil); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 lua_setfield(L, -2, "readuntil"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 lua_pushcfunction(L, rb_write); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 lua_setfield(L, -2, "write"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 lua_pushcfunction(L, rb_size); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 lua_setfield(L, -2, "size"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 lua_pushcfunction(L, rb_length); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 lua_setfield(L, -2, "length"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 lua_pushcfunction(L, rb_free); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 lua_setfield(L, -2, "free"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 lua_setfield(L, -2, "__index"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 } |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 lua_newtable(L); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 lua_pushcfunction(L, rb_new); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 lua_setfield(L, -2, "new"); |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 return 1; |
2b4432cc9c29
Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 } |