6.7.3.2 Growable memory buffers

The following words are useful for growable memory buffers. One can alternatively use $trings (see $tring words), and the differences are: When the used memory in the buffer shrinks, $trings may resize the buffer, while adjust-buffer does not, which may be preferable for a buffer that is reused all the time. However, $strings have one cell less memory overhead, and for longer-term storage the shrinking may be worthwhile.

buffer% ( u1 u2 –  ) gforth-experimental “buffer%”

u1 is the alignment and u2 is the size of a buffer descriptor.

init-buffer ( addr –  ) gforth-experimental “init-buffer”
adjust-buffer ( u addr –  ) gforth-experimental “adjust-buffer”

Adjust buffer% at addr to length u. This may grow the allocated area, but never shrinks it.

You can get the current address and length of such a buffer with 2@.

Typical usage:

create mybuf  buffer% %allot  mybuf init-buffer
s" frobnicate" mybuf adjust-buffer  mybuf 2@ move
mybuf 2@ type
s" foo"        mybuf adjust-buffer  mybuf 2@ move
mybuf 2@ type