[TUHS] Memory management in Dennis Ritchie's C Compiler

Dibyendu Majumdar mobile at majumdar.org.uk
Tue Aug 18 06:16:06 AEST 2020

On Mon, 17 Aug 2020 at 17:13, Dan Cross <crossd at gmail.com> wrote:
> From my light skimming of V10 sources, it appears that the various components of the default C compiler (that is, not LCC) either use malloc/free or call `sbrk` directly.

Yes, it only uses sbrk(). One consequence I think is that sbrk()
expands the process memory without invalidating existing use of memory
- so the code is able to periodically expand heap while retaining all
existing allocations.
A simple workaround I used was to preallocate a heap and just stub out
sbrk() calls - so that works. So in essence given a single chunk of
memory (if large enough - which is still quite small by today's
standards) the compiler manages fine.

However I find this unsatisfactory and would like to improve it. But
it is a bit difficult to understand how the memory is being used.

Memory can be used for declarations, trees (for expressions) and
strings as far as I can tell. Strings actually use the tree
allocation, and just pretend that a node is a string.
It seems that tree memory is allocated in a stack discipline. But what
puzzled me is that when a tree starts, about 512 bytes of memory are
left as gap for declarations to use. I have been trying to think in
what circumstances would you encounter a declaration while parsing an
expression - perhaps cast expressions? Anyway - if a declaration
occurs inside an expression (i.e. tree) then it only has 512 bytes
available. Of course this could be made bigger ... but at the least I
would like to have separate heaps for declarations, trees and strings.

I guess no one really dug into this code - as presumably once PCC came
along the original compiler by Dennis stopped being used.

Thanks and Regards

More information about the TUHS mailing list