]> gitweb.factorcode.org Git - factor.git/blob - vm/allot.hpp
VM: move allot_code_block() to the allot.hpp file
[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 allocation failed, do a full GC and compact the code heap.
13   // A full GC that occurs as a result of the data heap filling up does not
14   // trigger a compaction. This setup ensures that most GCs do not compact
15   // the code heap, but if the code fills up, it probably means it will be
16   // fragmented after GC anyway, so its best to compact.
17   if (block == NULL) {
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 requested_size = size + data->high_water_mark();
43   if (!data->tenured->can_allot_p(requested_size)) {
44     primitive_compact_gc();
45
46     // If it still won't fit, grow the heap
47     if (!data->tenured->can_allot_p(requested_size)) {
48       gc(collect_growing_data_heap_op, size);
49     }
50   }
51
52   object* obj = data->tenured->allot(size);
53
54   // Allows initialization code to store old->new pointers
55   // without hitting the write barrier in the common case of
56   // a nursery allocation
57   write_barrier(obj, size);
58
59   obj->initialize(type);
60   return obj;
61 }
62
63 // Allocates memory
64 inline object* factor_vm::allot_object(cell type, cell size) {
65   FACTOR_ASSERT(!current_gc);
66
67   bump_allocator *nursery = data->nursery;
68
69   // If the object is bigger than the nursery, allocate it in tenured space
70   if (size >= nursery->size)
71     return allot_large_object(type, size);
72
73   // If the object is smaller than the nursery, allocate it in the nursery,
74   // after a GC if needed
75   if (nursery->here + size > nursery->end)
76     primitive_minor_gc();
77
78   object* obj = nursery->allot(size);
79   obj->initialize(type);
80
81   return obj;
82 }
83
84 }