* [PATCH 0/2] Small extensions to the mem module
@ 2015-09-05 4:44 David Gibson
2015-09-05 4:44 ` [PATCH 1/2] mem: Remove array_size dependency David Gibson
2015-09-05 4:44 ` [PATCH 2/2] mem: Add memswap() function David Gibson
0 siblings, 2 replies; 5+ messages in thread
From: David Gibson @ 2015-09-05 4:44 UTC (permalink / raw)
To: ccan, rusty
Two small patches for the mem module.
1/2: Remove an unused dependency
2/2: Add a memswap() function
David Gibson (2):
mem: Remove array_size dependency
mem: Add memswap() function
ccan/mem/_info | 1 -
ccan/mem/mem.c | 21 +++++++++++++++++++++
ccan/mem/mem.h | 10 ++++++++++
ccan/mem/test/api.c | 20 ++++++++++++++++++--
4 files changed, 49 insertions(+), 3 deletions(-)
--
2.4.3
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] mem: Remove array_size dependency
2015-09-05 4:44 [PATCH 0/2] Small extensions to the mem module David Gibson
@ 2015-09-05 4:44 ` David Gibson
2015-09-05 4:44 ` [PATCH 2/2] mem: Add memswap() function David Gibson
1 sibling, 0 replies; 5+ messages in thread
From: David Gibson @ 2015-09-05 4:44 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 19b2228..e37b217 100644
--- a/ccan/mem/_info
+++ b/ccan/mem/_info
@@ -22,7 +22,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 2ee15c5..d97ab28 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] 5+ messages in thread
* [PATCH 2/2] mem: Add memswap() function
2015-09-05 4:44 [PATCH 0/2] Small extensions to the mem module David Gibson
2015-09-05 4:44 ` [PATCH 1/2] mem: Remove array_size dependency David Gibson
@ 2015-09-05 4:44 ` David Gibson
2015-09-08 18:38 ` Rusty Russell
1 sibling, 1 reply; 5+ messages in thread
From: David Gibson @ 2015-09-05 4:44 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 | 21 +++++++++++++++++++++
ccan/mem/mem.h | 10 ++++++++++
ccan/mem/test/api.c | 19 ++++++++++++++++++-
3 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/ccan/mem/mem.c b/ccan/mem/mem.c
index 853f975..bab1a86 100644
--- a/ccan/mem/mem.c
+++ b/ccan/mem/mem.c
@@ -40,3 +40,24 @@ void *memrchr(const void *s, int c, size_t n)
return NULL;
}
#endif
+
+#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];
+
+ 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 dcb44b8..41120a4 100644
--- a/ccan/mem/mem.h
+++ b/ccan/mem/mem.h
@@ -15,4 +15,14 @@ void *memmem(const void *haystack, size_t haystacklen,
void *memrchr(const void *s, int c, size_t n);
#endif
+/**
+ * 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 d97ab28..8885d48 100644
--- a/ccan/mem/test/api.c
+++ b/ccan/mem/test/api.c
@@ -1,15 +1,22 @@
+#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";
char haystack2[] = "ab\0ab\0ab\0ab";
char needle1[] = "ab";
char needle2[] = "d\0e";
+ char tmp1[SWAPSIZE], tmp2[SWAPSIZE];
/* This is how many tests you plan to run */
- plan_tests(19);
+ plan_tests(21);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
@@ -36,6 +43,16 @@ int main(void)
ok1(memrchr(needle1, '\0', 2) == NULL);
+ 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] 5+ messages in thread
* Re: [PATCH 2/2] mem: Add memswap() function
2015-09-05 4:44 ` [PATCH 2/2] mem: Add memswap() function David Gibson
@ 2015-09-08 18:38 ` Rusty Russell
2015-09-09 7:37 ` David Gibson
0 siblings, 1 reply; 5+ messages in thread
From: Rusty Russell @ 2015-09-08 18:38 UTC (permalink / raw)
To: David Gibson, ccan
David Gibson <david@gibson.dropbear.id.au> writes:
> Add a memswap() function to the mem module, which exchanges two (equal
> sized, non-overlapping) memory regions.
Nice!
I wonder about a #if CCAN_MEM_DEBUG'd test which assert() tests for
no overlap.
Though maybe it's cheap enough that a simple assert() is always OK?
Cheers,
Rusty.
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] mem: Add memswap() function
2015-09-08 18:38 ` Rusty Russell
@ 2015-09-09 7:37 ` David Gibson
0 siblings, 0 replies; 5+ messages in thread
From: David Gibson @ 2015-09-09 7:37 UTC (permalink / raw)
To: Rusty Russell; +Cc: ccan
[-- Attachment #1.1: Type: text/plain, Size: 732 bytes --]
On Wed, Sep 09, 2015 at 04:08:38AM +0930, Paul 'Rusty' Russell wrote:
> David Gibson <david@gibson.dropbear.id.au> writes:
> > Add a memswap() function to the mem module, which exchanges two (equal
> > sized, non-overlapping) memory regions.
>
> Nice!
>
> I wonder about a #if CCAN_MEM_DEBUG'd test which assert() tests for
> no overlap.
>
> Though maybe it's cheap enough that a simple assert() is always OK?
Good idea. And actually.. an exposed function to tell if memory
ranges overlap might be nice too, why not.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #1.2: Type: application/pgp-signature, Size: 819 bytes --]
[-- Attachment #2: Type: text/plain, Size: 127 bytes --]
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-09-09 7:37 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-05 4:44 [PATCH 0/2] Small extensions to the mem module David Gibson
2015-09-05 4:44 ` [PATCH 1/2] mem: Remove array_size dependency David Gibson
2015-09-05 4:44 ` [PATCH 2/2] mem: Add memswap() function David Gibson
2015-09-08 18:38 ` Rusty Russell
2015-09-09 7:37 ` David Gibson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox