X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libsrc%2Fcommon%2Fatexit.s;h=ea266acf8f9be470b594264e63b64580cfe9059a;hb=d036b8946f620c56ef3a551df6bf503138b8824c;hp=1751b80630f04677908c22696f0002cdc5360f62;hpb=48f1d0ad2894a3a93cd1e647455d4bc58cb26331;p=cc65 diff --git a/libsrc/common/atexit.s b/libsrc/common/atexit.s index 1751b8063..ea266acf8 100644 --- a/libsrc/common/atexit.s +++ b/libsrc/common/atexit.s @@ -4,34 +4,29 @@ ; int atexit (void (*f) (void)); ; -; The exit functions - - .export _atexit, doatexit - .import __errno, jmpvec + .export _atexit + .destructor doatexit, 28 + .import __errno + .import jmpvec .include "errno.inc" -.bss -ecount: .byte 0 ; Really an index, inc'ed by 2 -efunc: .word 0,0,0,0,0 ; 5 exit functions -maxcount = * - efunc - +; --------------------------------------------------------------------------- -.code +.proc _atexit -_atexit: - ldy ecount - cpy #maxcount ; slot available? - beq E0 ; jump if no + ldy exitfunc_index + cpy #exitfunc_max ; Slot available? + beq @Error ; Jump if no ; Enter the function into the table - sta efunc,y + sta exitfunc_table,y iny txa - sta efunc,y + sta exitfunc_table,y iny - sty ecount + sty exitfunc_index ; Done, return zero @@ -41,33 +36,47 @@ _atexit: ; Error, no space left -E0: lda #ENOSPC ; No space left - sta __errno - ldx #$00 - stx __errno+1 - dex - txa - rts +@Error: lda #ENOSPC ; No space left + sta __errno + ldx #$00 + stx __errno+1 + dex ; Make return value -1 + txa + rts + +.endproc + -; Function called from exit -doatexit: - ldy ecount ; get index - beq L9 ; jump if done - dey - lda efunc,y +; --------------------------------------------------------------------------- + +.code + +.proc doatexit + + ldy exitfunc_index ; Get index + beq @L9 ; Jump if done + dey + lda exitfunc_table,y sta jmpvec+2 dey - lda efunc,y + lda exitfunc_table,y sta jmpvec+1 - sty ecount - ldy #0 ; number of function parms - jsr jmpvec - jmp doatexit ; next one + sty exitfunc_index + jsr jmpvec ; Call the function + jmp doatexit ; Next one -L9: rts +@L9: rts +.endproc +; --------------------------------------------------------------------------- + +.bss +exitfunc_index: .res 1 ; Index into table, inc'ed by 2 +exitfunc_table: .res 10 ; 5 exit functions +exitfunc_max = <(* - exitfunc_table) +