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.
44 45 46 47 |
# File 'lib/osctld/id_range/allocation_table.rb', line 44 def initialize(block_count) @block_count = block_count @table = [] end |
Instance Attribute Details
#block_count ⇒ Integer (readonly)
41 42 43 |
# File 'lib/osctld/id_range/allocation_table.rb', line 41 def block_count @block_count end |
#table ⇒ Object (readonly, protected)
Returns the value of attribute table.
193 194 195 |
# File 'lib/osctld/id_range/allocation_table.rb', line 193 def table @table end |
Class Method Details
.load(block_count, data) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/osctld/id_range/allocation_table.rb', line 29 def self.load(block_count, data) t = new(block_count) data.each do |v| allocation = Allocation.load(v) t.allocate_at(allocation.block_index, allocation.block_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
203 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 |
# File 'lib/osctld/id_range/allocation_table.rb', line 203 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.block_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.block_index, a1.block_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.block_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
95 96 97 98 99 100 101 102 |
# File 'lib/osctld/id_range/allocation_table.rb', line 95 def allocate(count, owner) table_index, block_index, _count = free_segments(count).first return false if table_index.nil? alloc = Allocation.new(block_index, count, owner) table.insert(table_index, alloc) alloc.export end |
#allocate_at(index, count, owner) ⇒ Hash, false
Allocate blocks on a specific position
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/osctld/id_range/allocation_table.rb', line 74 def allocate_at(index, count, owner) raise ArgumentError, "unable to allocate #{count} blocks at #{index}" if count <= 0 raise ArgumentError, "unable to allocate #{count} blocks at #{index}" unless free_at?(index, count) alloc = Allocation.new(index, count, owner) table.each_with_index do |v, t_i| if v.block_index > index table.insert(t_i, alloc) return alloc.export end end table << alloc alloc.export end |
#count_allocated_blocks ⇒ Integer
130 131 132 |
# File 'lib/osctld/id_range/allocation_table.rb', line 130 def count_allocated_blocks table.inject(0) { |sum, v| sum + v.block_count } end |
#count_free_blocks ⇒ Integer
135 136 137 138 139 140 |
# File 'lib/osctld/id_range/allocation_table.rb', line 135 def count_free_blocks free_segments.inject(0) do |sum, v| _, _, count = v sum + count end end |
#dump ⇒ Object
147 148 149 |
# File 'lib/osctld/id_range/allocation_table.rb', line 147 def dump table.map(&:dump) end |
#empty? ⇒ Boolean
143 144 145 |
# File 'lib/osctld/id_range/allocation_table.rb', line 143 def empty? table.empty? end |
#export_all ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/osctld/id_range/allocation_table.rb', line 151 def export_all all_segments.map do |type, _table_index, block_index, count, owner| { type:, block_index:, block_count: count, owner: } end end |
#export_allocated ⇒ Object
162 163 164 |
# File 'lib/osctld/id_range/allocation_table.rb', line 162 def export_allocated table.map(&:export) end |
#export_at(index) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/osctld/id_range/allocation_table.rb', line 176 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:, block_index:, block_count: count, owner: } end end raise ArgumentError, 'index out of range' end |
#export_free ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/osctld/id_range/allocation_table.rb', line 166 def export_free free_segments.map do |_table_index, block_index, count| { block_index:, block_count: count } end end |
#free_at(index) ⇒ Boolean
Free allocation starting at a specified position
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/osctld/id_range/allocation_table.rb', line 107 def free_at(index) table.each_with_index do |alloc, t_i| if alloc.block_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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/osctld/id_range/allocation_table.rb', line 53 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`
121 122 123 124 125 126 127 |
# File 'lib/osctld/id_range/allocation_table.rb', line 121 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
252 253 254 255 256 257 258 |
# File 'lib/osctld/id_range/allocation_table.rb', line 252 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 |