Rewrite planemap and idalloc in pure C++

This commit is contained in:
2021-01-06 15:10:21 -05:00
parent b03aada315
commit 78f8610eb8
18 changed files with 913 additions and 520 deletions

View File

@@ -5,8 +5,8 @@
utils.lua
inspect.lua
ut-misc.lua
ut-table.lua
ut-idalloc.lua
ut-globaldb.lua
ut-cellgrid.lua

View File

@@ -1,12 +1,7 @@
local ut = {}
function ut.cellid()
local sc = cellgrid.scale()
local cid = cellgrid.cellid(0,0,0)
assert(cellgrid.cellid(0,0,0) == 0x0001000000000000)
assert(cellgrid.cellid( 1*sc, 2*sc, 3*sc) == 0x0001000100020003)
assert(cellgrid.cellid(-1*sc, -2*sc, -3*sc) == 0x0001FFFFFFFEFFFD)
assert(cellgrid.cellid(10000000, 0, 0) == 0)
function ut.cellgrid()
end
rununittests(ut)

View File

@@ -1,95 +0,0 @@
local ut = {}
local function nthbatch(i)
return 0x0001000000000000 + 256 * (i - 1)
end
local function equalbatches(q, batches)
if queue.size(q) ~= #batches then
return false
end
for i = 1, #batches do
if queue.nth(q, i) ~= nthbatch(batches[i]) then
return false
end
end
return true
end
function ut.idalloc_synch()
local allocator = {}
idalloc.initsynch(allocator, 10)
assert(allocator.id_salvaged == nil)
assert(allocator.id_nextbatch == nil)
assert(allocator.id_nextid == 0x001E000000000000)
assert(allocator.id_queuefill == 10)
local q1 = queue.create()
idalloc.refill(allocator, q1)
assert(queue.size(q1) == 0)
end
function ut.idalloc_master()
local allocator = {}
idalloc.initmaster(allocator, 3)
assert(queue.size(allocator.id_salvaged) == 0)
assert(allocator.id_nextbatch == nthbatch(1))
assert(allocator.id_nextid == 0x0010000000000000)
assert(allocator.id_queuefill == 3)
local q1 = queue.create()
local q2 = queue.create()
local q3 = queue.create()
local salv = allocator.id_salvaged
idalloc.refill(allocator, q1)
idalloc.refill(allocator, q2)
assert(equalbatches(q1, { 1, 2, 3 }))
assert(equalbatches(q2, { 4, 5, 6 }))
idalloc.unqueue(allocator, q1)
assert(equalbatches(salv, { 1, 2, 3 }))
queue.pop(q2)
assert(equalbatches(q2, { 5, 6 }))
idalloc.refill(allocator, q2)
assert(equalbatches(q2, { 5, 6, 1 }))
assert(equalbatches(salv, { 2, 3 }))
idalloc.refill(allocator, q3)
assert(equalbatches(q3, { 2, 3, 7 }))
end
function ut.idalloc_thread()
local allocator = {}
idalloc.initmaster(allocator, 3)
local salv = allocator.id_salvaged
local q = queue.create()
idalloc.refill(allocator, q)
local id = 0
local function useids()
for i = 1,10000 do
id = idalloc.allocid(allocator)
coroutine.yield()
end
end
local co = coroutine.create(useids)
coroutine.resume(co)
assert(id == 0x0010000000000000)
coroutine.resume(co)
assert(id == 0x0010000000000001)
idalloc.preparethread(q, co)
coroutine.resume(co)
assert(id == nthbatch(1) + 0)
coroutine.resume(co)
assert(id == nthbatch(1) + 1)
idalloc.salvagethread(allocator, co)
coroutine.resume(co)
assert(id == 0x0010000000000002)
assert(queue.size(salv) == 1)
assert(queue.nth(salv, 1) == nthbatch(1) + 2)
end
rununittests(ut)

12
luprex/syslua/ut-misc.lua Normal file
View File

@@ -0,0 +1,12 @@
local ut = {}
function ut.idalloc()
cunittests.idalloc()
end
function ut.planemap()
cunittests.planemap()
end
rununittests(ut)