Memory Allocation (malloc) and processes.

Last modified : 3 March, 2019

I was trying to understand how memory allocation works in C++ programs and this post describes what I discovered. It used to be that memory allocators depended on the brk()/sbrk() system call to extend the data section of a program. However I was working with a SWIG‘d binary and didn’t understand how two malloc systems were coordinating what regions of memory they were managing.

It turns out the newer generation of memory allocators use mmap() with the MAP_ANONYMOUS flag. Importantly:

	void *mmap(void *addr, size_t length, int prot, int flags,
		int fd, off_t offset);
	If addr is NULL, then the kernel chooses the (page-aligned) address
	at which to create the mapping; this is the most portable method of
	creating a new mapping.  If addr is not NULL, then the kernel takes
	it as a hint about where to place the mapping; on Linux, the mapping
	will be created at a nearby page boundary.  The address of the new
	mapping is returned as the result of the call.

So really its the kernel that assigns regions of memory to the malloc system which it then partitions for implementing whatever scheme it sees fit.

The next question in my mind was what is the significance of the program section and data section in real terms.

Multiple threads and stacks.

I should mention that in Linux, all threads are processes. There is a clone() system call that lets the caller specify the things it wants to share with the cloned process. A thread is simply a process that shares the address space, file descriptor etc.

However this raises the question about the placement of stacks. How do different threads, not write their stacks in overlapping memory locations?

This is handled by the parent process passing a child_stack pointer where the cloned process must keep its stack. Reading the man pages for system calls turns out to be really helpful. Who knew?

Also keep in mind that we have ASLR for security which complicates matters a little.

All content on this website is licensed as Creative Commons-Attribution-ShareAlike 4.0 License. Opinions expressed are solely my own.