Class: OsCtld::IdRange::AllocationTable
- Inherits:
-
Object
- Object
- OsCtld::IdRange::AllocationTable
- Defined in:
- lib/osctld/id_range/allocation_table.rb
Defined Under Namespace
Classes: Allocation
Instance Attribute Summary collapse
- #block_count ⇒ Integer readonly
-
#table ⇒ Object
readonly
protected
Returns the value of attribute table.
Class Method Summary collapse
Instance Method Summary collapse
-
#all_segments {|type, table_index, block_index, count, owner| ... } ⇒ Enumerator
protected
Iterate over allocated and free segments in the table.
-
#allocate(count, owner) ⇒ Hash, false
Allocate blocks anywhere in the table.
-
#allocate_at(index, count, owner) ⇒ Hash, false
Allocate blocks on a specific position.
- #count_allocated_blocks ⇒ Integer
- #count_free_blocks ⇒ Integer
- #dump ⇒ Object
- #empty? ⇒ Boolean
- #export_all ⇒ Object
- #export_allocated ⇒ Object
- #export_at(index) ⇒ Object
- #export_free ⇒ Object
-
#free_at(index) ⇒ Boolean
Free allocation starting at a specified position.
-
#free_at?(index, count) ⇒ Boolean
Check if blocks at specified position are free.
-
#free_by(owner) ⇒ Boolean
Free one allocation which is owned by `owner`.
-
#free_segments(count = 1) {|table_index, block_index, count| ... } ⇒ Enumerator
protected
Iterate over free segments in the table.
-
#initialize(block_count) ⇒ AllocationTable
constructor
A new instance of AllocationTable.
Constructor Details
#initialize(block_count) ⇒ AllocationTable
Returns a new instance of AllocationTable.
40 41 42 43 |
# File 'lib/osctld/id_range/allocation_table.rb', line 40 def initialize(block_count) @block_count = block_count @table = [] end |
Instance Attribute Details
#block_count ⇒ Integer (readonly)
37 38 39 |
# File 'lib/osctld/id_range/allocation_table.rb', line 37 def block_count @block_count end |
#table ⇒ Object (readonly, protected)
Returns the value of attribute table.
194 195 196 |
# File 'lib/osctld/id_range/allocation_table.rb', line 194 def table @table end |
Class Method Details
.load(block_count, data) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/osctld/id_range/allocation_table.rb', line 25 def self.load(block_count, data) t = new(block_count) data.each do |v| allocation = Allocation.load(v) t.allocate_at(allocation.index, allocation.count, allocation.owner) end t end |
Instance Method Details
#all_segments {|type, table_index, block_index, count, owner| ... } ⇒ Enumerator (protected)
Iterate over allocated and free segments in the table
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/osctld/id_range/allocation_table.rb', line 204 def all_segments Enumerator.new do |yielder| if table.empty? yielder << [:free, 0, 0, block_count, nil] next end t_i = 0 # Check if there is space before the first allocation free = table.first.index yielder << [:free, t_i, 0, free, nil] if free > 0 loop do next_t_i = t_i + 1 a1 = table[t_i] a2 = table[next_t_i] # Yield allocated block yielder << [:allocated, t_i, a1.index, a1.count, a1.owner] if a2.nil? # a1 is the last allocation, check if there is free space behind it free_count = block_count - a1.last_index - 1 if free_count > 0 yielder << [:free, next_t_i, a1.last_index + 1, free_count, nil] end break end # Report free space between a1 and a2 free = a2.index - a1.last_index - 1 yielder << [:free, next_t_i, a1.last_index + 1, free, nil] if free > 0 t_i = next_t_i end end end |
#allocate(count, owner) ⇒ Hash, false
Allocate blocks anywhere in the table
96 97 98 99 100 101 102 103 104 |
# File 'lib/osctld/id_range/allocation_table.rb', line 96 def allocate(count, owner) free_segments(count).each do |table_index, block_index, _count| alloc = Allocation.new(block_index, count, owner) table.insert(table_index, alloc) return alloc.export end false end |
#allocate_at(index, count, owner) ⇒ Hash, false
Allocate blocks on a specific position
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/osctld/id_range/allocation_table.rb', line 70 def allocate_at(index, count, owner) alloc = Allocation.new(index, count, owner) if table.empty? table << alloc return alloc.export elsif !free_at?(index, count) raise ArgumentError, "unable to allocate #{count} blocks at #{index}" end table.each_with_index do |v, t_i| if v.index > index table.insert(t_i, alloc) return alloc.export end end table << alloc alloc.export end |
#count_allocated_blocks ⇒ Integer
132 133 134 |
# File 'lib/osctld/id_range/allocation_table.rb', line 132 def count_allocated_blocks table.inject(0) { |sum, v| sum + v.count } end |
#count_free_blocks ⇒ Integer
137 138 139 140 141 142 |
# File 'lib/osctld/id_range/allocation_table.rb', line 137 def count_free_blocks free_segments.inject(0) do |sum, v| _, _, count = v sum + count end end |
#dump ⇒ Object
149 150 151 |
# File 'lib/osctld/id_range/allocation_table.rb', line 149 def dump table.map(&:dump) end |
#empty? ⇒ Boolean
145 146 147 |
# File 'lib/osctld/id_range/allocation_table.rb', line 145 def empty? table.empty? end |
#export_all ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/osctld/id_range/allocation_table.rb', line 153 def export_all all_segments.map do |type, table_index, block_index, count, owner| { type: type, block_index: block_index, block_count: count, owner: owner, } end end |
#export_allocated ⇒ Object
164 165 166 |
# File 'lib/osctld/id_range/allocation_table.rb', line 164 def export_allocated table.map(&:export) end |
#export_at(index) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/osctld/id_range/allocation_table.rb', line 178 def export_at(index) all_segments.each do |type, table_index, block_index, count, owner| if index >= block_index && index <= (block_index + count - 1) return { type: type, block_index: block_index, block_count: count, owner: owner, } end end raise ArgumentError, 'index out of range' end |
#export_free ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/osctld/id_range/allocation_table.rb', line 168 def export_free free_segments.map do |table_index, block_index, count| { block_index: block_index, block_count: count, } end end |
#free_at(index) ⇒ Boolean
Free allocation starting at a specified position
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/osctld/id_range/allocation_table.rb', line 109 def free_at(index) table.each_with_index do |alloc, t_i| if alloc.index == index table.delete_at(t_i) return true end end false end |
#free_at?(index, count) ⇒ Boolean
Check if blocks at specified position are free
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/osctld/id_range/allocation_table.rb', line 49 def free_at?(index, count) last_index = index + count - 1 free_segments(count).each do |_, free_index, free_count| last_free_index = free_index + free_count - 1 if index >= free_index && last_index <= last_free_index return true elsif free_index > last_index return false end end false end |
#free_by(owner) ⇒ Boolean
Free one allocation which is owned by `owner`
123 124 125 126 127 128 129 |
# File 'lib/osctld/id_range/allocation_table.rb', line 123 def free_by(owner) table.delete_if.with_index do |alloc, t_i| alloc.owner == owner end true end |
#free_segments(count = 1) {|table_index, block_index, count| ... } ⇒ Enumerator (protected)
Iterate over free segments in the table
253 254 255 256 257 258 259 |
# File 'lib/osctld/id_range/allocation_table.rb', line 253 def free_segments(count = 1) all_segments.select do |type, _, _, cnt| type == :free && cnt >= count end.map do |type, t_i, b_i, cnt| [t_i, b_i, cnt] end end |