From c15fd58d3bbbbfede3614ecbac5f9cfb6444fa75 Mon Sep 17 00:00:00 2001 From: cuz Date: Wed, 3 Dec 2003 10:15:33 +0000 Subject: [PATCH] Use structs git-svn-id: svn://svn.cc65.org/cc65/trunk@2707 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- asminc/_file.inc | 9 +++++---- asminc/_heap.inc | 19 +++++++++++-------- libsrc/atari/crt0.s | 6 +++--- libsrc/common/_fdesc.s | 6 +++--- libsrc/common/_file.s | 8 ++++---- libsrc/common/_fopen.s | 6 +++--- libsrc/common/_heapadd.s | 2 +- libsrc/common/_heapmaxavail.s | 6 +++--- libsrc/common/_heapmemavail.s | 4 ++-- libsrc/common/fclose.s | 6 +++--- libsrc/common/fmisc.s | 2 +- libsrc/common/fread.s | 6 +++--- libsrc/common/free.s | 22 +++++++++++----------- libsrc/common/fwrite.s | 6 +++--- libsrc/common/malloc.s | 16 ++++++++-------- 15 files changed, 64 insertions(+), 60 deletions(-) diff --git a/asminc/_file.inc b/asminc/_file.inc index daf33a987..916215bce 100644 --- a/asminc/_file.inc +++ b/asminc/_file.inc @@ -7,10 +7,11 @@ ; Assembler include file that makes the constants and structures in _file.h ; available for asm code. -; Struct _FILE offsets and size -_FILE_f_fd = $00 -_FILE_f_flags = $01 -_FILE_size = $02 +; Struct _FILE +.struct _FILE + f_fd .byte + f_flags .byte +.endstruct ; Flags field _FCLOSED = $00 diff --git a/asminc/_heap.inc b/asminc/_heap.inc index aef77cbf0..d3b274f9a 100644 --- a/asminc/_heap.inc +++ b/asminc/_heap.inc @@ -7,15 +7,18 @@ ; 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 +; 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. +.struct freeblock + size .word + next .word + prev .word +.endstruct -; 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 +HEAP_ADMIN_SPACE = 2 +HEAP_MIN_BLOCKSIZE = .sizeof (freeblock) ; Minimum size of an allocated block ; Variables .global __heaporg diff --git a/libsrc/atari/crt0.s b/libsrc/atari/crt0.s index dae781f02..5c5607e0c 100644 --- a/libsrc/atari/crt0.s +++ b/libsrc/atari/crt0.s @@ -96,13 +96,13 @@ L1: lda sp,x lda #0 jsr getfd - sta __filetab + (0 * _FILE_size) ; setup stdin + sta __filetab + (0 * .sizeof(_FILE)); setup stdin lda #0 jsr getfd - sta __filetab + (1 * _FILE_size) ; setup stdout + sta __filetab + (1 * .sizeof(_FILE)); setup stdout lda #0 jsr getfd - sta __filetab + (2 * _FILE_size) ; setup stderr + sta __filetab + (2 * .sizeof(_FILE)); setup stderr ; Push arguments and call main diff --git a/libsrc/common/_fdesc.s b/libsrc/common/_fdesc.s index 403af4b42..682bcb78e 100644 --- a/libsrc/common/_fdesc.s +++ b/libsrc/common/_fdesc.s @@ -15,12 +15,12 @@ ldy #0 lda #_FOPEN -Loop: and __filetab + _FILE_f_flags,y ; load flags +Loop: and __filetab + _FILE::f_flags,y ; load flags beq Found ; jump if closed -.repeat ::_FILE_size +.repeat .sizeof(_FILE) iny .endrepeat - cpy #(FOPEN_MAX * _FILE_size) ; Done? + cpy #(FOPEN_MAX * .sizeof(_FILE)) ; Done? bne Loop ; File table is full diff --git a/libsrc/common/_file.s b/libsrc/common/_file.s index fc3abf0c9..b34a135aa 100644 --- a/libsrc/common/_file.s +++ b/libsrc/common/_file.s @@ -5,7 +5,7 @@ ; .export __filetab - + .include "stdio.inc" .include "fcntl.inc" .include "_file.inc" @@ -27,12 +27,12 @@ __filetab: ; Standard file descriptors _stdin: - .word __filetab + (STDIN_FILENO * _FILE_size) + .word __filetab + (STDIN_FILENO * .sizeof(_FILE)) _stdout: - .word __filetab + (STDOUT_FILENO * _FILE_size) + .word __filetab + (STDOUT_FILENO * .sizeof(_FILE)) _stderr: - .word __filetab + (STDERR_FILENO * _FILE_size) + .word __filetab + (STDERR_FILENO * .sizeof(_FILE)) diff --git a/libsrc/common/_fopen.s b/libsrc/common/_fopen.s index da0e8d8d8..a7a9eb4b5 100644 --- a/libsrc/common/_fopen.s +++ b/libsrc/common/_fopen.s @@ -1,4 +1,4 @@ -; +; ; Ullrich von Bassewitz, 22.11.2002 ; ; FILE* __fastcall__ _fopen (const char* name, const char* mode, FILE* f); @@ -101,9 +101,9 @@ openok: ldy file sty ptr1 ldy file+1 sty ptr1+1 - ldy #_FILE_f_fd + ldy #_FILE::f_fd sta (ptr1),y ; file->f_fd = fd; - ldy #_FILE_f_flags + ldy #_FILE::f_flags lda #_FOPEN sta (ptr1),y ; file->f_flags = _FOPEN; diff --git a/libsrc/common/_heapadd.s b/libsrc/common/_heapadd.s index f2f9b6826..fbc210645 100644 --- a/libsrc/common/_heapadd.s +++ b/libsrc/common/_heapadd.s @@ -40,7 +40,7 @@ __heapadd: ; The block is large enough. Set the size field in the block. -@L1: ldy #freeblock_size +@L1: ldy #freeblock::size sta (ptr2),y iny txa diff --git a/libsrc/common/_heapmaxavail.s b/libsrc/common/_heapmaxavail.s index 5446c3f28..a606904ef 100644 --- a/libsrc/common/_heapmaxavail.s +++ b/libsrc/common/_heapmaxavail.s @@ -6,7 +6,7 @@ ; size_t __fastcall__ _heapmaxavail (void); ; ; - + .importzp ptr1, ptr2 .export __heapmaxavail @@ -42,7 +42,7 @@ __heapmaxavail: ; if (Size < F->size) { - ldy #freeblock_size + ldy #freeblock::size lda ptr2 sub (ptr1),y iny @@ -52,7 +52,7 @@ __heapmaxavail: ; Size = F->size; - ldy #freeblock_size + ldy #freeblock::size lda (ptr1),y sta ptr2 iny diff --git a/libsrc/common/_heapmemavail.s b/libsrc/common/_heapmemavail.s index b628b1b92..5c94305e4 100644 --- a/libsrc/common/_heapmemavail.s +++ b/libsrc/common/_heapmemavail.s @@ -19,7 +19,7 @@ __heapmemavail: -; size_t Size = 0; +; size_t Size = 0; lda #0 sta ptr2 @@ -39,7 +39,7 @@ __heapmemavail: ; Size += F->size; - ldy #freeblock_size + ldy #freeblock::size lda (ptr1),y add ptr2 sta ptr2 diff --git a/libsrc/common/fclose.s b/libsrc/common/fclose.s index 8acf49166..0d43aa47e 100644 --- a/libsrc/common/fclose.s +++ b/libsrc/common/fclose.s @@ -8,7 +8,7 @@ .export _fclose .import _close - .importzp ptr1 + .importzp ptr1 .include "errno.inc" .include "_file.inc" @@ -23,7 +23,7 @@ ; Check if the file is really open - ldy #_FILE_f_flags + ldy #_FILE::f_flags lda (ptr1),y and #_FOPEN bne @L1 @@ -43,7 +43,7 @@ @L1: lda #_FCLOSED sta (ptr1),y - ldy #_FILE_f_fd + ldy #_FILE::f_fd lda (ptr1),y ldx #0 jmp _close ; Will set errno and return an error flag diff --git a/libsrc/common/fmisc.s b/libsrc/common/fmisc.s index fc17a9a0a..c9125939e 100644 --- a/libsrc/common/fmisc.s +++ b/libsrc/common/fmisc.s @@ -16,7 +16,7 @@ getf: sta ptr1 stx ptr1+1 - ldy #_FILE_f_flags + ldy #_FILE::f_flags lda (ptr1),y ; get f->f_flags and #_FOPEN ; file open? beq @L1 ; jump if no diff --git a/libsrc/common/fread.s b/libsrc/common/fread.s index 781bc1a02..c4941513d 100644 --- a/libsrc/common/fread.s +++ b/libsrc/common/fread.s @@ -31,7 +31,7 @@ ; Check if the file is open - ldy #_FILE_f_flags + ldy #_FILE::f_flags lda (ptr1),y and #_FOPEN ; Is the file open? bne @L2 ; Branch if yes @@ -53,7 +53,7 @@ ; Build the stackframe for read() - ldy #_FILE_f_fd + ldy #_FILE::f_fd lda (ptr1),y ldx #$00 jsr pushax ; file->f_fd @@ -104,7 +104,7 @@ sta ptr1 lda file+1 sta ptr1+1 - ldy #_FILE_f_flags + ldy #_FILE::f_flags lda (ptr1),y ora tmp1 sta (ptr1),y diff --git a/libsrc/common/free.s b/libsrc/common/free.s index 0ba91f133..8c39bef78 100644 --- a/libsrc/common/free.s +++ b/libsrc/common/free.s @@ -86,7 +86,7 @@ _free: sta ptr2 sta ptr2 bcs @L1 dec ptr2+1 -@L1: ldy #freeblock_size+1 +@L1: ldy #freeblock::size+1 lda (ptr2),y ; High byte of size sta ptr1+1 ; Save it dey @@ -122,7 +122,7 @@ _free: sta ptr2 lda __heaplast+1 sta ptr1+1 ; Pointer to last block now in ptr1 - ldy #freeblock_size + ldy #freeblock::size lda (ptr1),y ; Low byte of block size add ptr1 tax @@ -144,7 +144,7 @@ _free: sta ptr2 ; Correct the next pointer of the now last block - ldy #freeblock_prev+1 ; Offset of ->prev field + ldy #freeblock::prev+1 ; Offset of ->prev field lda (ptr1),y sta ptr2+1 ; Remember f->prev in ptr2 sta __heaplast+1 @@ -282,10 +282,10 @@ heapadd: ; The free list is empty, so this is the first and only block. A contains ; zero if we come here. - ldy #freeblock_next-1 + ldy #freeblock::next-1 @L2: iny ; f->next = f->prev = 0; sta (ptr2),y - cpy #freeblock_prev+1 ; Done? + cpy #freeblock::prev+1 ; Done? bne @L2 lda ptr2 @@ -308,7 +308,7 @@ SearchFreeList: lda #0 sta ptr4 sta ptr4+1 ; left = 0; - ldy #freeblock_next+1 + ldy #freeblock::next+1 ldx ptr3 @Loop: lda ptr3+1 ; High byte of right @@ -367,7 +367,7 @@ CheckRightMerge: ; Merge with the right block. Do f->size += right->size; - ldy #freeblock_size + ldy #freeblock::size lda ptr1 add (ptr3),y sta (ptr2),y @@ -411,7 +411,7 @@ CheckRightMerge: ; No right merge, just set the link. NoRightMerge: - ldy #freeblock_next ; f->next = right; + ldy #freeblock::next ; f->next = right; lda ptr3 sta (ptr2),y iny ; Points to next+1 @@ -434,7 +434,7 @@ CheckLeftMerge: ; We don't have a left block, so f is actually the new freelist start - ldy #freeblock_prev + ldy #freeblock::prev sta (ptr2),y ; f->prev = 0; iny sta (ptr2),y @@ -449,7 +449,7 @@ CheckLeftMerge: ; Check if the left block is adjacent to the following one CheckLeftMerge2: - ldy #freeblock_size ; Calculate left + left->size + ldy #freeblock::size ; Calculate left + left->size lda (ptr4),y ; Low byte of left->size add ptr4 tax @@ -529,4 +529,4 @@ NoLeftMerge: - + diff --git a/libsrc/common/fwrite.s b/libsrc/common/fwrite.s index 0efc8fc69..ef9c08d9e 100644 --- a/libsrc/common/fwrite.s +++ b/libsrc/common/fwrite.s @@ -31,7 +31,7 @@ ; Check if the file is open - ldy #_FILE_f_flags + ldy #_FILE::f_flags lda (ptr1),y and #_FOPEN ; Is the file open? bne @L2 ; Branch if yes @@ -53,7 +53,7 @@ ; Build the stackframe for write() - ldy #_FILE_f_fd + ldy #_FILE::f_fd lda (ptr1),y ldx #$00 jsr pushax ; file->f_fd @@ -102,7 +102,7 @@ sta ptr1 lda file+1 sta ptr1+1 - ldy #_FILE_f_flags + ldy #_FILE::f_flags lda (ptr1),y ora #_FERROR sta (ptr1),y diff --git a/libsrc/common/malloc.s b/libsrc/common/malloc.s index 6b69b4199..fa1bd6e7c 100644 --- a/libsrc/common/malloc.s +++ b/libsrc/common/malloc.s @@ -150,21 +150,21 @@ _malloc: jmp @L4 -@L3: ldy #freeblock_size +@L3: ldy #freeblock::size lda (ptr2),y sub ptr1 tax ; Remember low byte for later - iny ; Y points to freeblock_size+1 + iny ; Y points to freeblock::size+1 lda (ptr2),y sbc ptr1+1 bcs BlockFound ; Beware: Contents of a/x/y are known! ; Next block in list - iny ; Points to freeblock_next + iny ; Points to freeblock::next lda (ptr2),y tax - iny ; Points to freeblock_next+1 + iny ; Points to freeblock::next+1 lda (ptr2),y stx ptr2 sta ptr2+1 @@ -221,13 +221,13 @@ BlockFound: ; does already contain the correct size word, all we have to do is to ; remove it from the free list. - ldy #freeblock_prev+1 ; Load f->prev + ldy #freeblock::prev+1 ; Load f->prev lda (ptr2),y sta ptr3+1 dey lda (ptr2),y sta ptr3 - dey ; Points to freeblock_next+1 + dey ; Points to freeblock::next+1 ora ptr3+1 beq @L1 ; Jump if f->prev zero @@ -312,10 +312,10 @@ SliceBlock: ; Fill the size into the admin space of the block and return the user pointer FillSizeAndRet: - ldy #freeblock_size ; *p = size; + ldy #freeblock::size ; *p = size; lda ptr1 ; Low byte of block size sta (ptr2),y - iny ; Points to freeblock_size+1 + iny ; Points to freeblock::size+1 lda ptr1+1 sta (ptr2),y -- 2.39.5