mirror of
https://github.com/cc65/cc65.git
synced 2024-12-26 08:32:00 +00:00
add extra underscore to heap stuff
This commit is contained in:
parent
df4b6f9d14
commit
b09024aa32
@ -28,8 +28,8 @@ HEAP_MIN_BLOCKSIZE = .sizeof (freeblock) ; Minimum size of an allocated block
|
||||
HEAP_ADMIN_SPACE = .sizeof (usedblock) ; Additional space for used bock
|
||||
|
||||
; Variables
|
||||
.global __heaporg
|
||||
.global __heapptr
|
||||
.global __heapend
|
||||
.global __heapfirst
|
||||
.global __heaplast
|
||||
.global ___heaporg
|
||||
.global ___heapptr
|
||||
.global ___heapend
|
||||
.global ___heapfirst
|
||||
.global ___heaplast
|
||||
|
@ -37,11 +37,11 @@ struct freeblock {
|
||||
|
||||
|
||||
/* Variables that describe the heap */
|
||||
extern unsigned* _heaporg; /* Bottom of heap */
|
||||
extern unsigned* _heapptr; /* Current top */
|
||||
extern unsigned* _heapend; /* Upper limit */
|
||||
extern struct freeblock* _heapfirst; /* First free block in list */
|
||||
extern struct freeblock* _heaplast; /* Last free block in list */
|
||||
extern unsigned* __heaporg; /* Bottom of heap */
|
||||
extern unsigned* __heapptr; /* Current top */
|
||||
extern unsigned* __heapend; /* Upper limit */
|
||||
extern struct freeblock* __heapfirst; /* First free block in list */
|
||||
extern struct freeblock* __heaplast; /* Last free block in list */
|
||||
|
||||
|
||||
|
||||
|
@ -92,17 +92,33 @@ int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size);
|
||||
*/
|
||||
#endif
|
||||
|
||||
void __fastcall__ _heapadd (void* mem, size_t size);
|
||||
void __fastcall__ __heapadd (void* mem, size_t size);
|
||||
/* Add a block to the heap */
|
||||
#if __CC65_STD__ == __CC65_STD_CC65__
|
||||
/* define old name with one underscore for backwards compatibility */
|
||||
#define _heapadd __heapadd
|
||||
#endif
|
||||
|
||||
size_t __fastcall__ _heapblocksize (const void* block);
|
||||
size_t __fastcall__ __heapblocksize (const void* block);
|
||||
/* Return the size of an allocated block */
|
||||
#if __CC65_STD__ == __CC65_STD_CC65__
|
||||
/* define old name with one underscore for backwards compatibility */
|
||||
#define _heapblocksize __heapblocksize
|
||||
#endif
|
||||
|
||||
size_t _heapmemavail (void);
|
||||
size_t __heapmemavail (void);
|
||||
/* Return the total free heap space */
|
||||
#if __CC65_STD__ == __CC65_STD_CC65__
|
||||
/* define old name with one underscore for backwards compatibility */
|
||||
#define _heapmemavail __heapmemavail
|
||||
#endif
|
||||
|
||||
size_t _heapmaxavail (void);
|
||||
size_t __heapmaxavail (void);
|
||||
/* Return the size of the largest free block on the heap */
|
||||
#if __CC65_STD__ == __CC65_STD_CC65__
|
||||
/* define old name with one underscore for backwards compatibility */
|
||||
#define _heapmaxavail __heapmaxavail
|
||||
#endif
|
||||
|
||||
|
||||
/* Random numbers */
|
||||
|
@ -13,15 +13,15 @@
|
||||
|
||||
.data
|
||||
|
||||
__heaporg:
|
||||
___heaporg:
|
||||
.word __BSS_RUN__+__BSS_SIZE__ ; Linker calculates this symbol
|
||||
__heapptr:
|
||||
___heapptr:
|
||||
.word __BSS_RUN__+__BSS_SIZE__ ; Dito
|
||||
__heapend:
|
||||
___heapend:
|
||||
.word __BSS_RUN__+__BSS_SIZE__
|
||||
__heapfirst:
|
||||
___heapfirst:
|
||||
.word 0
|
||||
__heaplast:
|
||||
___heaplast:
|
||||
.word 0
|
||||
|
||||
|
||||
@ -33,10 +33,10 @@ initheap:
|
||||
sec
|
||||
lda sp
|
||||
sbc #<__STACKSIZE__
|
||||
sta __heapend
|
||||
sta ___heapend
|
||||
lda sp+1
|
||||
sbc #>__STACKSIZE__
|
||||
sta __heapend+1
|
||||
sta ___heapend+1
|
||||
rts
|
||||
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
.importzp ptr1, ptr2
|
||||
.import popax
|
||||
.import heapadd
|
||||
.export __heapadd
|
||||
.export ___heapadd
|
||||
|
||||
.include "_heap.inc"
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
;-----------------------------------------------------------------------------
|
||||
; Code
|
||||
|
||||
__heapadd:
|
||||
___heapadd:
|
||||
sta ptr1 ; Store size in ptr1
|
||||
stx ptr1+1
|
||||
jsr popax ; Get the block pointer
|
||||
|
@ -7,7 +7,7 @@
|
||||
;
|
||||
|
||||
.importzp ptr1, ptr2
|
||||
.export __heapblocksize
|
||||
.export ___heapblocksize
|
||||
|
||||
.include "_heap.inc"
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
;-----------------------------------------------------------------------------
|
||||
; Code
|
||||
|
||||
__heapblocksize:
|
||||
___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
|
||||
|
@ -8,7 +8,7 @@
|
||||
;
|
||||
|
||||
.importzp ptr1, ptr2
|
||||
.export __heapmaxavail
|
||||
.export ___heapmaxavail
|
||||
|
||||
.include "_heap.inc"
|
||||
|
||||
@ -17,22 +17,22 @@
|
||||
;-----------------------------------------------------------------------------
|
||||
; Code
|
||||
|
||||
__heapmaxavail:
|
||||
___heapmaxavail:
|
||||
|
||||
; size_t Size = (_heapend - _heapptr) * sizeof (*_heapend);
|
||||
|
||||
lda __heapend
|
||||
sub __heapptr
|
||||
lda ___heapend
|
||||
sub ___heapptr
|
||||
sta ptr2
|
||||
lda __heapend+1
|
||||
sbc __heapptr+1
|
||||
lda ___heapend+1
|
||||
sbc ___heapptr+1
|
||||
sta ptr2+1
|
||||
|
||||
; struct freeblock* F = _heapfirst;
|
||||
|
||||
lda __heapfirst
|
||||
lda ___heapfirst
|
||||
sta ptr1
|
||||
lda __heapfirst+1
|
||||
lda ___heapfirst+1
|
||||
@L1: sta ptr1+1
|
||||
|
||||
; while (F) {
|
||||
|
@ -8,7 +8,7 @@
|
||||
;
|
||||
|
||||
.importzp ptr1, ptr2
|
||||
.export __heapmemavail
|
||||
.export ___heapmemavail
|
||||
|
||||
.include "_heap.inc"
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
;-----------------------------------------------------------------------------
|
||||
; Code
|
||||
|
||||
__heapmemavail:
|
||||
___heapmemavail:
|
||||
|
||||
; size_t Size = 0;
|
||||
|
||||
@ -27,9 +27,9 @@ __heapmemavail:
|
||||
|
||||
; struct freeblock* F = _heapfirst;
|
||||
|
||||
lda __heapfirst
|
||||
lda ___heapfirst
|
||||
sta ptr1
|
||||
lda __heapfirst+1
|
||||
lda ___heapfirst+1
|
||||
@L1: sta ptr1+1
|
||||
|
||||
; while (F) {
|
||||
@ -61,17 +61,17 @@ __heapmemavail:
|
||||
; return Size + (_heapend - _heapptr) * sizeof (*_heapend);
|
||||
|
||||
@L2: lda ptr2
|
||||
add __heapend
|
||||
add ___heapend
|
||||
sta ptr2
|
||||
lda ptr2+1
|
||||
adc __heapend+1
|
||||
adc ___heapend+1
|
||||
tax
|
||||
|
||||
lda ptr2
|
||||
sub __heapptr
|
||||
sub ___heapptr
|
||||
sta ptr2
|
||||
txa
|
||||
sbc __heapptr+1
|
||||
sbc ___heapptr+1
|
||||
tax
|
||||
lda ptr2
|
||||
|
||||
|
@ -105,27 +105,27 @@ _free: sta ptr2
|
||||
tay
|
||||
lda ptr2+1
|
||||
adc ptr1+1
|
||||
cpy __heapptr
|
||||
cpy ___heapptr
|
||||
bne heapadd ; Add to free list
|
||||
cmp __heapptr+1
|
||||
cmp ___heapptr+1
|
||||
bne heapadd
|
||||
|
||||
; The pointer is located at the heap top. Lower the heap top pointer to
|
||||
; release the block.
|
||||
|
||||
@L3: lda ptr2
|
||||
sta __heapptr
|
||||
sta ___heapptr
|
||||
lda ptr2+1
|
||||
sta __heapptr+1
|
||||
sta ___heapptr+1
|
||||
|
||||
; Check if the last block in the freelist is now at heap top. If so, remove
|
||||
; this block from the freelist.
|
||||
|
||||
lda __heaplast
|
||||
lda ___heaplast
|
||||
sta ptr1
|
||||
ora __heaplast+1
|
||||
ora ___heaplast+1
|
||||
beq @L9 ; Jump if free list empty
|
||||
lda __heaplast+1
|
||||
lda ___heaplast+1
|
||||
sta ptr1+1 ; Pointer to last block now in ptr1
|
||||
|
||||
ldy #freeblock::size
|
||||
@ -136,35 +136,35 @@ _free: sta ptr2
|
||||
lda (ptr1),y
|
||||
adc ptr1+1
|
||||
|
||||
cmp __heapptr+1
|
||||
cmp ___heapptr+1
|
||||
bne @L9 ; Jump if last block not on top of heap
|
||||
cpx __heapptr
|
||||
cpx ___heapptr
|
||||
bne @L9 ; Jump if last block not on top of heap
|
||||
|
||||
; Remove the last block
|
||||
|
||||
lda ptr1
|
||||
sta __heapptr
|
||||
sta ___heapptr
|
||||
lda ptr1+1
|
||||
sta __heapptr+1
|
||||
sta ___heapptr+1
|
||||
|
||||
; Correct the next pointer of the now last block
|
||||
|
||||
ldy #freeblock::prev+1 ; Offset of ->prev field
|
||||
lda (ptr1),y
|
||||
sta ptr2+1 ; Remember f->prev in ptr2
|
||||
sta __heaplast+1
|
||||
sta ___heaplast+1
|
||||
dey
|
||||
lda (ptr1),y
|
||||
sta ptr2 ; Remember f->prev in ptr2
|
||||
sta __heaplast
|
||||
ora __heaplast+1 ; -> prev == 0?
|
||||
sta ___heaplast
|
||||
ora ___heaplast+1 ; -> prev == 0?
|
||||
bne @L8 ; Jump if free list not empty
|
||||
|
||||
; Free list is now empty (A = 0)
|
||||
|
||||
sta __heapfirst
|
||||
sta __heapfirst+1
|
||||
sta ___heapfirst
|
||||
sta ___heapfirst+1
|
||||
|
||||
; Done
|
||||
|
||||
@ -283,9 +283,9 @@ _free: sta ptr2
|
||||
; Check if the free list is empty, storing _hfirst into ptr3 for later
|
||||
|
||||
heapadd:
|
||||
lda __heapfirst
|
||||
lda ___heapfirst
|
||||
sta ptr3
|
||||
lda __heapfirst+1
|
||||
lda ___heapfirst+1
|
||||
sta ptr3+1
|
||||
ora ptr3
|
||||
bne SearchFreeList
|
||||
@ -301,10 +301,10 @@ heapadd:
|
||||
|
||||
lda ptr2
|
||||
ldx ptr2+1
|
||||
sta __heapfirst
|
||||
stx __heapfirst+1 ; _heapfirst = f;
|
||||
sta __heaplast
|
||||
stx __heaplast+1 ; _heaplast = f;
|
||||
sta ___heapfirst
|
||||
stx ___heapfirst+1 ; _heapfirst = f;
|
||||
sta ___heaplast
|
||||
stx ___heaplast+1 ; _heaplast = f;
|
||||
|
||||
rts ; Done
|
||||
|
||||
@ -351,9 +351,9 @@ SearchFreeList:
|
||||
sta (ptr2),y ; Clear low byte of f->next
|
||||
|
||||
lda ptr2 ; _heaplast = f;
|
||||
sta __heaplast
|
||||
sta ___heaplast
|
||||
lda ptr2+1
|
||||
sta __heaplast+1
|
||||
sta ___heaplast+1
|
||||
|
||||
; Since we have checked the case that the freelist is empty before, if the
|
||||
; right pointer is NULL, the left *cannot* be NULL here. So skip the
|
||||
@ -414,9 +414,9 @@ CheckRightMerge:
|
||||
; f->next is zero, this is now the last block
|
||||
|
||||
@L1: lda ptr2 ; _heaplast = f;
|
||||
sta __heaplast
|
||||
sta ___heaplast
|
||||
lda ptr2+1
|
||||
sta __heaplast+1
|
||||
sta ___heaplast+1
|
||||
jmp CheckLeftMerge
|
||||
|
||||
; No right merge, just set the link.
|
||||
@ -451,9 +451,9 @@ CheckLeftMerge:
|
||||
sta (ptr2),y
|
||||
|
||||
lda ptr2 ; _heapfirst = f;
|
||||
sta __heapfirst
|
||||
sta ___heapfirst
|
||||
lda ptr2+1
|
||||
sta __heapfirst+1
|
||||
sta ___heapfirst+1
|
||||
|
||||
rts ; Done
|
||||
|
||||
@ -510,9 +510,9 @@ CheckLeftMerge2:
|
||||
; This is now the last block, do _heaplast = left
|
||||
|
||||
@L1: lda ptr4
|
||||
sta __heaplast
|
||||
sta ___heaplast
|
||||
lda ptr4+1
|
||||
sta __heaplast+1
|
||||
sta ___heaplast+1
|
||||
rts ; Done
|
||||
|
||||
; No merge of the left block, just set the link. Y points to size+1 if
|
||||
|
@ -140,9 +140,9 @@ _malloc:
|
||||
|
||||
; Load a pointer to the freelist into ptr2
|
||||
|
||||
@L2: lda __heapfirst
|
||||
@L2: lda ___heapfirst
|
||||
sta ptr2
|
||||
lda __heapfirst+1
|
||||
lda ___heapfirst+1
|
||||
sta ptr2+1
|
||||
|
||||
; Search the freelist for a block that is big enough. We will calculate
|
||||
@ -173,16 +173,16 @@ _malloc:
|
||||
|
||||
; We did not find a block big enough. Try to use new space from the heap top.
|
||||
|
||||
lda __heapptr
|
||||
lda ___heapptr
|
||||
add ptr1 ; _heapptr + size
|
||||
tay
|
||||
lda __heapptr+1
|
||||
lda ___heapptr+1
|
||||
adc ptr1+1
|
||||
bcs OutOfHeapSpace ; On overflow, we're surely out of space
|
||||
|
||||
cmp __heapend+1
|
||||
cmp ___heapend+1
|
||||
bne @L5
|
||||
cpy __heapend
|
||||
cpy ___heapend
|
||||
@L5: bcc TakeFromTop
|
||||
beq TakeFromTop
|
||||
|
||||
@ -196,13 +196,13 @@ Done: rts
|
||||
; There is enough space left, take it from the heap top
|
||||
|
||||
TakeFromTop:
|
||||
ldx __heapptr ; p = _heapptr;
|
||||
ldx ___heapptr ; p = _heapptr;
|
||||
stx ptr2
|
||||
ldx __heapptr+1
|
||||
ldx ___heapptr+1
|
||||
stx ptr2+1
|
||||
|
||||
sty __heapptr ; _heapptr += size;
|
||||
sta __heapptr+1
|
||||
sty ___heapptr ; _heapptr += size;
|
||||
sta ___heapptr+1
|
||||
jmp FillSizeAndRet ; Done
|
||||
|
||||
; We found a block big enough. If the block can hold just the
|
||||
@ -245,10 +245,10 @@ BlockFound:
|
||||
; Do _hfirst = f->next
|
||||
|
||||
@L1: lda (ptr2),y ; Load high byte of f->next
|
||||
sta __heapfirst+1
|
||||
sta ___heapfirst+1
|
||||
dey ; Points to next
|
||||
lda (ptr2),y ; Load low byte of f->next
|
||||
sta __heapfirst
|
||||
sta ___heapfirst
|
||||
|
||||
; Check f->next. Y points always to next if we come here
|
||||
|
||||
@ -275,10 +275,10 @@ BlockFound:
|
||||
; Do _hlast = f->prev
|
||||
|
||||
@L3: lda (ptr2),y ; Load low byte of f->prev
|
||||
sta __heaplast
|
||||
sta ___heaplast
|
||||
iny ; Points to prev+1
|
||||
lda (ptr2),y ; Load high byte of f->prev
|
||||
sta __heaplast+1
|
||||
sta ___heaplast+1
|
||||
jmp RetUserPtr ; Done
|
||||
|
||||
; We must slice the block found. Cut off space from the upper end, so we
|
||||
|
@ -74,12 +74,12 @@ void* __fastcall__ realloc (void* block, register size_t size)
|
||||
oldsize = b->size;
|
||||
|
||||
/* Is the block at the current heap top? */
|
||||
if (((unsigned) b) + oldsize == ((unsigned) _heapptr)) {
|
||||
if (((unsigned) b) + oldsize == ((unsigned) __heapptr)) {
|
||||
/* Check if we've enough memory at the heap top */
|
||||
newhptr = ((unsigned) _heapptr) - oldsize + size;
|
||||
if (newhptr <= ((unsigned) _heapend)) {
|
||||
newhptr = ((unsigned) __heapptr) - oldsize + size;
|
||||
if (newhptr <= ((unsigned) __heapend)) {
|
||||
/* Ok, there's space enough */
|
||||
_heapptr = (unsigned*) newhptr;
|
||||
__heapptr = (unsigned*) newhptr;
|
||||
b->size = size;
|
||||
b->start = b;
|
||||
return block;
|
||||
|
Loading…
Reference in New Issue
Block a user