To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

RewindableBumpPointerAllocator.scala 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package uvm.refimpl.mem.bumppointer

import uvm.refimpl._
import uvm.refimpl.mem._
import uvm.refimpl.mem.scanning._
import uvm.types._
import uvm.refimpl.mem.TypeSizes._
import com.typesafe.scalalogging.StrictLogging
import org.slf4j.LoggerFactory
import com.typesafe.scalalogging.Logger
import uvm.refimpl.mem.scanning.RefFieldHandler

object RewindableBumpPointerAllocator {
  val logger: Logger = Logger(LoggerFactory.getLogger(getClass.getName))
}

Kunshan Wang's avatar
Kunshan Wang committed
17
class RewindableBumpPointerAllocator(val begin: Word, val extend: Word, val microVM: MicroVM)
18
19
20
  extends Allocator {
  import RewindableBumpPointerAllocator._

Kunshan Wang's avatar
Kunshan Wang committed
21
  var top: Word = begin
22

Kunshan Wang's avatar
Kunshan Wang committed
23
  override def alloc(size: Word, align: Word, headerSize: Word): Word = {
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    val dataStart = top + WORD_SIZE_BYTES
    val iRef = dataStart + headerSize
    val dataEnd = iRef + size
    val nextTop = TypeSizes.alignUp(dataEnd, WORD_SIZE_BYTES)
    if (nextTop >= begin + extend) {
      throw new UvmRefImplException("Stack overflow or insufficient global memory.")
    }
    logger.debug(s"alloc(${size}, ${align}, ${headerSize}) top=${top} iRef=${iRef} nextTop=${nextTop}")
    MemUtils.zeroRegion(dataStart, nextTop - dataStart)
    MemorySupport.storeLong(nextTop, iRef)
    top = nextTop
    iRef
  }

Kunshan Wang's avatar
Kunshan Wang committed
38
  def rewind(newTop: Word) {
39
40
41
42
43
44
45
46
    top = newTop
  }

  def traverseFields(handler: RefFieldHandler) {
    logger.debug("Traversing a RewindableBumpPointerAllocator")
    var curTopLoc = top
    var reachBottom = false
    while (!reachBottom) {
Kunshan Wang's avatar
Kunshan Wang committed
47
      logger.debug("curTopLoc is 0x%x".format(curTopLoc))
48
      val iRef = MemorySupport.loadLong(curTopLoc)
Kunshan Wang's avatar
Kunshan Wang committed
49
      logger.debug("iRef is 0x%x".format(iRef))
50
51
52
      if (iRef != 0) {
        val hdr = HeaderUtils.getTag(iRef)
        val typeID = (hdr & 0xffffffffL).toInt
Kunshan Wang's avatar
Kunshan Wang committed
53
        logger.debug("hdr=0x%x, typeID=0x%x".format(hdr, typeID))
54
        val ty = microVM.globalBundle.typeNs(typeID)
Kunshan Wang's avatar
Kunshan Wang committed
55
        logger.debug("type=%s: %s".format(ty.repr, ty.toString))
56
        MemoryDataScanner.scanField(ty, 0, iRef, handler)
Kunshan Wang's avatar
Kunshan Wang committed
57
        var prevTopLoc: Word = 0L
58
59
60
61
62
63
64
65
66
67
68
69
        prevTopLoc = if (ty.isInstanceOf[TypeHybrid]) {
          iRef - TypeSizes.GC_HEADER_SIZE_HYBRID - WORD_SIZE_BYTES
        } else {
          iRef - TypeSizes.GC_HEADER_SIZE_SCALAR - WORD_SIZE_BYTES
        }
        curTopLoc = prevTopLoc
      } else {
        reachBottom = true
      }
    }
  }
}