]> gitweb.factorcode.org Git - factor.git/blob - vm/allot.hpp
Put brackets around ipv6 addresses in `inet6 present`
[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() << "\n";
24       std::cout << "Code heap free:               " << code->allocator->free_space << "\n";
25       std::cout << "Code heap free_block_count:   " << code->allocator->free_block_count << "\n";
26       std::cout << "Code heap largest_free_block: " << code->allocator->largest_free_block() << "\n";
27       std::cout << "Request       : " << block_size << "\n";
28       fatal_error("Out of memory in allot_code_block", 0);
29     }
30   }
31
32   // next time we do a minor GC, we have to trace this code block, since
33   // the fields of the code_block struct might point into nursery or aging
34   this->code->write_barrier(block);
35
36   block->set_type(type);
37   return block;
38 }
39
40 // Allocates memory
41 inline object* factor_vm::allot_large_object(cell type, cell size) {
42   // If tenured space does not have enough room, collect and compact
43   cell required_free = size + data->high_water_mark();
44   if (!data->tenured->can_allot_p(required_free)) {
45     primitive_compact_gc();
46
47     // If it still won't fit, grow the heap
48     if (!data->tenured->can_allot_p(required_free)) {
49       gc(COLLECT_GROWING_DATA_HEAP_OP, size);
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 }