More heap testing.

This commit is contained in:
Martin Haye 2015-08-28 14:04:08 -07:00
parent ec6368a10b
commit 159f956bf7
2 changed files with 109 additions and 6 deletions

View File

@ -688,6 +688,7 @@ heapSet: !zone
lda #0
sta heapTop
sta nHeapBlks
sta nTypes
; fall through to:
; Zero memory heapTop.heapEnd
heapClr: !zone
@ -717,10 +718,10 @@ heapAddType: !zone
bmi +
+prChr 'T'
brk
+ sta typeTblL,y ; addr hi
+ sta typeTblH,y ; addr hi
sta .ld+2
txa ; addr lo
sta typeTblH,y
sta typeTblL,y
.ld lda $1000,x ; self-modified above: fetch length byte
sta typeLen,y ; save that too
inc nTypes ; bump type count
@ -737,11 +738,15 @@ heapAlloc: !zone
txa
ldy #0
sta (pTmp),y ; save obj type or len on heap
tay ; test hi bit
bpl .gotlen
and #$7F
tay
lda typeLen,y
.gotlen ldy pTmp+1
+prStr : !text "heapAllocLen=", 0
+prA
+crout
sec ; add 1 for type byte
adc pTmp
bcc +
@ -750,6 +755,9 @@ heapAlloc: !zone
bcs .needgc
+ sta heapTop
sty heapTop+1
+prStr : !text "heapAllocRet=", 0
+prWord pTmp
+crout
ldx pTmp ; return ptr in X=lo/Y=hi
ldy pTmp+1
rts

View File

@ -109,7 +109,10 @@ byte typeTbl_Global[] = Global, players, 0
byte typeTbl_Player[] = Player, nextObj, items, 0
byte typeTbl_Item[] = Item, nextObj, 0
byte typeLengths[10]
byte typeCounts[256]
byte checkCounts[256]
word nextObjID = 0
///////////////////////////////////////////////////////////////////////////////////////////////////
// Predefined functions, for circular calls or out-of-order calls
@ -1473,8 +1476,14 @@ end
///////////////////////////////////////////////////////////////////////////////////////////////////
def newObj(typeNum)
word ptr
typeCounts[typeNum] = typeCounts[typeNum] + 1
return mmgr(HEAP_ALLOC, type)
ptr = mmgr(HEAP_ALLOC, typeNum)
if typeNum >= $80
nextObjID = nextObjID + 1
ptr=>id = nextObjID
fin
return ptr
end
///////////////////////////////////////////////////////////////////////////////////////////////////
@ -1507,16 +1516,102 @@ def initHeap()
mmgr(REQUEST_MEMORY, heapSize)
mmgr(LOCK_MEMORY, heapStart)
mmgr(HEAP_SET, heapStart)
mmgr(HEAP_ADD_TYPE, typeTbl_Global)
mmgr(HEAP_ADD_TYPE, typeTbl_Player)
mmgr(HEAP_ADD_TYPE, typeTbl_Item)
mmgr(HEAP_ADD_TYPE, @typeTbl_Global)
typeLengths[TYPE_GLOBAL & $7F] = Global
mmgr(HEAP_ADD_TYPE, @typeTbl_Player)
typeLengths[TYPE_PLAYER & $7F] = Global
mmgr(HEAP_ADD_TYPE, @typeTbl_Item)
typeLengths[TYPE_ITEM & $7F] = Global
newObj(TYPE_GLOBAL)
end
///////////////////////////////////////////////////////////////////////////////////////////////////
def randomLetter()
return (rand16() % 26) + 'A'
end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Locate the heap string with the specified length. If none exists, create a new random string.
def findTestString(len)
word p, t, p2
p = heapStart
while *p
t = ^p
if t == len
return p
elsif t < $80
p = p + t
elsif t < TYPE_GLOBAL or t > TYPE_ITEM
fatal("Unknown type in heap")
else
p = p + typeLengths[t & $7F]
fin
loop
p = newObj(len)
printf1("New string at %x\n", p)
p2 = p
for t = 1 to len
p2 = p2+1
printf1("Setting %x to 'X'\n", p2)
^$c083
^$c083
p2->0 = 'X'
next
return p
end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Check the object counts on the heap
def checkHeapCounts()
word p, t
byte bad
// Clear the check array
for t = 0 to 255; checkCounts[t] = 0; next
// Now traverse the heap, counting the number of each object type
p = heapStart
while *p
t = ^p
checkCounts[t] = checkCounts[t] + 1
if t < $80
p = p + t
elsif t < TYPE_GLOBAL or t > TYPE_ITEM
fatal("Unknown type in heap")
else
p = p + typeLengths[t & $7F]
fin
loop
// Then verify the counts
bad = 0
for t = 0 to 255
if typeCounts[t] <> checkCounts[t]
printf3("Count for type %d should be %d, got %d\n", t, checkCounts[t], typeCounts[t])
bad = bad+1
fin
next
if bad
fatal("Heap errors found.")
fin
end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Test out the heap
def testHeap()
word p
checkHeapCounts()
p = findTestString(10)
printf2("Test string: %x '%s'\n", p, p)
getUpperKey()
end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Main code.
//
initHeap()
testHeap()
loadTitle()
setCallbacks()
mapIs3D = OVERMAP_IS_3D