cleanups (centralize bytecode deletes)

master
Daniel Kolesa 2016-08-13 00:26:16 +01:00
parent be89f523f6
commit 75319a73ec
3 changed files with 28 additions and 22 deletions

View File

@ -3,6 +3,8 @@
#include <limits.h>
#include <ostd/memory.hh>
namespace cscript {
static inline ostd::Uint32 const *forcecode(CsState &cs, TaggedValue &v) {
@ -827,10 +829,9 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
(cs).stack = &aliaslink; \
if(!id->code) id->code = reinterpret_cast<Bytecode *>(compilecode(cs, id->get_str())); \
ostd::Uint32 *codep = reinterpret_cast<ostd::Uint32 *>(id->code); \
codep[0] += 0x100; \
bcode_incr(codep); \
runcode((cs), codep+1, (result)); \
codep[0] -= 0x100; \
if(int(codep[0]) < 0x100) delete[] codep; \
bcode_decr(codep); \
(cs).stack = aliaslink.next; \
(cs).identflags = oldflags; \
for(int i = 0; i < callargs; i++) \
@ -1094,7 +1095,7 @@ bool CsState::run_bool(Ident *id, TvalRange args) {
bool CsState::run_file(ostd::ConstCharRange fname) {
ostd::ConstCharRange oldsrcfile = src_file, oldsrcstr = src_str;
char *buf = nullptr;
ostd::Box<char[]> buf;
ostd::Size len;
ostd::FileStream f(fname, ostd::StreamMode::read);
@ -1102,19 +1103,17 @@ bool CsState::run_file(ostd::ConstCharRange fname) {
return false;
len = f.size();
buf = new char[len + 1];
if (f.get(buf, len) != len) {
delete[] buf;
buf = ostd::make_box<char[]>(len + 1);
if (!buf || f.get(buf.get(), len) != len) {
return false;
}
buf[len] = '\0';
src_file = fname;
src_str = ostd::ConstCharRange(buf, len);
run_int(buf);
src_str = ostd::ConstCharRange(buf.get(), len);
run_int(src_str);
src_file = oldsrcfile;
src_str = oldsrcstr;
delete[] buf;
return true;
}

View File

@ -211,6 +211,17 @@ ostd::String floatstr(float v);
bool cs_check_num(ostd::ConstCharRange s);
static inline void bcode_incr(ostd::Uint32 *bc) {
*bc += 0x100;
}
static inline void bcode_decr(ostd::Uint32 *bc) {
*bc -= 0x100;
if (ostd::Int32(*bc) < 0x100) {
delete[] bc;
}
}
} /* namespace cscript */
#endif /* LIBCUBESCRIPT_CS_VM_HH */

View File

@ -616,8 +616,7 @@ void Ident::get_cval(TaggedValue &v) const {
void Ident::clean_code() {
ostd::Uint32 *bcode = reinterpret_cast<ostd::Uint32 *>(code);
if (bcode) {
bcode[0] -= 0x100;
if (int(bcode[0]) < 0x100) delete[] bcode;
bcode_decr(bcode);
code = nullptr;
}
}
@ -2111,7 +2110,7 @@ ostd::Uint32 *compilecode(CsState &cs, ostd::ConstCharRange str) {
gs.gen_main(str);
ostd::Uint32 *code = new ostd::Uint32[gs.code.size()];
memcpy(code, gs.code.data(), gs.code.size() * sizeof(ostd::Uint32));
code[0] += 0x100;
bcode_incr(code);
return code;
}
@ -2119,16 +2118,16 @@ static void bcode_ref(ostd::Uint32 *code) {
if (!code) return;
switch (*code & CODE_OP_MASK) {
case CODE_START:
*code += 0x100;
bcode_incr(code);
return;
}
switch (code[-1]&CODE_OP_MASK) {
case CODE_START:
code[-1] += 0x100;
bcode_incr(&code[-1]);
break;
case CODE_OFFSET:
code -= int(code[-1] >> 8);
*code += 0x100;
bcode_incr(code);
break;
}
}
@ -2137,19 +2136,16 @@ static void bcode_unref(ostd::Uint32 *code) {
if (!code) return;
switch (*code & CODE_OP_MASK) {
case CODE_START:
*code -= 0x100;
if (int(*code) < 0x100) delete[] code;
bcode_decr(code);
return;
}
switch (code[-1]&CODE_OP_MASK) {
case CODE_START:
code[-1] -= 0x100;
if (int(code[-1]) < 0x100) delete[] &code[-1];
bcode_decr(&code[-1]);
break;
case CODE_OFFSET:
code -= int(code[-1] >> 8);
*code -= 0x100;
if (int(*code) < 0x100) delete[] code;
bcode_decr(code);
break;
}
}