ccan.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] mem: Small enhancements
@ 2015-09-09 21:54 David Gibson
  2015-09-09 21:54 ` [PATCH 1/3] mem: Remove array_size dependency David Gibson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: David Gibson @ 2015-09-09 21:54 UTC (permalink / raw)
  To: ccan, rusty

Several small enhancements to the mem module.

Changes since v1:
 * Added memoverlaps() function
  * Added an assert for memswap() if called with overlapping regions.

David Gibson (3):
  mem: Remove array_size dependency
  mem: Add function to check whether memory ranges overlap
  mem: Add memswap() function

 ccan/mem/_info      |  1 -
 ccan/mem/mem.c      | 24 ++++++++++++++++++++++++
 ccan/mem/mem.h      | 27 +++++++++++++++++++++++++++
 ccan/mem/test/api.c | 41 +++++++++++++++++++++++++++++++++++++++--
 4 files changed, 90 insertions(+), 3 deletions(-)

-- 
2.4.3

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] mem: Remove array_size dependency
  2015-09-09 21:54 [PATCH 0/3] mem: Small enhancements David Gibson
@ 2015-09-09 21:54 ` David Gibson
  2015-09-09 21:54 ` [PATCH 2/3] mem: Add function to check whether memory ranges overlap David Gibson
  2015-09-09 21:54 ` [PATCH 3/3] mem: Add memswap() function David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2015-09-09 21:54 UTC (permalink / raw)
  To: ccan, rusty

The mem module declares array_size as a test dependency, and includes it in
test/api.c, but doesn't actually use it.  This removes the unneeded
dependency.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 ccan/mem/_info      | 1 -
 ccan/mem/test/api.c | 1 -
 2 files changed, 2 deletions(-)

diff --git a/ccan/mem/_info b/ccan/mem/_info
index 6389e35..b95a575 100644
--- a/ccan/mem/_info
+++ b/ccan/mem/_info
@@ -23,7 +23,6 @@ int main(int argc, char *argv[])
 	}
 
 	if (strcmp(argv[1], "testdepends") == 0) {
-		printf("ccan/array_size");
 		return 0;
 	}
 
diff --git a/ccan/mem/test/api.c b/ccan/mem/test/api.c
index 9ec226d..d0178c3 100644
--- a/ccan/mem/test/api.c
+++ b/ccan/mem/test/api.c
@@ -1,4 +1,3 @@
-#include <ccan/array_size/array_size.h>
 #include <ccan/mem/mem.h>
 #include <ccan/tap/tap.h>
 
-- 
2.4.3

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] mem: Add function to check whether memory ranges overlap
  2015-09-09 21:54 [PATCH 0/3] mem: Small enhancements David Gibson
  2015-09-09 21:54 ` [PATCH 1/3] mem: Remove array_size dependency David Gibson
@ 2015-09-09 21:54 ` David Gibson
  2015-09-09 21:54 ` [PATCH 3/3] mem: Add memswap() function David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2015-09-09 21:54 UTC (permalink / raw)
  To: ccan, rusty

The test is simple, but every time I do it by hand, I always spend ages
convincing myself it's actually correct.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 ccan/mem/mem.h      | 17 +++++++++++++++++
 ccan/mem/test/api.c | 23 ++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h
index 1afe508..99c34c0 100644
--- a/ccan/mem/mem.h
+++ b/ccan/mem/mem.h
@@ -200,4 +200,21 @@ static inline bool memends_str(const void *a, size_t al, const char *s)
 	return memends(a, al, s, strlen(s));
 }
 
+/**
+ * memoverlaps - Do two memory ranges overlap?
+ * @a: pointer to first memory range
+ * @al: length of first memory range
+ * @b: pointer to second memory range
+ * @al: length of second memory range
+ */
+CONST_FUNCTION
+static inline bool memoverlaps(const void *a_, size_t al,
+			       const void *b_, size_t bl)
+{
+	const char *a = a_;
+	const char *b = b_;
+
+	return (a < (b + bl)) && (b < (a + al));
+}
+
 #endif /* CCAN_MEM_H */
diff --git a/ccan/mem/test/api.c b/ccan/mem/test/api.c
index d0178c3..a584c0f 100644
--- a/ccan/mem/test/api.c
+++ b/ccan/mem/test/api.c
@@ -11,7 +11,7 @@ int main(void)
 	char scan2[] = "\0\0\0b";
 
 	/* This is how many tests you plan to run */
-	plan_tests(46);
+	plan_tests(60);
 
 	ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
 	ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
@@ -75,6 +75,27 @@ int main(void)
 	ok1(!memends_str(S("a\0bcdef"), "a"));
 	ok1(memends_str(S("a\0bcdef"), "ef"));
 
+	ok1(!memoverlaps(haystack1, sizeof(haystack1),
+			 haystack2, sizeof(haystack2)));
+	ok1(!memoverlaps(haystack2, sizeof(haystack2),
+			 haystack1, sizeof(haystack1)));
+	ok1(memoverlaps(haystack1, sizeof(haystack1), haystack1, 1));
+	ok1(memoverlaps(haystack1, 1, haystack1, sizeof(haystack1)));
+	ok1(memoverlaps(haystack1, sizeof(haystack1),
+			haystack1 + sizeof(haystack1) - 1, 1));
+	ok1(memoverlaps(haystack1 + sizeof(haystack1) - 1, 1,
+			haystack1, sizeof(haystack1)));
+	ok1(!memoverlaps(haystack1, sizeof(haystack1),
+			 haystack1 + sizeof(haystack1), 1));
+	ok1(!memoverlaps(haystack1 + sizeof(haystack1), 1,
+			 haystack1, sizeof(haystack1)));
+	ok1(!memoverlaps(haystack1, sizeof(haystack1), haystack1 - 1, 1));
+	ok1(!memoverlaps(haystack1 - 1, 1, haystack1, sizeof(haystack1)));
+	ok1(memoverlaps(haystack1, 5, haystack1 + 4, 7));
+	ok1(!memoverlaps(haystack1, 5, haystack1 + 5, 6));
+	ok1(memoverlaps(haystack1 + 4, 7, haystack1, 5));
+	ok1(!memoverlaps(haystack1 + 5, 6, haystack1, 5));
+
 	/* This exits depending on whether all tests passed */
 	return exit_status();
 }
-- 
2.4.3

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] mem: Add memswap() function
  2015-09-09 21:54 [PATCH 0/3] mem: Small enhancements David Gibson
  2015-09-09 21:54 ` [PATCH 1/3] mem: Remove array_size dependency David Gibson
  2015-09-09 21:54 ` [PATCH 2/3] mem: Add function to check whether memory ranges overlap David Gibson
@ 2015-09-09 21:54 ` David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2015-09-09 21:54 UTC (permalink / raw)
  To: ccan, rusty

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>
---
 ccan/mem/mem.c      | 24 ++++++++++++++++++++++++
 ccan/mem/mem.h      | 10 ++++++++++
 ccan/mem/test/api.c | 19 ++++++++++++++++++-
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/ccan/mem/mem.c b/ccan/mem/mem.c
index 91c9961..5b4b350 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 99c34c0..dd66cc6 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 a584c0f..d820a38 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();
 }
-- 
2.4.3

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-09-09 21:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-09 21:54 [PATCH 0/3] mem: Small enhancements David Gibson
2015-09-09 21:54 ` [PATCH 1/3] mem: Remove array_size dependency David Gibson
2015-09-09 21:54 ` [PATCH 2/3] mem: Add function to check whether memory ranges overlap David Gibson
2015-09-09 21:54 ` [PATCH 3/3] mem: Add memswap() function David Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).