You are not logged in.
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
You will probably get to the person who wrote the code if you ask on the pacman-dev mailing list.
Offline
Offline
-O2 should not unroll this loop. Sure you don't have -funroll-loops set?
Offline
Thank you, now thats clear.
-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
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
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