[Mono-devel-list] malloc and free on CLI
kornelpal at hotmail.com
Sat Jul 16 16:12:41 EDT 2005
> From: Jeyasankar Kottalam
> How should I implement malloc and free on CLI? I've come up with a couple
> ideas, but none of them seem particularly good to me:
As far as I know malloc and free are C runtime library functions not
intrinsic functions. I think they should not be implemented at compiler
level. These functions are allocating memory from a heap.
>- The mysterious "localloc" instruction.
The memory allocated by localloc will be freed when the method return. This
can and should be used to implement the C "alloca" function and should be
compiled directly to localloc without any runtime library. Note that
"alloca" allocates memory from the stack while "localloc" may allocate
memory from heap but it's unimportant because it's the CLR's responsibility
to free the memory. Programmers responsibility is not to use the pointer
after leaving the method that allocated the memory.
If you are working ont the C compiler I think you should not care about the
runtime library. You should implement all the functionality required to
compile C source code to pure CIL code.
There should be a managed C runtime library but from the point of view of
the compiler the C runtime library will be regular managed assembly.
CLI has limited pointer support for example there are no object pointers and
there is no heap (CLR can have a heap but it is private).
>Problem: I don't think we can get a chunk of raw memory from the runtime.
>Seems like the best we could do is "new byte[foo]" which gives an _array
>object_. Also I don't know of any mechanism to keep the block from being
>shuffled around in memory by the garbage collector.
Using a byte arre could be a solution but this functionality should be
implemented in the C runtime library not in the compiler. So if you are not
working on the C runtime library you should not care about the runtime
library functions you only have to care about implementing all the required
When there will be a C compiler then existing C runtime library source codes
can be ported to managed code.
This is my opinion but others may have different opinions.
More information about the Mono-devel-list