fake_heap

Description of the page content

Forth utility to create fake definitions of the ANS Forth memory management words.

Tags:

This little tool was written as a quick solution to a problem: providing the lina Forth system with the ANS Forth words ALLOCATE, RESIZE and FREE. I needed that in order to compile a project (a text adventure in Spanish) with lina.

This tool was immediately deprecated... The details are in the code.

The complete solution written by Gordon Charlton in 1994 can be downloaded from this page.

Source code

\ fake_heap.fs

\ Copyright (C) 2011 Marcos Cruz (programandala.net)
\ License: http://programandala.net/license

\ History :
\ 2011-12-25 First version, a quick and provisional solution
\  to provide lina with ALLOCATE , RESIZE and FREE ,
\  needed to use many modules of the
\  Free Foundation Library.

\ To-do :
\ Create a header for every chunk ALLOTed and save there
\ the maximum space reserved,
\ the actual space used (it can be smaller after RESIZE),
\ a free flag and the address of the previous chunk.
\ That way, chunks can be reused.

\ Note :
\ After writing this little tool, I discovered in my
\ archive a complete implementation written in ANS Forth
\ by Gordon Charlton in 1994. It is used by hForth
\ and probably others.

0 value fake_allocate_error#

s" name" environment?  [IF]
  s" ciforth" compare 0=  [IF]
    \ lina error code
    \ -12 \ cannot allocate memory
    2 \ dictionary full
    to fake_allocate_error#
  [THEN]
[THEN]

fake_allocate_error# 0=  [IF]
  \ More standard error code
  -8  \ dictionary full
  to fake_allocate_error#
[THEN]

: fake_allocate  ( u -- a ior )
  dup unused <=
  if  here swap allot 0
  else fake_allocate_error#
  then
  ;
: fake_resize  ( a1 u -- a2 ior )
  \ Reserve new space and copy the previous content into it.
  dup fake_allocate ?dup
  if  ( a1 u x ior )  2swap 2drop
  else  ( a1 u a2 )  dup >r swap move r> 0
  then
  ;
: fake_free  ( a -- ior )
  drop 0
  ;

[undefined] allocate  [IF]
' fake_allocate alias allocate
[THEN]
[undefined] resize  [IF]
' fake_resize alias resize
[THEN]
[undefined] free  [IF]
' fake_free alias free
[THEN]

Downloads

Complete implementación in ANS Forth written by Gordon Charlton in 1994: