huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11916

Can a C/C++ compiler inline builtin functions like malloc()?

While inspecting the disassembly of below function,

void * malloc_float_align(size_t n, unsigned int a, float *& dizi)
    {
        void * adres=NULL;
        void * adres2=NULL;
        adres=malloc(n*sizeof(float)+a);
        size_t adr=(size_t)adres;
        size_t adr2=adr+a-(adr&(a-1u));
        adres2=(void * ) adr2;
        dizi=(float *)adres2;
        return adres;
    }

Builtin functions are not inlined even with the inline optimization flag set.

; Line 26
$LN4:
    push    rbx
    sub rsp, 32                 ; 00000020H
; Line 29
    mov ecx, 160                ; 000000a0H
    mov rbx, r8
    call    QWORD PTR __imp_malloc <------this is not inlined
; Line 31
    mov rcx, rax
; Line 33
    mov rdx, rax
    and ecx, 31
    sub rdx, rcx
    add rdx, 32                 ; 00000020H
    mov QWORD PTR [rbx], rdx
; Line 35
    add rsp, 32                 ; 00000020H
    pop rbx
    ret 0

Question: is this a must-have property of functions like malloc? Can we inline it some way to inspect it(or any other function like strcmp/new/free/delete)? Is this forbidden?

Upvotes: 2

Views: 1109

Answers (4)

Mats Petersson
Mats Petersson

Reputation: 129494

Typically the compiler will inline functions when it has the source code available during compilation (in other words, the function is defined, rather than just a prototype declaration) in a header file).

However, in this case, the function (malloc) is in a DLL, so clearly the source code is not available to the compiler during the compilation of your code. It has nothing to do with what malloc does (etc). However, it's also likely that malloc won't be inlined anyway, since it is a fairly large function [at least it often is], whcih prevents it from being inlined even if the source code is available.

If you are using Visual Studio, you can almost certainly find the source code for your runtime library, as it is supplied with the Visual Studio package.

(The C runtime functions are in a DLL because many different programs in the system use the same functions, so putting them in a DLL that is loaded once for all "users" of the functionality will give a good saving on the size of all the code in the system. Although malloc is perhaps only a few hundred bytes, a function like printf can easily add some 5-25KB to the size of an executable. Multiply that by the number of "users" of printf, and there is likely several hundred kilobytes just from that one function "saved" - and of course, all other functions such as fopen, fclose, malloc, calloc, free, and so on all add a little bit each to the overall size)

Upvotes: 3

Jonathan Leffler
Jonathan Leffler

Reputation: 754700

The main thing stopping the inlining of malloc() et al is their complexity — and the obvious fact that no inline definition of the function is provided. Besides, you may need different versions of the function at different times; it would be harder (messier) for tools like valgrind to work, and you could not arrange to use a debugging version of the functions if their code is expanded inline.

Upvotes: 1

RichieHindle
RichieHindle

Reputation: 281765

malloc and friends are implemented in the runtime library, so they're not available for inlining. They would need to have their implementation in their header files for that to happen.

If you want to see their disassembly, you could step into them with a debugger. Or, depending on the compiler and runtime you're using, the source code might be available. It is available for both gcc and msvc, for example.

Upvotes: 1

rici
rici

Reputation: 241901

A C compiler is allowed to inline malloc (or, as you see in your example, part of it), but it is not required to inline anything. The heuristics it uses need not be documented, and they're usually quite complex, but normally only short functions will be inlined, since otherwise code-bloat is likely.

Upvotes: 1

Related Questions