Cleaned up some inefficient memory manager code.

This commit is contained in:
Martin Haye 2015-08-19 07:53:08 -07:00
parent 2e4a992c75
commit a8ec4fd42c
2 changed files with 58 additions and 91 deletions

View File

@ -761,9 +761,31 @@ gcHash_chk: !zone
rts rts
.corrup jmp heapCorrupt .corrup jmp heapCorrupt
; Verify integrity of memory manager structures
memCheck: !zone
jsr heapCheck ; check heap if there is one
ldx #0 ; check main bank
jsr .chk
ldx #1 ; then aux
.chk lda tSegLink,x
tay
beq .done
lda tSegAdrLo,y ; verify addresses are in ascending order
cmp tSegAdrLo,x
lda tSegAdrHi,y
sbc tSegAdrHi,x
tya
tax
bcs .chk
jsr inlineFatal : !text "MTblCorrupt",0
.done rts
; Verify the integrity of the heap ; Verify the integrity of the heap
heapCheck: !zone heapCheck: !zone
lda heapTop lda heapStartPg
bne + ; skip check if no heap defined
rts
+ lda heapTop
sta pTmp sta pTmp
lda heapTop+1 lda heapTop+1
sta pTmp+1 sta pTmp+1
@ -1104,31 +1126,38 @@ scanForAvail: !zone
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
main_dispatch: !zone main_dispatch: !zone
!if SANITY_CHECK { jsr saneStart : jsr + : jmp saneEnd } !if SANITY_CHECK { jsr saneStart : jsr + : jmp saneEnd }
pha
lda #0
beq .go
aux_dispatch:
pha
lda #1
.go sta isAuxCmd
pla
+ cmp #REQUEST_MEMORY + cmp #REQUEST_MEMORY
bne + bne +
jmp main_request jmp mem_request
+ cmp #QUEUE_LOAD + cmp #QUEUE_LOAD
bne + bne +
jmp main_queueLoad jmp mem_queueLoad
+ cmp #LOCK_MEMORY + cmp #LOCK_MEMORY
bne + bne +
jmp main_lock jmp mem_lock
+ cmp #UNLOCK_MEMORY + cmp #UNLOCK_MEMORY
bne + bne +
jmp main_unlock jmp mem_unlock
+ cmp #FREE_MEMORY + cmp #FREE_MEMORY
bne + bne +
jmp main_free jmp mem_free
!if DEBUG { !if DEBUG {
+ cmp #DEBUG_MEM + cmp #DEBUG_MEM
bne + bne +
jmp main_debug jmp mem_debug
} }
+ cmp #CALC_FREE + cmp #CALC_FREE
bne shared_dispatch bne +
jmp main_calcFree jmp mem_calcFree
shared_dispatch: + cmp #RESET_MEMORY
cmp #RESET_MEMORY
bne + bne +
jmp reset jmp reset
+ cmp #SET_MEM_TARGET + cmp #SET_MEM_TARGET
@ -1136,39 +1165,14 @@ shared_dispatch:
stx targetAddr stx targetAddr
sty targetAddr+1 sty targetAddr+1
rts rts
+ cmp #CHECK_MEM
bne +
jmp memCheck
+ cmp #FATAL_ERROR + cmp #FATAL_ERROR
bne + bne +
jmp fatalError jmp fatalError
+ jmp nextLdVec ; Pass command to next chained loader + jmp nextLdVec ; Pass command to next chained loader
;------------------------------------------------------------------------------
aux_dispatch: !zone
!if SANITY_CHECK { jsr saneStart : jsr + : jmp saneEnd }
cmp #REQUEST_MEMORY
bne +
jmp aux_request
+ cmp #QUEUE_LOAD
bne +
jmp aux_queueLoad
+ cmp #LOCK_MEMORY
bne +
jmp aux_lock
+ cmp #UNLOCK_MEMORY
bne +
jmp aux_unlock
+ cmp #FREE_MEMORY
bne +
jmp aux_free
!if DEBUG {
+ cmp #DEBUG_MEM
bne +
jmp aux_debug
}
+ cmp #CALC_FREE
bne +
jmp aux_calcFree
+ jmp shared_dispatch
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; Sanity check mode ; Sanity check mode
!if SANITY_CHECK { !if SANITY_CHECK {
@ -1228,11 +1232,11 @@ printMem: !zone
jsr main_debug jsr main_debug
jmp aux_debug jmp aux_debug
main_debug: main_debug:
+prStr : !text "Listing main mem segments.",0 +prStr : !text "MainMem:",0
ldy #0 ldy #0
jmp .printSegs jmp .printSegs
aux_debug: aux_debug:
+prStr : !text "Listing aux mem segments.",0 +prStr : !text "AuxMem:",0
ldy #1 ldy #1
.printSegs: .printSegs:
tya tya
@ -1302,13 +1306,7 @@ reservedErr: !zone
jsr inlineFatal : !text "DblAlloc", 0 jsr inlineFatal : !text "DblAlloc", 0
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
main_request: !zone mem_request: !zone
lda #0 ; index for main mem
beq shared_request ; always taken
aux_request:
lda #1 ; index for aux mem
shared_request:
sta isAuxCmd ; save whether we're working on main or aux mem
stx reqLen ; save requested length stx reqLen ; save requested length
sty reqLen+1 ; all 16 bits sty reqLen+1 ; all 16 bits
shared_alloc: shared_alloc:
@ -1465,7 +1463,6 @@ coalesce: !zone
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
shared_scan: !zone shared_scan: !zone
php ; save carry (set to check active flg, clr to skip check) php ; save carry (set to check active flg, clr to skip check)
sta isAuxCmd ; save whether main or aux mem
jsr scanForAddr ; scan for block that matches jsr scanForAddr ; scan for block that matches
beq invalAddr ; if not found, invalid beq invalAddr ; if not found, invalid
bcs invalAddr ; if addr not exactly equal, invalid bcs invalAddr ; if addr not exactly equal, invalid
@ -1500,12 +1497,7 @@ shared_byteCodeAlso:
rts rts
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
main_lock: !zone mem_lock: !zone
lda #0 ; index for main-mem request
beq shared_lock ; always taken
aux_lock:
lda #1 ; index for aux-mem request
shared_lock:
sec ; do check active flag in scan sec ; do check active flag in scan
jsr shared_scan ; scan for exact memory block jsr shared_scan ; scan for exact memory block
ora #$40 ; set the 'locked' flag ora #$40 ; set the 'locked' flag
@ -1517,12 +1509,7 @@ shared_lock:
+ rts ; all done + rts ; all done
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
main_unlock: !zone mem_unlock: !zone
lda #0 ; index for main-mem request
beq shared_unlock ; always taken
aux_unlock:
lda #1 ; index for aux-mem request
shared_unlock:
sec ; do check active flag in scan sec ; do check active flag in scan
jsr shared_scan ; scan for exact memory block jsr shared_scan ; scan for exact memory block
and #$BF ; mask off the 'locked' flag and #$BF ; mask off the 'locked' flag
@ -1534,12 +1521,7 @@ shared_unlock:
+ rts ; all done + rts ; all done
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
main_free: !zone mem_free: !zone
lda #0 ; index for main-mem request
beq shared_free ; always taken
aux_free:
lda #1 ; index for aux-mem request
shared_free:
clc ; do not check for active flg (ok to multiple free) clc ; do not check for active flg (ok to multiple free)
jsr shared_scan ; scan for exact memory block jsr shared_scan ; scan for exact memory block
and #$3F ; remove the 'active' and 'locked' flags and #$3F ; remove the 'active' and 'locked' flags
@ -1564,18 +1546,14 @@ shared_free:
.fatal jsr inlineFatal : !text "NoFreeBcode", 0 .fatal jsr inlineFatal : !text "NoFreeBcode", 0
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
main_calcFree: !zone mem_calcFree: !zone
; Input: pTmp - address to scan for ; Input: pTmp - address to scan for
; Output: X-reg - segment found (zero if not found), N and Z set for X-reg ; Output: X-reg - segment found (zero if not found), N and Z set for X-reg
; carry clear if addr == seg start, set if addr != seg start ; carry clear if addr == seg start, set if addr != seg start
ldx #0
beq shared_calcFree
aux_calcFree:
ldx #1
shared_calcFree:
lda #0 ; clear out free space counter lda #0 ; clear out free space counter
sta reqLen sta reqLen
sta reqLen+1 sta reqLen+1
ldx isAuxCmd
.loop: ldy tSegLink,x ; grab link to next segment .loop: ldy tSegLink,x ; grab link to next segment
lda tSegType,x ; get type with flags lda tSegType,x ; get type with flags
bmi .next ; if active, skip to next bmi .next ; if active, skip to next
@ -1601,13 +1579,7 @@ shared_calcFree:
rts ; all done rts ; all done
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
main_queueLoad: !zone mem_queueLoad: !zone
lda #0 ; flag for main mem
beq shared_queueLoad ; always taken
aux_queueLoad:
lda #1 ; flag for aux mem
shared_queueLoad:
sta isAuxCmd ; save whether main or aux
stx resType ; save resource type stx resType ; save resource type
sty resNum ; save resource number sty resNum ; save resource number
cpx #RES_TYPE_MODULE ; loading a module? cpx #RES_TYPE_MODULE ; loading a module?

View File

@ -103,21 +103,16 @@ RESET_MEMORY = $10
; ;
; Output: None ; Output: None
; ;
; Mark all memory as inactive, except the following areas in main memory ; Mark all non-locked memory segments as inactive.
; which are always locked:
;
; 0000.01FF: Zero page and stack
; 0200.02FF: Input buffer and/or scratch space
; 0300.03FF: System vectors, scratch space
; 0400.07FF: Text display
; 0800.0xFF: The memory manager and its page table
; 4000.5FFF: Reserved during queue operations
; BF00.BFFF: ProDOS system page
; ;
; Note that this does *not* destroy the contents of memory, so for instance ; Note that this does *not* destroy the contents of memory, so for instance
; future RECALL_MEMORY commands may be able to re-use the existing contents ; future QUEUE_LOAD commands may be able to re-use the existing contents
; of memory if they haven't been reallocated to something else. ; of memory if they haven't been reallocated to something else.
; ;
; Also note that this does not erase the small-object heap if one has been
; established. But it also doesn't check that the heap has been properly
; protected by locking, that's your job.
;
; This command is acted upon and then passed on to chained loaders. ; This command is acted upon and then passed on to chained loaders.
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------