forked from mhoangvslev/libcod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsc_memory.cpp
265 lines (243 loc) · 4.94 KB
/
gsc_memory.cpp
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
#include "gsc_memory.hpp"
#if COMPILE_MEMORY == 1
void gsc_memory_malloc()
{
int bytes;
if ( ! stackGetParams("i", &bytes))
{
stackError("gsc_memory_malloc() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushInt((int) malloc(bytes));
}
void gsc_memory_free()
{
int memory;
if ( ! stackGetParams("i", &memory))
{
stackError("gsc_memory_free() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
free((void*)memory);
stackPushInt(0);
}
void gsc_memory_int_get()
{
int memory;
if ( ! stackGetParams("i", &memory))
{
stackError("gsc_memory_int_get() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushInt(*(int*)memory);
}
void gsc_memory_int_set()
{
int memory, value;
if ( ! stackGetParams("ii", &memory, &value))
{
stackError("gsc_memory_int_set() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
*(int*)memory = value;
stackPushInt(1);
}
void gsc_memory_memset()
{
int memory, value, bytes;
if ( ! stackGetParams("iii", &memory, &value, &bytes))
{
stackError("gsc_memory_memset() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
memset((void*)memory, value, bytes);
stackPushInt(1);
}
#include <vector>
struct binarybuffer
{
int address;
int pos;
std::vector<char *> *strings;
};
void gsc_binarybuffer_new()
{
int address;
if ( ! stackGetParams("i", &address))
{
stackError("gsc_binarybuffer_new() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
struct binarybuffer *bb = (struct binarybuffer *)malloc(sizeof(struct binarybuffer));
bb->address = address;
bb->pos = 0;
bb->strings = new std::vector<char *>();
stackPushInt((int)bb);
}
void gsc_binarybuffer_free()
{
struct binarybuffer *bb;
if ( ! stackGetParams("i", &bb))
{
stackError("gsc_binarybuffer_free() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
for (std::vector<char *>::const_iterator i = bb->strings->begin(); i != bb->strings->end(); i++)
free(*i);
delete bb->strings;
free(bb);
stackPushInt(1);
}
void gsc_binarybuffer_seek()
{
struct binarybuffer *bb;
int pos;
if ( ! stackGetParams("ii", &bb, &pos))
{
stackError("gsc_binarybuffer_seek() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
bb->pos = pos;
stackPushInt(1);
}
void gsc_binarybuffer_write()
{
struct binarybuffer *bb;
char *type;
if ( ! stackGetParams("is", &bb, &type))
{
stackError("gsc_binarybuffer_write() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
switch (type[0])
{
case 'i':
{
int tmp_int;
stackGetParamInt(2, &tmp_int);
*(int *)(bb->address + bb->pos) = tmp_int;
bb->pos += 4;
break;
}
case 'f':
{
float tmp_float;
stackGetParamFloat(2, &tmp_float);
*(float *)(bb->address + bb->pos) = tmp_float;
bb->pos += 4;
break;
}
case 'd':
{
float tmp_float;
stackGetParamFloat(2, &tmp_float);
*(double *)(bb->address + bb->pos) = (double)tmp_float;
bb->pos += 8;
break;
}
case 's':
{
char *tmp_str;
stackGetParamString(2, &tmp_str);
char *copy = (char *)malloc(strlen(tmp_str) + 1);
strcpy(copy, tmp_str);
bb->strings->push_back(copy);
*(char **)(bb->address + bb->pos) = copy;
bb->pos += 4;
break;
}
case 'c':
{
char *tmp_str;
stackGetParamString(2, &tmp_str);
*(char *)(bb->address + bb->pos) = tmp_str[0];
bb->pos += 1;
break;
}
case 'v':
{
float tmp_vector[3];
stackGetParamVector(2, tmp_vector);
*(float *)(bb->address + bb->pos + 0) = tmp_vector[0];
*(float *)(bb->address + bb->pos + 4) = tmp_vector[1];
*(float *)(bb->address + bb->pos + 8) = tmp_vector[2];
bb->pos += 12;
break;
}
}
stackPushInt(1);
}
void gsc_binarybuffer_read()
{
struct binarybuffer *bb;
char *type;
if ( ! stackGetParams("is", &bb, &type))
{
stackError("gsc_binarybuffer_read() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
switch (type[0])
{
case 'i':
{
int tmp_int;
tmp_int = *(int *)(bb->address + bb->pos);
bb->pos += 4;
stackPushInt(tmp_int);
return;
}
case 'f':
{
float tmp_float;
tmp_float = *(float *)(bb->address + bb->pos);
bb->pos += 4;
stackPushFloat(tmp_float);
return;
}
case 'd':
{
float tmp_float;
tmp_float = (float)*(double *)(bb->address + bb->pos);
bb->pos += 8;
stackPushFloat(tmp_float);
return;
}
case 's':
{
char *tmp_str;
tmp_str = *(char **)(bb->address + bb->pos);
bb->pos += 4;
stackPushString(tmp_str);
return;
}
case 'c':
{
char tmp_str[2];
tmp_str[0] = *(char *)(bb->address + bb->pos);
tmp_str[1] = '\0';
bb->pos += 1;
stackPushString(tmp_str);
return;
}
case 'v':
{
float *tmp_vector;
tmp_vector = (float *)(bb->address + bb->pos + 0);
bb->pos += 12;
stackPushVector(tmp_vector);
return;
}
}
stackPushUndefined();
}
#endif