File:  [gforth] / gforth / blocks.fs
Revision 1.6: download - view: text, annotated - select for diffs
Wed Sep 6 21:00:10 1995 UTC (23 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Removed BUGS, tried to clean up.

    1: \ A less simple implementation of the blocks wordset. 
    2: 
    3: \ An more efficient implementation would use mmap on OSs that
    4: \ provide it and many buffers on OSs that do not provide mmap.
    5: 
    6: \ Now, the replacement algorithm is "direct mapped"; change to LRU
    7: \ if too slow. Using more buffers helps, too.
    8: 
    9: \ I think I avoid the assumption 1 char = 1 here, but I have not tested this
   10: 
   11: \ 1024 constant chars/block \ mandated by the standard
   12: 
   13: require struct.fs
   14: 
   15: struct
   16:     1           cells: field buffer-block   \ the block number
   17:     1           cells: field buffer-fid     \ the block's fid
   18:     1           cells: field buffer-dirty   \ the block dirty flag
   19:     chars/block chars: field block-buffer   \ the data
   20:     0           cells: field next-buffer
   21: end-struct buffer-struct
   22: 
   23: Variable block-buffers
   24: Variable last-block
   25: 
   26: $20 Value buffers
   27: 
   28: User block-fid
   29: 
   30: : block-cold
   31:     defers 'cold  block-fid off  last-block off
   32:     buffers buffer-struct drop * allocate throw dup block-buffers !
   33:     buffers buffer-struct drop * erase ;
   34: 
   35: ' block-cold IS 'cold
   36: 
   37: block-cold
   38: 
   39: Defer flush-file
   40: 
   41: : use-file ( addr u -- )
   42:     2dup r/w bin open-file 0<>
   43:     if
   44: 	drop r/w bin create-file throw
   45:     else
   46: 	nip nip
   47:     then
   48:     block-fid @ IF  flush-file block-fid @ close-file throw  THEN
   49:     block-fid ! ;
   50: 
   51: \ the file is opened as binary file, since it either will contain text
   52: \ without newlines or binary data
   53: : get-block-fid ( -- fid )
   54:     block-fid @ 0=
   55:     if
   56: 	s" blocks.fb" use-file
   57:     then
   58:     block-fid @ ;
   59: 
   60: : block-position ( u -- )
   61:     \ positions the block file to the start of block u
   62:     1- chars/block chars um* get-block-fid reposition-file throw ;
   63: 
   64: : update ( -- )
   65:     last-block @ ?dup IF  buffer-dirty on  THEN ;
   66: 
   67: : save-buffer ( buffer -- ) >r
   68:     r@ buffer-dirty @ r@ buffer-block @ 0<> and
   69:     if
   70: 	r@ buffer-block @ block-position
   71: 	r@ block-buffer chars/block  r@ buffer-fid @  write-file throw
   72: 	r@ buffer-dirty off
   73:     endif
   74:     rdrop ;
   75: 
   76: : empty-buffer ( buffer -- )
   77:     buffer-block off ;
   78: 
   79: : save-buffers  ( -- )    block-buffers @
   80:     buffers 0 ?DO  dup save-buffer  next-buffer  LOOP  drop ;
   81: 
   82: : empty-buffers ( -- )    block-buffers @
   83:     buffers 0 ?DO  dup empty-buffer  next-buffer  LOOP  drop ;
   84: 
   85: : flush ( -- )
   86:     save-buffers
   87:     empty-buffers ;
   88: 
   89: ' flush IS flush-file
   90: 
   91: : get-buffer ( n -- a-addr )
   92:     buffers mod buffer-struct drop * block-buffers @ + ;
   93: 
   94: : block ( u -- a-addr )
   95:     dup 0= -35 and throw
   96:     dup get-buffer >r
   97:     dup r@ buffer-block @ <>
   98:     r@ buffer-fid @ block-fid @ <> and
   99:     if
  100: 	r@ save-buffer
  101: 	dup block-position
  102: 	r@ block-buffer chars/block get-block-fid read-file throw
  103: 	\ clear the rest of the buffer if the file is too short
  104: 	r@ block-buffer over chars + chars/block rot chars - blank
  105: 	r@ buffer-block !
  106: 	get-block-fid r@ buffer-fid !
  107:     else
  108: 	drop
  109:     then
  110:     r> dup last-block ! block-buffer ;
  111: 
  112: : buffer ( u -- a-addr )
  113:     \ reading in the block is unnecessary, but simpler
  114:     block ;
  115: 
  116: User scr 0 scr !
  117: 
  118: : updated?  ( n -- f )   scr @ buffer
  119:     [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
  120: 
  121: : list ( u -- )
  122:     \ calling block again and again looks inefficient but is necessary
  123:     \ in a multitasking environment
  124:     dup scr !
  125:     ." Screen " u.
  126:     updated?  0= IF ." not "  THEN  ." modified     " cr
  127:     16 0
  128:     ?do
  129: 	i 2 .r space scr @ block i 64 * chars + 64 type cr
  130:     loop ;
  131: 
  132: : (source)  ( -- addr len )
  133:   blk @ ?dup
  134:   IF    block chars/block
  135:   ELSE  tib #tib @
  136:   THEN ;
  137: 
  138: ' (source) IS source
  139: 
  140: : load ( i*x n -- j*x )
  141:   push-file
  142:   dup loadline ! blk ! >in off ( ['] ) interpret ( catch )
  143:   pop-file ( throw ) ;
  144: 
  145: : thru ( i*x n1 n2 -- j*x )
  146:   1+ swap 0 ?DO  I load  LOOP ;
  147: 
  148: : +load ( i*x n -- j*x )  blk @ + load ;
  149: 
  150: : +thru ( i*x n1 n2 -- j*x )
  151:   1+ swap 0 ?DO  I +load  LOOP ;
  152: 
  153: : --> ( -- )  refill drop ; immediate
  154: 
  155: : block-included ( addr u -- )
  156:     block-fid @ >r block-fid off use-file
  157:     1 load block-fid @ close-file throw flush
  158:     r> block-fid ! ;
  159: 
  160: : include ( "name" -- )
  161:     name 2dup dup 3 - /string s" .fb" compare
  162:     0= IF  block-included  ELSE  included  THEN ;
  163: 
  164: get-current environment-wordlist set-current
  165: true constant block
  166: true constant block-ext
  167: set-current
  168: 
  169: : bye  ['] flush catch drop bye ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>