Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions library-asm/alloc.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
; (a.k.a. Boriel)
; http://www.boriel.com
;
; This ASM library is licensed under the BSD license
; This ASM library is licensed under the MIT license
; you can use it for any purpose (even for commercial
; closed source programs).
;
; Please read the BSD license on the internet
; Please read the MIT license on the internet

; ----- IMPLEMENTATION NOTES ------
; The heap is implemented as a linked list of free blocks.
Expand Down Expand Up @@ -183,4 +183,3 @@ __MEM_SUBTRACT:

ENDP


45 changes: 45 additions & 0 deletions library-asm/calloc.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; vim: ts=4:et:sw=4:
; Copyleft (K) by Jose M. Rodriguez de la Rosa
; (a.k.a. Boriel)
; http://www.boriel.com
;
; This ASM library is licensed under the MIT license
; you can use it for any purpose (even for commercial
; closed source programs).
;
; Please read the MIT license on the internet

#include once <alloc.asm>


; ---------------------------------------------------------------------
; MEM_CALLOC
; Allocates a block of memory in the heap, and clears it filling it
; with 0 bytes
;
; Parameters
; BC = Length of requested memory block
;
; Returns:
; HL = Pointer to the allocated block in memory. Returns 0 (NULL)
; if the block could not be allocated (out of memory)
; ---------------------------------------------------------------------
__MEM_CALLOC:
push bc
call __MEM_ALLOC
pop bc
ld a, h
or l
ret z ; No memory
ld (hl), 0
dec bc
ld a, b
or c
ret z ; Already filled (1 byte-length block)
ld d, h
ld e, l
inc de
push hl
ldir
pop hl
ret
33 changes: 31 additions & 2 deletions library/alloc.bas
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ REM Avoid recursive / multiple inclusion
'
' Returns:
' 16 bits (pointer) unsigned integer. NULL is returned if not
' enough memory to alloc the block
' enough memory to allocate the block
' ----------------------------------------------------------------
function FASTCALL allocate(byval n as uinteger) as uinteger
' This is a FastCall function. This means:
Expand All @@ -41,6 +41,34 @@ function FASTCALL allocate(byval n as uinteger) as uinteger
end function


' ----------------------------------------------------------------
' function calloc
'
' Allocates the requested bytes in the heap (dynamic memory) and
' returns the address (16 bit, unsigned) of the new bloc. If
' no memory, NULL (0) is returned.
' The allocated block is cleared (filled with 0's) upon return.
'
' Parameters:
' n: number of bytes
'
' Returns:
' 16 bits (pointer) unsigned integer. NULL is returned if not
' enough memory to allocate the block
' ----------------------------------------------------------------
function FASTCALL callocate(byval n as uinteger) as uinteger
' This is a FastCall function. This means:
' 1.- The 16 bit 'n' parameter is received in hl
' 2.- Can return at any point with "ret"
' 3.- The result (16bit) must be returned in HL
asm
ld b, h
ld c, l
jp __MEM_CALLOC ; Since calloc is FASTCALL, we can return from there
end asm
end function


' ----------------------------------------------------------------
' sub free
'
Expand Down Expand Up @@ -146,7 +174,7 @@ function FASTCALL maxavail as uInteger
LOCAL LOOP, CONT

ld hl, ZXBASIC_MEM_HEAP
ld de, 0 ; Size acumulator
ld de, 0 ; Size accumulator

LOOP:
; BC = (HL) = Block size
Expand Down Expand Up @@ -193,6 +221,7 @@ end function
#require "alloc.asm"
#require "free.asm"
#require "realloc.asm"
#require "calloc.asm"

#endif

31 changes: 30 additions & 1 deletion tests/functional/prepro71.out
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ end function












function FASTCALL callocate(byval n as uinteger) as uinteger




asm
ld b, h
ld c, l
jp __MEM_CALLOC
end asm
end function








sub FASTCALL deallocate(byval addr as integer)


Expand Down Expand Up @@ -195,8 +223,9 @@ end function
#require "alloc.asm"
#require "free.asm"
#require "realloc.asm"
#require "calloc.asm"

#line 198 "/zxbasic/library/alloc.bas"
#line 227 "/zxbasic/library/alloc.bas"

#line 2 "prepro71.bi"

Expand Down
29 changes: 16 additions & 13 deletions tests/functional/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,30 @@ def _msg(msg, force=False):


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

if ignore_regexp is not None:
r = re.compile(ignore_regexp)
lines = [x for x in lines if not r.search(x)]
if ignore_regexp is not None:
r = re.compile(ignore_regexp)
lines = [x for x in lines if not r.search(x)]

if replace_regexp is not None and replace_what and replace_with is not None:
r = re.compile(replace_regexp)
lines = [x.replace(replace_what, replace_with, 1) if r.search(x) else x for x in lines]
if replace_regexp is not None and replace_what and replace_with is not None:
r = re.compile(replace_regexp)
lines = [x.replace(replace_what, replace_with, 1) if r.search(x) else x for x in lines]

if strip_blanks:
lines = [x.rstrip() for x in lines if x.rstrip()]

return lines


def is_same_file(fname1, fname2, ignore_regexp=None,
replace_regexp=None, replace_what='.', replace_with='.', diff=None, is_binary=False):
def is_same_file(fname1, fname2, ignore_regexp=None, replace_regexp=None, replace_what='.', replace_with='.',
diff=None, is_binary=False, strip_blanks=True):
""" Test if two files are the same.

If ignore_regexp is passed, it must be a Regular Expression
Expand All @@ -143,8 +146,8 @@ def is_same_file(fname1, fname2, ignore_regexp=None,
if is_binary:
return open(fname1, 'rb').read() == open(fname2, 'rb').read()

r1 = get_file_lines(fname1, ignore_regexp, replace_regexp, replace_what, replace_with)
r2 = get_file_lines(fname2, ignore_regexp, replace_regexp, replace_what, replace_with)
r1 = get_file_lines(fname1, ignore_regexp, replace_regexp, replace_what, replace_with, strip_blanks)
r2 = get_file_lines(fname2, ignore_regexp, replace_regexp, replace_what, replace_with, strip_blanks)
result = (r1 == r2)

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

with TempTestFile(func, tfname, UPDATE):
if not UPDATE:
result = is_same_file(okfile, tfname, replace_regexp=pattern_,
replace_what=ZXBASIC_ROOT, replace_with=_original_root)
result = is_same_file(okfile, tfname, replace_regexp=pattern_, replace_what=ZXBASIC_ROOT,
replace_with=_original_root, strip_blanks=True)
else:
updateTest(tfname, pattern_)
finally:
Expand Down