Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Windows and Linux native OS/system calls made from malloc()?

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?

  • for Windows
  • for Linux

what are the OS-specific functions which are called/implementations of malloc()?

like image 469
user997112 Avatar asked Aug 03 '13 13:08

user997112


People also ask

What system call does malloc use?

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.

Does malloc work on Windows?

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.

Is malloc an OS call?

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() .

How malloc is implemented in Linux?

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.


3 Answers

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)

like image 174
Mats Petersson Avatar answered Oct 25 '22 18:10

Mats Petersson


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.

like image 37
ouah Avatar answered Oct 25 '22 19:10

ouah


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.

like image 23
Dimitris Preketes Avatar answered Oct 25 '22 18:10

Dimitris Preketes