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

#include <api_stash.h>

int
_lstash__gc(lua_State *L)
{
	lstash_t *lstash = lua_touserdata(L, 1);
	atom_ser_t *ser = &lstash->ser;

	if(ser->buf)
		tlsf_free(ser->tlsf, ser->buf);

	return 0;
}

int
_lstash_write(lua_State *L)
{
	lstash_t *lstash = lua_touserdata(L, 1);
	atom_ser_t *ser = &lstash->ser;
	lforge_t *lforge = &lstash->lforge;

	lforge->depth = 0;
	lforge->last.frames = 0;
	lforge->forge = &lstash->forge;
	
	ser->offset = 0; // reset stash pointer
	lv2_atom_forge_set_sink(&lstash->forge, _sink, _deref, ser);

	LV2_Atom *atom = (LV2_Atom *)ser->buf;
	atom->type = 0;
	atom->size = 0;

	luaL_getmetatable(L, "lforge");
	lua_setmetatable(L, 1);

	return 1;
}

int
_lstash_read(lua_State *L)
{
	lstash_t *lstash = lua_touserdata(L, 1);
	atom_ser_t *ser = &lstash->ser;
	latom_t *latom = &lstash->latom;

	latom->atom = (const LV2_Atom *)ser->buf;
	latom->body.raw = LV2_ATOM_BODY_CONST(latom->atom);

	luaL_getmetatable(L, "latom");
	lua_setmetatable(L, 1);

	return 1;
}

int
_lstash(lua_State *L)
{
	moony_t *moony = lua_touserdata(L, lua_upvalueindex(1));

	lstash_t *lstash = moony_newuserdata(L, moony, MOONY_UDATA_STASH, false);

	// initialize forge (URIDs)
	memcpy(&lstash->forge, &moony->forge, sizeof(LV2_Atom_Forge));

	// initialize memory pool
	atom_ser_t *ser = &lstash->ser;
	ser->tlsf = moony->vm.tlsf;
	ser->size = 1024;
	ser->offset = 0; // reset stash pointer
	ser->buf = tlsf_malloc(moony->vm.tlsf, ser->size);

	if(!ser->buf)
		lua_pushnil(L); // memory allocation failed

	// start off as forge
	_lstash_write(L);

	return 1;
}

const luaL_Reg lstash_mt [] = {
	{NULL, NULL}
};