Class: Unistd

Inherits:
Object show all
Defined in:
lib/rlang/lib/unistd.rb

Class Method Summary collapse

Class Method Details

.sbrk(n) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rlang/lib/unistd.rb', line 32

def self.sbrk(n)
  # local variables used. All default type
  # wasm_mem_size:  current wasm memory size (in bytes)
  # heap_break:     current heap break
  #
  # new_heap_size:  new heap size (HEAP_SIZE + n)
  # new_heap_break: new heap break (HEAP + HEAP_SIZE + n)
  # more_pages:     how many more WASM pages are needed

  # return current break if n is 0
  heap_break = $HEAP + $HEAP_SIZE
  return heap_break if n == 0

  # check if new heap size is beyond authorized limit
  new_heap_size = $HEAP_SIZE + n
  return -1 if new_heap_size >= $HEAP_MAX_SIZE

  # We are good, so now check if we need more WASM 
  # memory pages (1 page = 64 KB)
  wasm_mem_size = Memory.size * 65536
  new_heap_break = heap_break + n

  # need to grow memory? if so by how many WASM pages ?
  if new_heap_break >= wasm_mem_size
    more_pages = (new_heap_break - wasm_mem_size) / 65536 + 1
  end

  # go grow WASM memory by so many pages. Return -1 if it fails
  return -1 if Memory.grow(more_pages) == -1

  # set new heap size
  $HEAP_SIZE = new_heap_size

  # heap_break is the address where the new free space starts
  return heap_break
end