class AutoC::Allocator

Standard C malloc()-based dynamic memory handler

Public Class Methods

new(= dependencies << STDLIB_H) click to toggle source
# File lib/autoc/allocators.rb, line 22
  def initialize = dependencies << STDLIB_H

  def allocate(type, count = 1, zero: false, **kws)
    zero ? "(#{type}*)calloc(#{count}, sizeof(#{type}))" : "(#{type}*)malloc((#{count})*sizeof(#{type}))"
  end

  def free(pointer) = "free(#{pointer})"

end # Allocator


# Boehm-Demers-Weiser garbage-collecting memory handler https://www.hboehm.info/gc/
class BDWAllocator

  include Singleton

  include Entity

  def initialize = dependencies << SystemHeader.new('gc.h')

  def allocate(type, count = 1, atomic: false, **kws)
    atomic ? "(#{type}*)GC_malloc_atomic((#{count})*sizeof(#{type}))" : "(#{type}*)GC_malloc((#{count})*sizeof(#{type}))"
  end

  def free(pointer) = nil

end 

Public Instance Methods

allocate(type, count = 1, zero: false, **kws) click to toggle source
# File lib/autoc/allocators.rb, line 24
def allocate(type, count = 1, zero: false, **kws)
  zero ? "(#{type}*)calloc(#{count}, sizeof(#{type}))" : "(#{type}*)malloc((#{count})*sizeof(#{type}))"
end
free(pointer) click to toggle source
# File lib/autoc/allocators.rb, line 28
  def free(pointer) = "free(#{pointer})"

end