void stmt_init(void) { int i; for (i = 0; i < MAX_SWITCH_NEST; i++) switch_arena[i] = arena_new(sizeof(SwitchLabel)*8, FALSE); label_names_arena = arena_new(sizeof(LabelName)*16, FALSE); }
static PageInfo* alloc_new_page(size_t cell_size) { SPEW(2, _ejs_log ("allocating new page for cell size %zd\n", cell_size)); PageInfo *rv = NULL; for (int i = 0; i < num_arenas; i ++) { rv = alloc_page_from_arena(heap_arenas[i], cell_size); if (rv) { SPEW(2, _ejs_log (" => %p", rv)); return rv; } } // need a new arena SPEW(2, _ejs_log ("unable to find page in current arenas, allocating a new one")); LOCK_ARENAS(); Arena* arena = arena_new(); UNLOCK_ARENAS(); if (arena == NULL) return NULL; rv = alloc_page_from_arena(arena, cell_size); SPEW(2, _ejs_log (" => %p", rv)); return rv; }
// topological sort lex contexts, and inline all refs // TODO integrate with external language definition void sb_inline_partial_references(Compiler* compiler) { // build partial node map and dep graph Val ins_list = AT(compiler->ast, 0); // node map for query struct ContextMap context_map; ContextMap.init(&context_map); size_t size = 0; for (Val curr_ins = ins_list; curr_ins != VAL_NIL; curr_ins = TAIL(curr_ins)) { Val child = HEAD(curr_ins); if (IS_A(child, "Lex")) { // Lex[context, rules] Val name = AT(child, 0); // todo error for duplicated lex name ContextMap.insert(&context_map, name, AT(child, 1)); size++; } } // build toposort data structure // dep_network: array of dep_node links DepNode* dep_network[size]; memset(dep_network, 0, sizeof(DepNode*) * size); Arena* node_arena = arena_new(); int dep_network_size = 0; ContextMapIter it; for (ContextMap.iter_init(&it, &context_map); !ContextMap.iter_is_end(&it); ContextMap.iter_next(&it)) { Val name = it.slot->k; DepNode* tail = _dep_node_new(node_arena); tail->ctx_name = name; dep_network[dep_network_size] = tail; Val rule_body = it.slot->v; for (Val rule_cons = rule_body; rule_cons != VAL_NIL; rule_cons = TAIL(rule_cons)) { Val rule = HEAD(rule_cons); if (IS_A(rule, "RefPartialContext")) { // RefPartialContext[context] tail->next = _dep_node_new(node_arena); tail = tail->next; tail->ctx_name = AT(rule, 0); } } dep_network_size++; } // toposort and expand for (; dep_network_size > 0; dep_network_size--) { Val name; if (_pop_zero_deg_node(dep_network, dep_network_size, &name)) { dep_network_size--; // _print_network(dep_network, dep_network_size); _remove_edges(dep_network, dep_network_size, name); _expand_lex_def(&context_map, name); } else { log_err("loop dependency found:"); _print_network(dep_network, dep_network_size); ContextMap.cleanup(&context_map); arena_delete(node_arena); COMPILE_ERROR("fatal"); } } // update lex nodes Val res = VAL_NIL; for (Val curr_ins = ins_list; curr_ins != VAL_NIL; curr_ins = TAIL(curr_ins)) { Val child = HEAD(curr_ins); if (IS_A(child, "Lex")) { // Lex[context, rules] Val name = AT(child, 0); if (nb_string_ptr(name)[0] != '*') { Val converted; ContextMap.find(&context_map, name, &converted); res = nb_cons_new(child, res); } } else { res = nb_cons_new(child, res); } } REPLACE(compiler->ast, nb_struct_set(compiler->ast, 0, res)); ContextMap.cleanup(&context_map); arena_delete(node_arena); }
int uct_initialize() { UCT_NODE_ARENA = arena_new(sizeof(UCTNode), 10000000); return 1; }
// add new gen, and return the number (doesn't select it) int32_t nb_gens_new_gen(Gens* g) { Arena* arena = arena_new(); int index = Arenas.size(&g->arenas); Arenas.push(&g->arenas, arena); return index; }
void init_local_table(void) { local_arena = arena_new(32768, FALSE); }
END(double) TEST(fixnum) value_t x = value_from_int(322); PT_ASSERT_EQ(value_to_int(x), 322); END(fixnum) TEST(bool) value_t x = value_true(); PT_ASSERT(value_to_bool(x)); x = value_false(); PT_ASSERT(!value_to_bool(x)); END(bool) TEST(ptr) struct arena_handle *a = arena_new((yu_allocator *)&mctx); struct boxed_value *v = arena_alloc_val(a); value_t x = value_from_ptr(&v); PT_ASSERT_EQ(value_get_ptr(x), v); PT_ASSERT_EQ(boxed_value_owner(v), a); END(ptr) TEST(value_type) struct arena_handle *a = arena_new((yu_allocator *)&mctx); struct boxed_value *v1 = arena_alloc_val(a), *v2 = arena_alloc_val(a); value_t w = value_from_int(655), x = value_true(), y = value_from_ptr(&v1), z = value_from_ptr(&v2); boxed_value_set_type(value_get_ptr(y), VALUE_REAL); boxed_value_set_type(value_get_ptr(z), VALUE_FIXNUM);