aboutsummaryrefslogtreecommitdiff
path: root/api_vm.c
blob: dbbe20a731868afea024354f1cc5d3239861d13d (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
/*
 * 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>

#define MEM_SIZE 0x40000UL // 256KB

//#define MOONY_LOG_MEM
#ifdef MOONY_LOG_MEM
static inline void
_log_mem(moony_t *moony, void *ptr, size_t osize, size_t nsize)
{
	moony_vm_t *vm = &moony->vm;
	if(moony->log)
	{
		char suffix = ' ';
		size_t used = vm->used;
		if(used >= 1024)
		{
			suffix = 'K';
			used >>= 10;
		}
		if(used >= 1024)
		{
			suffix = 'M';
			used >>= 10;
		}
		lv2_log_trace(&moony->logger, "used: %4zu%c, old: %4zu, new: %4zu, data: %p",
			used, suffix, osize, nsize, ptr);
	}
}
#endif

inline void *
moony_alloc(moony_t *moony, size_t nsize)
{
	moony_vm_t *vm = &moony->vm;
	vm->used += nsize;
	if(vm->used > (vm->space >> 1))
		moony_vm_mem_extend(vm);

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

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

inline void *
moony_realloc(moony_t *moony, void *buf, size_t osize, size_t nsize)
{
	moony_vm_t *vm = &moony->vm;
	vm->used -= osize;
	vm->used += nsize;
	if(vm->used > (vm->space >> 1))
		moony_vm_mem_extend(vm);

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

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

inline void
moony_free(moony_t *moony, void *buf, size_t osize)
{
	moony_vm_t *vm = &moony->vm;
	vm->used -= osize;
	if(vm->used > (vm->space >> 1))
		moony_vm_mem_extend(vm);

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

	tlsf_free(vm->tlsf, buf);
}

// rt
static void *
lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
	moony_t *moony = ud;
	moony_vm_t *vm = &moony->vm;

	if(nsize == 0)
	{
		if(ptr)
			moony_free(moony, ptr, osize);
		return NULL;
	}
	else
	{
		if(ptr)
			return moony_realloc(moony, ptr, osize, nsize);
		else
			return moony_alloc(moony, nsize);
	}
}

// non-rt
int
moony_vm_init(moony_vm_t *vm)
{
	moony_t *moony = (void *)vm - offsetof(moony_t, vm);
	memset(vm, 0x0, sizeof(moony_vm_t));

	// initialize array of increasing pool sizes
	vm->size[0] = MEM_SIZE;
	for(int i=1, sum=MEM_SIZE; i<MOONY_POOL_NUM; i++, sum*=2)
		vm->size[i] = sum;

	// allocate first pool
	vm->area[0] = moony_vm_mem_alloc(vm->size[0]);
	if(!vm->area[0])
		return -1;
	
	vm->tlsf = tlsf_create_with_pool(vm->area[0], vm->size[0]);
	if(!vm->tlsf)
		return -1;
	vm->pool[0] = tlsf_get_pool(vm->tlsf);
	vm->space += vm->size[0];
	
	vm->L = lua_newstate(lua_alloc, moony);
	if(!vm->L)
		return -1;

	luaL_requiref(vm->L, "base", luaopen_base, 0);
	luaL_requiref(vm->L, "coroutine", luaopen_coroutine, 1);
	luaL_requiref(vm->L, "table", luaopen_table, 1);
	luaL_requiref(vm->L, "string", luaopen_string, 1);
	luaL_requiref(vm->L, "utf8", luaopen_utf8, 1);
	luaL_requiref(vm->L, "math", luaopen_math, 1);
	lua_pop(vm->L, 6);

	//luaL_requiref(vm->L, "io", luaopen_io, 1);
	//luaL_requiref(vm->L, "os", luaopen_os, 1);
	//luaL_requiref(vm->L, "bit32", luaopen_bit32, 1);
	//luaL_requiref(vm->L, "debug", luaopen_debug, 1);
	//luaL_requiref(vm->L, "package", luaopen_package, 1);

	lua_gc(vm->L, LUA_GCSTOP, 0); // disable automatic garbage collection

	return 0;
}

// non-rt
int
moony_vm_deinit(moony_vm_t *vm)
{
	if(vm->L)
		lua_close(vm->L);
	vm->L = NULL;
	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;
	}

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

	return 0;
}

// non-rt
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);
	if(area)
		mlock(area, size);
#endif

	return area;
}

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

#if !defined(_WIN32)
	munlock(area, size);
#endif
	free(area);
}

// rt
int
moony_vm_mem_extend(moony_vm_t *vm)
{
	moony_t *moony = (void *)vm - offsetof(moony_t, vm);

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

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

		const moony_mem_t request = {
			.i = i,
			.mem = NULL
		};

		// schedule allocation of memory to _work
		LV2_Worker_Status status = moony->sched->schedule_work(
			moony->sched->handle, sizeof(moony_mem_t), &request);

		// toggle working flag
		if(status == LV2_WORKER_SUCCESS)
			moony->working = 1;

		return 0;
	}

	moony->fully_extended = 1;

	return -1;
}