I recently saw the following post:
A memory allocator isn't lower level than malloc. (The default allocator typically calls malloc directly or indirectly)
An allocator just allows you to specify different allocation strategies. For example, you might use an allocator which calls malloc once to retrieve a large pool of memory, and then for subsequent allocation requests, it just returns a small chunk of this pool.
Or you may use it as a hook to allow you to perform some additional task every time memory is allocated or freed.
As to your second question, malloc is the lowest you can go without losing portability. malloc is typically implemented using some OS-specific memory allocation function, so that would be lower level still. But that's unrelated to your main question, since C++ allocators are a higher-level abstraction.
from: C++: Memory allocators
My question is- how is malloc implemented in the following Operating systems?
what are the OS-specific functions which are called/implementations of malloc()?
For very large requests, malloc() uses the mmap() system call to find addressable memory space. This process helps reduce the negative effects of memory fragmentation when large blocks of memory are freed but locked by smaller, more recently allocated blocks lying between them and the end of the allocated space.
The malloc function allocates a memory block of at least size bytes. The malloc function guarantees alignment in Windows CE in the same way as LocalAlloc in the Win32 API. The block can be larger than size bytes because of space required for alignment and maintenance information.
There should be an entity that tracks which addresses are free and which are not. That's OS memory management unit. malloc()/free() will then have to call OS system calls. So no OS means no malloc()/free() .
When one calls malloc , memory is taken from the large heap cell, which is returned by malloc . The rest is formed into a new heap cell that consists of all the rest of the memory. When one frees memory, the heap cell is added to the end of the heap's free list.
In Windows, in recent versions of MSVC, malloc (and C++ new
, as it is implemented using the same fundamentals for the actual memory allocation part of new
) calls HeapAlloc()
. In other versions, such as g++ mingw, the C runtime is an older version, which doesn't call quite as directly to HeapAlloc
, but at the base of it, it still goes to HeapAlloc
- to find something different, we need to go back to Windows pre-95, which did have a GlobalAlloc
and LocalAlloc
set of functions - but I don't think people use 16-bit compilers these days - at least not for Windows programming.
In Linux, if you are using glibc
, it depends on the size of the allocation whether it calls sbrk
or mmap
- mmap
(with MAP_ANONYMOUS
in the flags) is used for larger allocations (over a threshold, which I believe is 2MB in the typical implementation)
My question is- how is malloc implemented in the following Operating systems?
On Linux there are two famous malloc
implementations:
dlmalloc (Doug Lea's malloc)
ptmalloc
On Linux libc like glibc, eglibc or newlib implement ptmalloc or a variant of ptmalloc.
what are the OS-specific functions which are called/implementations of malloc()?
On Unix and Linux systems sbrk
and mmap
system calls are used. See man 2 sbrk
and man 2 mmap
for more information.
Alright, I am not sure about Linux, but when it comes to windows...
Memory can be allocated in two categorized places.
1) Heaps (Process Heap, Custom Created Heaps) see -> http://msdn.microsoft.com/en-us/library/windows/desktop/aa366711(v=vs.85).aspx using functions like HeapAlloc & HeapFree. LocalAlloc and LocalFree can be used as 'shortcuts' to HeapAlloc when you want to allocate in the default process heap.
2) Virtual Memory (usually only process-specific due to access restrictions in global virtual memory for security), using VirtualAlloc, VirtualFree. see -> http://msdn.microsoft.com/en-us/library/windows/desktop/aa366916(v=vs.85).aspx
To my knowledge, malloc will use the heap allocation functions on windows.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With