Skip to content

Commit 15e99c9

Browse files
committed
Add callocate routine
This implements the equivalent for C calloc() function. Changes alloc.asm and related licenses from BSD to MIT. Also fixes some typos in alloc.bas comments.
1 parent 2c12a46 commit 15e99c9

3 files changed

Lines changed: 78 additions & 5 deletions

File tree

library-asm/alloc.asm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
; (a.k.a. Boriel)
44
; http://www.boriel.com
55
;
6-
; This ASM library is licensed under the BSD license
6+
; This ASM library is licensed under the MIT license
77
; you can use it for any purpose (even for commercial
88
; closed source programs).
99
;
10-
; Please read the BSD license on the internet
10+
; Please read the MIT license on the internet
1111

1212
; ----- IMPLEMENTATION NOTES ------
1313
; The heap is implemented as a linked list of free blocks.
@@ -183,4 +183,3 @@ __MEM_SUBTRACT:
183183
184184
ENDP
185185

186-

library-asm/calloc.asm

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; vim: ts=4:et:sw=4:
2+
; Copyleft (K) by Jose M. Rodriguez de la Rosa
3+
; (a.k.a. Boriel)
4+
; http://www.boriel.com
5+
;
6+
; This ASM library is licensed under the MIT license
7+
; you can use it for any purpose (even for commercial
8+
; closed source programs).
9+
;
10+
; Please read the MIT license on the internet
11+
12+
#include once <alloc.asm>
13+
14+
15+
; ---------------------------------------------------------------------
16+
; MEM_CALLOC
17+
; Allocates a block of memory in the heap, and clears it filling it
18+
; with 0 bytes
19+
;
20+
; Parameters
21+
; BC = Length of requested memory block
22+
;
23+
; Returns:
24+
; HL = Pointer to the allocated block in memory. Returns 0 (NULL)
25+
; if the block could not be allocated (out of memory)
26+
; ---------------------------------------------------------------------
27+
__MEM_CALLOC:
28+
push bc
29+
call __MEM_ALLOC
30+
pop bc
31+
ld a, h
32+
or l
33+
ret z ; No memory
34+
ld (hl), 0
35+
dec bc
36+
ld a, b
37+
or c
38+
ret z ; Already filled (1 byte-length block)
39+
ld d, h
40+
ld e, l
41+
inc de
42+
push hl
43+
ldir
44+
pop hl
45+
ret

library/alloc.bas

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ REM Avoid recursive / multiple inclusion
2626
'
2727
' Returns:
2828
' 16 bits (pointer) unsigned integer. NULL is returned if not
29-
' enough memory to alloc the block
29+
' enough memory to allocate the block
3030
' ----------------------------------------------------------------
3131
function FASTCALL allocate(byval n as uinteger) as uinteger
3232
' This is a FastCall function. This means:
@@ -41,6 +41,34 @@ function FASTCALL allocate(byval n as uinteger) as uinteger
4141
end function
4242

4343

44+
' ----------------------------------------------------------------
45+
' function calloc
46+
'
47+
' Allocates the requested bytes in the heap (dynamic memory) and
48+
' returns the address (16 bit, unsigned) of the new bloc. If
49+
' no memory, NULL (0) is returned.
50+
' The allocated block is cleared (filled with 0's) upon return.
51+
'
52+
' Parameters:
53+
' n: number of bytes
54+
'
55+
' Returns:
56+
' 16 bits (pointer) unsigned integer. NULL is returned if not
57+
' enough memory to allocate the block
58+
' ----------------------------------------------------------------
59+
function FASTCALL callocate(byval n as uinteger) as uinteger
60+
' This is a FastCall function. This means:
61+
' 1.- The 16 bit 'n' parameter is received in hl
62+
' 2.- Can return at any point with "ret"
63+
' 3.- The result (16bit) must be returned in HL
64+
asm
65+
ld b, h
66+
ld c, l
67+
jp __MEM_CALLOC ; Since calloc is FASTCALL, we can return from there
68+
end asm
69+
end function
70+
71+
4472
' ----------------------------------------------------------------
4573
' sub free
4674
'
@@ -146,7 +174,7 @@ function FASTCALL maxavail as uInteger
146174
LOCAL LOOP, CONT
147175

148176
ld hl, ZXBASIC_MEM_HEAP
149-
ld de, 0 ; Size acumulator
177+
ld de, 0 ; Size accumulator
150178

151179
LOOP:
152180
; BC = (HL) = Block size
@@ -193,6 +221,7 @@ end function
193221
#require "alloc.asm"
194222
#require "free.asm"
195223
#require "realloc.asm"
224+
#require "calloc.asm"
196225

197226
#endif
198227

0 commit comments

Comments
 (0)