Method: CTypes::Struct::Builder#pad

Defined in:
lib/ctypes/struct/builder.rb

#pad(bytes) ⇒ Object

allocate unused bytes in the CTypes::Struct This method is used to enforce alignment of other fields, or accurately mimic padding added in C structs by the compiler.

Examples:

CTypes::Helpers.struct do
  attribute :id, uint16       # 16-bit integer taking up two bytes
  pad 2                       # pad two bytes (16-bits) to align value
  attribute :value, uint32    # value aligned at 4-byte boundary
end

Parameters:

  • bytes (Integer)

    number of bytes to pad



221
222
223
224
225
226
227
228
229
230
# File 'lib/ctypes/struct/builder.rb', line 221

def pad(bytes)
  pad = Pad.new(bytes)

  # we use the Pad instance as the name of the field so Struct knows to
  # treat the field as padding
  @fields << [pad, pad]
  @bytes += bytes if @bytes

  self
end