mirror of
https://github.com/cc65/cc65.git
synced 2025-01-02 09:34:22 +00:00
Move _heap.h to the compiler include dir.
Create heap.inc and use that from the assembler code. Rename heap related _h... variables to _heap... Add _heapmaxavail and _heapmemavail functions. git-svn-id: svn://svn.cc65.org/cc65/trunk@1912 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
parent
8012074ea0
commit
581c12ce40
25
asminc/_heap.inc
Normal file
25
asminc/_heap.inc
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
;
|
||||||
|
; _heap.inc
|
||||||
|
;
|
||||||
|
; (C) Copyright 2003 Ullrich von Bassewitz (uz@cc65.org)
|
||||||
|
;
|
||||||
|
|
||||||
|
; Assembler include file that makes the constants and structures in _heap.h
|
||||||
|
; available for asm code.
|
||||||
|
|
||||||
|
HEAP_ADMIN_SPACE = 2
|
||||||
|
HEAP_MIN_BLOCKSIZE = 6 ; Minimum size of an allocated block
|
||||||
|
|
||||||
|
; Struct freeblock offsets and size. NOTE: For performance reasons, the asm
|
||||||
|
; code often uses increment/decrement operators to access other offsets, so
|
||||||
|
; just changing offsets here will probably not work.
|
||||||
|
freeblock_size = 0
|
||||||
|
freeblock_next = 2
|
||||||
|
freeblock_prev = 4
|
||||||
|
|
||||||
|
; Variables
|
||||||
|
.global __heaporg
|
||||||
|
.global __heapptr
|
||||||
|
.global __heapend
|
||||||
|
.global __heapfirst
|
||||||
|
.global __heaplast
|
@ -15,7 +15,7 @@
|
|||||||
/* Space needed for administering used blocks */
|
/* Space needed for administering used blocks */
|
||||||
#define HEAP_ADMIN_SPACE sizeof (unsigned)
|
#define HEAP_ADMIN_SPACE sizeof (unsigned)
|
||||||
|
|
||||||
/* The data type used to implement the free list.
|
/* The data type used to implement the free list.
|
||||||
* Beware: Field order is significant!
|
* Beware: Field order is significant!
|
||||||
*/
|
*/
|
||||||
struct freeblock {
|
struct freeblock {
|
||||||
@ -27,11 +27,11 @@ struct freeblock {
|
|||||||
|
|
||||||
|
|
||||||
/* Variables that describe the heap */
|
/* Variables that describe the heap */
|
||||||
extern unsigned* _horg; /* Bottom of heap */
|
extern unsigned* _heaporg; /* Bottom of heap */
|
||||||
extern unsigned* _hptr; /* Current top */
|
extern unsigned* _heapptr; /* Current top */
|
||||||
extern unsigned* _hend; /* Upper limit */
|
extern unsigned* _heapend; /* Upper limit */
|
||||||
extern struct freeblock* _hfirst; /* First free block in list */
|
extern struct freeblock* _heapfirst; /* First free block in list */
|
||||||
extern struct freeblock* _hlast; /* Last free block in list */
|
extern struct freeblock* _heaplast; /* Last free block in list */
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -57,8 +57,17 @@ void* __fastcall__ malloc (size_t size);
|
|||||||
void* __fastcall__ calloc (size_t count, size_t size);
|
void* __fastcall__ calloc (size_t count, size_t size);
|
||||||
void* __fastcall__ realloc (void* block, size_t size);
|
void* __fastcall__ realloc (void* block, size_t size);
|
||||||
void __fastcall__ free (void* block);
|
void __fastcall__ free (void* block);
|
||||||
/* Non standard functions */
|
|
||||||
void __fastcall__ _heapadd (void* mem, size_t size);
|
/* Non standard memory management functions */
|
||||||
|
|
||||||
|
void __fastcall__ _heapadd (void* mem, size_t size);
|
||||||
|
/* Add a block to the heap */
|
||||||
|
|
||||||
|
size_t __fastcall__ _heapmemavail (void);
|
||||||
|
/* Return the total free heap space */
|
||||||
|
|
||||||
|
size_t __fastcall__ _heapmaxavail (void);
|
||||||
|
/* Return the size of the largest free block on the heap */
|
||||||
|
|
||||||
/* Random numbers */
|
/* Random numbers */
|
||||||
#define RAND_MAX 0x7FFF
|
#define RAND_MAX 0x7FFF
|
||||||
|
@ -54,6 +54,8 @@ S_OBJS = _fdesc.o \
|
|||||||
_fopen.o \
|
_fopen.o \
|
||||||
_heap.o \
|
_heap.o \
|
||||||
_heapadd.o \
|
_heapadd.o \
|
||||||
|
_heapmaxavail.o \
|
||||||
|
_heapmemavail.o \
|
||||||
_oserror.o \
|
_oserror.o \
|
||||||
_printf.o \
|
_printf.o \
|
||||||
_swap.o \
|
_swap.o \
|
||||||
|
@ -4,23 +4,25 @@
|
|||||||
; Heap variables and initialization.
|
; Heap variables and initialization.
|
||||||
;
|
;
|
||||||
|
|
||||||
.export __horg, __hptr, __hend, __hfirst, __hlast
|
|
||||||
.constructor initheap, 24
|
.constructor initheap, 24
|
||||||
.import __BSS_RUN__, __BSS_SIZE__, __STACKSIZE__
|
.import __BSS_RUN__, __BSS_SIZE__, __STACKSIZE__
|
||||||
.importzp sp
|
.importzp sp
|
||||||
|
|
||||||
|
.include "_heap.inc"
|
||||||
|
|
||||||
|
|
||||||
.data
|
.data
|
||||||
|
|
||||||
__horg:
|
__heaporg:
|
||||||
.word __BSS_RUN__+__BSS_SIZE__ ; Linker calculates this symbol
|
.word __BSS_RUN__+__BSS_SIZE__ ; Linker calculates this symbol
|
||||||
__hptr:
|
__heapptr:
|
||||||
.word __BSS_RUN__+__BSS_SIZE__ ; Dito
|
.word __BSS_RUN__+__BSS_SIZE__ ; Dito
|
||||||
__hend:
|
__heapend:
|
||||||
.word __BSS_RUN__+__BSS_SIZE__
|
.word __BSS_RUN__+__BSS_SIZE__
|
||||||
__hfirst:
|
__heapfirst:
|
||||||
.word 0
|
.word 0
|
||||||
__hlast:
|
__heaplast:
|
||||||
.word 0
|
.word 0
|
||||||
|
|
||||||
|
|
||||||
; Initialization. Will be called from startup!
|
; Initialization. Will be called from startup!
|
||||||
@ -28,12 +30,12 @@ __hlast:
|
|||||||
.code
|
.code
|
||||||
|
|
||||||
initheap:
|
initheap:
|
||||||
sec
|
sec
|
||||||
lda sp
|
lda sp
|
||||||
sbc #<__STACKSIZE__
|
sbc #<__STACKSIZE__
|
||||||
sta __hend
|
sta __heapend
|
||||||
lda sp+1
|
lda sp+1
|
||||||
sbc #>__STACKSIZE__
|
sbc #>__STACKSIZE__
|
||||||
sta __hend+1
|
sta __heapend+1
|
||||||
rts
|
rts
|
||||||
|
|
||||||
|
@ -10,22 +10,16 @@
|
|||||||
.importzp ptr1, ptr2
|
.importzp ptr1, ptr2
|
||||||
.import popax
|
.import popax
|
||||||
.import heapadd
|
.import heapadd
|
||||||
.export _heapadd
|
.export __heapadd
|
||||||
|
|
||||||
|
.include "_heap.inc"
|
||||||
|
|
||||||
.macpack generic
|
.macpack generic
|
||||||
|
|
||||||
; Offsets into struct freeblock and other constant stuff
|
;-----------------------------------------------------------------------------
|
||||||
|
|
||||||
size = 0
|
|
||||||
next = 2
|
|
||||||
prev = 4
|
|
||||||
admin_space = 2
|
|
||||||
min_size = 6
|
|
||||||
|
|
||||||
|
|
||||||
; Code
|
; Code
|
||||||
|
|
||||||
_heapadd:
|
__heapadd:
|
||||||
sta ptr1 ; Store size in ptr1
|
sta ptr1 ; Store size in ptr1
|
||||||
stx ptr1+1
|
stx ptr1+1
|
||||||
jsr popax ; Get the block pointer
|
jsr popax ; Get the block pointer
|
||||||
@ -39,14 +33,14 @@ _heapadd:
|
|||||||
lda ptr1 ; Load low byte
|
lda ptr1 ; Load low byte
|
||||||
ldx ptr1+1 ; Load/check high byte
|
ldx ptr1+1 ; Load/check high byte
|
||||||
bne @L1
|
bne @L1
|
||||||
cmp #min_size
|
cmp #HEAP_MIN_BLOCKSIZE
|
||||||
bcs @L1
|
bcs @L1
|
||||||
|
|
||||||
rts ; Block not large enough
|
rts ; Block not large enough
|
||||||
|
|
||||||
; The block is large enough. Set the size field in the block.
|
; The block is large enough. Set the size field in the block.
|
||||||
|
|
||||||
@L1: ldy #size
|
@L1: ldy #freeblock_size
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
iny
|
iny
|
||||||
txa
|
txa
|
||||||
@ -56,5 +50,3 @@ _heapadd:
|
|||||||
|
|
||||||
jmp heapadd
|
jmp heapadd
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
77
libsrc/common/_heapmaxavail.s
Normal file
77
libsrc/common/_heapmaxavail.s
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
;
|
||||||
|
; Ullrich von Bassewitz, 2003-02-01
|
||||||
|
;
|
||||||
|
; Return the size of the largest free block on the heap.
|
||||||
|
;
|
||||||
|
; size_t __fastcall__ _heapmaxavail (void);
|
||||||
|
;
|
||||||
|
;
|
||||||
|
|
||||||
|
.importzp ptr1, ptr2
|
||||||
|
.export __heapmaxavail
|
||||||
|
|
||||||
|
.include "_heap.inc"
|
||||||
|
|
||||||
|
.macpack generic
|
||||||
|
|
||||||
|
;-----------------------------------------------------------------------------
|
||||||
|
; Code
|
||||||
|
|
||||||
|
__heapmaxavail:
|
||||||
|
|
||||||
|
; size_t Size = (_heapend - _heapptr) * sizeof (*_heapend);
|
||||||
|
|
||||||
|
lda __heapend
|
||||||
|
sub __heapptr
|
||||||
|
sta ptr2
|
||||||
|
lda __heapend+1
|
||||||
|
sbc __heapptr+1
|
||||||
|
sta ptr2+1
|
||||||
|
|
||||||
|
; struct freeblock* F = _heapfirst;
|
||||||
|
|
||||||
|
lda __heapfirst
|
||||||
|
sta ptr1
|
||||||
|
lda __heapfirst+1
|
||||||
|
@L1: sta ptr1+1
|
||||||
|
|
||||||
|
; while (F) {
|
||||||
|
|
||||||
|
ora ptr1
|
||||||
|
beq @L3 ; Jump if end of free list reached
|
||||||
|
|
||||||
|
; if (Size < F->size) {
|
||||||
|
|
||||||
|
ldy #freeblock_size
|
||||||
|
lda ptr2
|
||||||
|
sub (ptr1),y
|
||||||
|
iny
|
||||||
|
lda ptr2+1
|
||||||
|
sbc (ptr1),y
|
||||||
|
bcs @L2
|
||||||
|
|
||||||
|
; Size = F->size;
|
||||||
|
|
||||||
|
ldy #freeblock_size
|
||||||
|
lda (ptr1),y
|
||||||
|
sta ptr2
|
||||||
|
iny
|
||||||
|
lda (ptr1),y
|
||||||
|
sta ptr2+1
|
||||||
|
|
||||||
|
; F = F->next;
|
||||||
|
|
||||||
|
@L2: iny ; Points to F->next
|
||||||
|
lda (ptr1),y
|
||||||
|
tax
|
||||||
|
iny
|
||||||
|
lda (ptr1),y
|
||||||
|
stx ptr1
|
||||||
|
jmp @L1
|
||||||
|
|
||||||
|
; return Size;
|
||||||
|
|
||||||
|
@L3: lda ptr2
|
||||||
|
ldx ptr2+1
|
||||||
|
rts
|
||||||
|
|
81
libsrc/common/_heapmemavail.s
Normal file
81
libsrc/common/_heapmemavail.s
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
;
|
||||||
|
; Ullrich von Bassewitz, 2003-02-01
|
||||||
|
;
|
||||||
|
; Return the amount of free memory on the heap.
|
||||||
|
;
|
||||||
|
; size_t __fastcall__ _heapmemavail (void);
|
||||||
|
;
|
||||||
|
;
|
||||||
|
|
||||||
|
.importzp ptr1, ptr2
|
||||||
|
.export __heapmemavail
|
||||||
|
|
||||||
|
.include "_heap.inc"
|
||||||
|
|
||||||
|
.macpack generic
|
||||||
|
|
||||||
|
;-----------------------------------------------------------------------------
|
||||||
|
; Code
|
||||||
|
|
||||||
|
__heapmemavail:
|
||||||
|
|
||||||
|
; size_t Size = 0;
|
||||||
|
|
||||||
|
lda #0
|
||||||
|
sta ptr2
|
||||||
|
sta ptr2+1
|
||||||
|
|
||||||
|
; struct freeblock* F = _heapfirst;
|
||||||
|
|
||||||
|
lda __heapfirst
|
||||||
|
sta ptr1
|
||||||
|
lda __heapfirst+1
|
||||||
|
@L1: sta ptr1+1
|
||||||
|
|
||||||
|
; while (F) {
|
||||||
|
|
||||||
|
ora ptr1
|
||||||
|
beq @L2 ; Jump if end of free list reached
|
||||||
|
|
||||||
|
; Size += F->size;
|
||||||
|
|
||||||
|
ldy #freeblock_size
|
||||||
|
lda (ptr1),y
|
||||||
|
add ptr2
|
||||||
|
sta ptr2
|
||||||
|
iny
|
||||||
|
lda (ptr1),y
|
||||||
|
adc ptr2+1
|
||||||
|
sta ptr2+1
|
||||||
|
|
||||||
|
; F = F->next;
|
||||||
|
|
||||||
|
iny ; Points to F->next
|
||||||
|
lda (ptr1),y
|
||||||
|
tax
|
||||||
|
iny
|
||||||
|
lda (ptr1),y
|
||||||
|
stx ptr1
|
||||||
|
jmp @L1
|
||||||
|
|
||||||
|
; return Size + (_heapend - _heapptr) * sizeof (*_heapend);
|
||||||
|
|
||||||
|
@L2: lda ptr2
|
||||||
|
add __heapend
|
||||||
|
sta ptr2
|
||||||
|
lda ptr2+1
|
||||||
|
adc __heapend+1
|
||||||
|
tax
|
||||||
|
|
||||||
|
lda ptr2
|
||||||
|
sub __heapptr
|
||||||
|
sta ptr2
|
||||||
|
txa
|
||||||
|
sbc __heapptr+1
|
||||||
|
tax
|
||||||
|
lda ptr2
|
||||||
|
|
||||||
|
rts
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -60,20 +60,13 @@
|
|||||||
;
|
;
|
||||||
|
|
||||||
.importzp ptr1, ptr2, ptr3, ptr4
|
.importzp ptr1, ptr2, ptr3, ptr4
|
||||||
.import __hptr, __hfirst, __hlast, __hend
|
|
||||||
.export _free, heapadd
|
.export _free, heapadd
|
||||||
|
|
||||||
|
.include "_heap.inc"
|
||||||
|
|
||||||
.macpack generic
|
.macpack generic
|
||||||
|
|
||||||
; Offsets into struct freeblock and other constant stuff
|
;-----------------------------------------------------------------------------
|
||||||
|
|
||||||
size = 0
|
|
||||||
next = 2
|
|
||||||
prev = 4
|
|
||||||
admin_space = 2
|
|
||||||
min_size = 6
|
|
||||||
|
|
||||||
|
|
||||||
; Code
|
; Code
|
||||||
|
|
||||||
_free: sta ptr2
|
_free: sta ptr2
|
||||||
@ -89,11 +82,11 @@ _free: sta ptr2
|
|||||||
; Remember the block size in ptr1.
|
; Remember the block size in ptr1.
|
||||||
|
|
||||||
lda ptr2
|
lda ptr2
|
||||||
sub #admin_space
|
sub #HEAP_ADMIN_SPACE
|
||||||
sta ptr2
|
sta ptr2
|
||||||
bcs @L1
|
bcs @L1
|
||||||
dec ptr2+1
|
dec ptr2+1
|
||||||
@L1: ldy #size+1
|
@L1: ldy #freeblock_size+1
|
||||||
lda (ptr2),y ; High byte of size
|
lda (ptr2),y ; High byte of size
|
||||||
sta ptr1+1 ; Save it
|
sta ptr1+1 ; Save it
|
||||||
dey
|
dey
|
||||||
@ -106,66 +99,66 @@ _free: sta ptr2
|
|||||||
tay
|
tay
|
||||||
lda ptr2+1
|
lda ptr2+1
|
||||||
adc ptr1+1
|
adc ptr1+1
|
||||||
cpy __hptr
|
cpy __heapptr
|
||||||
bne heapadd ; Add to free list
|
bne heapadd ; Add to free list
|
||||||
cmp __hptr+1
|
cmp __heapptr+1
|
||||||
bne heapadd
|
bne heapadd
|
||||||
|
|
||||||
; The pointer is located at the heap top. Lower the heap top pointer to
|
; The pointer is located at the heap top. Lower the heap top pointer to
|
||||||
; release the block.
|
; release the block.
|
||||||
|
|
||||||
@L3: lda ptr2
|
@L3: lda ptr2
|
||||||
sta __hptr
|
sta __heapptr
|
||||||
lda ptr2+1
|
lda ptr2+1
|
||||||
sta __hptr+1
|
sta __heapptr+1
|
||||||
|
|
||||||
; Check if the last block in the freelist is now at heap top. If so, remove
|
; Check if the last block in the freelist is now at heap top. If so, remove
|
||||||
; this block from the freelist.
|
; this block from the freelist.
|
||||||
|
|
||||||
lda __hlast
|
lda __heaplast
|
||||||
sta ptr1
|
sta ptr1
|
||||||
ora __hlast+1
|
ora __heaplast+1
|
||||||
beq @L9 ; Jump if free list empty
|
beq @L9 ; Jump if free list empty
|
||||||
lda __hlast+1
|
lda __heaplast+1
|
||||||
sta ptr1+1 ; Pointer to last block now in ptr1
|
sta ptr1+1 ; Pointer to last block now in ptr1
|
||||||
|
|
||||||
ldy #size
|
ldy #freeblock_size
|
||||||
lda (ptr1),y ; Low byte of block size
|
lda (ptr1),y ; Low byte of block size
|
||||||
add ptr1
|
add ptr1
|
||||||
tax
|
tax
|
||||||
iny ; High byte of block size
|
iny ; High byte of block size
|
||||||
lda (ptr1),y
|
lda (ptr1),y
|
||||||
adc ptr1+1
|
adc ptr1+1
|
||||||
|
|
||||||
cmp __hptr+1
|
cmp __heapptr+1
|
||||||
bne @L9 ; Jump if last block not on top of heap
|
bne @L9 ; Jump if last block not on top of heap
|
||||||
cpx __hptr
|
cpx __heapptr
|
||||||
bne @L9 ; Jump if last block not on top of heap
|
bne @L9 ; Jump if last block not on top of heap
|
||||||
|
|
||||||
; Remove the last block
|
; Remove the last block
|
||||||
|
|
||||||
lda ptr1
|
lda ptr1
|
||||||
sta __hptr
|
sta __heapptr
|
||||||
lda ptr1+1
|
lda ptr1+1
|
||||||
sta __hptr+1
|
sta __heapptr+1
|
||||||
|
|
||||||
; Correct the next pointer of the now last block
|
; Correct the next pointer of the now last block
|
||||||
|
|
||||||
ldy #prev+1 ; Offset of ->prev field
|
ldy #freeblock_prev+1 ; Offset of ->prev field
|
||||||
lda (ptr1),y
|
lda (ptr1),y
|
||||||
sta ptr2+1 ; Remember f->prev in ptr2
|
sta ptr2+1 ; Remember f->prev in ptr2
|
||||||
sta __hlast+1
|
sta __heaplast+1
|
||||||
dey
|
dey
|
||||||
lda (ptr1),y
|
lda (ptr1),y
|
||||||
sta ptr2 ; Remember f->prev in ptr2
|
sta ptr2 ; Remember f->prev in ptr2
|
||||||
sta __hlast
|
sta __heaplast
|
||||||
ora __hlast+1 ; -> prev == 0?
|
ora __heaplast+1 ; -> prev == 0?
|
||||||
bne @L8 ; Jump if free list not empty
|
bne @L8 ; Jump if free list not empty
|
||||||
|
|
||||||
; Free list is now empty (A = 0)
|
; Free list is now empty (A = 0)
|
||||||
|
|
||||||
sta __hfirst
|
sta __heapfirst
|
||||||
sta __hfirst+1
|
sta __heapfirst+1
|
||||||
|
|
||||||
; Done
|
; Done
|
||||||
|
|
||||||
@ -176,9 +169,9 @@ _free: sta ptr2
|
|||||||
@L8: lda #$00
|
@L8: lda #$00
|
||||||
dey ; Points to high byte of ->next
|
dey ; Points to high byte of ->next
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
dey ; Low byte of f->prev->next
|
dey ; Low byte of f->prev->next
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
rts ; Done
|
rts ; Done
|
||||||
|
|
||||||
; The block is not on top of the heap. Add it to the free list. This was
|
; The block is not on top of the heap. Add it to the free list. This was
|
||||||
; formerly a separate function called __hadd that was implemented in C as
|
; formerly a separate function called __hadd that was implemented in C as
|
||||||
@ -235,10 +228,10 @@ _free: sta ptr2
|
|||||||
; /* Merge with the right block */
|
; /* Merge with the right block */
|
||||||
; f->size += right->size;
|
; f->size += right->size;
|
||||||
; if (f->next = right->next) {
|
; if (f->next = right->next) {
|
||||||
; f->next->prev = f;
|
; f->next->prev = f;
|
||||||
; } else {
|
; } else {
|
||||||
; /* This is now the last block */
|
; /* This is now the last block */
|
||||||
; _hlast = f;
|
; _hlast = f;
|
||||||
; }
|
; }
|
||||||
; } else {
|
; } else {
|
||||||
; /* No merge, just set the link */
|
; /* No merge, just set the link */
|
||||||
@ -256,10 +249,10 @@ _free: sta ptr2
|
|||||||
; /* Merge with the left block */
|
; /* Merge with the left block */
|
||||||
; left->size += f->size;
|
; left->size += f->size;
|
||||||
; if (left->next = f->next) {
|
; if (left->next = f->next) {
|
||||||
; left->next->prev = left;
|
; left->next->prev = left;
|
||||||
; } else {
|
; } else {
|
||||||
; /* This is now the last block */
|
; /* This is now the last block */
|
||||||
; _hlast = left;
|
; _hlast = left;
|
||||||
; }
|
; }
|
||||||
; } else {
|
; } else {
|
||||||
; /* No merge, just set the link */
|
; /* No merge, just set the link */
|
||||||
@ -279,9 +272,9 @@ _free: sta ptr2
|
|||||||
; Check if the free list is empty, storing _hfirst into ptr3 for later
|
; Check if the free list is empty, storing _hfirst into ptr3 for later
|
||||||
|
|
||||||
heapadd:
|
heapadd:
|
||||||
lda __hfirst
|
lda __heapfirst
|
||||||
sta ptr3
|
sta ptr3
|
||||||
lda __hfirst+1
|
lda __heapfirst+1
|
||||||
sta ptr3+1
|
sta ptr3+1
|
||||||
ora ptr3
|
ora ptr3
|
||||||
bne SearchFreeList
|
bne SearchFreeList
|
||||||
@ -289,49 +282,49 @@ heapadd:
|
|||||||
; The free list is empty, so this is the first and only block. A contains
|
; The free list is empty, so this is the first and only block. A contains
|
||||||
; zero if we come here.
|
; zero if we come here.
|
||||||
|
|
||||||
ldy #next-1
|
ldy #freeblock_next-1
|
||||||
@L2: iny ; f->next = f->prev = 0;
|
@L2: iny ; f->next = f->prev = 0;
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
cpy #prev+1 ; Done?
|
cpy #freeblock_prev+1 ; Done?
|
||||||
bne @L2
|
bne @L2
|
||||||
|
|
||||||
lda ptr2
|
lda ptr2
|
||||||
ldx ptr2+1
|
ldx ptr2+1
|
||||||
sta __hfirst
|
sta __heapfirst
|
||||||
stx __hfirst+1 ; _hfirst = f;
|
stx __heapfirst+1 ; _heapfirst = f;
|
||||||
sta __hlast
|
sta __heaplast
|
||||||
stx __hlast+1 ; _hlast = f;
|
stx __heaplast+1 ; _heaplast = f;
|
||||||
|
|
||||||
rts ; Done
|
rts ; Done
|
||||||
|
|
||||||
; We have to search the free list. As we are doing so, check if it is possible
|
; We have to search the free list. As we are doing so, check if it is possible
|
||||||
; to combine this block with another, already existing block. Beware: The
|
; to combine this block with another, already existing block. Beware: The
|
||||||
; block may be the "missing link" between two blocks.
|
; block may be the "missing link" between two blocks.
|
||||||
; ptr3 contains _hfirst (the start value of the search) when execution reaches
|
; ptr3 contains _hfirst (the start value of the search) when execution reaches
|
||||||
; this point, Y contains size+1. We do also know that _hfirst (and therefore
|
; this point, Y contains size+1. We do also know that _heapfirst (and therefore
|
||||||
; ptr3) is not zero on entry.
|
; ptr3) is not zero on entry.
|
||||||
|
|
||||||
SearchFreeList:
|
SearchFreeList:
|
||||||
lda #0
|
lda #0
|
||||||
sta ptr4
|
sta ptr4
|
||||||
sta ptr4+1 ; left = 0;
|
sta ptr4+1 ; left = 0;
|
||||||
ldy #next+1
|
ldy #freeblock_next+1
|
||||||
ldx ptr3
|
ldx ptr3
|
||||||
|
|
||||||
@Loop: lda ptr3+1 ; High byte of right
|
@Loop: lda ptr3+1 ; High byte of right
|
||||||
cmp ptr2+1
|
cmp ptr2+1
|
||||||
bne @L1
|
bne @L1
|
||||||
cpx ptr2
|
cpx ptr2
|
||||||
beq @L2
|
beq @L2
|
||||||
@L1: bcs CheckRightMerge
|
@L1: bcs CheckRightMerge
|
||||||
|
|
||||||
@L2: stx ptr4 ; left = right;
|
@L2: stx ptr4 ; left = right;
|
||||||
sta ptr4+1
|
sta ptr4+1
|
||||||
|
|
||||||
dey ; Points to next
|
dey ; Points to next
|
||||||
lda (ptr3),y ; right = right->next;
|
lda (ptr3),y ; right = right->next;
|
||||||
tax
|
tax
|
||||||
iny ; Points to next+1
|
iny ; Points to next+1
|
||||||
lda (ptr3),y
|
lda (ptr3),y
|
||||||
stx ptr3
|
stx ptr3
|
||||||
sta ptr3+1
|
sta ptr3+1
|
||||||
@ -342,14 +335,14 @@ SearchFreeList:
|
|||||||
; a merge. The new block is the new freelist end.
|
; a merge. The new block is the new freelist end.
|
||||||
; A is zero when we come here, Y points to next+1
|
; A is zero when we come here, Y points to next+1
|
||||||
|
|
||||||
sta (ptr2),y ; Clear high byte of f->next
|
sta (ptr2),y ; Clear high byte of f->next
|
||||||
dey
|
dey
|
||||||
sta (ptr2),y ; Clear low byte of f->next
|
sta (ptr2),y ; Clear low byte of f->next
|
||||||
|
|
||||||
lda ptr2 ; _hlast = f;
|
lda ptr2 ; _heaplast = f;
|
||||||
sta __hlast
|
sta __heaplast
|
||||||
lda ptr2+1
|
lda ptr2+1
|
||||||
sta __hlast+1
|
sta __heaplast+1
|
||||||
|
|
||||||
; Since we have checked the case that the freelist is empty before, if the
|
; 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
|
; right pointer is NULL, the left *cannot* be NULL here. So skip the
|
||||||
@ -362,7 +355,7 @@ SearchFreeList:
|
|||||||
|
|
||||||
CheckRightMerge:
|
CheckRightMerge:
|
||||||
lda ptr2
|
lda ptr2
|
||||||
add ptr1 ; f + size
|
add ptr1 ; f + size
|
||||||
tax
|
tax
|
||||||
lda ptr2+1
|
lda ptr2+1
|
||||||
adc ptr1+1
|
adc ptr1+1
|
||||||
@ -374,11 +367,11 @@ CheckRightMerge:
|
|||||||
|
|
||||||
; Merge with the right block. Do f->size += right->size;
|
; Merge with the right block. Do f->size += right->size;
|
||||||
|
|
||||||
ldy #size
|
ldy #freeblock_size
|
||||||
lda ptr1
|
lda ptr1
|
||||||
add (ptr3),y
|
add (ptr3),y
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
iny ; Points to size+1
|
iny ; Points to size+1
|
||||||
lda ptr1+1
|
lda ptr1+1
|
||||||
adc (ptr3),y
|
adc (ptr3),y
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
@ -386,153 +379,154 @@ CheckRightMerge:
|
|||||||
; Set f->next = right->next and remember f->next in ptr1 (we don't need the
|
; Set f->next = right->next and remember f->next in ptr1 (we don't need the
|
||||||
; size stored there any longer)
|
; size stored there any longer)
|
||||||
|
|
||||||
iny ; Points to next
|
iny ; Points to next
|
||||||
lda (ptr3),y ; Low byte of right->next
|
lda (ptr3),y ; Low byte of right->next
|
||||||
sta (ptr2),y ; Store to low byte of f->next
|
sta (ptr2),y ; Store to low byte of f->next
|
||||||
sta ptr1
|
sta ptr1
|
||||||
iny ; Points to next+1
|
iny ; Points to next+1
|
||||||
lda (ptr3),y ; High byte of right->next
|
lda (ptr3),y ; High byte of right->next
|
||||||
sta (ptr2),y ; Store to high byte of f->next
|
sta (ptr2),y ; Store to high byte of f->next
|
||||||
sta ptr1+1
|
sta ptr1+1
|
||||||
ora ptr1
|
ora ptr1
|
||||||
beq @L1 ; Jump if f->next zero
|
beq @L1 ; Jump if f->next zero
|
||||||
|
|
||||||
; f->next->prev = f;
|
; f->next->prev = f;
|
||||||
|
|
||||||
iny ; Points to prev
|
iny ; Points to prev
|
||||||
lda ptr2 ; Low byte of f
|
lda ptr2 ; Low byte of f
|
||||||
sta (ptr1),y ; Low byte of f->next->prev
|
sta (ptr1),y ; Low byte of f->next->prev
|
||||||
iny ; Points to prev+1
|
iny ; Points to prev+1
|
||||||
lda ptr2+1 ; High byte of f
|
lda ptr2+1 ; High byte of f
|
||||||
sta (ptr1),y ; High byte of f->next->prev
|
sta (ptr1),y ; High byte of f->next->prev
|
||||||
jmp CheckLeftMerge ; Done
|
jmp CheckLeftMerge ; Done
|
||||||
|
|
||||||
; f->next is zero, this is now the last block
|
; f->next is zero, this is now the last block
|
||||||
|
|
||||||
@L1: lda ptr2 ; _hlast = f;
|
@L1: lda ptr2 ; _heaplast = f;
|
||||||
sta __hlast
|
sta __heaplast
|
||||||
lda ptr2+1
|
lda ptr2+1
|
||||||
sta __hlast+1
|
sta __heaplast+1
|
||||||
jmp CheckLeftMerge
|
jmp CheckLeftMerge
|
||||||
|
|
||||||
; No right merge, just set the link.
|
; No right merge, just set the link.
|
||||||
|
|
||||||
NoRightMerge:
|
NoRightMerge:
|
||||||
ldy #next ; f->next = right;
|
ldy #freeblock_next ; f->next = right;
|
||||||
lda ptr3
|
lda ptr3
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
iny ; Points to next+1
|
iny ; Points to next+1
|
||||||
lda ptr3+1
|
lda ptr3+1
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
|
|
||||||
iny ; Points to prev
|
iny ; Points to prev
|
||||||
lda ptr2 ; right->prev = f;
|
lda ptr2 ; right->prev = f;
|
||||||
sta (ptr3),y
|
sta (ptr3),y
|
||||||
iny ; Points to prev+1
|
iny ; Points to prev+1
|
||||||
lda ptr2+1
|
lda ptr2+1
|
||||||
sta (ptr3),y
|
sta (ptr3),y
|
||||||
|
|
||||||
; Check if the left pointer is zero
|
; Check if the left pointer is zero
|
||||||
|
|
||||||
CheckLeftMerge:
|
CheckLeftMerge:
|
||||||
lda ptr4 ; left == NULL?
|
lda ptr4 ; left == NULL?
|
||||||
ora ptr4+1
|
ora ptr4+1
|
||||||
bne CheckLeftMerge2 ; Jump if there is a left block
|
bne CheckLeftMerge2 ; Jump if there is a left block
|
||||||
|
|
||||||
; We don't have a left block, so f is actually the new freelist start
|
; We don't have a left block, so f is actually the new freelist start
|
||||||
|
|
||||||
ldy #prev
|
ldy #freeblock_prev
|
||||||
sta (ptr2),y ; f->prev = 0;
|
sta (ptr2),y ; f->prev = 0;
|
||||||
iny
|
iny
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
|
|
||||||
lda ptr2 ; _hfirst = f;
|
lda ptr2 ; _heapfirst = f;
|
||||||
sta __hfirst
|
sta __heapfirst
|
||||||
lda ptr2+1
|
lda ptr2+1
|
||||||
sta __hfirst+1
|
sta __heapfirst+1
|
||||||
|
|
||||||
rts ; Done
|
rts ; Done
|
||||||
|
|
||||||
; Check if the left block is adjacent to the following one
|
; Check if the left block is adjacent to the following one
|
||||||
|
|
||||||
CheckLeftMerge2:
|
CheckLeftMerge2:
|
||||||
ldy #size ; Calculate left + left->size
|
ldy #freeblock_size ; Calculate left + left->size
|
||||||
lda (ptr4),y ; Low byte of left->size
|
lda (ptr4),y ; Low byte of left->size
|
||||||
add ptr4
|
add ptr4
|
||||||
tax
|
tax
|
||||||
iny ; Points to size+1
|
iny ; Points to size+1
|
||||||
lda (ptr4),y ; High byte of left->size
|
lda (ptr4),y ; High byte of left->size
|
||||||
adc ptr4+1
|
adc ptr4+1
|
||||||
|
|
||||||
cpx ptr2
|
cpx ptr2
|
||||||
bne NoLeftMerge
|
bne NoLeftMerge
|
||||||
cmp ptr2+1
|
cmp ptr2+1
|
||||||
bne NoLeftMerge ; Jump if blocks not adjacent
|
bne NoLeftMerge ; Jump if blocks not adjacent
|
||||||
|
|
||||||
; Merge with the left block. Do left->size += f->size;
|
; Merge with the left block. Do left->size += f->size;
|
||||||
|
|
||||||
dey ; Points to size
|
dey ; Points to size
|
||||||
lda (ptr4),y
|
lda (ptr4),y
|
||||||
add (ptr2),y
|
add (ptr2),y
|
||||||
sta (ptr4),y
|
sta (ptr4),y
|
||||||
iny ; Points to size+1
|
iny ; Points to size+1
|
||||||
lda (ptr4),y
|
lda (ptr4),y
|
||||||
adc (ptr2),y
|
adc (ptr2),y
|
||||||
sta (ptr4),y
|
sta (ptr4),y
|
||||||
|
|
||||||
; Set left->next = f->next and remember left->next in ptr1.
|
; Set left->next = f->next and remember left->next in ptr1.
|
||||||
|
|
||||||
iny ; Points to next
|
iny ; Points to next
|
||||||
lda (ptr2),y ; Low byte of f->next
|
lda (ptr2),y ; Low byte of f->next
|
||||||
sta (ptr4),y
|
sta (ptr4),y
|
||||||
sta ptr1
|
sta ptr1
|
||||||
iny ; Points to next+1
|
iny ; Points to next+1
|
||||||
lda (ptr2),y ; High byte of f->next
|
lda (ptr2),y ; High byte of f->next
|
||||||
sta (ptr4),y
|
sta (ptr4),y
|
||||||
sta ptr1+1
|
sta ptr1+1
|
||||||
ora ptr1 ; left->next == NULL?
|
ora ptr1 ; left->next == NULL?
|
||||||
beq @L1
|
beq @L1
|
||||||
|
|
||||||
; Do left->next->prev = left
|
; Do left->next->prev = left
|
||||||
|
|
||||||
iny ; Points to prev
|
iny ; Points to prev
|
||||||
lda ptr4 ; Low byte of left
|
lda ptr4 ; Low byte of left
|
||||||
sta (ptr1),y
|
sta (ptr1),y
|
||||||
iny
|
iny
|
||||||
lda ptr4+1 ; High byte of left
|
lda ptr4+1 ; High byte of left
|
||||||
sta (ptr1),y
|
sta (ptr1),y
|
||||||
rts ; Done
|
rts ; Done
|
||||||
|
|
||||||
; This is now the last block, do _hlast = left
|
; This is now the last block, do _heaplast = left
|
||||||
|
|
||||||
@L1: lda ptr4
|
@L1: lda ptr4
|
||||||
sta __hlast
|
sta __heaplast
|
||||||
lda ptr4+1
|
lda ptr4+1
|
||||||
sta __hlast+1
|
sta __heaplast+1
|
||||||
rts ; Done
|
rts ; Done
|
||||||
|
|
||||||
; No merge of the left block, just set the link. Y points to size+1 if
|
; No merge of the left block, just set the link. Y points to size+1 if
|
||||||
; we come here. Do left->next = f.
|
; we come here. Do left->next = f.
|
||||||
|
|
||||||
NoLeftMerge:
|
NoLeftMerge:
|
||||||
iny ; Points to next
|
iny ; Points to next
|
||||||
lda ptr2 ; Low byte of left
|
lda ptr2 ; Low byte of left
|
||||||
sta (ptr4),y
|
sta (ptr4),y
|
||||||
iny
|
iny
|
||||||
lda ptr2+1 ; High byte of left
|
lda ptr2+1 ; High byte of left
|
||||||
sta (ptr4),y
|
sta (ptr4),y
|
||||||
|
|
||||||
; Do f->prev = left
|
; Do f->prev = left
|
||||||
|
|
||||||
iny ; Points to prev
|
iny ; Points to prev
|
||||||
lda ptr4
|
lda ptr4
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
iny
|
iny
|
||||||
lda ptr4+1
|
lda ptr4+1
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
rts ; Done
|
rts ; Done
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -106,50 +106,43 @@
|
|||||||
|
|
||||||
|
|
||||||
.importzp ptr1, ptr2, ptr3
|
.importzp ptr1, ptr2, ptr3
|
||||||
.import __hptr, __hfirst, __hlast, __hend
|
|
||||||
.export _malloc
|
.export _malloc
|
||||||
|
|
||||||
|
.include "_heap.inc"
|
||||||
|
|
||||||
.macpack generic
|
.macpack generic
|
||||||
|
|
||||||
; Offsets into struct freeblock and other constant stuff
|
;-----------------------------------------------------------------------------
|
||||||
|
|
||||||
size = 0
|
|
||||||
next = 2
|
|
||||||
prev = 4
|
|
||||||
admin_space = 2
|
|
||||||
min_size = 6
|
|
||||||
|
|
||||||
|
|
||||||
; Code
|
; Code
|
||||||
|
|
||||||
_malloc:
|
_malloc:
|
||||||
sta ptr1 ; Store size in ptr1
|
sta ptr1 ; Store size in ptr1
|
||||||
stx ptr1+1
|
stx ptr1+1
|
||||||
|
|
||||||
; Check for a size of zero, if so, return NULL
|
; Check for a size of zero, if so, return NULL
|
||||||
|
|
||||||
ora ptr1+1
|
ora ptr1+1
|
||||||
beq Done ; a/x already contains zero
|
beq Done ; a/x already contains zero
|
||||||
|
|
||||||
; Add the administration space and round up the size if needed
|
; Add the administration space and round up the size if needed
|
||||||
|
|
||||||
lda ptr1
|
lda ptr1
|
||||||
add #admin_space
|
add #HEAP_ADMIN_SPACE
|
||||||
sta ptr1
|
sta ptr1
|
||||||
bcc @L1
|
bcc @L1
|
||||||
inc ptr1+1
|
inc ptr1+1
|
||||||
@L1: ldx ptr1+1
|
@L1: ldx ptr1+1
|
||||||
bne @L2
|
bne @L2
|
||||||
cmp #min_size+1
|
cmp #HEAP_MIN_BLOCKSIZE+1
|
||||||
bcs @L2
|
bcs @L2
|
||||||
lda #min_size
|
lda #HEAP_MIN_BLOCKSIZE
|
||||||
sta ptr1 ; High byte is already zero
|
sta ptr1 ; High byte is already zero
|
||||||
|
|
||||||
; Load a pointer to the freelist into ptr2
|
; Load a pointer to the freelist into ptr2
|
||||||
|
|
||||||
@L2: lda __hfirst
|
@L2: lda __heapfirst
|
||||||
sta ptr2
|
sta ptr2
|
||||||
lda __hfirst+1
|
lda __heapfirst+1
|
||||||
sta ptr2+1
|
sta ptr2+1
|
||||||
|
|
||||||
; Search the freelist for a block that is big enough. We will calculate
|
; Search the freelist for a block that is big enough. We will calculate
|
||||||
@ -157,21 +150,21 @@ _malloc:
|
|||||||
|
|
||||||
jmp @L4
|
jmp @L4
|
||||||
|
|
||||||
@L3: ldy #size
|
@L3: ldy #freeblock_size
|
||||||
lda (ptr2),y
|
lda (ptr2),y
|
||||||
sub ptr1
|
sub ptr1
|
||||||
tax ; Remember low byte for later
|
tax ; Remember low byte for later
|
||||||
iny ; Y points to size+1
|
iny ; Y points to freeblock_size+1
|
||||||
lda (ptr2),y
|
lda (ptr2),y
|
||||||
sbc ptr1+1
|
sbc ptr1+1
|
||||||
bcs BlockFound ; Beware: Contents of a/x/y are known!
|
bcs BlockFound ; Beware: Contents of a/x/y are known!
|
||||||
|
|
||||||
; Next block in list
|
; Next block in list
|
||||||
|
|
||||||
iny ; Points to next
|
iny ; Points to freeblock_next
|
||||||
lda (ptr2),y
|
lda (ptr2),y
|
||||||
tax
|
tax
|
||||||
iny ; Points to next+1
|
iny ; Points to freeblock_next+1
|
||||||
lda (ptr2),y
|
lda (ptr2),y
|
||||||
stx ptr2
|
stx ptr2
|
||||||
sta ptr2+1
|
sta ptr2+1
|
||||||
@ -180,16 +173,16 @@ _malloc:
|
|||||||
|
|
||||||
; We did not find a block big enough. Try to use new space from the heap top.
|
; We did not find a block big enough. Try to use new space from the heap top.
|
||||||
|
|
||||||
lda __hptr
|
lda __heapptr
|
||||||
add ptr1 ; _hptr + size
|
add ptr1 ; _heapptr + size
|
||||||
tay
|
tay
|
||||||
lda __hptr+1
|
lda __heapptr+1
|
||||||
adc ptr1+1
|
adc ptr1+1
|
||||||
bcs OutOfHeapSpace ; On overflow, we're surely out of space
|
bcs OutOfHeapSpace ; On overflow, we're surely out of space
|
||||||
|
|
||||||
cmp __hend+1
|
cmp __heapend+1
|
||||||
bne @L5
|
bne @L5
|
||||||
cpy __hend
|
cpy __heapend
|
||||||
@L5: bcc TakeFromTop
|
@L5: bcc TakeFromTop
|
||||||
beq TakeFromTop
|
beq TakeFromTop
|
||||||
|
|
||||||
@ -199,18 +192,18 @@ OutOfHeapSpace:
|
|||||||
lda #0
|
lda #0
|
||||||
tax
|
tax
|
||||||
Done: rts
|
Done: rts
|
||||||
|
|
||||||
; There is enough space left, take it from the heap top
|
; There is enough space left, take it from the heap top
|
||||||
|
|
||||||
TakeFromTop:
|
TakeFromTop:
|
||||||
ldx __hptr ; p = hptr;
|
ldx __heapptr ; p = _heapptr;
|
||||||
stx ptr2
|
stx ptr2
|
||||||
ldx __hptr+1
|
ldx __heapptr+1
|
||||||
stx ptr2+1
|
stx ptr2+1
|
||||||
|
|
||||||
sty __hptr ; hptr += size;
|
sty __heapptr ; _heapptr += size;
|
||||||
sta __hptr+1
|
sta __heapptr+1
|
||||||
jmp FillSizeAndRet ; Done
|
jmp FillSizeAndRet ; Done
|
||||||
|
|
||||||
; We found a block big enough. If the block can hold just the
|
; We found a block big enough. If the block can hold just the
|
||||||
; requested size, use the block in full. Beware: When slicing blocks,
|
; requested size, use the block in full. Beware: When slicing blocks,
|
||||||
@ -220,73 +213,73 @@ TakeFromTop:
|
|||||||
; flag is set if the high byte of this remaining size is zero.
|
; flag is set if the high byte of this remaining size is zero.
|
||||||
|
|
||||||
BlockFound:
|
BlockFound:
|
||||||
bne SliceBlock ; Block is large enough to slice
|
bne SliceBlock ; Block is large enough to slice
|
||||||
cpx #min_size+1 ; Check low byte
|
cpx #HEAP_MIN_BLOCKSIZE+1 ; Check low byte
|
||||||
bcs SliceBlock ; Jump if block is large enough to slice
|
bcs SliceBlock ; Jump if block is large enough to slice
|
||||||
|
|
||||||
; The block is too small to slice it. Use the block in full. The block
|
; The block is too small to slice it. Use the block in full. The block
|
||||||
; does already contain the correct size word, all we have to do is to
|
; does already contain the correct size word, all we have to do is to
|
||||||
; remove it from the free list.
|
; remove it from the free list.
|
||||||
|
|
||||||
ldy #prev+1 ; Load f->prev
|
ldy #freeblock_prev+1 ; Load f->prev
|
||||||
lda (ptr2),y
|
lda (ptr2),y
|
||||||
sta ptr3+1
|
sta ptr3+1
|
||||||
dey
|
dey
|
||||||
lda (ptr2),y
|
lda (ptr2),y
|
||||||
sta ptr3
|
sta ptr3
|
||||||
dey ; Points to next+1
|
dey ; Points to freeblock_next+1
|
||||||
ora ptr3+1
|
ora ptr3+1
|
||||||
beq @L1 ; Jump if f->prev zero
|
beq @L1 ; Jump if f->prev zero
|
||||||
|
|
||||||
; We have a previous block, ptr3 contains its address.
|
; We have a previous block, ptr3 contains its address.
|
||||||
; Do f->prev->next = f->next
|
; Do f->prev->next = f->next
|
||||||
|
|
||||||
lda (ptr2),y ; Load high byte of f->next
|
lda (ptr2),y ; Load high byte of f->next
|
||||||
sta (ptr3),y ; Store high byte of f->prev->next
|
sta (ptr3),y ; Store high byte of f->prev->next
|
||||||
dey ; Points to next
|
dey ; Points to next
|
||||||
lda (ptr2),y ; Load low byte of f->next
|
lda (ptr2),y ; Load low byte of f->next
|
||||||
sta (ptr3),y ; Store low byte of f->prev->next
|
sta (ptr3),y ; Store low byte of f->prev->next
|
||||||
jmp @L2
|
jmp @L2
|
||||||
|
|
||||||
; This is the first block, correct the freelist pointer
|
; This is the first block, correct the freelist pointer
|
||||||
; Do _hfirst = f->next
|
; Do _hfirst = f->next
|
||||||
|
|
||||||
@L1: lda (ptr2),y ; Load high byte of f->next
|
@L1: lda (ptr2),y ; Load high byte of f->next
|
||||||
sta __hfirst+1
|
sta __heapfirst+1
|
||||||
dey ; Points to next
|
dey ; Points to next
|
||||||
lda (ptr2),y ; Load low byte of f->next
|
lda (ptr2),y ; Load low byte of f->next
|
||||||
sta __hfirst
|
sta __heapfirst
|
||||||
|
|
||||||
; Check f->next. Y points always to next if we come here
|
; Check f->next. Y points always to next if we come here
|
||||||
|
|
||||||
@L2: lda (ptr2),y ; Load low byte of f->next
|
@L2: lda (ptr2),y ; Load low byte of f->next
|
||||||
sta ptr3
|
sta ptr3
|
||||||
iny ; Points to next+1
|
iny ; Points to next+1
|
||||||
lda (ptr2),y ; Load high byte of f->next
|
lda (ptr2),y ; Load high byte of f->next
|
||||||
sta ptr3+1
|
sta ptr3+1
|
||||||
iny ; Points to prev
|
iny ; Points to prev
|
||||||
ora ptr3
|
ora ptr3
|
||||||
beq @L3 ; Jump if f->next zero
|
beq @L3 ; Jump if f->next zero
|
||||||
|
|
||||||
; We have a next block, ptr3 contains its address.
|
; We have a next block, ptr3 contains its address.
|
||||||
; Do f->next->prev = f->prev
|
; Do f->next->prev = f->prev
|
||||||
|
|
||||||
lda (ptr2),y ; Load low byte of f->prev
|
lda (ptr2),y ; Load low byte of f->prev
|
||||||
sta (ptr3),y ; Store low byte of f->next->prev
|
sta (ptr3),y ; Store low byte of f->next->prev
|
||||||
iny ; Points to prev+1
|
iny ; Points to prev+1
|
||||||
lda (ptr2),y ; Load high byte of f->prev
|
lda (ptr2),y ; Load high byte of f->prev
|
||||||
sta (ptr3),y ; Store high byte of f->prev->next
|
sta (ptr3),y ; Store high byte of f->prev->next
|
||||||
jmp RetUserPtr ; Done
|
jmp RetUserPtr ; Done
|
||||||
|
|
||||||
; This is the last block, correct the freelist pointer.
|
; This is the last block, correct the freelist pointer.
|
||||||
; Do _hlast = f->prev
|
; Do _hlast = f->prev
|
||||||
|
|
||||||
@L3: lda (ptr2),y ; Load low byte of f->prev
|
@L3: lda (ptr2),y ; Load low byte of f->prev
|
||||||
sta __hlast
|
sta __heaplast
|
||||||
iny ; Points to prev+1
|
iny ; Points to prev+1
|
||||||
lda (ptr2),y ; Load high byte of f->prev
|
lda (ptr2),y ; Load high byte of f->prev
|
||||||
sta __hlast+1
|
sta __heaplast+1
|
||||||
jmp RetUserPtr ; Done
|
jmp RetUserPtr ; Done
|
||||||
|
|
||||||
; We must slice the block found. Cut off space from the upper end, so we
|
; We must slice the block found. Cut off space from the upper end, so we
|
||||||
; can leave the actual free block chain intact.
|
; can leave the actual free block chain intact.
|
||||||
@ -295,23 +288,23 @@ SliceBlock:
|
|||||||
|
|
||||||
; Decrement the size of the block. Y points to size+1.
|
; Decrement the size of the block. Y points to size+1.
|
||||||
|
|
||||||
dey ; Points to size
|
dey ; Points to size
|
||||||
lda (ptr2),y ; Low byte of f->size
|
lda (ptr2),y ; Low byte of f->size
|
||||||
sub ptr1
|
sub ptr1
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
tax ; Save low byte of f->size in X
|
tax ; Save low byte of f->size in X
|
||||||
iny ; Points to size+1
|
iny ; Points to size+1
|
||||||
lda (ptr2),y ; High byte of f->size
|
lda (ptr2),y ; High byte of f->size
|
||||||
sbc ptr1+1
|
sbc ptr1+1
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
|
|
||||||
; Set f to the space above the current block, which is the new block returned
|
; Set f to the space above the current block, which is the new block returned
|
||||||
; to the caller.
|
; to the caller.
|
||||||
|
|
||||||
txa ; Get low byte of f->size
|
txa ; Get low byte of f->size
|
||||||
add ptr2
|
add ptr2
|
||||||
tax
|
tax
|
||||||
lda (ptr2),y ; Get high byte of f->size
|
lda (ptr2),y ; Get high byte of f->size
|
||||||
adc ptr2+1
|
adc ptr2+1
|
||||||
stx ptr2
|
stx ptr2
|
||||||
sta ptr2+1
|
sta ptr2+1
|
||||||
@ -319,17 +312,17 @@ SliceBlock:
|
|||||||
; Fill the size into the admin space of the block and return the user pointer
|
; Fill the size into the admin space of the block and return the user pointer
|
||||||
|
|
||||||
FillSizeAndRet:
|
FillSizeAndRet:
|
||||||
ldy #size ; *p = size;
|
ldy #freeblock_size ; *p = size;
|
||||||
lda ptr1 ; Low byte of block size
|
lda ptr1 ; Low byte of block size
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
iny ; Points to size+1
|
iny ; Points to freeblock_size+1
|
||||||
lda ptr1+1
|
lda ptr1+1
|
||||||
sta (ptr2),y
|
sta (ptr2),y
|
||||||
|
|
||||||
RetUserPtr:
|
RetUserPtr:
|
||||||
lda ptr2 ; return ++p;
|
lda ptr2 ; return ++p;
|
||||||
ldx ptr2+1
|
ldx ptr2+1
|
||||||
add #admin_space
|
add #HEAP_ADMIN_SPACE
|
||||||
bcc @L9
|
bcc @L9
|
||||||
inx
|
inx
|
||||||
@L9: rts
|
@L9: rts
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "_heap.h"
|
#include <_heap.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -73,13 +73,13 @@ void* __fastcall__ realloc (void* block, size_t size)
|
|||||||
diff = size - oldsize;
|
diff = size - oldsize;
|
||||||
|
|
||||||
/* Is the block at the current heap top? */
|
/* Is the block at the current heap top? */
|
||||||
if (((int) b) + oldsize == ((int) _hptr)) {
|
if (((int) b) + oldsize == ((int) _heapptr)) {
|
||||||
/* Check if we've enough memory at the heap top */
|
/* Check if we've enough memory at the heap top */
|
||||||
int newhptr;
|
int newhptr;
|
||||||
newhptr = ((int) _hptr) + diff;
|
newhptr = ((int) _heapptr) + diff;
|
||||||
if (newhptr <= ((int) _hend)) {
|
if (newhptr <= ((int) _heapend)) {
|
||||||
/* Ok, there's space enough */
|
/* Ok, there's space enough */
|
||||||
_hptr = (unsigned*) newhptr;
|
_heapptr = (unsigned*) newhptr;
|
||||||
*b = size;
|
*b = size;
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
@ -109,3 +109,4 @@ void* __fastcall__ realloc (void* block, size_t size)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user