PAD 从这里偏移

PAD offset from HERE

我使用的 Forth 版本 (Mecrisp) 缺少 PAD 字。但是,它有 HERE。现在,我想我可以使用类似的东西自己定义 PAD:

hex deadbeef constant offset
: pad here offset + ;

我的问题是如何找到安全的偏移值?事实上,偏移量为零字节是否安全——我猜这使得 PAD 等同于 HERE?

一个安全的方法是在字典中为 PAD 分配一个专用缓冲区(通过 ALLOT) or dynamically in memory (via ALLOCATE)。

一个例子:

1024 constant pad-size
create pad pad-size allocate

标准只保证 PAD 大小的 84 个字符,请参阅 3.3.3.6 Other transient regions:

部分

The size of the scratch area whose address is returned by PAD shall be at least 84 characters.

一个好的 Forth-system 当然应该为 PAD 提供可用的大小(参见 environmental queries),一个好的程序应该限制在 84 个字符或确定可用的大小。使用长度不确定的缓冲区是一种不好的做法,在一般情况下会导致内存损坏错误甚至漏洞。

字典中的空闲space可以通过UNUSED word. But this space cannot be safely used without allocating — since many Forth implementations use this space by themselves. For example, all the buffers mentioned in the section 3.3.3.6确定可以位于字典空闲space区域。但是,按照标准,PAD缓冲区不得与内存中的其他缓冲区重叠。因此需要知道特定的 Forth-system 实现细节来确定安全的 offset 值并定义将使用字典 free space 的 PAD。了解这些细节的最可靠方法——阅读特定的 Forth-system 源代码。

即使在那种情况下,定义如下内容也很好:

: pad-size ( -- u ) unused offset - ; \ assuming that no other buffers after PAD
: pad-buf ( -- addr u ) pad pad-size ;