diff --git a/library-asm/alloc.asm b/library-asm/alloc.asm index bd67bb050..de9b4840e 100644 --- a/library-asm/alloc.asm +++ b/library-asm/alloc.asm @@ -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. @@ -183,4 +183,3 @@ __MEM_SUBTRACT: ENDP - diff --git a/library-asm/calloc.asm b/library-asm/calloc.asm new file mode 100644 index 000000000..6dca645aa --- /dev/null +++ b/library-asm/calloc.asm @@ -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 + + +; --------------------------------------------------------------------- +; 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 diff --git a/library/alloc.bas b/library/alloc.bas index 87447db88..92ddf570a 100644 --- a/library/alloc.bas +++ b/library/alloc.bas @@ -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: @@ -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 ' @@ -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 @@ -193,6 +221,7 @@ end function #require "alloc.asm" #require "free.asm" #require "realloc.asm" +#require "calloc.asm" #endif diff --git a/tests/functional/prepro71.out b/tests/functional/prepro71.out index d47278e9c..6f6e1676f 100644 --- a/tests/functional/prepro71.out +++ b/tests/functional/prepro71.out @@ -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) @@ -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" diff --git a/tests/functional/test.py b/tests/functional/test.py index 7c767d5c3..a9f2650ef 100755 --- a/tests/functional/test.py +++ b/tests/functional/test.py @@ -102,7 +102,7 @@ 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 and load its lines, discarding those not important for comparison. """ @@ -110,19 +110,22 @@ def get_file_lines(filename, ignore_regexp=None, replace_regexp=None, 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 @@ -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: @@ -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: