Sha256: 6ecc052e439a8b7405c10fca423ddfc4fa148da9c7de1bfbaff2f192d6291113

Contents?: true

Size: 1.16 KB

Versions: 23

Compression:

Stored size: 1.16 KB

Contents

class Ragweed::Arena
  # I want 3 lambdas:
  # * "get" should take no arguments and result in the address of a fresh
  #   4k page.
  # * "free" should free any 4k page returned by "get"
  # * "copy" should implement memcpy, copying a string into a 4k page.
  def initialize(get, free, copy)
    @get = get
    @free = free
    @copy = copy
    @pages = []
    @avail = 0
    @off = 0
  end

  private

  def get
    p = @get.call
    @pages << p
    @cur = p
    @avail = 4096
    @off = 0
  end

  public

  # Allocate any size less than 4090 from the arena.
  def alloc(sz)
    raise "can't handle > page size now" if sz > 4090
    get if sz > @avail
    ret = @off
    @off += sz
    round = 4 - (@off % 4)
    if (@off + round) > 4096
      @avail = 0
      @off = 4096
    else
      @off += round
      @avail -= (sz + round)
    end

    return Ragweed::Ptr.new(@cur + ret)
  end

  # Copy a buffer into the arena and return its new address.
  def copy(buf)
    ret = alloc(buf.size)
    @copy.call(ret, buf)
    return ret
  end

  # Release the whole arena all at once.
  def release; @pages.each {|p| @free.call(p)}; end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
ragweed-0.2.9 lib/ragweed/arena.rb
ragweed-0.2.8 lib/ragweed/arena.rb
ragweed-0.2.7 lib/ragweed/arena.rb
ragweed-0.2.6-java lib/ragweed/arena.rb
ragweed-0.2.6 lib/ragweed/arena.rb
ragweed-0.2.5-java lib/ragweed/arena.rb
ragweed-0.2.5 lib/ragweed/arena.rb
ragweed-0.2.4-java lib/ragweed/arena.rb
ragweed-0.2.4 lib/ragweed/arena.rb
ragweed-0.2.3-java lib/ragweed/arena.rb
ragweed-0.2.3 lib/ragweed/arena.rb
ragweed-0.2.2 lib/ragweed/arena.rb
ragweed-0.2.1-java lib/ragweed/arena.rb
ragweed-0.2.1 lib/ragweed/arena.rb
ragweed-0.2.0-java lib/ragweed/arena.rb
ragweed-0.2.0 lib/ragweed/arena.rb
ragweed-0.2.0.pre3-java lib/ragweed/arena.rb
ragweed-0.2.0.pre3 lib/ragweed/arena.rb
ragweed-0.2.0.pre2-java lib/ragweed/arena.rb
ragweed-0.2.0.pre2 lib/ragweed/arena.rb