Merge pull request #5 from nineties/add_free

Add 'free'
This commit is contained in:
Koichi NAKAMURA 2021-01-17 20:23:18 +09:00 committed by GitHub
commit 6050e60671
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 1 deletions

View file

@ -2399,6 +2399,9 @@ BLOCK-SIZE remaining-size !
then then
; ;
\ Bootstrapping version of free do nothing.
: (free) ( addr -- ) ;
( === File I/O === ) ( === File I/O === )
3 constant SYS-READ 3 constant SYS-READ
@ -2440,11 +2443,16 @@ BLOCK-SIZE remaining-size !
( === Heap Memory === ) ( === Heap Memory === )
need-defined (allocate) need-defined (allocate)
need-defined (free)
: allocate ( size -- addr e ) : allocate ( size -- addr e )
(allocate) dup 0<> if success else ALLOCATE-ERROR then (allocate) dup 0<> if success else ALLOCATE-ERROR then
; ;
: free ( addr -- )
(free)
;
\ allocate heap memory \ allocate heap memory
: %allocate ( align size -- addr e ) : %allocate ( align size -- addr e )
over + allocate ?dup unless over + allocate ?dup unless
@ -2604,7 +2612,7 @@ need-defined (read)
2rot -2rot 2tuck 2over 2nip 2swap 2dup 2drop 3dup 3drop depth 2rot -2rot 2tuck 2over 2nip 2swap 2dup 2drop 3dup 3drop depth
rp0 rp@ rp! r> >r r@ rdrop rpick rdepth rp0 rp@ rp! r> >r r@ rdrop rpick rdepth
allocate allot memcpy strlen streq strneq strcpy strcpy, allocate free allot memcpy strlen streq strneq strcpy strcpy,
cell cell+ cell- cells char+ char- chars align aligned +! -! cell cell+ cell- cells char+ char- chars align aligned +! -!
if else then unless begin until again while repeat if else then unless begin until again while repeat

View file

@ -207,5 +207,6 @@ defcode("(allocate)", allocate) {
push((cell) p); push((cell) p);
next(); next();
} }
defcode("(free)", free_) { free((void*) pop()); }
#endif #endif

View file

@ -281,7 +281,10 @@ def allocate():
addr = len(memory) * CELL addr = len(memory) * CELL
memory.extend([0]*n) memory.extend([0]*n)
push(addr) push(addr)
def free():
pop() # Bootstrap version do nothing
add_simple_operator('(allocate)', allocate) add_simple_operator('(allocate)', allocate)
add_simple_operator('(free)', free)
start = read(HERE_CELL) start = read(HERE_CELL)
comma(find('k')) comma(find('k'))