14.6.1.0707 ALLOCATE MEMORY

( u -- a-addr ior )

Allocate u address units of contiguous data space. The data-space pointer is unaffected by this operation. The initial content of the allocated space is undefined.

If the allocation succeeds, a-addr is the aligned starting address of the allocated space and ior is zero.

If the operation fails, a-addr does not represent a valid address and ior is the implementation-defined I/O result code.

See:

Testing:

VARIABLE datsp
HERE datsp !

T{ 50 CELLS ALLOCATE SWAP addr ! -> 0 }T
T{ addr @ ALIGNED -> addr @ }T    \ Test address is aligned
T{ HERE -> datsp @ }T              \ Check data space pointer is unaffected
addr @ 50 write-cell-mem
addr @ 50 check-cell-mem         \ Check we can access the heap
T{ addr @ FREE -> 0 }T

T{ 99 ALLOCATE SWAP addr ! -> 0 }T
T{ addr @ ALIGNED -> addr @ }T    \ Test address is aligned
T{ addr @ FREE -> 0 }T
T{ HERE -> datsp @ }T              \ Data space pointer unaffected by FREE
T{ -1 ALLOCATE SWAP DROP 0= -> <FALSE> }T    \ Memory allocate failed

ContributeContributions

MitraArdronavatar of MitraArdron write-cell-mem undefinedRequest for clarification2021-01-13 03:56:09

In the test cases for ALLOCATE it uses words which as far as I can see are neither standard nor defined. Specifically write-cell-mem and check-cell-mem which aren't defined.

Also ... It looks to me like ALLOCATE has to store the size somewhere so that FREE can work, since FREE doesn't get passed the size. Should that be clarified?

PeterKnaggsavatar of PeterKnaggs

See F.16 The optional Memory-Allocation word set for the definition of check-cell-mem and other support words.

Yes, we assume ALLOCATE uses a fat pointer, a data structures known only to the compiler is stored in the member immediately proceeding the address returned by ALLOCATE. However, this is only one implementation method, and the committee does not wish to limit implementation to this method only.

GeraldWodniavatar of GeraldWodni

same answer as with the GT-1 question:

The committee discussed that issue:

It would blow up the testcases if we add all required test-words for every word making the document too big.

We could do this on the website itself though: the best solution would be to write a forth program which finds all required references and updates the test definitions for the words. However this is a lot of work. If you are interested, we would welcome a pull request for this feature on the forth-standard.org repository.

Closed
Reply New Version