2004-07-17 12:05:36 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 2004-07-17
|
|
|
|
;
|
|
|
|
; size_t __fastcall__ _heapblocksize (const void* ptr);
|
|
|
|
;
|
|
|
|
; Return the size of an allocated block.
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.importzp ptr1, ptr2
|
|
|
|
.export __heapblocksize
|
2004-07-17 12:05:36 +00:00
|
|
|
|
|
|
|
.include "_heap.inc"
|
|
|
|
|
2004-07-17 12:18:51 +00:00
|
|
|
.macpack generic
|
|
|
|
.macpack cpu
|
|
|
|
|
2004-07-17 12:05:36 +00:00
|
|
|
;-----------------------------------------------------------------------------
|
|
|
|
; Code
|
|
|
|
|
|
|
|
__heapblocksize:
|
|
|
|
|
2005-01-05 21:05:11 +00:00
|
|
|
; Below the user data is a pointer that points to the start of the real
|
|
|
|
; (raw) memory block. The first word of this block is the size. To access
|
|
|
|
; the raw block pointer, we will decrement the high byte of the pointer,
|
|
|
|
; the pointer is then at offset 254/255.
|
2004-07-17 12:05:36 +00:00
|
|
|
|
2005-01-05 21:05:11 +00:00
|
|
|
sta ptr1
|
2004-07-17 12:05:36 +00:00
|
|
|
dex
|
|
|
|
stx ptr1+1
|
2005-01-05 21:05:11 +00:00
|
|
|
ldy #$FE
|
|
|
|
lda (ptr1),y
|
|
|
|
sta ptr2 ; Place the raw block pointer into ptr2
|
|
|
|
iny
|
|
|
|
lda (ptr1),y
|
2012-03-25 13:13:37 +00:00
|
|
|
sta ptr2+1
|
2004-07-17 12:05:36 +00:00
|
|
|
|
2005-01-05 21:05:11 +00:00
|
|
|
; Load the size from the raw block
|
2004-07-17 12:05:36 +00:00
|
|
|
|
2005-01-05 21:05:11 +00:00
|
|
|
ldy #usedblock::size+1
|
|
|
|
lda (ptr2),y
|
2004-07-17 12:05:36 +00:00
|
|
|
tax
|
2004-07-17 12:18:51 +00:00
|
|
|
.if (.cpu .bitand CPU_ISET_65SC02)
|
2005-01-05 21:05:11 +00:00
|
|
|
lda (ptr2)
|
2004-07-17 12:18:51 +00:00
|
|
|
.else
|
2004-07-17 12:05:36 +00:00
|
|
|
dey
|
2005-01-05 21:05:11 +00:00
|
|
|
lda (ptr2),y
|
2004-07-17 12:18:51 +00:00
|
|
|
.endif
|
|
|
|
|
2005-01-05 21:05:11 +00:00
|
|
|
; Correct the raw block size so that is shows the user visible portion. To
|
|
|
|
; do that, we must decrease the size by the amount of unused memory, which is
|
|
|
|
; the difference between the user space pointer and the raw memory block
|
|
|
|
; pointer. Since we have decremented the user space pointer by 256, we will
|
|
|
|
; have to correct the result.
|
|
|
|
;
|
|
|
|
; return size - (ptr1 + 256 - ptr2)
|
|
|
|
; return size - ptr1 - 256 + ptr2
|
2004-07-17 12:18:51 +00:00
|
|
|
|
2005-01-05 21:05:11 +00:00
|
|
|
dex ; - 256
|
|
|
|
add ptr2
|
|
|
|
pha
|
|
|
|
txa
|
|
|
|
adc ptr2+1
|
|
|
|
tax
|
|
|
|
pla
|
|
|
|
sub ptr1
|
|
|
|
pha
|
|
|
|
txa
|
|
|
|
sbc ptr1+1
|
|
|
|
tax
|
|
|
|
pla
|
2004-07-17 12:05:36 +00:00
|
|
|
|
|
|
|
; Done
|
|
|
|
|
2005-01-05 21:05:11 +00:00
|
|
|
rts
|
2004-07-17 12:05:36 +00:00
|
|
|
|