]> git.sur5r.net Git - cc65/blob - libsrc/common/atexit.s
ea266acf8f9be470b594264e63b64580cfe9059a
[cc65] / libsrc / common / atexit.s
1 ;
2 ; Ullrich von Bassewitz, 06.06.1998
3 ;
4 ; int atexit (void (*f) (void));
5 ;
6
7         .export         _atexit
8         .destructor     doatexit, 28
9         .import         __errno
10         .import         jmpvec
11
12         .include        "errno.inc"
13
14 ; ---------------------------------------------------------------------------
15
16 .proc   _atexit
17
18         ldy     exitfunc_index
19         cpy     #exitfunc_max           ; Slot available?
20         beq     @Error                  ; Jump if no
21
22 ; Enter the function into the table
23
24         sta     exitfunc_table,y
25         iny
26         txa
27         sta     exitfunc_table,y
28         iny
29         sty     exitfunc_index
30
31 ; Done, return zero
32
33         lda     #0
34         tax
35         rts
36
37 ; Error, no space left
38
39 @Error: lda     #ENOSPC         ; No space left
40         sta     __errno
41         ldx     #$00
42         stx     __errno+1
43         dex                     ; Make return value -1
44         txa
45         rts
46
47 .endproc
48
49
50
51 ; ---------------------------------------------------------------------------
52
53 .code
54
55 .proc   doatexit
56
57         ldy     exitfunc_index          ; Get index
58         beq     @L9                     ; Jump if done
59         dey
60         lda     exitfunc_table,y
61         sta     jmpvec+2
62         dey
63         lda     exitfunc_table,y
64         sta     jmpvec+1
65         sty     exitfunc_index
66         jsr     jmpvec                  ; Call the function
67         jmp     doatexit                ; Next one
68
69 @L9:    rts
70
71 .endproc
72
73
74
75 ; ---------------------------------------------------------------------------
76
77 .bss
78 exitfunc_index: .res    1       ; Index into table, inc'ed by 2
79 exitfunc_table: .res    10      ; 5 exit functions
80 exitfunc_max    = <(* - exitfunc_table)
81
82