| 37 | } |
| 38 | }; |
| 39 | |
| 40 | # GC don't care how this work. It just have to work _fast_. |
| 41 | class Allocator { |
| 42 | has $object_size; |
| 43 | |
| 44 | method allocate() { ... }; |
| 45 | method free($obj) { ... }; |
| 46 | |
| 47 | method Bool is_owned($obj) { ... }; |
| 48 | }; |
| 49 | |
| 50 | class PoolAllocator { |
| 51 | # 4KB page |
| 52 | # We reuse storage to store index of next free object. |
| 53 | # For detailed design see Alexandresku's SmallObjectAllocator |
| 54 | # In the nutshell: |
| 55 | # Initial content of $current_index, @objects is |
| 56 | # 0 => [1, 2, 3, -1] |
| 57 | # first alloc: |
| 58 | # 1 => [*, 2, 3, -1] |
| 59 | # second alloc: |
| 60 | # 2 => [*, *, 3, -1] |
| 61 | # free first object |
| 62 | # 0 => [2, *, 3, -1] |
| 63 | class Page { |
| 64 | # fixed size header. |
| 65 | my int $current_index; |
| 66 | my int $object_size; |
| 67 | my Page* $next_free_page; |
| 68 | |
| 69 | # Number of objects is (4096-sizeof(header))/$object_size; |
| 70 | my @objects; |
| 71 | |
| 72 | method BUILD() { |
| 73 | self.current_index := 0; |
| 74 | # Initialize "linked list" |
| 75 | for 0..+self.objects-1 -> $i { |
| 76 | self.objects[$i] = $i+1; |
| 77 | } |
| 78 | # Terminate list |
| 79 | self.objects[*-1] := -1; |
| 80 | } |
| 81 | |
| 82 | method allocate() { |
| 83 | # Use @objects as "linked list" |
| 84 | my $res = (void*)self.objects[self.current_index]; |
| 85 | self.current_index = (int)*$res; |
| 86 | $res; |
| 87 | } |
| 88 | |
| 89 | method free($obj) { |
| 90 | # Get index of freed object |
| 91 | my $index := ptr_diff($obj - @objects)/self.object_size; |
| 92 | |
| 93 | # Store current_index in it. |
| 94 | self.objects[$index] = self.current_index; |
| 95 | self.current_index = $index; |
| 96 | } |
| 97 | |
| 98 | method is_full() { self.current_index < 0 }; |
| 99 | }; |
| 100 | |
| 101 | # Pool data |
| 102 | # Sorted array of pages to use binary search for finding pointer. |
| 103 | has @pages of Page; |
| 104 | # Linked list to pages with free storage. |
| 105 | has $current_page; |
| 106 | |
| 107 | has $object_size; |
| 108 | |
| 109 | method BUILD() { |
| 110 | self.current_page = Page->new(self.object_size); |
| 111 | self.pages.push(self.current_page); |
| 112 | } |
| 113 | |
| 114 | method allocate() { |
| 115 | # Invariant: current page always have space. |
| 116 | my $res = self.current_page.allocate(); |
| 117 | |
| 118 | # Allocate new page if it's filled |
| 119 | if self.current_page.is_full { |
| 120 | # Try to reuse old page |
| 121 | my $page = self.current_page.next_free_page; |
| 122 | if $page { |
| 123 | self.current_page.next_free_page := NULL; |
| 124 | self.current_page = $page; |
| 125 | } |
| 126 | else { |
| 127 | # We have to allocate new one. |
| 128 | $page = Page->new(self.object_size); |
| 129 | self.current_page = $page; |
| 130 | |
| 131 | # Use binary search to put $page into proper position. |
| 132 | my $pos = lower_bound(self.pages, $page); |
| 133 | self.pages.splice($pos+1, 0, 0, $page); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | method free($obj) { |
| 139 | my $page = self.find_page($obj); |
| 140 | assert($page); |
| 141 | # If page was full than put it on free list. |
| 142 | my $put_in_free_list := !$page.is_full; |
| 143 | |
| 144 | $page.free($obj); |
| 145 | |
| 146 | if $put_in_free_list { |
| 147 | $page.next_free_page = self.current_page; |
| 148 | self.current_page = $page; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | # Find page which holds $obj. |
| 153 | method find_page($obj) { |
| 154 | my $pos := lower_bound(self.pages, $obj); |
| 155 | if ptr_diff(self.pages[$pos], $obj) < 4096 { |
| 156 | return self.pages[$pos]; |
| 157 | } |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | # Pool owns $obj if there is Page for it. |
| 162 | method bool is_owned($obj) { |
| 163 | self.find_page($obj) != NULL; |