]> git.sur5r.net Git - cc65/blob - libsrc/common/calloc.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / calloc.s
1 ;
2 ; Ullrich von Bassewitz, 15.11.2001
3 ;
4 ; Allocate a block and zero it.
5 ;
6 ; void* __fastcall__ calloc (size_t count, size_t size);
7 ;
8
9         .export _calloc
10         .import _malloc, __bzero
11         .import tosumulax, pushax
12
13
14 ; -------------------------------------------------------------------------
15
16 .proc   _calloc
17
18 ; We have the first argument in a/x and the second on the stack. Calling
19 ; tosumulax will give the product of both in a/x.
20
21         jsr     tosumulax
22
23 ; Save size for later
24
25         sta     Size
26         stx     Size+1
27
28 ; malloc() is a fastcall function, so we do already have the argument in
29 ; the right place
30
31         jsr     _malloc
32
33 ; Check for a NULL pointer
34
35         cpx     #0
36         bne     ClearBlock
37         cmp     #0
38         bne     ClearBlock
39
40 ; We have a NULL pointer, bail out
41
42         rts
43
44 ; No NULL pointer, clear the block. _bzero will return a pointer to the
45 ; block which is exactly what we want.
46
47 ClearBlock:
48         jsr     pushax                  ; ptr
49         lda     Size
50         ldx     Size+1                  ; Size
51         jmp     __bzero
52
53 .endproc
54
55 ; -------------------------------------------------------------------------
56 ; Data
57
58 .bss
59
60 Size:   .res    2
61
62
63