ALLOCATE

( 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.

Reply New Version