]> gitweb.factorcode.org Git - factor.git/blob - vm/allot.hpp
VM: undo 7d9bad465ca447dc5b407044a6ce4f49c4f40686
[factor.git] / vm / allot.hpp
1 namespace factor {
2
3 // It is up to the caller to fill in the object's fields in a
4 // meaningful fashion!
5
6 // Allocates memory
7 inline code_block* factor_vm::allot_code_block(cell size,
8                                                code_block_type type) {
9   cell block_size = size + sizeof(code_block);
10   code_block* block = code->allocator->allot(block_size);
11
12   if (block == NULL) {
13     // If allocation failed, do a full GC and compact the code heap.
14     // A full GC that occurs as a result of the data heap filling up does not
15     // trigger a compaction. This setup ensures that most GCs do not compact
16     // the code heap, but if the code fills up, it probably means it will be
17     // fragmented after GC anyway, so its best to compact.
18     primitive_compact_gc();
19     block = code->allocator->allot(block_size);
20
21     // Insufficient room even after code GC, give up
22     if (block == NULL) {
23       std::cout << "Code heap used: " << code->allocator->occupied_space()
24                 << "\n";
25       std::cout << "Code heap free: " << code->allocator->free_space << "\n";
26       std::cout << "Request       : " << block_size << "\n";
27       fatal_error("Out of memory in allot_code_block", 0);
28     }
29   }
30
31   // next time we do a minor GC, we have to trace this code block, since
32   // the fields of the code_block struct might point into nursery or aging
33   this->code->write_barrier(block);
34
35   block->set_type(type);
36   return block;
37 }
38
39 // Allocates memory
40 inline object* factor_vm::allot_large_object(cell type, cell size) {
41   // If tenured space does not have enough room, collect and compact
42   cell required_free = size + data->high_water_mark();
43   if (!data->tenured->can_allot_p(required_free)) {
44     primitive_compact_gc();
45
46     // If it still won't fit, grow the heap
47     if (!data->tenured->can_allot_p(required_free)) {
48       gc(COLLECT_GROWING_DATA_HEAP_OP, size);
49     }
50   }
51   object* obj = data->tenured->allot(size);
52
53   // Allows initialization code to store old->new pointers
54   // without hitting the write barrier in the common case of
55   // a nursery allocation
56   write_barrier(obj, size);
57
58   obj->initialize(type);
59   return obj;
60 }
61
62 // Allocates memory
63 inline object* factor_vm::allot_object(cell type, cell size) {
64   FACTOR_ASSERT(!current_gc);
65
66   bump_allocator *nursery = data->nursery;
67
68   // If the object is bigger than the nursery, allocate it in tenured space
69   if (size >= nursery->size)
70     return allot_large_object(type, size);
71
72   // If the object is smaller than the nursery, allocate it in the nursery,
73   // after a GC if needed
74   if (nursery->here + size > nursery->end)
75     primitive_minor_gc();
76
77   object* obj = nursery->allot(size);
78   obj->initialize(type);
79
80   return obj;
81 }
82
83 }