I'm working on a simple stack machine written in C, mostly for learning purposes. After using malloc/free
for my memory operations, I thought it would be a good idea to read some memory allocation specific code from modern virtual machines.
I downloaded Lua source code and started reading it. After a while, I realized there are lots of macro stuff involved, and I couldn't find the code where real memory allocation is done (ie. malloc
call).
find . -exec grep -i "malloc" '{}' \; -print
It printed only some Lua macros that have malloc
word in their names. The Lua VM (and programming language) doesn't use malloc
at all!
So this leads me to the question: how do modern VMs handle memory allocation? How does Lua allocate memory from the heap? Are there any ways for allocation other than malloc
? What are the pros/cons of other methods?
I'm also wondering about best-practices, design-patterns, etc. for safely working on allocated memory. I see in Lua's source that there is lots of indirection before allocating memory. Where can I learn about this stuff?