fungos / cr

cr.h: A Simple C Hot Reload Header-only Library
https://fungos.github.io/cr-simple-c-hot-reload/
MIT License
1.54k stars 101 forks source link

Use ElfW() macro to avoid individual architecture data type definition #58

Closed nihguy closed 4 years ago

nihguy commented 4 years ago

The ElfW(Type) macro definition turns its argument into the name of an ELF data type suitable for the hardware architecture. For example, ElfW(Ehdr) yields the data type name Elf32_Ehdr on a 32-bit platforms, and Elf64_Ehdr on 64-bit platforms, as explains the link.h header file.

I'm curious: Is do{ }while(0); really necessary? Why?

fungos commented 4 years ago

This do/while works as a goto statement, so we can skip a block of code and go directly to the cleanup in case of error.

Thank you for this PR.

nihguy commented 4 years ago

Great solution! I did a simple benchmark test using both do/while and nested ifs on g++ 9 and the former gets a better performance. It's just 1 or 2 ns, but the code block doesn't sacrifice performance to make the code cleaner.

A new approach to the utility belt. Thank you for your answer.