File:  [gforth] / gforth / blocks.fs
Revision 1.41: download - view: text, annotated - select for diffs
Sun Mar 9 15:16:46 2003 UTC (21 years ago) by anton
Branches: MAIN
CVS tags: v0-6-2, v0-6-1, v0-6-0, HEAD
updated copyright years

    1: \ A less simple implementation of the blocks wordset. 
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: 
   22: \ A more efficient implementation would use mmap on OSs that
   23: \ provide it and many buffers on OSs that do not provide mmap.
   24: 
   25: \ Now, the replacement algorithm is "direct mapped"; change to LRU
   26: \ if too slow. Using more buffers helps, too.
   27: 
   28: \ I think I avoid the assumption 1 char = 1 here, but I have not tested this
   29: 
   30: \ 1024 constant chars/block \ mandated by the standard
   31: 
   32: require struct.fs
   33: 
   34: struct
   35:     cell%		field buffer-block   \ the block number
   36:     cell%		field buffer-fid     \ the block's fid
   37:     cell%		field buffer-dirty   \ the block dirty flag
   38:     char% chars/block * field block-buffer   \ the data
   39:     cell% 0 *		field next-buffer
   40: end-struct buffer-struct
   41: 
   42: Variable block-buffers
   43: Variable last-block
   44: 
   45: $20 Value buffers
   46: 
   47: \ limit block files to 2GB; gforth <0.6.0 erases larger block files on
   48: \ 32-bit systems
   49: $200000 Value block-limit
   50: 
   51: User block-fid
   52: User block-offset ( -- addr ) \ gforth
   53: \G User variable containing the number of the first block (default
   54: \G since 0.5.0: 0).  Block files created with Gforth versions before
   55: \G 0.5.0 have the offset 1.  If you use these files you can: @code{1
   56: \G offset !}; or add 1 to every block number used; or prepend 1024
   57: \G characters to the file.
   58: 0 block-offset !  \ store 1 here fore 0.4.0 compatibility
   59: 
   60: ' block-offset alias offset \ !! eliminate this?
   61: 
   62: : block-cold ( -- )
   63:     block-fid off  last-block off
   64:     buffer-struct buffers * %alloc dup block-buffers ! ( addr )
   65:     buffer-struct %size buffers * erase ;
   66: 
   67: ' block-cold INIT8 chained
   68: 
   69: block-cold
   70: 
   71: Defer flush-blocks ( -- ) \ gforth
   72: 
   73: : open-blocks ( c-addr u -- ) \ gforth
   74: \g Use the file, whose name is given by @i{c-addr u}, as the blocks file.
   75:     try ( c-addr u )
   76: 	2dup open-fpath-file throw
   77: 	rot close-file throw  2dup file-status throw bin open-file throw
   78: 	>r 2drop r>
   79:     recover ( c-addr u ior )
   80: 	>r 2dup file-status nip 0= r> and throw \ does it really not exist?
   81: 	r/w bin create-file throw
   82:     endtry
   83:     block-fid @ IF
   84: 	flush-blocks block-fid @ close-file throw
   85:     THEN
   86:     block-fid ! ;
   87: 
   88: : use ( "file" -- ) \ gforth
   89:     \g Use @i{file} as the blocks file.
   90:     name open-blocks ;
   91: 
   92: \ the file is opened as binary file, since it either will contain text
   93: \ without newlines or binary data
   94: : get-block-fid ( -- wfileid ) \ gforth
   95:     \G Return the file-id of the current blocks file. If no blocks
   96:     \G file has been opened, use @file{blocks.fb} as the default
   97:     \G blocks file.
   98:     block-fid @ 0=
   99:     if
  100: 	s" blocks.fb" open-blocks
  101:     then
  102:     block-fid @ ;
  103: 
  104: : block-position ( u -- ) \ block
  105: \G Position the block file to the start of block @i{u}.
  106:     dup block-limit u>= -35 and throw
  107:     offset @ - chars/block chars um* get-block-fid reposition-file throw ;
  108: 
  109: : update ( -- ) \ block
  110:     \G Mark the state of the current block buffer as assigned-dirty.
  111:     last-block @ ?dup IF  buffer-dirty on  THEN ;
  112: 
  113: : save-buffer ( buffer -- ) \ gforth
  114:     >r
  115:     r@ buffer-dirty @ r@ buffer-block @ 0<> and
  116:     if
  117: 	r@ buffer-block @ block-position
  118: 	r@ block-buffer chars/block  r@ buffer-fid @  write-file throw
  119: 	r@ buffer-fid @ flush-file throw
  120: 	r@ buffer-dirty off 
  121:     endif
  122:     rdrop ;
  123: 
  124: : empty-buffer ( buffer -- ) \ gforth
  125:     buffer-block off ;
  126: 
  127: : save-buffers  ( -- ) \ block
  128:     \G Transfer the contents of each @code{update}d block buffer to
  129:     \G mass storage, then mark all block buffers as assigned-clean.
  130:     block-buffers @
  131:     buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
  132: 
  133: : empty-buffers ( -- ) \ block-ext
  134:     \G Mark all block buffers as unassigned; if any had been marked as
  135:     \G assigned-dirty (by @code{update}), the changes to those blocks
  136:     \G will be lost.
  137:     block-buffers @
  138:     buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
  139: 
  140: : flush ( -- ) \ block
  141:     \G Perform the functions of @code{save-buffers} then
  142:     \G @code{empty-buffers}.
  143:     save-buffers
  144:     empty-buffers ;
  145: 
  146: ' flush IS flush-blocks
  147: 
  148: : get-buffer ( u -- a-addr ) \ gforth
  149:     0 buffers um/mod drop buffer-struct %size * block-buffers @ + ;
  150: 
  151: : block ( u -- a-addr ) \ gforthman- block
  152:     \G If a block buffer is assigned for block @i{u}, return its
  153:     \G start address, @i{a-addr}. Otherwise, assign a block buffer
  154:     \G for block @i{u} (if the assigned block buffer has been
  155:     \G @code{update}d, transfer the contents to mass storage), read
  156:     \G the block into the block buffer and return its start address,
  157:     \G @i{a-addr}.
  158:     dup offset @ u< -35 and throw
  159:     dup get-buffer >r
  160:     dup r@ buffer-block @ <>
  161:     r@ buffer-fid @ block-fid @ <> or
  162:     if
  163: 	r@ save-buffer
  164: 	dup block-position
  165: 	r@ block-buffer chars/block get-block-fid read-file throw
  166: 	\ clear the rest of the buffer if the file is too short
  167: 	r@ block-buffer over chars + chars/block rot chars - blank
  168: 	r@ buffer-block !
  169: 	get-block-fid r@ buffer-fid !
  170:     else
  171: 	drop
  172:     then
  173:     r> dup last-block ! block-buffer ;
  174: 
  175: : buffer ( u -- a-addr ) \ block
  176:     \G If a block buffer is assigned for block @i{u}, return its
  177:     \G start address, @i{a-addr}. Otherwise, assign a block buffer
  178:     \G for block @i{u} (if the assigned block buffer has been
  179:     \G @code{update}d, transfer the contents to mass storage) and
  180:     \G return its start address, @i{a-addr}.  The subtle difference
  181:     \G between @code{buffer} and @code{block} mean that you should
  182:     \G only use @code{buffer} if you don't care about the previous
  183:     \G contents of block @i{u}. In Gforth, this simply calls
  184:     \G @code{block}.
  185:     \ reading in the block is unnecessary, but simpler
  186:     block ;
  187: 
  188: User scr ( -- a-addr ) \ block-ext s-c-r
  189:     \G @code{User} variable -- @i{a-addr} is the address of a cell containing
  190:     \G the block number of the block most recently processed by
  191:     \G @code{list}.
  192: 0 scr !
  193: 
  194: \ nac31Mar1999 moved "scr @" to list to make the stack comment correct
  195: : updated?  ( n -- f ) \ gforth
  196:     \G Return true if @code{updated} has been used to mark block @i{n}
  197:     \G as assigned-dirty.
  198:     buffer
  199:     [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
  200: 
  201: : list ( u -- ) \ block-ext
  202:     \G Display block @i{u}. In Gforth, the block is displayed as 16
  203:     \G numbered lines, each of 64 characters.
  204:     \ calling block again and again looks inefficient but is necessary
  205:     \ in a multitasking environment
  206:     dup scr !
  207:     ." Screen " u.
  208:     scr @ updated?  0= IF ." not "  THEN  ." modified     " cr
  209:     16 0
  210:     ?do
  211: 	i 2 .r space scr @ block i 64 * chars + 64 type cr
  212:     loop ;
  213: 
  214: [IFDEF] current-input
  215: :noname  2 <> -12 and throw >in ! blk ! ;
  216:                               \ restore-input
  217: :noname  blk @ >in @ 2 ;      \ save-input
  218: :noname  2 ;                  \ source-id "*a block*"
  219: :noname  1 blk +! 1 loadline +! true ;      \ refill
  220: :noname  blk @ block chars/block ;  \ source
  221: 
  222: Create block-input   A, A, A, A, A,
  223: 
  224: : load  ( i*x n -- j*x ) \ block
  225:     \G Save the current input source specification. Store @i{n} in
  226:     \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
  227:     \G area is exhausted, restore the input source specification.
  228:     block-input 0 new-tib dup loadline ! blk !  s" * a block*" loadfilename 2!
  229:     ['] interpret catch pop-file throw ;
  230: [ELSE]
  231: : (source)  ( -- c-addr u )
  232:   blk @ ?dup
  233:   IF    block chars/block
  234:   ELSE  tib #tib @
  235:   THEN ;
  236: 
  237: ' (source) IS source ( -- c-addr u ) \ core
  238: \G @i{c-addr} is the address of the input buffer and @i{u} is the
  239: \G number of characters in it.
  240: 
  241: : load ( i*x n -- j*x ) \ block
  242:     \G Save the current input source specification. Store @i{n} in
  243:     \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
  244:     \G area is exhausted, restore the input source specification.
  245:     s" * a block*" loadfilename>r
  246:     push-file
  247:     dup loadline ! blk ! >in off ['] interpret catch
  248:     pop-file
  249:     r>loadfilename
  250:     throw ;
  251: [THEN]
  252: 
  253: : thru ( i*x n1 n2 -- j*x ) \ block-ext
  254:     \G @code{load} the blocks @i{n1} through @i{n2} in sequence.
  255:     1+ swap ?DO  I load  LOOP ;
  256: 
  257: : +load ( i*x n -- j*x ) \ gforth
  258:     \G Used within a block to load the block specified as the
  259:     \G current block + @i{n}.
  260:     blk @ + load ;
  261: 
  262: : +thru ( i*x n1 n2 -- j*x ) \ gforth
  263:     \G Used within a block to load the range of blocks specified as the
  264:     \G current block + @i{n1} thru the current block + @i{n2}.
  265:     1+ swap ?DO  I +load  LOOP ;
  266: 
  267: : --> ( -- ) \ gforthman- gforth chain
  268:     \G If this symbol is encountered whilst loading block @i{n},
  269:     \G discard the remainder of the block and load block @i{n+1}. Used
  270:     \G for chaining multiple blocks together as a single loadable
  271:     \G unit.  Not recommended, because it destroys the independence of
  272:     \G loading.  Use @code{thru} (which is standard) or @code{+thru}
  273:     \G instead.
  274:     refill drop ; immediate
  275: 
  276: : block-included ( a-addr u -- ) \ gforth
  277:     \G Use within a block that is to be processed by @code{load}. Save
  278:     \G the current blocks file specification, open the blocks file
  279:     \G specified by @i{a-addr u} and @code{load} block 1 from that
  280:     \G file (which may in turn chain or load other blocks). Finally,
  281:     \G close the blocks file and restore the original blocks file.
  282:     block-fid @ >r block-fid off open-blocks
  283:     1 load block-fid @ close-file throw flush
  284:     r> block-fid ! ;
  285: 
  286: \ thrown out because it may provide unpleasant surprises - anton
  287: \ : include ( "name" -- )
  288: \     name 2dup dup 3 - /string s" .fb" compare
  289: \     0= IF  block-included  ELSE  included  THEN ;
  290: 
  291: get-current environment-wordlist set-current
  292: true constant block
  293: true constant block-ext
  294: set-current
  295: 
  296: : bye ( -- ) \ tools-ext
  297:   \G Return control to the host operating system (if any).
  298:   ['] flush catch drop bye ;

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