Arena storage pool: Difference between revisions

Line 530:
 
There is some ability to specify data types for various objects which allows for (but does not guarantee) more efficient memory layout, but again, it is considered to be an implementation detail, the use that the virtual machine makes of that information varies by implementation maturity and capabilities.
 
=={{header|Phix}}==
Phix applications do not generally need to allocate and free memory explicitly, except for use in ffi, and even then the cffi package or
any of the GUI wrappers can handle most or all of it for you automatically. Both arwen and win32lib (both now superceded by pGUI, and note that that both are 32-bit only, with 4-byte alignment) contain arena storage implementations which may be of interest: see eg allocate_Rect() in demo\arwen\Quick_Allocations.ew, which also offers performance benefits via a circular buffer for short-term use, and also w32new_memset()/w32acquire_mem()/w32release_mem() in win32lib.
 
The simplest approach however is to rely on automatic memory management (as used by pGUI, and first implemented after arwen and win32lib were originally written):
<lang Phix>atom mem = allocate(size,true)</lang>
If the optional cleanup flag is non-zero (or true, as above), the memory is automatically released once it is no longer required
(ie when the variable mem drops out of scope or gets overwritten, assuming you have not made a copy of it elsewhere, which would
all be handled quite properly and seamlessly, with the deallocation not happening until all copies were also overwritten
or discarded), otherwise (ie cleanup is zero or omitted) the application should invoke free() manually.
 
For completeness, here is a very simplistic arena manager, with just a single pool, not that it would be tricky to implement multiple pools:
<lang Phix>sequence ap = {}
function ap_allocate(integer size)
-- allocate some memory and add it to the arena pool 'ap' for later release
atom res = allocate(size)
ap = append(ap,res)
return res
end function
procedure ap_free()
-- free all memory allocated in arena pool 'ap'
free(ap)
ap = {}
end procedure</lang>
 
=={{header|PicoLisp}}==
7,795

edits