Class: HeapInfo::UnsortedBin

Inherits:
Fastbin show all
Defined in:
lib/heapinfo/arena.rb

Overview

Class for record unsorted bin type chunk.

Direct Known Subclasses

Smallbin

Instance Attribute Summary collapse

Attributes inherited from Fastbin

#fd, #index

Attributes inherited from Chunk

#base, #data, #prev_size, #size_t

Instance Method Summary collapse

Methods inherited from Fastbin

#bk_of, #fd_of, #idx_to_size, #list, #title

Methods inherited from Chunk

#bintype, #flags, #mmapped?, #non_main_arena?, #prev_inuse?, #size, #to_s

Constructor Details

#initialize(*args) ⇒ UnsortedBin

Instantiate a HeapInfo::UnsortedBin object.


167
168
169
170
# File 'lib/heapinfo/arena.rb', line 167

def initialize(*args)
  super
  @bk = Helper.unpack(size_t, @data[@size_t, @size_t])
end

Instance Attribute Details

#bkInteger (readonly)


162
163
164
# File 'lib/heapinfo/arena.rb', line 162

def bk
  @bk
end

Instance Method Details

#inspect(size: 2) ⇒ String


175
176
177
178
179
# File 'lib/heapinfo/arena.rb', line 175

def inspect(size: 2)
  list = link_list(size)
  return '' if list.size <= 1 && Helper.class_name(self) != 'UnsortedBin' # bad..
  title + pretty_list(list) + "\n"
end

Return the double link list with bin in the center.

The list will like […, bk of bk, bk of bin, bin, fd of bin, fd of fd, …].


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/heapinfo/arena.rb', line 206

def link_list(expand_size)
  list = [@base]
  # fd
  work = proc do |ptr, nxt, append|
    sz = 0
    dup = {}
    while ptr != @base && sz < expand_size
      append.call ptr
      break if ptr.nil? # invalid pointer
      break if dup[ptr] # looped
      dup[ptr] = true
      ptr = __send__(nxt, ptr)
      sz += 1
    end
  end
  work.call(@fd, :fd_of, ->(ptr) { list << ptr })
  work.call(@bk, :bk_of, ->(ptr) { list.unshift ptr })
  list
end

#pretty_list(list) ⇒ String

Wrapper the double-linked list with color codes.


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/heapinfo/arena.rb', line 184

def pretty_list(list)
  center = nil
  list.map.with_index do |c, idx|
    next center = Helper.color('[self]', sev: :bin) if c == @base
    color_c = Helper.color(format('%#x', c))
    fwd = fd_of(c)
    next "#{color_c}(invalid)" if fwd.nil? # invalid c
    bck = bk_of(c)
    if center.nil? # bk side
      Helper.color(format('%s%s', color_c, fwd == list[idx + 1] ? nil : format('(%#x)', fwd)))
    else # fd side
      Helper.color(format('%s%s', bck == list[idx - 1] ? nil : format('(%#x)', bck), color_c))
    end
  end.join(' === ')
end