$>z" & Global Memory pool

From: Mike Ghan <mikeg_at_logix-controls.com>
Date: Fri, 20 Oct 2000 12:02:40 -0700

> This is seemingly impossible unless you are willing to allocate
> from the global memory pool another buffer and guarantee that the
> program will eventually release it. Reason: you are asking for a
> string of CNT+1 characters to fit into a buffer at ADDR which
> is only guaranteed to be CNT characters long. If the input is the
> address of a counted string in memory, you can use:
>
> : $>Z ( c-addr -- z-addr )
> DUP COUNT 2DUP + 1- >R OVER 1- SWAP CMOVE 0 R> C! ;
>
>
> Rick
>

Here is my string buffer mechanism (it uses allocated memory and could be set to
a fairly large setting):

{ ============================================================================
   String Buffers - Adapted from QPAD in Strings2.F (C) Forth Inc

   Changes:
   Created 05/26/2000 by Mike Ghan
   Made Buffer Allocation Global - not by User Var 8/28/2000 MAG
   256 bytes per buffer 10/6/2000 MAG

=========================================================================== }

{ ---------------------------------------------------------------------------
String buffer

We use a buffer in Allocated Memory for strings. A constant size is used to
simplify the calculation of the correct offset, which should properly be a
"mod" operation. Note that a mask of $7F00 corresponds to a buffer size of
$8000 (32k) and a max string length of $100 (256).

SBUF is the address of the string buffer.
SHEAD is the most recent string.
---------------------------------------------------------------------------- }

0 VALUE (SBUF) \ Global Pointer to Buffer Area

VARIABLE SHEAD \ Global Buffer Offset

\ User Vars
 #USER
   CELL +USER (SPAD) \ String Buffer Pointer
 TO #USER

: SPAD ( -- addr ) (SPAD) @ ;
: !SPAD ( addr -- ) (SPAD) ! ; \ Helper for Save/Restore

: /SPAD ( -- ) \ Grab next buffer
   SHEAD @ $100 + $7F00 AND DUP SHEAD ! (SBUF) + (SPAD) ! ;

: /STR-BUF ( -- ) (SBUF) ?EXIT $8000 ALLOCATE THROW TO (SBUF) ;
: STR-BUF/ ( -- ) (SBUF) ?DUP -EXIT FREE THROW 0 TO (SBUF) ;

/STR-BUF

:ONSYSLOAD ( -- ) 0 TO (SBUF) /STR-BUF ;
:ONSYSEXIT ( -- ) STR-BUF/ ;
Received on Fri Oct 20 2000 - 12:13:53 PDT


Subscribe to our e-mail list service. It's free for all SwiftForth and SwiftX users!

This archive was generated 03-Feb-2012. Archive updated nightly.