Class: Capnp::BufferList

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Helpers, T::Sig
Includes:
List
Defined in:
lib/capnp/runtime/list/buffer/list.rb

Direct Known Subclasses

BufferString, Data, NumericList, StructList

Constant Summary collapse

Elem =
type_member(:out)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from List

#[], #each, #to_obj

Constructor Details

#initialize(data, length, element_type, element_size, data_words, pointer_words) ⇒ BufferList

Returns a new instance of BufferList.



28
29
30
31
32
33
34
35
# File 'lib/capnp/runtime/list/buffer/list.rb', line 28

def initialize(data, length, element_type, element_size, data_words, pointer_words)
  @data = data
  @length = length
  @element_type = element_type
  @element_size = element_size
  @data_words = data_words
  @pointer_words = pointer_words
end

Instance Attribute Details

#element_typeObject (readonly)

Returns the value of attribute element_type.



99
100
101
# File 'lib/capnp/runtime/list/buffer/list.rb', line 99

def element_type
  @element_type
end

#lengthObject (readonly)

Returns the value of attribute length.



96
97
98
# File 'lib/capnp/runtime/list/buffer/list.rb', line 96

def length
  @length
end

Class Method Details

.from_pointer(pointer_ref) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/capnp/runtime/list/buffer/list.rb', line 38

def self.from_pointer(pointer_ref)
  # Process far pointers
  pointer_ref, content_ref = pointer_ref.segment.message.dereference_pointer(pointer_ref)

  # Grab lower 32 bits as offset and upper 32 bits as size
  pointer_data = pointer_ref.read_bytes(0, Capnp::WORD_SIZE)
  offset_part, size_part = T.cast(pointer_data.unpack("l<L<"), [Integer, Integer])

  # Check for NULL pointer
  return nil if offset_part.zero? && size_part.zero?

  # Check this is a list pointer
  pointer_type = offset_part & 0b11
  raise Capnp::Error.new("List pointer has type #{pointer_type}") unless pointer_type == 1

  # Determine the length of the list
  length = size_part >> 3

  # Determine the size of the data section and individual elements
  element_type = size_part & 0b111
  element_size = case element_type
  # Void type elements
  when 0 then 0
  # Bit type elements
  when 1 then 1
  # Integer type elements
  when 2, 3, 4, 5 then 1 << (element_type - 2)
  # Pointer type elements
  when 6 then Capnp::WORD_SIZE
  # Composite type elements
  else
    0 # (Set below)
  end

  # Extract data section
  if content_ref.nil?
    data_offset = ((offset_part >> 2) + 1) * Capnp::WORD_SIZE
    data_ref = pointer_ref.offset_position(data_offset)
  else
    data_ref = content_ref
  end

  # Fetch tag for composite type elements
  data_words = 0
  pointers_words = 0
  if element_type == 7
    # Decode tag as a struct pointer
    length, data_words, pointers_words = Capnp::Struct.decode_pointer(data_ref)
    data_ref = data_ref.offset_position(Capnp::WORD_SIZE)

    # Calculate element size
    element_size = (data_words + pointers_words) * Capnp::WORD_SIZE
  end

  new(data_ref, length, element_type, element_size, data_words, pointers_words)
end