]> git.sur5r.net Git - cc65/blob - libsrc/common/_heapblocksize.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / _heapblocksize.s
1 ;
2 ; Ullrich von Bassewitz, 2004-07-17
3 ;
4 ; size_t __fastcall__ _heapblocksize (const void* ptr);
5 ;
6 ; Return the size of an allocated block.
7 ;
8
9         .importzp       ptr1, ptr2
10         .export         __heapblocksize
11
12         .include        "_heap.inc"
13
14         .macpack        generic
15         .macpack        cpu
16
17 ;-----------------------------------------------------------------------------
18 ; Code
19
20 __heapblocksize:
21
22 ; Below the user data is a pointer that points to the start of the real
23 ; (raw) memory block. The first word of this block is the size. To access
24 ; the raw block pointer, we will decrement the high byte of the pointer,
25 ; the pointer is then at offset 254/255.
26
27         sta     ptr1
28         dex
29         stx     ptr1+1
30         ldy     #$FE
31         lda     (ptr1),y
32         sta     ptr2            ; Place the raw block pointer into ptr2
33         iny
34         lda     (ptr1),y
35         sta     ptr2+1
36
37 ; Load the size from the raw block
38
39         ldy     #usedblock::size+1
40         lda     (ptr2),y
41         tax
42 .if (.cpu .bitand CPU_ISET_65SC02)
43         lda     (ptr2)
44 .else
45         dey
46         lda     (ptr2),y
47 .endif
48
49 ; Correct the raw block size so that is shows the user visible portion. To
50 ; do that, we must decrease the size by the amount of unused memory, which is
51 ; the difference between the user space pointer and the raw memory block
52 ; pointer. Since we have decremented the user space pointer by 256, we will
53 ; have to correct the result.
54 ;
55 ;       return size - (ptr1 + 256 - ptr2)
56 ;       return size - ptr1 - 256 + ptr2
57
58         dex                     ; - 256
59         add     ptr2
60         pha
61         txa
62         adc     ptr2+1
63         tax
64         pla
65         sub     ptr1
66         pha
67         txa
68         sbc     ptr1+1
69         tax
70         pla
71
72 ; Done
73
74         rts
75