Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import java.io.{DataInputStream, DataOutputStream, InputStream, OutputStream}
import scala.concurrent.util.Unsafe.{instance => unsafe}

class UnsafeBitArray(val numberOfBits: Long) {
private val indices = math.ceil(numberOfBits.toDouble / 64).toLong
private val ptr = unsafe.allocateMemory(8L * indices)
unsafe.setMemory(ptr, 8L * indices, 0.toByte)

private val indices = math.ceil(numberOfBits.toDouble / 8).toLong
private val ptr = unsafe.allocateMemory(indices)
unsafe.setMemory(ptr, indices, 0.toByte)
def get(index: Long): Boolean = {
(unsafe.getLong(ptr + (index >>> 6) * 8L) & (1L << index)) != 0
(unsafe.getByte(ptr + (index >>> 3)) & (1L << (index & 0x7))) != 0
}

def set(index: Long): Unit = {
val offset = ptr + (index >>> 6) * 8L
val long = unsafe.getLong(offset)
unsafe.putLong(offset, long | (1L << index))
val offset = ptr + (index >>> 3)
val value = unsafe.getByte(offset)
unsafe.putByte(offset, (value | (1L << (index & 0x7))).toByte)
}

def combine(that: UnsafeBitArray, combiner: (Long, Long) => Long): UnsafeBitArray = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ class UnsafeBitArraySpec extends Properties("UnsafeBitArray") {

def genCommand(state: State): Gen[Command] =
for {
i <- Gen.choose[Long](0, state.size)
i <- Gen.choose[Long](0, state.size - 1)
} yield commandSequence(SetItem(i), GetItem(i))

case class SetItem(i: Long) extends UnitCommand {
def run(sut: Sut): Unit = sut.synchronized(sut.set(i))
def nextState(state: State) = state
def nextState(state: State): State = state
def preCondition(state: State) = true
def postCondition(state: State, success: Boolean) = success
}

case class GetItem(i: Long) extends SuccessCommand {
type Result = Boolean
def run(sut: Sut): Boolean = sut.synchronized(sut.get(i))
def nextState(state: State) = state
def nextState(state: State): State = state
def preCondition(state: State) = true
def postCondition(state: State, result: Boolean): Prop = result
}
Expand Down