Module: Snow::CStruct::StructBase::Allocators

Defined in:
lib/snow-data/c_struct/struct_base.rb,
lib/snow-data/c_struct/struct_base.rb

Overview

Allocator methods for struct types defined through CStruct. Namely structs’ ::new, ::wrap, and ::[] (new array) class methods.

Instance Method Summary collapse

Instance Method Details

#[](length) ⇒ Object

call-seq:

Struct[length] => Struct::Array

Allocates an array of structs with the requested length.



247
248
249
# File 'lib/snow-data/c_struct/struct_base.rb', line 247

def [](length)
  self::Array.new(length)
end

#alloca(&block) ⇒ Object



226
227
228
# File 'lib/snow-data/c_struct/struct_base.rb', line 226

def alloca(&block)
  __alloca__(self::SIZE, &block)
end

#new {|inst| ... } ⇒ Object

call-seq:

new { |struct| ... } => new_struct
new => new_struct

Allocates a new struct and returns it. If a block is given, the new struct is first yielded to the block then returned. You may use this to initialize the block or do whatever else you like with it.

Yields:

  • (inst)


219
220
221
222
223
# File 'lib/snow-data/c_struct/struct_base.rb', line 219

def new(&block)
  inst = __malloc__(self::SIZE, self::ALIGNMENT)
  yield(inst) if block_given?
  inst
end

#wrap(address, alignment = self::ALIGNMENT) ⇒ Object

Returns a struct object that wraps an existing memory address. The returned object does not own the memory associated with the address, and as such the wrapped memory may be subject to deallocation at any time, either by the garbage collector or otherwise, if not kept around somehow.



237
238
239
# File 'lib/snow-data/c_struct/struct_base.rb', line 237

def wrap(address, alignment = self::ALIGNMENT)
  __wrap__(address, self::SIZE, alignment)
end