-
Notifications
You must be signed in to change notification settings - Fork 29
/
lunatik.h
305 lines (256 loc) · 9.06 KB
/
lunatik.h
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
/*
* SPDX-FileCopyrightText: (c) 2023-2024 Ring Zero Desenvolvimento de Software LTDA
* SPDX-License-Identifier: MIT OR GPL-2.0-only
*/
#ifndef lunatik_h
#define lunatik_h
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/kref.h>
#include <lua.h>
#include <lauxlib.h>
#define LUNATIK_VERSION "Lunatik 3.5"
#define lunatik_locker(o, mutex_op, spin_op) \
do { \
if ((o)->sleep) \
mutex_op(&(o)->mutex); \
else \
spin_op(&(o)->spin); \
} while(0)
#define lunatik_newlock(o) lunatik_locker((o), mutex_init, spin_lock_init);
#define lunatik_freelock(o) lunatik_locker((o), mutex_destroy, (void));
#define lunatik_lock(o) lunatik_locker((o), mutex_lock, spin_lock_bh)
#define lunatik_unlock(o) lunatik_locker((o), mutex_unlock, spin_unlock_bh)
#define lunatik_toruntime(L) (*(lunatik_object_t **)lua_getextraspace(L))
#define lunatik_cannotsleep(L, s) ((s) && !lunatik_toruntime(L)->sleep)
#define lunatik_getstate(runtime) ((lua_State *)runtime->private)
static inline bool lunatik_isready(lua_State *L)
{
bool ready;
lua_rawgetp(L, LUA_REGISTRYINDEX, L);
ready = lua_toboolean(L, -1);
lua_pop(L, 1);
return ready;
}
#define lunatik_handle(runtime, handler, ret, ...) \
do { \
lua_State *L = lunatik_getstate(runtime); \
int n = lua_gettop(L); \
ret = handler(L, ## __VA_ARGS__); \
lua_settop(L, n); \
} while(0)
#define lunatik_run(runtime, handler, ret, ...) \
do { \
lunatik_lock(runtime); \
if (unlikely(!lunatik_getstate(runtime))) \
ret = -ENXIO; \
else \
lunatik_handle(runtime, handler, ret, ## __VA_ARGS__); \
lunatik_unlock(runtime); \
} while(0)
typedef struct lunatik_reg_s {
const char *name;
lua_Integer value;
} lunatik_reg_t;
typedef struct lunatik_namespace_s {
const char *name;
const lunatik_reg_t *reg;
} lunatik_namespace_t;
typedef struct lunatik_class_s {
const char *name;
const luaL_Reg *methods;
void (*release)(void *);
bool sleep;
bool pointer;
} lunatik_class_t;
typedef struct lunatik_object_s {
struct kref kref;
const lunatik_class_t *class;
void *private;
union {
struct mutex mutex;
spinlock_t spin;
};
bool sleep;
} lunatik_object_t;
extern lunatik_object_t *lunatik_env;
static inline int lunatik_trylock(lunatik_object_t *object)
{
return object->sleep ? mutex_trylock(&object->mutex) : spin_trylock_bh(&object->spin);
}
int lunatik_runtime(lunatik_object_t **pruntime, const char *script, bool sleep);
int lunatik_stop(lunatik_object_t *runtime);
static inline void *lunatik_realloc(lua_State *L, void *ptr, size_t size)
{
void *ud = NULL;
lua_Alloc alloc = lua_getallocf(L, &ud);
return alloc(ud, ptr, LUA_TNONE, size);
}
#define lunatik_malloc(L, s) lunatik_realloc((L), NULL, (s))
#define lunatik_free(p) kfree(p)
#define lunatik_gfp(runtime) (runtime->sleep ? GFP_KERNEL : GFP_ATOMIC)
static inline void *lunatik_checknull(lua_State *L, void *ptr)
{
if (ptr == NULL)
luaL_error(L, "not enough memory");
return ptr;
}
#define lunatik_checkalloc(L, s) (lunatik_checknull((L), lunatik_malloc((L), (s))))
static inline void lunatik_checkfield(lua_State *L, int idx, const char *field, int type)
{
int _type = lua_getfield(L, idx, field);
if (_type != type)
luaL_error(L, "bad field '%s' (%s expected, got %s)", field,
lua_typename(L, type), lua_typename(L, _type));
}
static inline lunatik_object_t *lunatik_checkruntime(lua_State *L, bool sleep)
{
lunatik_object_t *runtime = lunatik_toruntime(L);
if (runtime->sleep != sleep)
luaL_error(L, "cannot use %ssleepable runtime in this context", runtime->sleep ? "" : "non-");
return runtime;
}
#define lunatik_setruntime(L, libname, priv) ((priv)->runtime = lunatik_checkruntime((L), lua##libname##_class.sleep))
static inline void lunatik_checkclass(lua_State *L, const lunatik_class_t *class)
{
if (lunatik_cannotsleep(L, class->sleep))
luaL_error(L, "cannot use '%s' class on non-sleepable runtime", class->name);
}
static inline void lunatik_setclass(lua_State *L, const lunatik_class_t *class)
{
if (luaL_getmetatable(L, class->name) == LUA_TNIL)
luaL_error(L, "metatable not found (%s)", class->name);
lua_setmetatable(L, -2);
lua_pushlightuserdata(L, (void *)class);
lua_setiuservalue(L, -2, 1); /* pop class */
}
static inline void lunatik_setobject(lunatik_object_t *object, const lunatik_class_t *class, bool sleep)
{
kref_init(&object->kref);
object->private = NULL;
object->class = class;
object->sleep = sleep;
lunatik_newlock(object);
}
lunatik_object_t *lunatik_newobject(lua_State *L, const lunatik_class_t *class, size_t size);
lunatik_object_t *lunatik_createobject(const lunatik_class_t *class, size_t size, bool sleep);
lunatik_object_t **lunatik_checkpobject(lua_State *L, int ix);
void lunatik_cloneobject(lua_State *L, lunatik_object_t *object);
void lunatik_releaseobject(struct kref *kref);
int lunatik_closeobject(lua_State *L);
int lunatik_deleteobject(lua_State *L);
int lunatik_monitorobject(lua_State *L);
#define LUNATIK_ERR_NULLPTR "null-pointer dereference"
#define lunatik_newpobject(L, n) (lunatik_object_t **)lua_newuserdatauv((L), sizeof(lunatik_object_t *), (n))
#define lunatik_argchecknull(L, o, i) luaL_argcheck((L), (o) != NULL, (i), LUNATIK_ERR_NULLPTR)
#define lunatik_checkobject(L, i) (*lunatik_checkpobject((L), (i)))
#define lunatik_toobject(L, i) (*(lunatik_object_t **)lua_touserdata((L), (i)))
#define lunatik_getobject(o) kref_get(&(o)->kref)
#define lunatik_putobject(o) kref_put(&(o)->kref, lunatik_releaseobject)
static inline void lunatik_require(lua_State *L, const char *libname)
{
lua_getglobal(L, "require");
lua_pushstring(L, libname);
lua_call(L, 1, 0);
}
static inline void lunatik_pushobject(lua_State *L, lunatik_object_t *object)
{
lunatik_cloneobject(L, object);
lunatik_getobject(object);
}
static inline bool lunatik_hasindex(lua_State *L, int index)
{
bool hasindex = lua_getfield(L, index, "__index") != LUA_TNIL;
lua_pop(L, 1);
return hasindex;
}
static inline void lunatik_newclass(lua_State *L, const lunatik_class_t *class)
{
luaL_newmetatable(L, class->name); /* mt = {} */
luaL_setfuncs(L, class->methods, 0);
if (!lunatik_hasindex(L, -1)) {
lua_pushvalue(L, -1); /* push mt */
lua_setfield(L, -2, "__index"); /* mt.__index = mt */
}
lua_pop(L, 1); /* pop mt */
}
static inline lunatik_class_t *lunatik_getclass(lua_State *L, int ix)
{
if (lua_isuserdata(L, ix) && lua_getiuservalue(L, ix, 1) != LUA_TNONE) {
lunatik_class_t *class = (lunatik_class_t *)lua_touserdata(L, -1);
lua_pop(L, 1); /* class */
return class;
}
return NULL;
}
#define lunatik_isobject(L, ix) (lunatik_getclass((L), (ix)) != NULL)
static inline lunatik_object_t *lunatik_testobject(lua_State *L, int ix)
{
lunatik_object_t **pobject;
lunatik_class_t *class = lunatik_getclass(L, ix);
return class != NULL && (pobject = luaL_testudata(L, ix, class->name)) != NULL ? *pobject : NULL;
}
static void inline lunatik_newnamespaces(lua_State *L, const lunatik_namespace_t *namespaces)
{
for (; namespaces->name; namespaces++) {
const lunatik_reg_t *reg;
lua_newtable(L); /* namespace = {} */
for (reg = namespaces->reg; reg->name; reg++) {
lua_pushinteger(L, reg->value);
lua_setfield(L, -2, reg->name); /* namespace[name] = value */
}
lua_setfield(L, -2, namespaces->name); /* lib.namespace = namespace */
}
}
#define LUNATIK_NEWLIB(libname, funcs, class, namespaces) \
int luaopen_##libname(lua_State *L); \
int luaopen_##libname(lua_State *L) \
{ \
const lunatik_class_t *cls = class; /* avoid -Waddress */ \
const lunatik_namespace_t *nss = namespaces; /* avoid -Waddress */ \
luaL_newlib(L, funcs); \
if (cls) { \
lunatik_checkclass(L, cls); \
lunatik_newclass(L, cls); \
} \
if (nss) \
lunatik_newnamespaces(L, nss); \
return 1; \
} \
EXPORT_SYMBOL_GPL(luaopen_##libname)
#define LUNATIK_LIB(libname) \
int luaopen_##libname(lua_State *L); \
#define LUNATIK_OBJECTCHECKER(checker, T) \
static inline T checker(lua_State *L, int ix) \
{ \
lunatik_object_t *object = lunatik_checkobject(L, ix); \
return (T)object->private; \
}
#define LUNATIK_PRIVATECHECKER(checker, T) \
static inline T checker(lua_State *L, int ix) \
{ \
T private = (T)lunatik_toobject(L, ix)->private; \
/* avoid use-after-free */ \
lunatik_argchecknull(L, private, ix); \
return private; \
}
#define lunatik_getregistry(L, key) lua_rawgetp((L), LUA_REGISTRYINDEX, (key))
static inline void lunatik_setregistry(lua_State *L, int ix, void *key)
{
lua_pushvalue(L, ix);
lua_rawsetp(L, LUA_REGISTRYINDEX, key); /* pop value */
}
static inline void lunatik_registerobject(lua_State *L, int ix, lunatik_object_t *object)
{
lunatik_setregistry(L, ix, object->private); /* private */
lunatik_setregistry(L, -1, object); /* prevent object from being GC'ed (unless stopped) */
}
static inline void lunatik_unregisterobject(lua_State *L, lunatik_object_t *object)
{
lua_pushnil(L);
lunatik_setregistry(L, -1, object->private); /* remove private */
lunatik_setregistry(L, -1, object); /* remove object, now it might be GC'ed */
lua_pop(L, 1); /* pop nil */
}
#endif