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

#idx_to_size, #list, #title

Methods inherited from Chunk

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

Constructor Details

#initializeUnsortedBin

Instantiate a HeapInfo::UnsortedBin object.

See Also:

  • for more information.


172
173
174
175
# File 'lib/heapinfo/arena.rb', line 172

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

Instance Attribute Details

#bkInteger (readonly)

Returns:

  • (Integer)


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

def bk
  @bk
end

Instance Method Details

#inspect(size: 2) ⇒ String

Returns Unsorted bin layouts wrapper with color codes.

Parameters:

  • size (Integer) (defaults to: 2)

    At most expand size. For size = 2, the expand list would be bk, bk, bin, fd, fd.

Returns:

  • (String)

    Unsorted bin layouts wrapper with color codes.



180
181
182
183
184
185
# File 'lib/heapinfo/arena.rb', line 180

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, ...].

Parameters:

  • expand_size (Integer)

    At most expand size. For size = 2, the expand list would be bk, bk, bin, fd, fd.

Returns:

  • (Array<Integer>)

    The linked list.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/heapinfo/arena.rb', line 214

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? || dup[ptr] # invalid or duplicated pointer

      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 doubly linked list with color codes.

Parameters:

  • list (Array<Integer>)

    The list from #link_list.

Returns:

  • (String)

    Wrapper with color codes.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/heapinfo/arena.rb', line 190

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
      format('%s%s', color_c, fwd == list[idx + 1] ? nil : Helper.color(format('(%#x)', fwd)))
    else # fd side
      format('%s%s', bck == list[idx - 1] ? nil : Helper.color(format('(%#x)', bck)), color_c)
    end
  end.join(' === ')
end