Class: Ragweed::Arena

Inherits:
Object show all
Defined in:
lib/ragweed/arena.rb

Instance Method Summary collapse

Constructor Details

#initialize(get, free, copy) ⇒ 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.



7
8
9
10
11
12
13
14
# File 'lib/ragweed/arena.rb', line 7

def initialize(get, free, copy)
  @get = get
  @free = free
  @copy = copy
  @pages = []
  @avail = 0
  @off = 0
end

Instance Method Details

#alloc(sz) ⇒ Object

Allocate any size less than 4090 from the arena.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ragweed/arena.rb', line 29

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(buf) ⇒ Object

Copy a buffer into the arena and return its new address.



47
48
49
50
51
# File 'lib/ragweed/arena.rb', line 47

def copy(buf)
  ret = alloc(buf.size)
  @copy.call(ret, buf)
  return ret
end

#releaseObject

Release the whole arena all at once.



54
# File 'lib/ragweed/arena.rb', line 54

def release; @pages.each {|p| @free.call(p)}; end