On Jul 30, 1:13 am, Mark Wills <
markrobertwi...@yahoo.co.uk> wrote:
> However, you might want to consider a different version of colon for
> words that need allocation and de-allocation, to differentiate them
> more clearly from 'normal' definitions. Something like a: and ;a for
> words that require memory allocation/de-allocation:
>
> a: some-word <code for the word> ;a
>
> The above could be built something like this:
>
> : a: postpone : postpone (allocateMemory) ;
> : ;a postpone (releaseMemory) postpone ; ; immediate
>
> Now there is no requirement for the variable _releaseMemory. You would
> use it like this:
>
> a: fred <some code here> ;a
>
> And the memory allocation and de-allocation calls are automatically
> compiled into the definition for you. The fact that the word is
> created with a: tells you that it's a memory allocating word. Just
> document your application code so that others (or you after 12
> months!) will know what is special about words defined with a:
This is the best solution so far.
Actually though, what you are asking for doesn't make much sense. What
is that allocated memory supposed to be used for? Why do you expect
every A: word to need the same amount of memory?
If you want some memory temporarily allocated within a colon word,
just use local variables --- they get allocated at the beginning of
the colon word and semi-colon deallocates them at termination --- just
what you seem to be asking for.
If one-cell local variables aren't adequate, such as if you need a
record or an array, then I would still use a local variable but I
would ALLOCATE some memory (the size of the record or array) at the
beginning and put the address in the local, and then FREE it at the
end. If you are going to have such a great number of these kinds of
words that you want to automate the process, then you could write
macros similar to what Mark did above.
You can use MACRO: from my novice package
(
http://www.forth.org/novice.html) rather than raw POSTPONE and
EVALUATE to improve readability:
macro: ]stuff
] literal dup alloc locals| stuff stuff-size |
stuff stuff-size erase ;
macro: stuff>
stuff dealloc ;
: test
[ 10 ]stuff
cr ." adr: " stuff .
cr ." size: " stuff-size .
cr ." contents:"
stuff stuff-size bounds do cr I c@ . loop
stuff> ;
This is somewhat different than Mark's example. I don't incorporate :
and ; into my macros. I just provide ]STUFF and STUFF> that bracket
the code. Inside of these, you have the STUFF local variable that
points to a block of memory that is as large as you specified (10
bytes in the TEST example), and the STUFF-SIZE local variable that is
the size of the block of memory (10 in the TEST example). If you run
TEST, the adr will be different every time, the size will be 10, and
the contents will be 10 zeros. Like this:
test
adr: 41213732
size: 10
contents:
0
0
0
0
0
0
0
0
0
0 ok
All of this seems way more complicated than anything that I would
normally do when writing a program. What are you trying to accomplish?
If you told us what your application is, we would be able to help you
better. What you are asking for doesn't seem to make much sense. What
is that memory going to be used for?