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

View File

@ -211,6 +211,17 @@ ostd::String floatstr(float v);
bool cs_check_num(ostd::ConstCharRange s); 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 */ } /* namespace cscript */
#endif /* LIBCUBESCRIPT_CS_VM_HH */ #endif /* LIBCUBESCRIPT_CS_VM_HH */

View File

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