Since I tried creating my own Lisp interpreter (just for learning purposes, as I knew it wouldn't get too far), I have read and tried to understand the internal workings of the Elisp interpreter and always wondered how Emacs does its garbage collection.
I know that the main idea in a Lisp interpreter is that every object that is not reachable from the stack or is the variable or function value of a symbol (basically accessible from the
obarray), is considered garbage and is collected when new memory is needed. But what happens when the only reference to an object is in the stack?
How does Emacs know which block of data corresponds to a Lisp object? After all, they are mixed with the complete C/computer stack with function return addresses and C variables not necessarily used inside the Elisp side of Emacs.
After reading the Emacs source file
alloc.c, I understand this is done through a tree which holds the address of all allocated memory blocks, and that should in theory solve the problem, but what happens when by pure chance a block of memory stores a valid Elisp object even when it is not supposed to be one? So, could someone please explain to me how Emacs marks/uses the C stack for Elisp function calls?