Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 1 | #include "swap.h" |
| 2 | |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | void swap_loop(char* restrict a, char* restrict b, size_t len) { |
| 7 | while (len--) { |
| 8 | char tmp = *a; |
| 9 | *a++ = *b; |
| 10 | *b++ = tmp; |
| 11 | }; |
| 12 | } |
| 13 | |
| 14 | void swap_malloc(char* restrict a, char* restrict b, size_t len) { |
| 15 | char *temp = malloc(len); |
| 16 | memmove(temp,a,len); |
| 17 | memmove(a,b,len); |
| 18 | memmove(b,temp,len); |
| 19 | free(temp); |
| 20 | } |
| 21 | |
| 22 | void swap_alloca(char* restrict a, char* restrict b, size_t len) { |
| 23 | char temp[len]; |
| 24 | memmove(temp,a,len); |
| 25 | memmove(a,b,len); |
| 26 | memmove(b,temp,len); |
| 27 | } |