Re: [PATCH 1/5] add SWAP macro
- From
Johannes Sixt <j6t@kdbg.org>
- Date
- Jan 30, 2017, 18:41 UTC
- Message-ID
- <aa653d57-4a97-ac50-b20c-f94ed43a22fb@kdbg.org>
- In-Reply-To
- <alpine.DEB.2.20.1701301643260.3469@virtualbox>
Am 30.01.2017 um 17:01 schrieb Johannes Schindelin:
Show 23 quoted lines
> On Sat, 28 Jan 2017, René Scharfe wrote:
>> diff --git a/git-compat-util.h b/git-compat-util.h
>> index 87237b092b..66cd466eea 100644
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -527,6 +527,16 @@ static inline int ends_with(const char *str, const char *suffix)
>> return strip_suffix(str, suffix, &len);
>> }
>>
>> +#define SWAP(a, b) do { \
>> + void *_swap_a_ptr = &(a); \
>> + void *_swap_b_ptr = &(b); \
>> + unsigned char _swap_buffer[sizeof(a)]; \
>> + memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
>> + memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
>> + BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
>> + memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
>> +} while (0)
>> +
>> #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
>
> It may seem as a matter of taste, or maybe not: I prefer this without the
> _swap_a_ptrThe purpose of these pointers is certainly to avoid that the macro arguments are evaluated more than once.
-- Hannes