5

I'm currently studying the malloc implementation for my homework.

I know there exists some versions of malloc implementation like ptmalloc, used by glibc, and jemalloc, used by FreeBSD.

I wonder which version of implementation is adopted by visual C++? Or the VC++ team just implemented their own version?

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
lei_z
  • 1,049
  • 2
  • 13
  • 27
  • 5
    The have implemented their own, using Windows' [HeapAlloc](http://msdn.microsoft.com/en-us/library/windows/desktop/aa366597%28v=vs.85%29.aspx) function. – Bo Persson May 12 '12 at 13:10
  • 1
    @BoPersson: strictly speaking, that's not really **their** own. They just reused what Windows guys implemented. –  May 12 '12 at 13:10
  • I don't understand that why don't they just adopt some existing implementations like Linux and unix do? – lei_z May 12 '12 at 16:30
  • @stone199141, most likely because of platform differences. Windows needs to accommodate distinct memory heaps (while on Linux you just say "the" memory heap) through its `HeapAlloc` function. Maybe the algorithm behind the allocation is the same as one you've mentioned, but it's closed-source, so we may never know. – zneak May 12 '12 at 23:53
  • 3
    @stone199141: "why don't they just" There's no reason for them to do that. Their current implementation works fine, and linux code will come with strings attached ((L)GPL license). They could probably grab something bsd-licensed - if they wanted, but why bother if current system works? – SigTerm May 13 '12 at 00:46
  • 2
    @Fanael I'd expect the Windows team and the VC++ team to have worked together to get a reasonable allocator implementation. After all, it's a really critical component of the C (and C++, and many other languages) runtime; overall, it tends to be very “hot” code and it would be worth taking the time to really optimize it to death. Plus, _they work on the same site_ so there's really no reason not to… – Donal Fellows May 13 '12 at 01:03

1 Answers1

10

When you call malloc or new in a VC++ compiled program without writing your own redirector you end up in HeapAlloc, that is also known as NT Heap.

NT Heap is developed by the Windows memory team. These guys are responsible for all memory management in the OS. They allocate virtual space for user-mode processes; they handle memory for drivers, etc. It is logical that the same team provides code for small allocations in the application code. From my experience, NT heap is a good thing. It does not have any major flaws. When you have very specific requirements, some other allocator may perform better. In the general case, NT heap is the right starting point. Most likely it will satisfy your needs.

Note that VC++ is a compiler. It creates an executable that runs under control of the operating system. It is not correct to speak about any "VC++ executing environment".

Nevertheless, I know that the compiler itself is not using NT heap when it compiles the code. They use their own allocator. I do not know the exact reasons why they did that.

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51