-
Notifications
You must be signed in to change notification settings - Fork 29
/
gsc_memory.cpp
executable file
·264 lines (247 loc) · 6.31 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
#include "gsc_memory.hpp"
#if COMPILE_MEMORY == 0
#warning gsc_memory.cpp is not compiled
#else
void gsc_memory_malloc() {
int bytes;
if ( ! stackGetParams("i", &bytes)) {
printf("scriptengine> wrongs args for gsc_memory_malloc(bytes);\n");
stackPushUndefined();
return;
}
#if DEBUG_MEMORY
printf("gsc_memory_malloc(bytes=%d)\n", bytes);
#endif
stackReturnInt((int) malloc(bytes));
}
void gsc_memory_free() {
int memory;
if ( ! stackGetParams("i", &memory)) {
printf("scriptengine> wrongs args for gsc_memory_free(memory);\n");
stackPushUndefined();
return;
}
#if DEBUG_MEMORY
printf("gsc_memory_free(memory=%d)\n", memory);
#endif
free((void*)memory);
stackReturnInt(0);
}
void gsc_memory_int_get() {
int memory;
if ( ! stackGetParams("i", &memory)) {
printf("scriptengine> wrongs args for gsc_memory_int_get(memory);\n");
stackPushUndefined();
return;
}
#if DEBUG_MEMORY
printf("gsc_memory_int_get(memory=%d)\n", memory);
#endif
stackReturnInt(*(int*)memory);
}
void gsc_memory_int_set() {
int memory, value;
if ( ! stackGetParams("ii", &memory, &value)) {
printf("scriptengine> wrongs args for gsc_memory_int_set(memory, value);\n");
stackPushUndefined();
return;
}
#if DEBUG_MEMORY
printf("gsc_memory_int_set(memory=%d, value=%d)\n", memory, value);
#endif
*(int*)memory = value;
stackReturnInt(1);
}
void gsc_memory_memset() {
int memory, value, bytes;
if ( ! stackGetParams("iii", &memory, &value, &bytes)) {
printf("scriptengine> wrongs args for gsc_memory_memset(memory, value, bytes);\n");
stackPushUndefined();
return;
}
#if DEBUG_MEMORY
printf("gsc_memory_memset(memory=%d, value=%d, bytes=%d)\n", memory, value, bytes);
#endif
memset((void*)memory, value, bytes);
stackReturnInt(1);
}
#include <vector>
struct binarybuffer {
int address;
int pos;
std::vector<char *> *strings;
};
void gsc_binarybuffer_new() {
int address;
if ( ! stackGetParams("i", &address)) {
printf("scriptengine> wrongs args for binarybuffer_new(address);\n");
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)) {
printf("scriptengine> wrongs args for binarybuffer_free(binarybuffer);\n");
stackPushUndefined();
return;
}
for (std::vector<char *>::const_iterator i = bb->strings->begin(); i != bb->strings->end(); i++)
free(*i);
delete bb->strings;
free(bb);
stackReturnInt(1);
}
void gsc_binarybuffer_seek() {
struct binarybuffer *bb;
int pos;
if ( ! stackGetParams("ii", &bb, &pos)) {
printf("scriptengine> wrongs args for binarybuffer_seek(binarybuffer, pos);\n");
stackPushUndefined();
return;
}
bb->pos = pos;
stackReturnInt(1);
}
void gsc_binarybuffer_write() {
struct binarybuffer *bb;
char *type;
if ( ! stackGetParams("is", &bb, &type)) {
printf("scriptengine> wrongs args for binarybuffer_write(binarybuffer, type [, value] );\n");
stackPushUndefined();
return;
}
switch (type[0]) {
case 'i': {
int tmp_int;
stackGetParamInt(2, &tmp_int);
*(int *)(bb->address + bb->pos) = tmp_int;
#if DEBUG_MEMORY
printf("type=i bb->address=%.8p + %d = %d\n", bb->address, bb->pos, *(int *)(bb->address + bb->pos));
#endif
bb->pos += 4;
break;
}
case 'f': {
float tmp_float;
stackGetParamFloat(2, &tmp_float);
*(float *)(bb->address + bb->pos) = tmp_float;
#if DEBUG_MEMORY
printf("type=f bb->address=%.8p + %d = %f\n", bb->address, bb->pos, *(float *)(bb->address + bb->pos));
#endif
bb->pos += 4;
break;
}
case 'd': {
float tmp_float;
stackGetParamFloat(2, &tmp_float);
*(double *)(bb->address + bb->pos) = (double)tmp_float;
#if DEBUG_MEMORY
printf("type=d bb->address=%.8p + %d = %f\n", bb->address, bb->pos, *(double *)(bb->address + bb->pos));
#endif
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;
#if DEBUG_MEMORY
printf("type=s bb->address=%.8p + %d = %s\n", bb->address, bb->pos, *(char **)(bb->address + bb->pos));
#endif
bb->pos += 4;
break;
}
case 'c': {
char *tmp_str;
stackGetParamString(2, &tmp_str);
*(char *)(bb->address + bb->pos) = tmp_str[0];
#if DEBUG_MEMORY
printf("type=c bb->address=%.8p + %d = %c\n", bb->address, bb->pos, *(char *)(bb->address + bb->pos));
#endif
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];
#if DEBUG_MEMORY
printf("type=v bb->address=%.8p + %d = (%f,%f,%f)\n", bb->address, bb->pos,
*(float *)(bb->address + bb->pos + 0),
*(float *)(bb->address + bb->pos + 4),
*(float *)(bb->address + bb->pos + 8)
);
#endif
bb->pos += 12;
break;
}
}
stackReturnInt(1);
}
void gsc_binarybuffer_read() {
struct binarybuffer *bb;
char *type;
if ( ! stackGetParams("is", &bb, &type)) {
printf("scriptengine> wrongs args for binarybuffer_read(binarybuffer, type);\n");
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