Class: Heapviz::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/heapviz/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, obj_start_address, capacity, slot_size) ⇒ Page

Returns a new instance of Page.



5
6
7
8
9
10
11
12
# File 'lib/heapviz/page.rb', line 5

def initialize(address, obj_start_address, capacity, slot_size)
  @address = address
  @obj_start_address = obj_start_address
  @capacity = capacity
  @slot_size = slot_size

  @live_objects = []
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



3
4
5
# File 'lib/heapviz/page.rb', line 3

def address
  @address
end

#live_objectsObject (readonly)

Returns the value of attribute live_objects.



3
4
5
# File 'lib/heapviz/page.rb', line 3

def live_objects
  @live_objects
end

#slot_sizeObject (readonly)

Returns the value of attribute slot_size.



3
4
5
# File 'lib/heapviz/page.rb', line 3

def slot_size
  @slot_size
end

Instance Method Details

#each_slotObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/heapviz/page.rb', line 19

def each_slot
  return enum_for(:each_slot) unless block_given?

  objs = sorted_objects

  @capacity.times do |i|
    expected = @obj_start_address + (i * slot_size)
    if objs.any? && objs.first.address == expected
      yield objs.shift
    else
      yield nil
    end
  end
end

#fill_slot(slot) ⇒ Object



14
15
16
17
# File 'lib/heapviz/page.rb', line 14

def fill_slot(slot)
  fail "slot size mismatch" if slot.size != @slot_size
  @live_objects << slot
end

#fragmented?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/heapviz/page.rb', line 50

def fragmented?
  !full?
end

#full?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/heapviz/page.rb', line 38

def full?
  @live_objects.count == @capacity
end

#live_object_countObject



46
47
48
# File 'lib/heapviz/page.rb', line 46

def live_object_count
  @live_objects.count
end

#pinned_countObject



42
43
44
# File 'lib/heapviz/page.rb', line 42

def pinned_count
  @pinned_count ||= live_objects.find_all { |lo| lo.dig("flags", "pinned") }.count
end

#sorted_objectsObject



34
35
36
# File 'lib/heapviz/page.rb', line 34

def sorted_objects
  @live_objects.sort
end

#to_sObject



54
55
56
# File 'lib/heapviz/page.rb', line 54

def to_s
  "{page: #{address.to_s(16)}, slot_size: #{slot_size}, full: #{full?}}"
end