2001-11-15 21:53:00 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 15.11.2001
|
|
|
|
;
|
|
|
|
; Allocate a block and zero it.
|
|
|
|
;
|
|
|
|
; void* __fastcall__ calloc (size_t count, size_t size);
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _calloc
|
2022-08-29 21:20:48 +00:00
|
|
|
.import _malloc, ___bzero
|
2013-05-09 11:56:54 +00:00
|
|
|
.import tosumulax, pushax
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
; -------------------------------------------------------------------------
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.proc _calloc
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
; We have the first argument in a/x and the second on the stack. Calling
|
|
|
|
; tosumulax will give the product of both in a/x.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr tosumulax
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
; Save size for later
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
sta Size
|
|
|
|
stx Size+1
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
; malloc() is a fastcall function, so we do already have the argument in
|
|
|
|
; the right place
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr _malloc
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
; Check for a NULL pointer
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
cpx #0
|
|
|
|
bne ClearBlock
|
|
|
|
cmp #0
|
|
|
|
bne ClearBlock
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
; We have a NULL pointer, bail out
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2001-11-15 21:53:00 +00:00
|
|
|
|
2012-11-12 23:07:02 +00:00
|
|
|
; No NULL pointer, clear the block. _bzero will return a pointer to the
|
2001-11-15 21:53:00 +00:00
|
|
|
; block which is exactly what we want.
|
|
|
|
|
|
|
|
ClearBlock:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr pushax ; ptr
|
|
|
|
lda Size
|
|
|
|
ldx Size+1 ; Size
|
2022-08-29 21:20:48 +00:00
|
|
|
jmp ___bzero
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
; -------------------------------------------------------------------------
|
|
|
|
; Data
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Size: .res 2
|
2001-11-15 21:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|