Skip to content

Commit 17ea665

Browse files
authored
Merge pull request #215 from boriel/feature/mem_libs
Feature/mem libs
2 parents 2c12a46 + 1c58301 commit 17ea665

5 files changed

Lines changed: 124 additions & 19 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

tests/functional/prepro71.out

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ end function
4949

5050

5151

52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
function FASTCALL callocate(byval n as uinteger) as uinteger
62+
63+
64+
65+
66+
asm
67+
ld b, h
68+
ld c, l
69+
jp __MEM_CALLOC
70+
end asm
71+
end function
72+
73+
74+
75+
76+
77+
78+
79+
5280
sub FASTCALL deallocate(byval addr as integer)
5381

5482

@@ -195,8 +223,9 @@ end function
195223
#require "alloc.asm"
196224
#require "free.asm"
197225
#require "realloc.asm"
226+
#require "calloc.asm"
198227

199-
#line 198 "/zxbasic/library/alloc.bas"
228+
#line 227 "/zxbasic/library/alloc.bas"
200229

201230
#line 2 "prepro71.bi"
202231

tests/functional/test.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,30 @@ def _msg(msg, force=False):
102102

103103

104104
def get_file_lines(filename, ignore_regexp=None, replace_regexp=None,
105-
replace_what='.', replace_with='.'):
105+
replace_what='.', replace_with='.', strip_blanks=True):
106106
""" Opens source file <filename> and load its lines,
107107
discarding those not important for comparison.
108108
"""
109109
from api.utils import open_file
110110
with open_file(filename, 'rt', 'utf-8') as f:
111111
lines = [x for x in f]
112112

113-
if ignore_regexp is not None:
114-
r = re.compile(ignore_regexp)
115-
lines = [x for x in lines if not r.search(x)]
113+
if ignore_regexp is not None:
114+
r = re.compile(ignore_regexp)
115+
lines = [x for x in lines if not r.search(x)]
116116

117-
if replace_regexp is not None and replace_what and replace_with is not None:
118-
r = re.compile(replace_regexp)
119-
lines = [x.replace(replace_what, replace_with, 1) if r.search(x) else x for x in lines]
117+
if replace_regexp is not None and replace_what and replace_with is not None:
118+
r = re.compile(replace_regexp)
119+
lines = [x.replace(replace_what, replace_with, 1) if r.search(x) else x for x in lines]
120+
121+
if strip_blanks:
122+
lines = [x.rstrip() for x in lines if x.rstrip()]
120123

121124
return lines
122125

123126

124-
def is_same_file(fname1, fname2, ignore_regexp=None,
125-
replace_regexp=None, replace_what='.', replace_with='.', diff=None, is_binary=False):
127+
def is_same_file(fname1, fname2, ignore_regexp=None, replace_regexp=None, replace_what='.', replace_with='.',
128+
diff=None, is_binary=False, strip_blanks=True):
126129
""" Test if two files are the same.
127130
128131
If ignore_regexp is passed, it must be a Regular Expression
@@ -143,8 +146,8 @@ def is_same_file(fname1, fname2, ignore_regexp=None,
143146
if is_binary:
144147
return open(fname1, 'rb').read() == open(fname2, 'rb').read()
145148

146-
r1 = get_file_lines(fname1, ignore_regexp, replace_regexp, replace_what, replace_with)
147-
r2 = get_file_lines(fname2, ignore_regexp, replace_regexp, replace_what, replace_with)
149+
r1 = get_file_lines(fname1, ignore_regexp, replace_regexp, replace_what, replace_with, strip_blanks)
150+
r2 = get_file_lines(fname2, ignore_regexp, replace_regexp, replace_what, replace_with, strip_blanks)
148151
result = (r1 == r2)
149152

150153
if not result:
@@ -278,8 +281,8 @@ def testPREPRO(fname, pattern_=None, inline=None):
278281

279282
with TempTestFile(func, tfname, UPDATE):
280283
if not UPDATE:
281-
result = is_same_file(okfile, tfname, replace_regexp=pattern_,
282-
replace_what=ZXBASIC_ROOT, replace_with=_original_root)
284+
result = is_same_file(okfile, tfname, replace_regexp=pattern_, replace_what=ZXBASIC_ROOT,
285+
replace_with=_original_root, strip_blanks=True)
283286
else:
284287
updateTest(tfname, pattern_)
285288
finally:

0 commit comments

Comments
 (0)