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
|
/* Copyright (c) 2010-2012, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "lua_common.h"
#include "mem_pool.h"
/* Public prototypes */
struct memory_pool_s *lua_check_mempool (lua_State * L);
gint luaopen_mempool (lua_State * L);
/* Lua bindings */
LUA_FUNCTION_DEF (mempool, create);
LUA_FUNCTION_DEF (mempool, memory_pool_add_destructor);
LUA_FUNCTION_DEF (mempool, memory_pool_delete);
LUA_FUNCTION_DEF (mempool, memory_pool_stat);
LUA_FUNCTION_DEF (mempool, memory_pool_get_size);
LUA_FUNCTION_DEF (mempool, memory_pool_set_variable);
LUA_FUNCTION_DEF (mempool, memory_pool_get_variable);
static const struct luaL_reg mempoollib_m[] = {
LUA_INTERFACE_DEF (mempool, memory_pool_add_destructor),
LUA_INTERFACE_DEF (mempool, memory_pool_stat),
LUA_INTERFACE_DEF (mempool, memory_pool_get_size),
LUA_INTERFACE_DEF (mempool, memory_pool_set_variable),
LUA_INTERFACE_DEF (mempool, memory_pool_get_variable),
{"__gc", lua_mempool_memory_pool_delete},
{"__tostring", lua_class_tostring},
{NULL, NULL}
};
static const struct luaL_reg mempoollib_f[] = {
LUA_INTERFACE_DEF (mempool, create),
{NULL, NULL}
};
/*
* Struct for lua destructor
*/
struct lua_mempool_udata {
lua_State *L;
gint cbref;
memory_pool_t *mempool;
};
struct memory_pool_s *
lua_check_mempool (lua_State * L)
{
void *ud = luaL_checkudata (L, 1, "rspamd{mempool}");
luaL_argcheck (L, ud != NULL, 1, "'mempool' expected");
return ud ? *((struct memory_pool_s **)ud) : NULL;
}
static int
lua_mempool_create (lua_State *L)
{
struct memory_pool_s *mempool = memory_pool_new (memory_pool_get_size ()), **pmempool;
if (mempool) {
pmempool = lua_newuserdata (L, sizeof (struct memory_pool_s *));
lua_setclass (L, "rspamd{mempool}", -1);
*pmempool = mempool;
}
else {
lua_pushnil (L);
}
return 1;
}
static void
lua_mempool_destructor_func (gpointer p)
{
struct lua_mempool_udata *ud = p;
gboolean need_unlock = FALSE;
/* Avoid LOR here as mutex can be acquired before in lua_call */
if (g_mutex_trylock (lua_mtx)) {
need_unlock = TRUE;
}
lua_rawgeti (ud->L, LUA_REGISTRYINDEX, ud->cbref);
if (lua_pcall (ud->L, 0, 0, 0) != 0) {
msg_info ("call to destructor failed: %s", lua_tostring (ud->L, -1));
}
luaL_unref (ud->L, LUA_REGISTRYINDEX, ud->cbref);
if (need_unlock) {
g_mutex_unlock (lua_mtx);
}
}
static int
lua_mempool_memory_pool_add_destructor (lua_State *L)
{
struct memory_pool_s *mempool = lua_check_mempool (L);
struct lua_mempool_udata *ud;
if (mempool) {
if (lua_isfunction (L, 2)) {
ud = memory_pool_alloc (mempool, sizeof (struct lua_mempool_udata));
lua_pushvalue (L, 2);
/* Get a reference */
ud->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
ud->L = L;
ud->mempool = mempool;
memory_pool_add_destructor (mempool, lua_mempool_destructor_func, ud);
}
else {
msg_err ("trying to add destructor without function");
}
}
else {
lua_pushnil (L);
}
return 1;
}
static int
lua_mempool_memory_pool_delete (lua_State *L)
{
struct memory_pool_s *mempool = lua_check_mempool (L);
if (mempool) {
memory_pool_delete (mempool);
return 0;
}
else {
lua_pushnil (L);
}
return 1;
}
static int
lua_mempool_memory_pool_stat (lua_State *L)
{
struct memory_pool_s *mempool = lua_check_mempool (L);
if (mempool) {
}
else {
lua_pushnil (L);
}
return 1;
}
static int
lua_mempool_memory_pool_get_size (lua_State *L)
{
struct memory_pool_s *mempool = lua_check_mempool (L);
if (mempool) {
lua_pushinteger (L, memory_pool_get_size ());
return 0;
}
else {
lua_pushnil (L);
}
return 1;
}
static int
lua_mempool_memory_pool_set_variable (lua_State *L)
{
struct memory_pool_s *mempool = lua_check_mempool (L);
const gchar *var = luaL_checkstring (L, 2),
*value = luaL_checkstring (L, 3);
if (mempool && var && value) {
memory_pool_set_variable (mempool, var, memory_pool_strdup (mempool, value), NULL);
return 0;
}
else {
lua_pushnil (L);
}
return 1;
}
static int
lua_mempool_memory_pool_get_variable (lua_State *L)
{
struct memory_pool_s *mempool = lua_check_mempool (L);
const gchar *var = luaL_checkstring (L, 2);
gchar *value;
if (mempool && var) {
value = memory_pool_get_variable (mempool, var);
if (value) {
lua_pushstring (L, value);
}
else {
lua_pushnil (L);
}
}
else {
lua_pushnil (L);
}
return 1;
}
gint
luaopen_mempool (lua_State * L)
{
luaL_newmetatable (L, "rspamd{mempool}");
lua_pushstring (L, "__index");
lua_pushvalue (L, -2);
lua_settable (L, -3);
lua_pushstring (L, "class");
lua_pushstring (L, "rspamd{mempool}");
lua_rawset (L, -3);
luaL_openlib (L, NULL, mempoollib_m, 0);
luaL_openlib(L, "rspamd_mempool", mempoollib_f, 0);
lua_pop (L, 1); /* remove metatable from stack */
return 1;
}
|