aboutsummaryrefslogtreecommitdiff
path: root/api/api_vm.c
blob: ade9457a0cfd347dc5279d7b0f231509a81bddc8 (plain)
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
/*
 * Copyright (c) 2015 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#if !defined(_WIN32)
# include <sys/mman.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <moony.h>
#include <api_vm.h>

#include <lualib.h>
#include <lauxlib.h>

#include <laes128.h>

//FIXME put those into a header
extern int luaopen_lpeg(lua_State *L);
extern int luaopen_base64(lua_State *L);
extern int luaopen_ascii85(lua_State *L);
extern int luaopen_mathx(lua_State *L);
extern int luaopen_complex(lua_State *L);
extern int luaopen_random(lua_State *L);

//#define MOONY_LOG_MEM
#ifdef MOONY_LOG_MEM
__realtime static inline void
_log_mem(moony_vm_t *vm, void *ptr, size_t osize, size_t nsize)
{
	moony_t *moony = vm->data;

	if(moony->log)
	{
		char suffix0 = ' ';
		size_t space = vm->space;
		if(space >= 1024)
		{
			suffix0 = 'K';
			space >>= 10;
		}
		if(space >= 1024)
		{
			suffix0 = 'M';
			space >>= 10;
		}

		char suffix1 = ' ';
		size_t used = vm->used;
		if(used >= 1024)
		{
			suffix1 = 'K';
			used >>= 10;
		}
		if(used >= 1024)
		{
			suffix1 = 'M';
			used >>= 10;
		}

		lv2_log_trace(&moony->logger, "space: %4zu%c, used: %4zu%c, old: %4zu, new: %4zu, data: %p\n",
			space, suffix0, used, suffix1, osize, nsize, ptr);
	}
}
#endif

__realtime inline void *
moony_rt_alloc(moony_vm_t *vm, size_t nsize)
{
	vm->used += nsize;
	if(vm->used > (vm->space >> 1))
		moony_vm_mem_extend(vm);

#ifdef MOONY_LOG_MEM
	_log_mem(vm, NULL, 0, nsize);
#endif

	return tlsf_malloc(vm->tlsf, nsize);
}

__realtime inline void *
moony_rt_realloc(moony_vm_t *vm, void *buf, size_t osize, size_t nsize)
{
	vm->used -= osize;
	vm->used += nsize;
	if(vm->used > (vm->space >> 1))
		moony_vm_mem_extend(vm);

#ifdef MOONY_LOG_MEM
	_log_mem(vm, buf, osize, nsize);
#endif

	return tlsf_realloc(vm->tlsf, buf, nsize);
}

__realtime inline void
moony_rt_free(moony_vm_t *vm, void *buf, size_t osize)
{
	vm->used -= osize;
	if(vm->used > (vm->space >> 1))
		moony_vm_mem_extend(vm);

#ifdef MOONY_LOG_MEM
	_log_mem(vm, buf, osize, 0);
#endif

	tlsf_free(vm->tlsf, buf);
}

__realtime static void *
lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
	moony_vm_t *vm = ud;

	if(nsize == 0)
	{
		if(ptr)
			moony_rt_free(vm, ptr, osize);
		return NULL;
	}
	else
	{
		if(ptr)
			return moony_rt_realloc(vm, ptr, osize, nsize);
		else
			return moony_rt_alloc(vm, nsize);
	}
}

__non_realtime moony_vm_t *
moony_vm_new(size_t mem_size, bool testing, void *data)
{
	moony_vm_t *vm = calloc(1, sizeof(moony_vm_t));
	if(!vm)
		return NULL;

	vm->data = data;

	// initialize array of increasing pool sizes
	vm->size[0] = mem_size;

	// allocate first pool
	vm->area[0] = moony_vm_mem_alloc(vm->size[0]);
	if(!vm->area[0])
	{
		free(vm);
		return NULL;
	}
	
	vm->tlsf = tlsf_create_with_pool(vm->area[0], vm->size[0]);
	if(!vm->tlsf)
	{
		moony_vm_mem_free(vm->area[0], vm->size[0]);
		free(vm);
		return NULL;
	}

	vm->pool[0] = tlsf_get_pool(vm->tlsf);
	vm->space += vm->size[0];

	lua_State *L = lua_newstate(lua_alloc, vm);
	if(!L)
	{
		free(vm);
		return NULL;
	}

	vm->L = L;

	const int n = lua_gettop(L);

	luaL_requiref(L, "base", luaopen_base, 0);

	luaL_requiref(L, "coroutine", luaopen_coroutine, 1);
	luaL_requiref(L, "table", luaopen_table, 1);
	luaL_requiref(L, "string", luaopen_string, 1);
	luaL_requiref(L, "math", luaopen_math, 1);
	luaL_requiref(L, "utf8", luaopen_utf8, 1);
	luaL_requiref(L, "debug", luaopen_debug, 1);

	luaL_requiref(L, "lpeg", luaopen_lpeg, 1);
	luaL_requiref(L, "base64", luaopen_base64, 1);
	luaL_requiref(L, "ascii85", luaopen_ascii85, 1);
	luaL_requiref(L, "aes128", luaopen_aes128, 1);
	luaL_requiref(L, "mathx", luaopen_mathx, 1);
	luaL_requiref(L, "complex", luaopen_complex, 1);
	luaL_requiref(L, "random", luaopen_random, 1);

	if(testing)
	{
		luaL_requiref(L, "io", luaopen_io, 1);
		luaL_requiref(L, "package", luaopen_package, 1);
		//luaL_requiref(L, "os", luaopen_os, 1);
		//luaL_requiref(L, "bit32", luaopen_bit32, 1);
	}

	lua_settop(L, n);

	if(!testing)
	{
		// clear dofile
		lua_pushnil(L);
		lua_setglobal(L, "dofile");

		// clear loadfile
		lua_pushnil(L);
		lua_setglobal(L, "loadfile");
	}

	// clear math.random[seed]
	lua_getglobal(L, "math");

	lua_pushnil(L);
	lua_setfield(L, -2, "random");

	lua_pushnil(L);
	lua_setfield(L, -2, "randomseed");

	lua_pop(L, 1); // math

#ifdef USE_MANUAL_GC
	// manual garbage collector
	lua_gc(L, LUA_GCSTOP, 0); // disable automatic garbage collection
	lua_gc(L, LUA_GCSETPAUSE, 0); // don't wait to start a new cycle
	lua_gc(L, LUA_GCSETSTEPMUL, 100); // set step size to run 'as fast a memory allocation'
#else
	// automatic garbage collector
	lua_gc(L, LUA_GCRESTART, 0); // enable automatic garbage collection
	lua_gc(L, LUA_GCSETPAUSE, 105); // next step when memory increased by 5%
	lua_gc(L, LUA_GCSETSTEPMUL, 105); // run 5% faster than memory allocation
#endif

	return vm;
}

__non_realtime void
moony_vm_free(moony_vm_t *vm)
{
	if(vm->L)
		lua_close(vm->L);

	if(vm->ser.buf)
		moony_rt_free(vm, vm->ser.buf, vm->ser.size);

	vm->used = 0;

	for(int i=(MOONY_POOL_NUM-1); i>=0; i--)
	{
		if(!vm->area[i])
			continue; // this memory slot is unused, skip it

		tlsf_remove_pool(vm->tlsf, vm->pool[i]);
		moony_vm_mem_free(vm->area[i], vm->size[i]);
		vm->space -= vm->size[i];

		vm->area[i] = NULL;
		vm->pool[i] = NULL;
		vm->size[i] = 0;
	}

	assert(vm->space == 0);
	tlsf_destroy(vm->tlsf);
	vm->tlsf = NULL;

	free(vm);
}

__non_realtime void *
moony_vm_mem_alloc(size_t size)
{
	void *area = NULL;

	//printf("moony_vm_mem_alloc: %zu\n", size);

#if defined(_WIN32)
	area = _aligned_malloc(size, 8);
#else
	posix_memalign(&area, 8, size);
#endif
	if(!area)
		return NULL;

	mlock(area, size);
	memset(area, 0x0, size);
	return area;
}

__non_realtime void
moony_vm_mem_free(void *area, size_t size)
{
	if(!area)
		return;
	
	//printf("moony_vm_mem_free: %zu\n", size);

	munlock(area, size);
	free(area);
}

__realtime int
moony_vm_mem_extend(moony_vm_t *vm)
{
	moony_t *moony = vm->data;

	// request processing or fully extended?
	if(vm->allocating || vm->fully_extended)
		return -1;

	for(int i=1; i<MOONY_POOL_NUM; i++)
	{
		if(vm->area[i]) // pool already allocated/in-use
			continue;

		if(vm->nrt)
		{
			vm->size[i] = vm->size[i-1] * 2;
			vm->area[i] = moony_vm_mem_alloc(vm->size[i]);
			if(vm->area[i])
			{
				vm->pool[i] = tlsf_add_pool(vm->tlsf,
					vm->area[i], vm->size[i]); //FIXME stoat complains about printf

				if(vm->pool[i])
				{
					vm->space += vm->size[i];
				}
				else
				{
					moony_vm_mem_free(vm->area[i], vm->size[i]);
					vm->size[i] = 0;
					vm->area[i] = NULL;
				}
			}
		}
		else
		{
			moony_job_t *req;
			if((req = varchunk_write_request(moony->from_dsp, sizeof(moony_job_t))))
			{
				req->type = MOONY_JOB_MEM_ALLOC;
				req->mem.size = vm->size[i-1] * 2;
				req->mem.ptr = NULL;

				varchunk_write_advance(moony->from_dsp, sizeof(moony_job_t));
				if(moony_wake_worker(moony->sched) == LV2_WORKER_SUCCESS)
					vm->allocating = true; // toggle working flag
			}
		}

		return 0;
	}

	vm->fully_extended = true;

	return -1;
}

void 
moony_vm_nrt_enter(moony_vm_t *vm)
{
	vm->nrt = true;
}

void
moony_vm_nrt_leave(moony_vm_t *vm)
{
	vm->nrt = false;
}