forked from brianuuuu/atomdict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua-ad.c
398 lines (349 loc) · 8.31 KB
/
lua-ad.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <lua.h>
#include <lauxlib.h>
#include <assert.h>
#include <stdio.h>
/*
integer new_handle( { keyname = initvalue , ... } , typename or nil) -- create an atomdict handle
delete_handle(integer handle) -- release an atomdict handle
userdata:object new(integer handle) -- create an atomdict object from a handle
barrier() -- call barrier to commit atomdict objects
dump(object) -- dump for debug
dump_handle(handle)
*/
#include "atomdict.h"
#define MAX_HANDLE (1024 * 16)
#define MAX_OBJECT 1024
static struct atomdict * G[MAX_HANDLE];
struct barrier;
struct ad_object {
struct atomdict *ad;
struct barrier *barrier;
int version;
int col;
};
struct barrier {
int version;
int object;
struct ad_object *obj[MAX_OBJECT];
};
static int
_find_slot(lua_State *L , struct atomdict *ad) {
int i,j;
for (j=0;j<2;j++) {
for (i=0;i<MAX_HANDLE;i++) {
if (G[i] == NULL) {
if (__sync_bool_compare_and_swap(&(G[i]), NULL , ad)) {
return i;
}
}
}
}
atomdict_delete(ad);
return luaL_error(L, "Can't find free slot for atomdict");
}
/*
table [key - value]
string typename (or nil)
*/
static int
_new_handle(lua_State * L) {
struct atomdict *ad = atomdict_new();
int idx = _find_slot(L,ad);
luaL_checktype(L,1,LUA_TTABLE);
const char * typename = NULL;
if (lua_gettop(L) > 1) {
typename = luaL_checkstring(L,2);
}
int n=0;
lua_pushnil(L); /* first key */
while (lua_next(L, 1) != 0) {
++n;
lua_pop(L, 1);
}
struct atomslot slot[n+1];
int i;
lua_pushnil(L);
for (i=0;i<n;i++) {
int r = lua_next(L, 1);
assert(r!=0);
luaL_checktype(L,-2,LUA_TSTRING);
slot[i].key = lua_tostring(L,-2);
int type = lua_type(L,-1);
switch (type) {
case LUA_TSTRING:
slot[i].vs = lua_tostring(L,-1);
slot[i].vn = 0;
break;
case LUA_TNUMBER:
slot[i].vn = (float)lua_tonumber(L,-1);
slot[i].vs = NULL;
break;
default:
return luaL_error(L,"Unsupport type for atomdict : %s",lua_typename(L,type));
}
lua_pop(L,1);
}
slot[n].key = NULL;
slot[n].vs = NULL;
slot[n].vn = 0;
int r = atomdict_init(ad, slot, typename);
if (r != 0) {
return luaL_error(L, "Init atomdict error");
}
lua_pushinteger(L,idx);
return 1;
}
static int
_delete_handle(lua_State *L) {
int idx = luaL_checkinteger(L,1);
struct atomdict *ad = G[idx];
G[idx] = NULL;
__sync_synchronize();
atomdict_delete(ad);
return 0;
}
#define _update_object(L,object) if (object->barrier->version == object->version) {} else _update_object_(L,object->barrier,object)
static void
_update_object_(lua_State *L, struct barrier *b, struct ad_object *object) {
if (b->object >= MAX_OBJECT) {
luaL_error(L, "Commit object too many");
}
object->version = b->version;
b->obj[b->object++] = object;
object->col = atomdict_grab(object->ad);
}
static void
_gen_keyindex(lua_State *L, struct atomdict *ad) {
lua_newtable(L);
int i = 0;
for (;;) {
struct atomslot slot;
const char * key = atomdict_key(ad, i , &slot);
if (key == NULL) {
return;
}
if (slot.vs == NULL) {
lua_pushinteger(L,(i+1));
} else {
lua_pushinteger(L,-(i+1));
}
lua_setfield(L,-2,key);
++i;
}
}
static void
_gen_keyindex_from_meta(lua_State *L, struct atomdict *ad, const char *typename) {
lua_getfield(L,-1,"type");
// stack now : ... , metatable , typecache
lua_getfield(L, -1 , typename);
if (lua_isnil(L,-1)) {
lua_pop(L,-1);
_gen_keyindex(L, ad);
lua_pushvalue(L,-1);
lua_setfield(L,-3,typename);
// stack now : ... , metatable, typecache, keyindex
}
lua_remove(L,-2);
}
/*
uv1: barrier
uv2: metatable
integer handle
return userdata
*/
static int
_new_object(lua_State *L) {
struct barrier * b = lua_touserdata(L, lua_upvalueindex(1));
int index = luaL_checkinteger(L,1);
struct atomdict *ad = G[index];
if (ad == NULL) {
return luaL_error(L, "Atomdict binding a null handle");
}
struct ad_object *object = lua_newuserdata(L,sizeof(struct ad_object));
object->ad = ad;
object->barrier = b;
object->version = -1;
_update_object(L, object);
lua_pushvalue(L, lua_upvalueindex(2));
// stack now : handle , ... , object, metatable
const char * typename = atomdict_typename(ad);
if (typename) {
_gen_keyindex_from_meta(L, ad, typename);
} else {
_gen_keyindex(L, ad);
}
// stack now : handle , ... , object, metatable, keyindex
lua_setuservalue(L,-3);
lua_setmetatable(L,-2);
return 1;
}
static int
_name_object(lua_State *L) {
struct ad_object *object = lua_touserdata(L,1);
const char * typename = atomdict_typename(object->ad);
if (typename == NULL) {
lua_pushliteral(L, "[ATOMDICT]");
} else {
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addchar(&b, '[');
luaL_addstring(&b, typename);
luaL_addchar(&b, ']');
luaL_pushresult(&b);
}
return 1;
}
static int
_get_object(lua_State *L) {
struct ad_object *object = lua_touserdata(L,1);
_update_object(L, object);
lua_getuservalue(L,1);
lua_pushvalue(L,2);
lua_rawget(L,-2);
if (lua_isnil(L,-1)) {
return luaL_error(L, "Invalid key %s", luaL_checkstring(L,2));
}
int index = lua_tointeger(L,-1);
if (index > 0) {
float v = atomdict_get_number(object->ad, object->col, index-1);
lua_pushnumber(L,v);
} else {
const char * v = atomdict_get_string(object->ad, object->col, -(index+1));
lua_pushstring(L,v);
}
return 1;
}
static int
_set_object(lua_State *L) {
struct ad_object *object = lua_touserdata(L,1);
_update_object(L, object);
lua_getuservalue(L,1);
lua_pushvalue(L,2);
// stack now : object key value , ... , type , key
lua_rawget(L,-2);
if (lua_isnil(L,-1)) {
return luaL_error(L, "Invalid key %s", luaL_checkstring(L,2));
}
int index = lua_tointeger(L,-1);
int new_col ;
if (index > 0) {
float v = luaL_checknumber(L,3);
new_col = atomdict_set_number(object->ad, object->col, index-1, v);
} else {
const char * v = luaL_checkstring(L,3);
new_col = atomdict_set_string(object->ad, object->col, -(index+1), v);
}
if (new_col < 0) {
return luaL_error(L, "atomdict set %s error", lua_tostring(L,2));
}
object->col = new_col;
return 0;
}
/*
userdata:object
string or nil (key)
*/
static int
_next_property(lua_State *L) {
if (lua_gettop(L) == 1) {
lua_getuservalue(L,1);
lua_pushnil(L);
} else {
lua_getuservalue(L,1);
lua_pushvalue(L,2);
}
int end = lua_next(L,-2);
// stack now: object , keyindex, (nextkey , value)
if (end == 0) {
return 0;
}
int index = lua_tointeger(L,-1);
lua_pop(L,1);
struct ad_object *object = lua_touserdata(L,1);
_update_object(L, object);
if (index > 0) {
float v = atomdict_get_number(object->ad, object->col, index-1);
lua_pushnumber(L,v);
} else {
const char * v = atomdict_get_string(object->ad, object->col, -(index+1));
lua_pushstring(L,v);
}
return 2;
}
static int
_pairs_object(lua_State *L) {
lua_pushcfunction(L,_next_property);
lua_pushvalue(L,1);
lua_pushnil(L);
return 3;
}
static int
_create_meta(lua_State *L) {
luaL_Reg meta[] = {
{ "__index", _get_object },
{ "__newindex", _set_object },
{ "__tostring", _name_object },
{ "__pairs", _pairs_object },
{ NULL , NULL },
};
luaL_newlib(L,meta);
lua_newtable(L);
lua_setfield(L,-2,"type");
return 1;
}
static int
_barrier(lua_State *L) {
struct barrier * b = lua_touserdata(L, lua_upvalueindex(1));
++b->version;
int i;
for (i=0;i<b->object;i++) {
atomdict_commit(b->obj[i]->ad, b->obj[i]->col);
}
b->object = 0;
return 0;
}
static int
_dump_object(lua_State *L) {
struct ad_object * obj = lua_touserdata(L,1);
atomdict_dump(obj->ad, obj->col);
return 0;
}
static int
_dump_handle(lua_State *L) {
int idx = luaL_checkinteger(L,1);
struct atomdict * ad = G[idx];
if (ad == NULL) {
return luaL_error(L, "atomdict handle %d is invalid", idx);
}
atomdict_dump(ad, -1);
return 0;
}
int
luaopen_atomdict(lua_State *L) {
luaL_checkversion(L);
// stack now : barrier
luaL_Reg l[] = {
{ "new_handle", _new_handle },
{ "delete_handle", _delete_handle },
{ "dump_handle", _dump_handle },
{ "dump", _dump_object },
{ NULL , NULL },
};
luaL_newlib(L,l);
struct barrier * b = lua_newuserdata(L, sizeof(struct barrier));
b->version = 0;
b->object = 0;
// stack now : libtable barrier
lua_pushvalue(L,-1);
_create_meta(L);
// stack now : libtable barrier barrier metatable
lua_pushcclosure(L, _new_object, 2);
lua_setfield(L,-3, "new");
// stack now : libtable barrier
luaL_Reg l2[] = {
{ "barrier", _barrier },
{ NULL , NULL },
};
luaL_setfuncs(L, l2, 1);
return 1;
}