]> git.sur5r.net Git - cc65/blob - libsrc/common/_heapadd.s
d6703e82069dddd141722a632a0ab79c09dc38cf
[cc65] / libsrc / common / _heapadd.s
1 ;
2 ; Ullrich von Bassewitz, 21.7.2000
3 ;
4 ; Add a block to the heap free list
5 ;
6 ; void __fastcall__ _heapadd (void* mem, size_t size);
7 ;
8 ;
9
10         .importzp       ptr1, ptr2
11         .import         popax
12         .import         heapadd
13         .export         _heapadd
14
15         .macpack        generic
16
17 ; Offsets into struct freeblock and other constant stuff
18
19 size            = 0
20 next            = 2
21 prev            = 4
22 admin_space     = 2
23 min_size        = 6
24
25
26 ; Code
27
28 _heapadd:
29         sta     ptr1            ; Store size in ptr1
30         stx     ptr1+1
31         jsr     popax           ; Get the block pointer
32         sta     ptr2
33         stx     ptr2+1          ; Store block pointer in ptr2
34
35 ; Check if size is greater or equal than min_size. Otherwise we don't care
36 ; about the block (this may only happen for user supplied blocks, blocks
37 ; from the heap are always large enough to hold a freeblock structure).
38
39         lda     ptr1            ; Load low byte
40         ldx     ptr1+1          ; Load/check high byte
41         bne     @L1
42         cmp     #min_size
43         bcs     @L1
44
45         rts                     ; Block not large enough
46
47 ; The block is large enough. Set the size field in the block.
48
49 @L1:    ldy     #size
50         sta     (ptr2),y
51         iny
52         txa
53         sta     (ptr2),y
54
55 ; Call the internal function since variables are now setup correctly
56
57         jmp     heapadd
58
59
60