summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2015-09-09 21:27:32 +1000
committerDavid Gibson <david@gibson.dropbear.id.au>2015-09-13 19:17:14 +1000
commit428d3b0217b32892c2fd309358619a4877113833 (patch)
tree3d4b52ae1bf2f3999f501e5d610fd17c10c47f91
parent8f6126000bfebc71f566ce56387f86fcd567e2c7 (diff)
mem: Add memswap() function
Add a memswap() function to the mem module, which exchanges two (equal sized, non-overlapping) memory regions. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r--ccan/mem/mem.c24
-rw-r--r--ccan/mem/mem.h10
-rw-r--r--ccan/mem/test/api.c19
3 files changed, 52 insertions, 1 deletions
diff --git a/ccan/mem/mem.c b/ccan/mem/mem.c
index 91c9961a..5b4b3508 100644
--- a/ccan/mem/mem.c
+++ b/ccan/mem/mem.c
@@ -2,6 +2,7 @@
#include "config.h"
+#include <assert.h>
#include <string.h>
#include <ccan/mem/mem.h>
@@ -64,3 +65,26 @@ void *memcchr(void const *data, int c, size_t data_len)
return NULL;
}
+
+#define MEMSWAP_TMP_SIZE 256
+
+void memswap(void *a, void *b, size_t n)
+{
+ char *ap = a;
+ char *bp = b;
+ char tmp[MEMSWAP_TMP_SIZE];
+
+ assert(!memoverlaps(a, n, b, n));
+
+ while (n) {
+ size_t m = n > MEMSWAP_TMP_SIZE ? MEMSWAP_TMP_SIZE : n;
+
+ memcpy(tmp, bp, m);
+ memcpy(bp, ap, m);
+ memcpy(ap, tmp, m);
+
+ ap += m;
+ bp += m;
+ n -= m;
+ }
+}
diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h
index 99c34c0a..dd66cc62 100644
--- a/ccan/mem/mem.h
+++ b/ccan/mem/mem.h
@@ -217,4 +217,14 @@ static inline bool memoverlaps(const void *a_, size_t al,
return (a < (b + bl)) && (b < (a + al));
}
+/*
+ * memswap - Exchange two memory regions
+ * @a: first region
+ * @b: second region
+ * @n: length of the regions
+ *
+ * Undefined results if the two memory regions overlap.
+ */
+void memswap(void *a, void *b, size_t n);
+
#endif /* CCAN_MEM_H */
diff --git a/ccan/mem/test/api.c b/ccan/mem/test/api.c
index a584c0f3..d820a38d 100644
--- a/ccan/mem/test/api.c
+++ b/ccan/mem/test/api.c
@@ -1,6 +1,12 @@
+#include "config.h"
+
+#include <assert.h>
+
#include <ccan/mem/mem.h>
#include <ccan/tap/tap.h>
+#define SWAPSIZE 12
+
int main(void)
{
char haystack1[] = "abcd\0efgh";
@@ -9,9 +15,10 @@ int main(void)
char needle2[] = "d\0e";
char scan1[] = "aaaab";
char scan2[] = "\0\0\0b";
+ char tmp1[SWAPSIZE], tmp2[SWAPSIZE];
/* This is how many tests you plan to run */
- plan_tests(60);
+ plan_tests(62);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
@@ -96,6 +103,16 @@ int main(void)
ok1(memoverlaps(haystack1 + 4, 7, haystack1, 5));
ok1(!memoverlaps(haystack1 + 5, 6, haystack1, 5));
+ assert(sizeof(haystack1) <= SWAPSIZE);
+ assert(sizeof(haystack2) <= SWAPSIZE);
+ memset(tmp1, 0, sizeof(tmp1));
+ memset(tmp2, 0, sizeof(tmp2));
+ memcpy(tmp1, haystack1, sizeof(haystack1));
+ memcpy(tmp2, haystack2, sizeof(haystack2));
+ memswap(tmp1, tmp2, SWAPSIZE);
+ ok1(memcmp(tmp1, haystack2, sizeof(haystack2)) == 0);
+ ok1(memcmp(tmp2, haystack1, sizeof(haystack1)) == 0);
+
/* This exits depending on whether all tests passed */
return exit_status();
}