Re: inlining callbacks question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Christopher Layne writes:
 > Given this simple toy code:
 > 
 > /* */
 > #include <unistd.h>
 > #include <stdlib.h>
 > #include <stdio.h>
 > 
 > typedef struct cbt__ cbt;
 > typedef void (*cbt_cb_display)(cbt *);
 > 
 > struct cbt__ {
 >         char *text;
 >         cbt_cb_display display;
 > };
 > 
 > static void cbt_display(cbt *c)
 > {
 >         fprintf(stdout, "c->text == %s\n", c->text);
 > 
 >         return;
 > }
 > 
 > extern cbt *cbt_new(void)
 > {
 >         cbt *c;
 > 
 >         c = malloc(sizeof *c);
 >         c->text = NULL;
 >         c->display = cbt_display;
 > 
 >         return c;
 > }
 > 
 > int main(int argc, char **argv)
 > {
 >         cbt *c;
 > 
 >         if (argc <= 1) return EXIT_FAILURE;
 > 
 >         c = cbt_new();
 >         c->text = argv[1];
 > #ifdef CB
 >         c->display(c);
 > #else
 >         cbt_display(c);
 > #endif
 > 
 >         return 0;
 > }
 > /* */
 > 
 > I notice that it seems impossible to get that callback to ever be inlined.
 > 
 > $ gcc -DCB -W -Wall -pedantic -O2 -S -o /dev/stdout cb.c
 > [...]
 >         movl    %eax, (%esp)
 >         call    *4(%eax)
 >         xorl    %eax, %eax
 > [...]
 > 
 > $ gcc -DCB -W -Wall -pedantic -O3 -S -o /dev/stdout cb.c
 > [...]
 >         movl    %eax, (%esp)
 >         call    cbt_display
 >         xorl    %eax, %eax
 > [...]

With your unaltered source code, I get this at -O3:

main:
.LFB28:
        subl    $1, %edi
        pushq   %rbx
.LCFI1:
        movl    $1, %eax
        movq    %rsi, %rbx
        jle     .L8
        movl    $16, %edi
        call    malloc
        movq    8(%rbx), %rdx
        movq    stdout(%rip), %rdi
        movl    $.LC0, %esi
        movq    $cbt_display, 8(%rax)
        movq    %rdx, (%rax)
        xorl    %eax, %eax
        call    fprintf
        xorl    %eax, %eax
.L8:
        popq    %rbx
        ret

What version of gcc are you using?

Andrew.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux