X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=code.cpp;h=645ad257042b5973c20d4b08e5701e7be9e812a0;hp=861d5520bd4742881d93c77559dcd669706b697c;hb=451873ae529289f4a22d8bbce6ebf0e074da8f2b;hpb=35a8c3c9af67542d50d6a07a00501f6fe3767365 diff --git a/code.cpp b/code.cpp index 861d552..645ad25 100644 --- a/code.cpp +++ b/code.cpp @@ -106,35 +106,39 @@ void code_pop_statement(code_t *code) code->columnnums.pop_back(); } -code_t *code_init() { +void *code_t::operator new(std::size_t bytes) { + return mem_a(bytes); +} + +void code_t::operator delete(void *ptr) { + mem_d(ptr); +} + +code_t::code_t() +{ static lex_ctx_t empty_ctx = {0, 0, 0}; static prog_section_function_t empty_function = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}}; static prog_section_statement_t empty_statement = {0,{0},{0},{0}}; static prog_section_def_t empty_def = {0, 0, 0}; - code_t *code = (code_t*)mem_a(sizeof(code_t)); - int i = 0; + string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024); - memset(code, 0, sizeof(code_t)); - code->entfields = 0; - code->string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024); + // The way progs.dat is suppose to work is odd, there needs to be + // some null (empty) statements, functions, and 28 globals + globals.insert(globals.begin(), 28, 0); - /* - * The way progs.dat is suppose to work is odd, there needs to be - * some null (empty) statements, functions, and 28 globals - */ - for(; i < 28; i++) - code->globals.push_back(0); + chars.push_back('\0'); + functions.push_back(empty_function); - code->chars.push_back('\0'); - code->functions.push_back(empty_function); + code_push_statement(this, &empty_statement, empty_ctx); - code_push_statement(code, &empty_statement, empty_ctx); - - code->defs.push_back(empty_def); - code->fields.push_back(empty_def); + defs.push_back(empty_def); + fields.push_back(empty_def); +} - return code; +code_t::~code_t() +{ + util_htdel(string_cache); } void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin); @@ -167,7 +171,6 @@ uint32_t code_genstring(code_t *code, const char *str) { CODE_HASH_LEAVE(existing) = code->chars.size(); code->chars.insert(code->chars.end(), str, str + strlen(str) + 1); - //vec_append(code->chars, strlen(str)+1, str); util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing)); return CODE_HASH_LEAVE(existing); @@ -292,7 +295,7 @@ static void code_stats(const char *filename, const char *lnofile, code_t *code, bool code_write(code_t *code, const char *filename, const char *lnofile) { prog_header_t code_header; - FILE *fp = NULL; + FILE *fp = nullptr; code_create_header(code, &code_header, filename, lnofile); @@ -320,7 +323,7 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) { } fclose(fp); - fp = NULL; + fp = nullptr; } fp = fopen(filename, "wb"); @@ -343,8 +346,3 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) { code_stats(filename, lnofile, code, &code_header); return true; } - -void code_cleanup(code_t *code) { - util_htdel(code->string_cache); - mem_d(code); -}