Class: OsCtld::IdRange::AllocationTable

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/id_range/allocation_table.rb

Defined Under Namespace

Classes: Allocation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_count) ⇒ AllocationTable

Returns a new instance of AllocationTable.

Parameters:

  • block_count (Integer)


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_countInteger (readonly)

Returns:

  • (Integer)


37
38
39
# File 'lib/osctld/id_range/allocation_table.rb', line 37

def block_count
  @block_count
end

#tableObject (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

Yield Parameters:

  • type (:allocated, :free)
  • table_index (Integer)

    index in @table

  • block_index (Integer)

    index of the block in the entire allocation table

  • count (Integer)

    number of free blocks at this position

  • owner (String)

Returns:

  • (Enumerator)


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

Parameters:

  • count (Integer)

    number of blocks

  • owner (String)

    arbitrary allocation owner

Returns:

  • (Hash, false)


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

Parameters:

  • index (Integer)

    block index

  • count (Integer)

    number of blocks

  • owner (String)

    arbitrary allocation owner

Returns:

  • (Hash, false)


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_blocksInteger

Returns:

  • (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_blocksInteger

Returns:

  • (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

#dumpObject



149
150
151
# File 'lib/osctld/id_range/allocation_table.rb', line 149

def dump
  table.map(&:dump)
end

#empty?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/osctld/id_range/allocation_table.rb', line 145

def empty?
  table.empty?
end

#export_allObject



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_allocatedObject



164
165
166
# File 'lib/osctld/id_range/allocation_table.rb', line 164

def export_allocated
  table.map(&:export)
end

#export_at(index) ⇒ Object

Parameters:

  • index (Integer)

    block index

Raises:

  • (ArgumentError)


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_freeObject



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

Parameters:

  • index (Integer)

    block index

Returns:

  • (Boolean)


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

Parameters:

  • index (Integer)

    block index

  • count (Integer)

    number of blocks from `index` that have to be free

Returns:

  • (Boolean)


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`

Parameters:

  • owner (String)

Returns:

  • (Boolean)


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

Parameters:

  • count (Integer) (defaults to: 1)

    specify a minimum free segment size, i.e. a number of blocks

Yield Parameters:

  • table_index (Integer)

    index in @table

  • block_index (Integer)

    index of the block in the entire allocation table

  • count (Integer)

    number of free blocks at this position

Returns:

  • (Enumerator)


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