5 callback_heap::callback_heap(cell size, factor_vm* parent) {
6 seg = new segment(size, true);
8 fatal_error("Out of memory in callback_heap constructor", size);
9 allocator = new free_list_allocator<code_block>(size, seg->start);
10 this->parent = parent;
14 callback_heap::~callback_heap() {
22 void factor_vm::init_callbacks(cell size) {
23 callbacks = new callback_heap(size, this);
26 bool callback_heap::setup_seh_p() {
27 #if defined(WINDOWS) && defined(FACTOR_X86)
34 bool callback_heap::return_takes_param_p() {
35 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
42 instruction_operand callback_heap::callback_operand(code_block* stub,
44 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
45 tagged<byte_array> relocation_template(
46 array_nth(code_template.untagged(), 0));
48 relocation_entry entry(relocation_template->data<relocation_entry>()[index]);
49 return instruction_operand(entry, stub, 0);
52 void callback_heap::store_callback_operand(code_block* stub, cell index) {
53 parent->store_external_address(callback_operand(stub, index));
56 void callback_heap::store_callback_operand(code_block* stub, cell index,
58 callback_operand(stub, index).store_value(value);
61 void callback_heap::update(code_block* stub) {
62 store_callback_operand(stub, setup_seh_p() ? 2 : 1,
63 callback_entry_point(stub));
67 code_block* callback_heap::add(cell owner, cell return_rewind) {
68 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
69 tagged<byte_array> insns(array_nth(code_template.untagged(), 1));
70 cell size = array_capacity(insns.untagged());
72 cell bump = align(size + sizeof(code_block), data_alignment);
73 code_block* stub = allocator->allot(bump);
75 parent->general_error(ERROR_CALLBACK_SPACE_OVERFLOW,
80 stub->header = bump & ~7;
82 stub->parameters = false_object;
83 stub->relocation = false_object;
85 memcpy((void*)stub->entry_point(), insns->data<void>(), size);
87 /* Store VM pointer */
88 store_callback_operand(stub, 0, (cell)parent);
93 store_callback_operand(stub, 1);
98 /* Store VM pointer */
99 store_callback_operand(stub, index + 2, (cell) parent);
101 /* On x86, the RET instruction takes an argument which depends on
102 the callback's calling convention */
103 if (return_takes_param_p())
104 store_callback_operand(stub, index + 3, return_rewind);
111 struct callback_updater {
112 callback_heap* callbacks;
114 explicit callback_updater(callback_heap* callbacks)
115 : callbacks(callbacks) {}
117 void operator()(code_block* stub, cell size) {
118 callbacks->update(stub);
122 void callback_heap::update() {
123 callback_updater updater(this);
124 allocator->iterate(updater);
127 /* Allocates memory (add(), allot_alien())*/
128 void factor_vm::primitive_callback() {
129 cell return_rewind = to_cell(ctx->pop());
130 tagged<word> w(ctx->pop());
134 cell func = callbacks->add(w.value(), return_rewind)->entry_point();
135 CODE_TO_FUNCTION_POINTER_CALLBACK(this, func);
136 ctx->push(allot_alien((void*)func));
139 void factor_vm::primitive_free_callback() {
140 void* entry_point = alien_offset(ctx->pop());
141 code_block* stub = (code_block*)entry_point - 1;
142 callbacks->allocator->free(stub);
145 /* Allocates memory */
146 void factor_vm::primitive_callback_room() {
147 allocator_room room = callbacks->allocator->as_allocator_room();
148 ctx->push(tag<byte_array>(byte_array_from_value(&room)));