File:  [gforth] / gforth / blocks.fs
Revision 1.49: download - view: text, annotated - select for diffs
Sun Feb 18 18:59:40 2007 UTC (12 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
deal properly with the absence of RECOVER, next try

    1: \ A less simple implementation of the blocks wordset. 
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2006 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: :noname ( -- )
   68:     defers 'cold
   69:     block-cold
   70: ; is 'cold
   71: 
   72: block-cold
   73: 
   74: Defer flush-blocks ( -- ) \ gforth
   75: 
   76: : open-blocks ( c-addr u -- ) \ gforth
   77: \g Use the file, whose name is given by @i{c-addr u}, as the blocks file.
   78:     try ( c-addr u )
   79: 	2dup open-fpath-file throw
   80: 	rot close-file throw  2dup file-status throw bin open-file throw
   81: 	>r 2drop r> 0
   82:     restore endtry
   83:     ?dup-if ( c-addr u ior )
   84: 	>r 2dup file-status nip 0= r> and throw \ does it really not exist?
   85: 	r/w bin create-file throw
   86:     then
   87:     block-fid @ IF
   88: 	flush-blocks block-fid @ close-file throw
   89:     THEN
   90:     block-fid ! ;
   91: 
   92: : use ( "file" -- ) \ gforth
   93:     \g Use @i{file} as the blocks file.
   94:     name open-blocks ;
   95: 
   96: \ the file is opened as binary file, since it either will contain text
   97: \ without newlines or binary data
   98: : get-block-fid ( -- wfileid ) \ gforth
   99:     \G Return the file-id of the current blocks file. If no blocks
  100:     \G file has been opened, use @file{blocks.fb} as the default
  101:     \G blocks file.
  102:     block-fid @ 0=
  103:     if
  104: 	s" blocks.fb" open-blocks
  105:     then
  106:     block-fid @ ;
  107: 
  108: : block-position ( u -- ) \ block
  109: \G Position the block file to the start of block @i{u}.
  110:     dup block-limit u>= -35 and throw
  111:     offset @ - chars/block chars um* get-block-fid reposition-file throw ;
  112: 
  113: : update ( -- ) \ block
  114:     \G Mark the state of the current block buffer as assigned-dirty.
  115:     last-block @ ?dup IF  buffer-dirty on  THEN ;
  116: 
  117: : save-buffer ( buffer -- ) \ gforth
  118:     >r
  119:     r@ buffer-dirty @
  120:     if
  121: 	r@ buffer-block @ block-position
  122: 	r@ block-buffer chars/block  r@ buffer-fid @  write-file throw
  123: 	r@ buffer-fid @ flush-file throw
  124: 	r@ buffer-dirty off 
  125:     endif
  126:     rdrop ;
  127: 
  128: : empty-buffer ( buffer -- ) \ gforth
  129:     buffer-block off ;
  130: 
  131: : save-buffers  ( -- ) \ block
  132:     \G Transfer the contents of each @code{update}d block buffer to
  133:     \G mass storage, then mark all block buffers as assigned-clean.
  134:     block-buffers @
  135:     buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
  136: 
  137: : empty-buffers ( -- ) \ block-ext
  138:     \G Mark all block buffers as unassigned; if any had been marked as
  139:     \G assigned-dirty (by @code{update}), the changes to those blocks
  140:     \G will be lost.
  141:     block-buffers @
  142:     buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
  143: 
  144: : flush ( -- ) \ block
  145:     \G Perform the functions of @code{save-buffers} then
  146:     \G @code{empty-buffers}.
  147:     save-buffers
  148:     empty-buffers ;
  149: 
  150: ' flush IS flush-blocks
  151: 
  152: : get-buffer ( u -- a-addr ) \ gforth
  153:     0 buffers um/mod drop buffer-struct %size * block-buffers @ + ;
  154: 
  155: : block ( u -- a-addr ) \ gforthman- block
  156:     \G If a block buffer is assigned for block @i{u}, return its
  157:     \G start address, @i{a-addr}. Otherwise, assign a block buffer
  158:     \G for block @i{u} (if the assigned block buffer has been
  159:     \G @code{update}d, transfer the contents to mass storage), read
  160:     \G the block into the block buffer and return its start address,
  161:     \G @i{a-addr}.
  162:     dup offset @ u< -35 and throw
  163:     dup get-buffer >r
  164:     dup r@ buffer-block @ <>
  165:     r@ buffer-fid @ block-fid @ <> or
  166:     if
  167: 	r@ save-buffer
  168: 	dup block-position
  169: 	r@ block-buffer chars/block get-block-fid read-file throw
  170: 	\ clear the rest of the buffer if the file is too short
  171: 	r@ block-buffer over chars + chars/block rot chars - blank
  172: 	r@ buffer-block !
  173: 	get-block-fid r@ buffer-fid !
  174:     else
  175: 	drop
  176:     then
  177:     r> dup last-block ! block-buffer ;
  178: 
  179: : buffer ( u -- a-addr ) \ block
  180:     \G If a block buffer is assigned for block @i{u}, return its
  181:     \G start address, @i{a-addr}. Otherwise, assign a block buffer
  182:     \G for block @i{u} (if the assigned block buffer has been
  183:     \G @code{update}d, transfer the contents to mass storage) and
  184:     \G return its start address, @i{a-addr}.  The subtle difference
  185:     \G between @code{buffer} and @code{block} mean that you should
  186:     \G only use @code{buffer} if you don't care about the previous
  187:     \G contents of block @i{u}. In Gforth, this simply calls
  188:     \G @code{block}.
  189:     \ reading in the block is unnecessary, but simpler
  190:     block ;
  191: 
  192: User scr ( -- a-addr ) \ block-ext s-c-r
  193:     \G @code{User} variable -- @i{a-addr} is the address of a cell containing
  194:     \G the block number of the block most recently processed by
  195:     \G @code{list}.
  196: 0 scr !
  197: 
  198: \ nac31Mar1999 moved "scr @" to list to make the stack comment correct
  199: : updated?  ( n -- f ) \ gforth
  200:     \G Return true if @code{updated} has been used to mark block @i{n}
  201:     \G as assigned-dirty.
  202:     buffer
  203:     [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
  204: 
  205: : list ( u -- ) \ block-ext
  206:     \G Display block @i{u}. In Gforth, the block is displayed as 16
  207:     \G numbered lines, each of 64 characters.
  208:     \ calling block again and again looks inefficient but is necessary
  209:     \ in a multitasking environment
  210:     dup scr !
  211:     ." Screen " u.
  212:     scr @ updated?  0= IF ." not "  THEN  ." modified     " cr
  213:     16 0
  214:     ?do
  215: 	i 2 .r space scr @ block i 64 * chars + 64 type cr
  216:     loop ;
  217: 
  218: [IFDEF] current-input
  219: :noname  2 <> -12 and throw >in ! blk ! ;
  220:                               \ restore-input
  221: :noname  blk @ >in @ 2 ;      \ save-input
  222: :noname  2 ;                  \ source-id "*a block*"
  223: :noname  1 blk +! 1 loadline +! >in off true ;      \ refill
  224: :noname  blk @ block chars/block ;  \ source
  225: 
  226: Create block-input   A, A, A, A, A,
  227: 
  228: : load  ( i*x n -- j*x ) \ block
  229:     \G Save the current input source specification. Store @i{n} in
  230:     \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
  231:     \G area is exhausted, restore the input source specification.
  232:     block-input 0 new-tib dup loadline ! blk !  s" * a block*" loadfilename 2!
  233:     ['] interpret catch pop-file throw ;
  234: [ELSE]
  235: : (source)  ( -- c-addr u )
  236:   blk @ ?dup
  237:   IF    block chars/block
  238:   ELSE  tib #tib @
  239:   THEN ;
  240: 
  241: ' (source) IS source ( -- c-addr u ) \ core
  242: \G @i{c-addr} is the address of the input buffer and @i{u} is the
  243: \G number of characters in it.
  244: 
  245: : load ( i*x n -- j*x ) \ block
  246:     \G Save the current input source specification. Store @i{n} in
  247:     \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
  248:     \G area is exhausted, restore the input source specification.
  249:     s" * a block*" loadfilename>r
  250:     push-file
  251:     dup loadline ! blk ! >in off ['] interpret catch
  252:     pop-file
  253:     r>loadfilename
  254:     throw ;
  255: [THEN]
  256: 
  257: : thru ( i*x n1 n2 -- j*x ) \ block-ext
  258:     \G @code{load} the blocks @i{n1} through @i{n2} in sequence.
  259:     1+ swap ?DO  I load  LOOP ;
  260: 
  261: : +load ( i*x n -- j*x ) \ gforth
  262:     \G Used within a block to load the block specified as the
  263:     \G current block + @i{n}.
  264:     blk @ + load ;
  265: 
  266: : +thru ( i*x n1 n2 -- j*x ) \ gforth
  267:     \G Used within a block to load the range of blocks specified as the
  268:     \G current block + @i{n1} thru the current block + @i{n2}.
  269:     1+ swap ?DO  I +load  LOOP ;
  270: 
  271: : --> ( -- ) \ gforthman- gforth chain
  272:     \G If this symbol is encountered whilst loading block @i{n},
  273:     \G discard the remainder of the block and load block @i{n+1}. Used
  274:     \G for chaining multiple blocks together as a single loadable
  275:     \G unit.  Not recommended, because it destroys the independence of
  276:     \G loading.  Use @code{thru} (which is standard) or @code{+thru}
  277:     \G instead.
  278:     refill drop ; immediate
  279: 
  280: : block-included ( a-addr u -- ) \ gforth
  281:     \G Use within a block that is to be processed by @code{load}. Save
  282:     \G the current blocks file specification, open the blocks file
  283:     \G specified by @i{a-addr u} and @code{load} block 1 from that
  284:     \G file (which may in turn chain or load other blocks). Finally,
  285:     \G close the blocks file and restore the original blocks file.
  286:     block-fid @ >r block-fid off open-blocks
  287:     1 load block-fid @ close-file throw flush
  288:     r> block-fid ! ;
  289: 
  290: \ thrown out because it may provide unpleasant surprises - anton
  291: \ : include ( "name" -- )
  292: \     name 2dup dup 3 - /string s" .fb" compare
  293: \     0= IF  block-included  ELSE  included  THEN ;
  294: 
  295: get-current environment-wordlist set-current
  296: true constant block
  297: true constant block-ext
  298: set-current
  299: 
  300: : bye ( -- ) \ tools-ext
  301:   \G Return control to the host operating system (if any).
  302:   ['] flush catch drop bye ;

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