Annotate

util-src/ringbuffer.c @ 8757:861a7d6c12d8

mod_http_files: Correct cache insertion (fixes #1130)
author Kim Alvefur <zash@zash.se>
date Tue, 17 Apr 2018 18:40:57 +0200
parent 7969:1c6a07606309
child 8543:0e1d8f2f02bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 #include <stdlib.h>
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 #include <unistd.h>
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 #include <string.h>
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 #include <stdio.h>
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 #include <lua.h>
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 #include <lauxlib.h>
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 typedef struct {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 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
12 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
13 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
14 size_t blen; /* current content size */
7828
b4a08a514ebc util.ringbuffer: Allocate buffer itself as part of userdata (simpler, single allocation, no need for __gc)
Kim Alvefur <zash@zash.se>
parents: 7827
diff changeset
15 char buffer[];
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 } ringbuffer;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
18 char readchar(ringbuffer *b) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 b->blen--;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 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
21 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
23 void writechar(ringbuffer *b, char c) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 b->blen++;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 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
26 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 /* make sure position counters stay within the allocation */
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
29 void modpos(ringbuffer *b) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 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
31 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
32 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
7941
8067828e7e40 util.ringbuffer: Change types of length related variables to size_t [-Wsign-compare]
Kim Alvefur <zash@zash.se>
parents: 7889
diff changeset
34 int find(ringbuffer *b, const char *s, size_t l) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 size_t i, j;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 int m;
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 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
39 return 0;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 }
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 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
43 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
44 m = 1;
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(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
47 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
48 m = 0;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 break;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 if(m) {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 return i + l;
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 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 return 0;
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
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
61 int rb_find(lua_State *L) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 size_t l, m;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
63 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
64 const char *s = luaL_checklstring(L, 2, &l);
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 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
66
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if(m > 0) {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 lua_pushinteger(L, m);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 return 1;
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
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 return 0;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
75 int rb_read(lua_State *L) {
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
76 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
7941
8067828e7e40 util.ringbuffer: Change types of length related variables to size_t [-Wsign-compare]
Kim Alvefur <zash@zash.se>
parents: 7889
diff changeset
77 size_t r = luaL_checkinteger(L, 2);
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 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
79
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 if(r > b->blen) {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 lua_pushnil(L);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 return 1;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 }
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((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
86 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
87 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
88 lua_concat(L, 2);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 } else {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 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
91 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 if(!peek) {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 b->blen -= r;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 b->rpos += r;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 modpos(b);
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
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 return 1;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
102 int rb_readuntil(lua_State *L) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 size_t l, m;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
104 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
105 const char *s = luaL_checklstring(L, 2, &l);
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 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
107
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 if(m > 0) {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 lua_settop(L, 1);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 lua_pushinteger(L, m);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return rb_read(L);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 }
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 return 0;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
117 int rb_write(lua_State *L) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 size_t l, w = 0;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
119 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
120 const char *s = luaL_checklstring(L, 2, &l);
7117
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 /* Does `l` bytes fit? */
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 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
124 lua_pushnil(L);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 return 1;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 }
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 while(l-- > 0) {
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 writechar(b, *s++);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 w++;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 }
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 modpos(b);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 lua_pushinteger(L, w);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 return 1;
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
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
140 int rb_tostring(lua_State *L) {
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
141 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
7888
74187ee6ed55 util.ringbuffer: Remove address of buffer itself from __tostring since is now in the same struct
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
142 lua_pushfstring(L, "ringbuffer: %p %d/%d", b, b->blen, b->alen);
7117
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
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
146 int rb_length(lua_State *L) {
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
147 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 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
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
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
152 int rb_size(lua_State *L) {
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
153 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
7117
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->alen);
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
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
158 int rb_free(lua_State *L) {
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
159 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
7117
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 - b->blen);
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
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
164 int rb_new(lua_State *L) {
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE));
7828
b4a08a514ebc util.ringbuffer: Allocate buffer itself as part of userdata (simpler, single allocation, no need for __gc)
Kim Alvefur <zash@zash.se>
parents: 7827
diff changeset
166 ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size);
b4a08a514ebc util.ringbuffer: Allocate buffer itself as part of userdata (simpler, single allocation, no need for __gc)
Kim Alvefur <zash@zash.se>
parents: 7827
diff changeset
167
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 b->rpos = 0;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 b->wpos = 0;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 b->alen = size;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 b->blen = 0;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 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
174 lua_setmetatable(L, -2);
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 return 1;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
179 int luaopen_util_ringbuffer(lua_State *L) {
7818
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 7117
diff changeset
180 #if (LUA_VERSION_NUM > 501)
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 7117
diff changeset
181 luaL_checkversion(L);
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 7117
diff changeset
182 #endif
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7888
diff changeset
183
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 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
185 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
186 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
187 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
188 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
189
7969
1c6a07606309 util-src: Specify size of various tables to be allocated
Kim Alvefur <zash@zash.se>
parents: 7941
diff changeset
190 lua_createtable(L, 0, 7); /* __index */
7117
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 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
193 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
194 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
195 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
196 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
197 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
198 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
199 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
200 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
201 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
202 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
203 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
204 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
205 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
206 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 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
208 }
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209
7969
1c6a07606309 util-src: Specify size of various tables to be allocated
Kim Alvefur <zash@zash.se>
parents: 7941
diff changeset
210 lua_createtable(L, 0, 1);
7117
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 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
212 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
213 return 1;
2b4432cc9c29 Add util.ringbuffer, a ringbuffer with a file handle like interface
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 }