1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-26 05:29:30 +00:00
cc65/libsrc/common/_heapmemavail.s

79 lines
1.4 KiB
ArmAsm
Raw Normal View History

;
; Ullrich von Bassewitz, 2003-02-01
;
; Return the amount of free memory on the heap.
;
; size_t _heapmemavail (void);
;
;
.importzp ptr1, ptr2
2022-08-29 17:55:48 +00:00
.export ___heapmemavail
.include "_heap.inc"
.macpack generic
;-----------------------------------------------------------------------------
; Code
2022-08-29 17:55:48 +00:00
___heapmemavail:
; size_t Size = 0;
lda #0
sta ptr2
sta ptr2+1
; struct freeblock* F = _heapfirst;
2022-08-29 17:55:48 +00:00
lda ___heapfirst
sta ptr1
2022-08-29 17:55:48 +00:00
lda ___heapfirst+1
@L1: sta ptr1+1
; while (F) {
ora ptr1
beq @L2 ; Jump if end of free list reached
; Size += F->size;
ldy #freeblock::size
lda (ptr1),y
add ptr2
sta ptr2
iny
lda (ptr1),y
adc ptr2+1
sta ptr2+1
; F = F->next;
iny ; Points to F->next
lda (ptr1),y
tax
iny
lda (ptr1),y
stx ptr1
jmp @L1
; return Size + (_heapend - _heapptr) * sizeof (*_heapend);
@L2: lda ptr2
2022-08-29 17:55:48 +00:00
add ___heapend
sta ptr2
lda ptr2+1
2022-08-29 17:55:48 +00:00
adc ___heapend+1
tax
lda ptr2
2022-08-29 17:55:48 +00:00
sub ___heapptr
sta ptr2
txa
2022-08-29 17:55:48 +00:00
sbc ___heapptr+1
tax
lda ptr2
rts