fake_heap

Priskribo de la ĉi-paĝa enhavo

Forth-programo kreanta surogatojn de la ANS -forthaj vortoj por memorumado.

Etikedoj:

Mi verkis ĉi programeton por rapide solvi problemon: La Fortho-sistemo lina malhavas la ANS -forthajn vortojn ALLOCATE, RESIZE kaj FREE. Mi bezonis ilin por verkata projekto (teksta aventurludo en la hispana) kiun mi volis kongruigi anke kun lina.

Ĉi ilo estis tuj forlasita, ĉar tuj posto ol verki ĝin mi trovis kompletan solvon al la sama problemo: ANS-fortha difino de la mankantaj vortoj, verkita de Gordon Charlton en 1994; anke ĝin oni povas deŝuti sube.

Fontkodo

\ 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]

Deŝutoj

Kompleta ANS -fortha solvo verkita de Gordon Charlton en 1994: