Comparison

util-src/ringbuffer.c @ 7889:b8d694646597

util-src/*.c: Attach pointer * to name instead of type
author Kim Alvefur <zash@zash.se>
date Sun, 12 Feb 2017 16:42:29 +0100
parent 7888:74187ee6ed55
child 7941:8067828e7e40
comparison
equal deleted inserted replaced
7888:74187ee6ed55 7889:b8d694646597
13 size_t alen; /* allocated size */ 13 size_t alen; /* allocated size */
14 size_t blen; /* current content size */ 14 size_t blen; /* current content size */
15 char buffer[]; 15 char buffer[];
16 } ringbuffer; 16 } ringbuffer;
17 17
18 char readchar(ringbuffer* b) { 18 char readchar(ringbuffer *b) {
19 b->blen--; 19 b->blen--;
20 return b->buffer[(b->rpos++) % b->alen]; 20 return b->buffer[(b->rpos++) % b->alen];
21 } 21 }
22 22
23 void writechar(ringbuffer* b, char c) { 23 void writechar(ringbuffer *b, char c) {
24 b->blen++; 24 b->blen++;
25 b->buffer[(b->wpos++) % b->alen] = c; 25 b->buffer[(b->wpos++) % b->alen] = c;
26 } 26 }
27 27
28 /* make sure position counters stay within the allocation */ 28 /* make sure position counters stay within the allocation */
29 void modpos(ringbuffer* b) { 29 void modpos(ringbuffer *b) {
30 b->rpos = b->rpos % b->alen; 30 b->rpos = b->rpos % b->alen;
31 b->wpos = b->wpos % b->alen; 31 b->wpos = b->wpos % b->alen;
32 } 32 }
33 33
34 int find(ringbuffer* b, const char* s, int l) { 34 int find(ringbuffer *b, const char *s, int l) {
35 size_t i, j; 35 size_t i, j;
36 int m; 36 int m;
37 37
38 if(b->rpos == b->wpos) { /* empty */ 38 if(b->rpos == b->wpos) { /* empty */
39 return 0; 39 return 0;
56 } 56 }
57 57
58 return 0; 58 return 0;
59 } 59 }
60 60
61 int rb_find(lua_State* L) { 61 int rb_find(lua_State *L) {
62 size_t l, m; 62 size_t l, m;
63 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 63 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
64 const char* s = luaL_checklstring(L, 2, &l); 64 const char *s = luaL_checklstring(L, 2, &l);
65 m = find(b, s, l); 65 m = find(b, s, l);
66 66
67 if(m > 0) { 67 if(m > 0) {
68 lua_pushinteger(L, m); 68 lua_pushinteger(L, m);
69 return 1; 69 return 1;
70 } 70 }
71 71
72 return 0; 72 return 0;
73 } 73 }
74 74
75 int rb_read(lua_State* L) { 75 int rb_read(lua_State *L) {
76 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 76 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
77 int r = luaL_checkinteger(L, 2); 77 int r = luaL_checkinteger(L, 2);
78 int peek = lua_toboolean(L, 3); 78 int peek = lua_toboolean(L, 3);
79 79
80 if(r > b->blen) { 80 if(r > b->blen) {
81 lua_pushnil(L); 81 lua_pushnil(L);
97 } 97 }
98 98
99 return 1; 99 return 1;
100 } 100 }
101 101
102 int rb_readuntil(lua_State* L) { 102 int rb_readuntil(lua_State *L) {
103 size_t l, m; 103 size_t l, m;
104 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 104 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
105 const char* s = luaL_checklstring(L, 2, &l); 105 const char *s = luaL_checklstring(L, 2, &l);
106 m = find(b, s, l); 106 m = find(b, s, l);
107 107
108 if(m > 0) { 108 if(m > 0) {
109 lua_settop(L, 1); 109 lua_settop(L, 1);
110 lua_pushinteger(L, m); 110 lua_pushinteger(L, m);
112 } 112 }
113 113
114 return 0; 114 return 0;
115 } 115 }
116 116
117 int rb_write(lua_State* L) { 117 int rb_write(lua_State *L) {
118 size_t l, w = 0; 118 size_t l, w = 0;
119 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 119 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
120 const char* s = luaL_checklstring(L, 2, &l); 120 const char *s = luaL_checklstring(L, 2, &l);
121 121
122 /* Does `l` bytes fit? */ 122 /* Does `l` bytes fit? */
123 if((l + b->blen) > b->alen) { 123 if((l + b->blen) > b->alen) {
124 lua_pushnil(L); 124 lua_pushnil(L);
125 return 1; 125 return 1;
135 lua_pushinteger(L, w); 135 lua_pushinteger(L, w);
136 136
137 return 1; 137 return 1;
138 } 138 }
139 139
140 int rb_tostring(lua_State* L) { 140 int rb_tostring(lua_State *L) {
141 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 141 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
142 lua_pushfstring(L, "ringbuffer: %p %d/%d", b, b->blen, b->alen); 142 lua_pushfstring(L, "ringbuffer: %p %d/%d", b, b->blen, b->alen);
143 return 1; 143 return 1;
144 } 144 }
145 145
146 int rb_length(lua_State* L) { 146 int rb_length(lua_State *L) {
147 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 147 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
148 lua_pushinteger(L, b->blen); 148 lua_pushinteger(L, b->blen);
149 return 1; 149 return 1;
150 } 150 }
151 151
152 int rb_size(lua_State* L) { 152 int rb_size(lua_State *L) {
153 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 153 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
154 lua_pushinteger(L, b->alen); 154 lua_pushinteger(L, b->alen);
155 return 1; 155 return 1;
156 } 156 }
157 157
158 int rb_free(lua_State* L) { 158 int rb_free(lua_State *L) {
159 ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); 159 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
160 lua_pushinteger(L, b->alen - b->blen); 160 lua_pushinteger(L, b->alen - b->blen);
161 return 1; 161 return 1;
162 } 162 }
163 163
164 int rb_new(lua_State* L) { 164 int rb_new(lua_State *L) {
165 size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE)); 165 size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE));
166 ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size); 166 ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size);
167 167
168 b->rpos = 0; 168 b->rpos = 0;
169 b->wpos = 0; 169 b->wpos = 0;
174 lua_setmetatable(L, -2); 174 lua_setmetatable(L, -2);
175 175
176 return 1; 176 return 1;
177 } 177 }
178 178
179 int luaopen_util_ringbuffer(lua_State* L) { 179 int luaopen_util_ringbuffer(lua_State *L) {
180 #if (LUA_VERSION_NUM > 501) 180 #if (LUA_VERSION_NUM > 501)
181 luaL_checkversion(L); 181 luaL_checkversion(L);
182 #endif 182 #endif
183
183 if(luaL_newmetatable(L, "ringbuffer_mt")) { 184 if(luaL_newmetatable(L, "ringbuffer_mt")) {
184 lua_pushcfunction(L, rb_tostring); 185 lua_pushcfunction(L, rb_tostring);
185 lua_setfield(L, -2, "__tostring"); 186 lua_setfield(L, -2, "__tostring");
186 lua_pushcfunction(L, rb_length); 187 lua_pushcfunction(L, rb_length);
187 lua_setfield(L, -2, "__len"); 188 lua_setfield(L, -2, "__len");