[BusyBox] xfuncts.c xfree() ?

Axel Kittenberger Axel.Kittenberger at maxxio.at
Thu Dec 6 07:13:21 UTC 2001


> For better debugability, I generally prefer static inlines, which
> should be similarly optimized.   But in this case, I for such a
> trivial case, if the compiler can better optimize the #define,
> I'm ok with it.  Do you have a minute to check?

Okidoky, both variants with and without debug, on a x86 with-O2 optimize.
---
#include <stdlib.h>
#define DEBUG 1

static inline void xfree(void *arg) {
#if DEBUG
    if (arg)
#endif
        free(arg);
}

void test(void * a)
{
    xfree(a);
}
--- compiles to ---
test:
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
        movl 8(%ebp),%eax
        testl %eax,%eax
        je .L21
        addl $-12,%esp
        pushl %eax
        call free
.L21:
        movl %ebp,%esp
        popl %ebp
        ret
----

----
#include <stdlib.h>
#define DEBUG 0

static inline void xfree(void *arg) {
#if DEBUG
    if (arg)
#endif
        free(arg);
}

void test(void * a)
{
    xfree(a);
}
--- compiles to ---
test:
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
        movl 8(%ebp),%eax
        addl $-12,%esp
        pushl %eax
        call free
        movl %ebp,%esp
        popl %ebp
        ret
-----------

------
#define DEBUG 1

static inline void xfree(void *arg) {
    if (!DEBUG || arg)
        free(arg);
}

void test(void * a)
{
    xfree(a);
}
--- compiles to ---
test:
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
        movl 8(%ebp),%eax
        testl %eax,%eax
        je .L21
        addl $-12,%esp
        pushl %eax
        call free
.L21:
        movl %ebp,%esp
        popl %ebp
        ret
----

------
#define DEBUG 0

static inline void xfree(void *arg) {
    if (!DEBUG || arg)
        free(arg);
}

void test(void * a)
{
    xfree(a);
}
--- compiles to ---
test:
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
        movl 8(%ebp),%eax
        addl $-12,%esp
        pushl %eax
        call free
        movl %ebp,%esp
        popl %ebp
        ret
------

As excepted pretty equivalent. I personally only prefer normal if(0/1) over 
#ifdef since the symantic of the false path gets checked by the compiler.

- Axel





More information about the busybox mailing list