File: _heapblocksize.s

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (75 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;
; Ullrich von Bassewitz, 2004-07-17
;
; size_t __fastcall__ _heapblocksize (const void* ptr);
;
; Return the size of an allocated block.
;

        .importzp       ptr1, ptr2
        .export         __heapblocksize

        .include        "_heap.inc"

        .macpack        generic
        .macpack        cpu

;-----------------------------------------------------------------------------
; Code

__heapblocksize:

; 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.

        sta     ptr1
        dex
        stx     ptr1+1
        ldy     #$FE
        lda     (ptr1),y
        sta     ptr2            ; Place the raw block pointer into ptr2
        iny
        lda     (ptr1),y
        sta     ptr2+1

; Load the size from the raw block

        ldy     #usedblock::size+1
        lda     (ptr2),y
        tax
.if (.cpu .bitand CPU_ISET_65SC02)
        lda     (ptr2)
.else
        dey
        lda     (ptr2),y
.endif

; 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

        dex                     ; - 256
        add     ptr2
        pha
        txa
        adc     ptr2+1
        tax
        pla
        sub     ptr1
        pha
        txa
        sbc     ptr1+1
        tax
        pla

; Done

        rts