You are not logged in.

#1 2009-02-22 04:14:27

herr.jth
Member
From: Germany
Registered: 2007-08-18
Posts: 12

Question about #define-Wrappers in libalpm

Hi,
I`ve started reading through the libalpm sources and one thing is completely unclear to me:

The wrappers in util.h are all implemented inside a do-while loop that runs only once, no matter what happens. FREE for example:

#define FREE(p) do { free(p); p = NULL; } while(0)

Why is that? In fact, compiled with -O2 gcc optimizes the loop away on my machine.

Has anyone an explanation for me why this is done and whats the advantage of it?

Offline

#2 2009-02-22 04:18:13

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,392
Website

Re: Question about #define-Wrappers in libalpm

You will probably get to the person who wrote the code if you ask on the pacman-dev mailing list.

Offline

#3 2009-02-22 08:40:03

Husio
Member
From: Europe
Registered: 2005-12-04
Posts: 359
Website

Re: Question about #define-Wrappers in libalpm

Offline

#4 2009-02-22 10:20:09

bwalk
Member
Registered: 2007-03-21
Posts: 177

Re: Question about #define-Wrappers in libalpm

-O2 should not unroll this loop. Sure you don't have -funroll-loops set?

Offline

#5 2009-02-22 13:43:58

herr.jth
Member
From: Germany
Registered: 2007-08-18
Posts: 12

Re: Question about #define-Wrappers in libalpm

Thank you, now thats clear.

bwalk wrote:

-O2 should not unroll this loop. Sure you don't have -funroll-loops set?

This is my testprogram:

#include <stdlib.h>

#define FREE(p) do { free(p); p = NULL; } while(0)

int main(void) {
        char *ptr = (char*)malloc(100);
        FREE(ptr);
        return 0;
}

I compiled it with gcc -O2 and the FREE(ptr)-Part translates to

mov    %eax,(%esp)
call   0x804831c <free@plt>
add    $0x4,%esp
xor    %eax,%eax

I think the xor sets the pointer to 0x0, so no loop there.

According to gcc.gnu.org -floop-optimize is already activated by the -O switch. It also says that -floop-optimize might perform loop-unrolling. But I'am not sure if loop unrolling applies here, since the loop isn`t rolled out but completely optimized away. However, I think that -floop-optimize is responsible for that.

Offline

#6 2009-02-22 15:59:51

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Question about #define-Wrappers in libalpm

c-faq is a great resource for the first kind of question : http://c-faq.com/cpp/multistmt.html


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#7 2009-02-23 03:57:39

Xilon
Member
Registered: 2007-01-01
Posts: 243

Re: Question about #define-Wrappers in libalpm

herr.jth wrote:

Hi,
I`ve started reading through the libalpm sources and one thing is completely unclear to me:

The wrappers in util.h are all implemented inside a do-while loop that runs only once, no matter what happens. FREE for example:

#define FREE(p) do { free(p); p = NULL; } while(0)

Why is that? In fact, compiled with -O2 gcc optimizes the loop away on my machine.

That's kinda the point. It's simply to add syntactic sugar (making it behave like a function, as Allan pointed out). The loop gets optimized out so you simply get the "free(p); p = NULL;", while still being able to do "FREE(p);"

Offline

Board footer

Powered by FluxBB