ccan.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] Replace TCON() with TCON_WRAP()
@ 2017-07-23 12:29 David Gibson
  2017-07-23 12:29 ` [PATCH 1/4] tlist: Use TCON_WRAP instead of TCON David Gibson
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: David Gibson @ 2017-07-23 12:29 UTC (permalink / raw)
  To: ccan

ccan/tcon has two basic ways to be used.  With TCON() which adds a
type canary to a user's existing structure, or with TCON_WRAP(), which
wraps a user's structure creating a new structure with the canaries.

TCON() is older and has several modules already using it.
Unfortunately, it uses flexible-array members which aren't allowed in
the middle of structures, except as a gcc extension.  That means with
a "normal" compiler TCON() invocations can only appear at the end of a
structure.  More importantly it also means a structure including them
can never be embedded in another structure except at the end.

In short, using TCON() is either non-portable, or badly violates
principle of least surprise.  So, this series removes use of it from
all existing ccan modules.

David Gibson (4):
  tlist: Use TCON_WRAP instead of TCON
  jset: Use TCON_WRAP instead of TCON
  jmap: Use TCON_WRAP instead of TCON
  objset: Use TCON_WRAP instead of TCON

 ccan/jmap/jmap.h      | 59 +++++++++++++++++++++++++++++----------------------
 ccan/jmap/test/run.c  |  8 +++----
 ccan/jset/jset.h      | 45 ++++++++++++++++++++++++---------------
 ccan/jset/test/run.c  |  9 ++++----
 ccan/objset/objset.h  | 31 +++++++++++++++------------
 ccan/tlist/_info      |  6 +-----
 ccan/tlist/test/run.c | 24 ++++++++++-----------
 ccan/tlist/tlist.h    | 25 +++++++++++-----------
 8 files changed, 114 insertions(+), 93 deletions(-)

-- 
2.13.3

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

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

* [PATCH 1/4] tlist: Use TCON_WRAP instead of TCON
  2017-07-23 12:29 [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
@ 2017-07-23 12:29 ` David Gibson
  2017-07-23 12:29 ` [PATCH 2/4] jset: " David Gibson
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: David Gibson @ 2017-07-23 12:29 UTC (permalink / raw)
  To: ccan

TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension.  TCON_WRAP() avoids this and so
is more portable.

This doesn't change the tlist interface, only its internals.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 ccan/tlist/_info      |  6 +-----
 ccan/tlist/test/run.c | 24 ++++++++++++------------
 ccan/tlist/tlist.h    | 25 ++++++++++++-------------
 3 files changed, 25 insertions(+), 30 deletions(-)

diff --git a/ccan/tlist/_info b/ccan/tlist/_info
index 4c3394c9..c3c116d2 100644
--- a/ccan/tlist/_info
+++ b/ccan/tlist/_info
@@ -15,11 +15,7 @@
  *	#include <stdlib.h>
  *	#include <ccan/tlist/tlist.h>
  *
- *	// We could use TLIST_TYPE(children, struct child) to define this.
- *	struct tlist_children {
- *		struct list_head raw;
- *		TCON(struct child *canary);
- *	};
+ *	TLIST_TYPE(children, struct child);
  *	struct parent {
  *		const char *name;
  *		unsigned int num_children;
diff --git a/ccan/tlist/test/run.c b/ccan/tlist/test/run.c
index d36cd8bc..739c85f0 100644
--- a/ccan/tlist/test/run.c
+++ b/ccan/tlist/test/run.c
@@ -37,10 +37,10 @@ int main(int argc, char *argv[])
 	tlist_add(&parent.children, &c2, list);
 	/* Test tlist_add and !tlist_empty. */
 	ok1(!tlist_empty(&parent.children));
-	ok1(c2.list.next == &parent.children.raw.n);
-	ok1(c2.list.prev == &parent.children.raw.n);
-	ok1(parent.children.raw.n.next == &c2.list);
-	ok1(parent.children.raw.n.prev == &c2.list);
+	ok1(c2.list.next == &tcon_unwrap(&parent.children)->n);
+	ok1(c2.list.prev == &tcon_unwrap(&parent.children)->n);
+	ok1(tcon_unwrap(&parent.children)->n.next == &c2.list);
+	ok1(tcon_unwrap(&parent.children)->n.prev == &c2.list);
 	ok1(tlist_next(&parent.children, &c2, list) == NULL);
 	ok1(tlist_prev(&parent.children, &c2, list) == NULL);
 	/* Test tlist_check */
@@ -50,12 +50,12 @@ int main(int argc, char *argv[])
 	tlist_add(&parent.children, &c1, list);
 	/* Test list_add and !list_empty. */
 	ok1(!tlist_empty(&parent.children));
-	ok1(c2.list.next == &parent.children.raw.n);
+	ok1(c2.list.next == &tcon_unwrap(&parent.children)->n);
 	ok1(c2.list.prev == &c1.list);
-	ok1(parent.children.raw.n.next == &c1.list);
-	ok1(parent.children.raw.n.prev == &c2.list);
+	ok1(tcon_unwrap(&parent.children)->n.next == &c1.list);
+	ok1(tcon_unwrap(&parent.children)->n.prev == &c2.list);
 	ok1(c1.list.next == &c2.list);
-	ok1(c1.list.prev == &parent.children.raw.n);
+	ok1(c1.list.prev == &tcon_unwrap(&parent.children)->n);
 	ok1(tlist_next(&parent.children, &c1, list) == &c2);
 	ok1(tlist_next(&parent.children, &c2, list) == NULL);
 	ok1(tlist_prev(&parent.children, &c2, list) == &c1);
@@ -67,13 +67,13 @@ int main(int argc, char *argv[])
 	tlist_add_tail(&parent.children, &c3, list);
 	/* Test list_add_tail and !list_empty. */
 	ok1(!tlist_empty(&parent.children));
-	ok1(parent.children.raw.n.next == &c1.list);
-	ok1(parent.children.raw.n.prev == &c3.list);
+	ok1(tcon_unwrap(&parent.children)->n.next == &c1.list);
+	ok1(tcon_unwrap(&parent.children)->n.prev == &c3.list);
 	ok1(c1.list.next == &c2.list);
-	ok1(c1.list.prev == &parent.children.raw.n);
+	ok1(c1.list.prev == &tcon_unwrap(&parent.children)->n);
 	ok1(c2.list.next == &c3.list);
 	ok1(c2.list.prev == &c1.list);
-	ok1(c3.list.next == &parent.children.raw.n);
+	ok1(c3.list.next == &tcon_unwrap(&parent.children)->n);
 	ok1(c3.list.prev == &c2.list);
 	ok1(tlist_next(&parent.children, &c1, list) == &c2);
 	ok1(tlist_next(&parent.children, &c2, list) == &c3);
diff --git a/ccan/tlist/tlist.h b/ccan/tlist/tlist.h
index a99191a2..37882ee7 100644
--- a/ccan/tlist/tlist.h
+++ b/ccan/tlist/tlist.h
@@ -9,10 +9,10 @@
  * @suffix: the name to use (struct tlist_@suffix)
  * @type: the type the list will contain (void for any type)
  *
- * This declares a structure "struct tlist_@suffix" to use for
- * lists containing this type.  The actual list can be accessed using
- * ".raw" or tlist_raw().  For maximum portability, place tlists
- * embedded in structures as the last member.
+ * This declares a structure "struct tlist_@suffix" to use for lists
+ * containing this type.  The actual list can be accessed using
+ * tlist_raw().  For maximum portability, place tlists embedded in
+ * structures as the last member.
  *
  * Example:
  *	// Defines struct tlist_children
@@ -30,8 +30,7 @@
  */
 #define TLIST_TYPE(suffix, type)			\
 	struct tlist_##suffix {				\
-		struct list_head raw;			\
-		TCON(type *canary);			\
+		TCON_WRAP(struct list_head, type *canary);	\
 	}
 
 /**
@@ -46,7 +45,7 @@
  * Example:
  *	static struct tlist_children my_list = TLIST_INIT(my_list);
  */
-#define TLIST_INIT(name) { LIST_HEAD_INIT(name.raw) }
+#define TLIST_INIT(name) TCON_WRAP_INIT(LIST_HEAD_INIT(*tcon_unwrap(&name)))
 
 /**
  * tlist_check - check head of a list for consistency
@@ -75,7 +74,7 @@
  *	}
  */
 #define tlist_check(h, abortstr) \
-	list_check(&(h)->raw, (abortstr))
+	list_check(tcon_unwrap(h), (abortstr))
 
 /**
  * tlist_init - initialize a tlist
@@ -88,7 +87,7 @@
  *	tlist_init(&parent->children);
  *	parent->num_children = 0;
  */
-#define tlist_init(h) list_head_init(&(h)->raw)
+#define tlist_init(h) list_head_init(tcon_unwrap(h))
 
 /**
  * tlist_raw - unwrap the typed list and check the type
@@ -99,7 +98,7 @@
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying list.
  */
-#define tlist_raw(h, expr) (&tcon_check((h), canary, (expr))->raw)
+#define tlist_raw(h, expr) tcon_unwrap(tcon_check((h), canary, (expr)))
 
 /**
  * tlist_add - add an entry at the start of a linked list.
@@ -174,7 +173,7 @@
  * Example:
  *	assert(tlist_empty(&parent->children) == (parent->num_children == 0));
  */
-#define tlist_empty(h) list_empty(&(h)->raw)
+#define tlist_empty(h) list_empty(tcon_unwrap(h))
 
 /**
  * tlist_top - get the first entry in a list
@@ -191,7 +190,7 @@
  */
 #define tlist_top(h, member)						\
 	((tcon_type((h), canary))					\
-	 list_top_(&(h)->raw,						\
+	 list_top_(tcon_unwrap((h)),					\
 		   (char *)(&(h)->_tcon[0].canary->member) -		\
 		   (char *)((h)->_tcon[0].canary)))
 
@@ -210,7 +209,7 @@
  */
 #define tlist_tail(h, member)						\
 	((tcon_type((h), canary))					\
-	 list_tail_(&(h)->raw,						\
+	 list_tail_(tcon_unwrap(h),					\
 		    (char *)(&(h)->_tcon[0].canary->member) -		\
 		    (char *)((h)->_tcon[0].canary)))
 
-- 
2.13.3

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

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

* [PATCH 2/4] jset: Use TCON_WRAP instead of TCON
  2017-07-23 12:29 [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
  2017-07-23 12:29 ` [PATCH 1/4] tlist: Use TCON_WRAP instead of TCON David Gibson
@ 2017-07-23 12:29 ` David Gibson
  2017-07-31  6:25   ` Rusty Russell
  2017-07-23 12:29 ` [PATCH 3/4] jmap: " David Gibson
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: David Gibson @ 2017-07-23 12:29 UTC (permalink / raw)
  To: ccan

TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension.  TCON_WRAP() avoids this and so
is more portable.

This doesn't change the jset interface, only its internals.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 ccan/jset/jset.h     | 45 ++++++++++++++++++++++++++++-----------------
 ccan/jset/test/run.c |  9 +++++----
 2 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/ccan/jset/jset.h b/ccan/jset/jset.h
index ba72f097..904982d6 100644
--- a/ccan/jset/jset.h
+++ b/ccan/jset/jset.h
@@ -30,8 +30,7 @@ struct jset {
  *	};
  */
 #define JSET_MEMBERS(type)			\
-	struct jset raw;			\
-	TCON(type canary)
+	TCON_WRAP(struct jset, type canary) jset_
 
 /**
  * jset_new - create a new, empty jset.
@@ -47,6 +46,14 @@ struct jset {
  */
 #define jset_new(type) ((type *)jset_new_(sizeof(type)))
 
+
+/**
+ * jset_raw_ - unwrap the typed set (without type checking)
+ * @set: the typed jset
+ */
+#define jset_raw_(set)	(tcon_unwrap(&(set)->jset_))
+
+
 /**
  * jset_free - destroy a jset.
  * @set: the set returned from jset_new.
@@ -54,7 +61,7 @@ struct jset {
  * Example:
  *	jset_free(set);
  */
-#define jset_free(set) jset_free_(&(set)->raw)
+#define jset_free(set) jset_free_(jset_raw_(set))
 
 /**
  * jset_error - test for an error in the a previous jset_ operation.
@@ -74,8 +81,8 @@ struct jset {
  *	if (errstr)
  *		errx(1, "Woah, error on newly created set?! %s", errstr);
  */
-#define jset_error(set) \
-	jset_error_(&(set)->raw)
+#define jset_error(set) jset_error_(jset_raw_(set))
+
 
 /**
  * jset_raw - unwrap the typed set and check the type
@@ -86,7 +93,9 @@ struct jset {
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jset.
  */
-#define jset_raw(set, expr) (&tcon_check((set), canary, (expr))->raw)
+#define jset_raw(set, expr) \
+	(tcon_unwrap(tcon_check(&(set)->jset_, canary, (expr))))
+
 
 /**
  * jset_test - test a bit in the bitset.
@@ -137,8 +146,8 @@ struct jset {
  *	// We expect 1000 entries.
  *	assert(jset_count(set) == 1000);
  */
-#define jset_count(set)				\
-	jset_popcount_(&(set)->raw, 0, -1UL)
+#define jset_count(set)	\
+	jset_popcount_(jset_raw_(set), 0, -1UL)
 
 /**
  * jset_popcount - get population of (some part of) bitset.
@@ -186,7 +195,7 @@ struct jset {
  *	}
  */
 #define jset_nth(set, n, invalid)					\
-	tcon_cast((set), canary,					\
+	tcon_cast(&(set)->jset_, canary,				\
 		  jset_nth_(jset_raw((set), (invalid)),			\
 			    (n), (unsigned long)(invalid)))
 
@@ -205,7 +214,7 @@ struct jset {
  *	printf("\n");
  */
 #define jset_first(set)						\
-	tcon_cast((set), canary, jset_first_(&(set)->raw))
+	tcon_cast(&(set)->jset_, canary, jset_first_(jset_raw_(set)))
 
 /**
  * jset_next - return the next bit which is set (must not contain 0).
@@ -216,7 +225,8 @@ struct jset {
  * jset_first.
  */
 #define jset_next(set, prev)						\
-	tcon_cast((set), canary, jset_next_(&(set)->raw, (unsigned long)(prev)))
+	tcon_cast(&(set)->jset_, canary,				\
+		  jset_next_(jset_raw_(set), (unsigned long)(prev)))
 
 /**
  * jset_last - return the last bit which is set (must not contain 0).
@@ -230,7 +240,7 @@ struct jset {
  *	printf("\n");
  */
 #define jset_last(set)						\
-	tcon_cast((set), canary, jset_last_(&(set)->raw))
+	tcon_cast(&(set)->jset_, canary, jset_last_(jset_raw_(set)))
 
 /**
  * jset_prev - return the previous bit which is set (must not contain 0).
@@ -241,7 +251,8 @@ struct jset {
  * jset_last.
  */
 #define jset_prev(set, prev)						\
-	tcon_cast((set), canary, jset_prev_(&(set)->raw, (unsigned long)(prev)))
+	tcon_cast(&(set)->jset_, canary,				\
+		  jset_prev_(jset_raw_(set), (unsigned long)(prev)))
 
 /**
  * jset_first_clear - return the first bit which is unset
@@ -251,17 +262,17 @@ struct jset {
  * set is full.
  */
 #define jset_first_clear(set)						\
-	tcon_cast((set), canary, jset_next_clear_(&(set)->raw, 0))
+	tcon_cast(&(set)->jset_, canary, jset_next_clear_(jset_raw_(set), 0))
 
 #define jset_next_clear(set, prev)					\
-	tcon_cast((set), canary, jset_next_clear_(&(set)->raw,		\
+	tcon_cast(&(set)->jset_, canary, jset_next_clear_(jset_raw_(set), \
 						  (unsigned long)(prev)))
 
 #define jset_last_clear(set)					\
-	tcon_cast((set), canary, jset_last_clear_(&(set)->raw))
+	tcon_cast(&(set)->jset_, canary, jset_last_clear_(jset_raw_(set)))
 
 #define jset_prev_clear(set, prev)					\
-	tcon_cast((set), canary, jset_prev_clear_(&(set)->raw,		\
+	tcon_cast(&(set)->jset_, canary, jset_prev_clear_(jset_raw_(set), \
 						  (unsigned long)(prev)))
 
 /* Raw functions */
diff --git a/ccan/jset/test/run.c b/ccan/jset/test/run.c
index a0fb8a8c..425546fd 100644
--- a/ccan/jset/test/run.c
+++ b/ccan/jset/test/run.c
@@ -34,7 +34,8 @@ int main(int argc, char *argv[])
 		jset_set(set, 1 + (i << 4));
 
 	/* This only take 1.7MB on my 32-bit system. */
-	diag("%u bytes memory used\n", (unsigned)Judy1MemUsed(set->raw.judy));
+	diag("%u bytes memory used\n",
+	     (unsigned)Judy1MemUsed(jset_raw_(set)->judy));
 
 	ok1(jset_popcount(set, 0, -1) == 1000000);
 	ok1(jset_nth(set, 0, -1) == 1);
@@ -53,13 +54,13 @@ int main(int argc, char *argv[])
 	ok1(jset_error(set) == NULL);
 
 	/* Test error handling */
-	JU_ERRNO(&set->raw.err) = 100;
-	JU_ERRID(&set->raw.err) = 991;
+	JU_ERRNO(&jset_raw_(set)->err) = 100;
+	JU_ERRID(&jset_raw_(set)->err) = 991;
 	err = jset_error(set);
 	ok1(err);
 	ok1(strstr(err, "100"));
 	ok1(strstr(err, "991"));
-	ok1(err == set->raw.errstr);
+	ok1(err == jset_raw_(set)->errstr);
 	jset_free(set);
 
 	return exit_status();
-- 
2.13.3

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

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

* [PATCH 3/4] jmap: Use TCON_WRAP instead of TCON
  2017-07-23 12:29 [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
  2017-07-23 12:29 ` [PATCH 1/4] tlist: Use TCON_WRAP instead of TCON David Gibson
  2017-07-23 12:29 ` [PATCH 2/4] jset: " David Gibson
@ 2017-07-23 12:29 ` David Gibson
  2017-07-23 12:29 ` [PATCH 4/4] objset: " David Gibson
  2017-07-27  7:27 ` [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
  4 siblings, 0 replies; 8+ messages in thread
From: David Gibson @ 2017-07-23 12:29 UTC (permalink / raw)
  To: ccan

TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension.  TCON_WRAP() avoids this and so
is more portable.

This doesn't change the jmap interface, only its internals.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 ccan/jmap/jmap.h     | 59 ++++++++++++++++++++++++++++++----------------------
 ccan/jmap/test/run.c |  8 +++----
 2 files changed, 38 insertions(+), 29 deletions(-)

diff --git a/ccan/jmap/jmap.h b/ccan/jmap/jmap.h
index e30a6ece..40147ace 100644
--- a/ccan/jmap/jmap.h
+++ b/ccan/jmap/jmap.h
@@ -41,8 +41,7 @@ struct jmap {
  *	};
  */
 #define JMAP_MEMBERS(itype, ctype)		\
-	struct jmap raw;			\
-	TCON(itype icanary; ctype ccanary)
+	TCON_WRAP(struct jmap, itype icanary; ctype ccanary) jmap_
 
 /**
  * jmap_new - create a new, empty jmap.
@@ -62,13 +61,19 @@ struct jmap {
 #define jmap_new(type) ((type *)jmap_new_(sizeof(type)))
 
 /**
+ * jmap_raw_ - unwrap the typed map (no type checking)
+ * @map: the typed jmap
+ */
+#define jmap_raw_(map)	tcon_unwrap(&(map)->jmap_)
+
+/**
  * jmap_free - destroy a jmap.
  * @map: the map returned from jmap_new.
  *
  * Example:
  *	jmap_free(map);
  */
-#define jmap_free(map) jmap_free_(&(map)->raw)
+#define jmap_free(map) jmap_free_(jmap_raw_(map))
 
 /**
  * jmap_error - test for an error in the a previous jmap_ operation.
@@ -88,7 +93,7 @@ struct jmap {
  *	if (errstr)
  *		errx(1, "Woah, error on newly created map?! %s", errstr);
  */
-#define jmap_error(map) jmap_error_(&(map)->raw)
+#define jmap_error(map) jmap_error_(jmap_raw_(map))
 
 /**
  * jmap_rawi - unwrap the typed map and check the index type
@@ -99,7 +104,8 @@ struct jmap {
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
-#define jmap_rawi(map, expr) (&tcon_check((map), icanary, (expr))->raw)
+#define jmap_rawi(map, expr) \
+	tcon_unwrap(tcon_check(&(map)->jmap_, icanary, (expr)))
 
 /**
  * jmap_rawc - unwrap the typed map and check the contents type
@@ -110,7 +116,8 @@ struct jmap {
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
-#define jmap_rawc(map, expr) (&tcon_check((map), ccanary, (expr))->raw)
+#define jmap_rawc(map, expr) \
+	tcon_unwrap(tcon_check(&(map)->jmap_, ccanary, (expr)))
 
 /**
  * jmap_rawci - unwrap the typed map and check the index and contents types
@@ -122,8 +129,9 @@ struct jmap {
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
-#define jmap_rawci(map, iexpr, cexpr) \
-	(&tcon_check(tcon_check((map), ccanary, (cexpr)), icanary, (iexpr))->raw)
+#define jmap_rawci(map, iexpr, cexpr)				\
+	tcon_unwrap(tcon_check(tcon_check(&(map)->jmap_,\
+					  ccanary, (cexpr)), icanary, (iexpr)))
 
 /**
  * jmap_add - add or replace a value for a given index in the map.
@@ -199,7 +207,7 @@ struct jmap {
  *	jmap_getval()
  */
 #define jmap_get(map, index)						\
-	tcon_cast((map), ccanary,					\
+	tcon_cast(&(map)->jmap_, ccanary,				\
 		  jmap_get_(jmap_rawi((map), (index)), (unsigned long)(index)))
 
 /**
@@ -210,7 +218,7 @@ struct jmap {
  *	assert(jmap_count(map) < 1000);
  */
 #define jmap_count(map)				\
-	jmap_popcount_(&(map)->raw, 0, -1UL)
+	jmap_popcount_(jmap_raw_(map), 0, -1UL)
 
 /**
  * jmap_popcount - get population of (some part of) the map.
@@ -249,8 +257,8 @@ struct jmap {
  *	jmap_nthval();
  */
 #define jmap_nth(map, n, invalid)					\
-	tcon_cast((map), icanary,					\
-		  jmap_nth_(jmap_rawi((map), (invalid)),			\
+	tcon_cast(&(map)->jmap_, icanary,				\
+		  jmap_nth_(jmap_rawi((map), (invalid)),		\
 			    (n), (unsigned long)(invalid)))
 
 /**
@@ -270,7 +278,7 @@ struct jmap {
  *	jmap_firstval()
  */
 #define jmap_first(map)						\
-	tcon_cast((map), icanary, jmap_first_(&(map)->raw))
+	tcon_cast(&(map)->jmap_, icanary, jmap_first_(jmap_raw_(map)))
 
 /**
  * jmap_next - return the next index in the map.
@@ -282,8 +290,8 @@ struct jmap {
  *	jmap_nextval()
  */
 #define jmap_next(map, prev)						\
-	tcon_cast((map), icanary, jmap_next_(jmap_rawi((map), (prev)),	\
-					     (unsigned long)(prev)))
+	tcon_cast(&(map)->jmap_, icanary, jmap_next_(jmap_rawi((map), (prev)),	\
+						     (unsigned long)(prev)))
 
 /**
  * jmap_last - return the last index in the map.
@@ -301,7 +309,7 @@ struct jmap {
  *	jmap_lastval()
  */
 #define jmap_last(map)						\
-	tcon_cast((map), icanary, jmap_last_(&(map)->raw))
+	tcon_cast(&(map)->jmap_, icanary, jmap_last_(jmap_raw_(map)))
 
 /**
  * jmap_prev - return the previous index in the map (must not contain 0)
@@ -315,7 +323,8 @@ struct jmap {
  *	jmap_prevval()
  */
 #define jmap_prev(map, prev)						\
-	tcon_cast((map), icanary, jmap_prev_(jmap_rawi((map), (prev)), (prev)))
+	tcon_cast(&(map)->jmap_, icanary,				\
+		  jmap_prev_(jmap_rawi((map), (prev)), (prev)))
 
 /**
  * jmap_getval - access a value in-place for a given index.
@@ -346,7 +355,7 @@ struct jmap {
  *	jmap_putval(), jmap_firstval()
  */
 #define jmap_getval(map, index)						\
-	tcon_cast_ptr((map), ccanary,					\
+	tcon_cast_ptr(&(map)->jmap_, ccanary,				\
 		      jmap_getval_(jmap_rawi((map), (index)),		\
 				   (unsigned long)(index)))
 
@@ -393,7 +402,7 @@ struct jmap {
  *	jmap_nth();
  */
 #define jmap_nthval(map, n, index)					\
-	tcon_cast_ptr((map), ccanary,					\
+	tcon_cast_ptr(&(map)->jmap_, ccanary,				\
 		      jmap_nthval_(jmap_rawi((map), *(index)), (n), (index)))
 
 /**
@@ -415,8 +424,8 @@ struct jmap {
  * See Also:
  *	jmap_first, jmap_nextval()
  */
-#define jmap_firstval(map, index)	      \
-	tcon_cast_ptr((map), ccanary,				\
+#define jmap_firstval(map, index)					\
+	tcon_cast_ptr(&(map)->jmap_, ccanary,				\
 		      jmap_firstval_(jmap_rawi((map), *(index)),	\
 				     (unsigned long *)(index)))
 
@@ -432,7 +441,7 @@ struct jmap {
  *	jmap_firstval(), jmap_putval()
  */
 #define jmap_nextval(map, index)				\
-	tcon_cast_ptr((map), ccanary,				\
+	tcon_cast_ptr(&(map)->jmap_, ccanary,			\
 		      jmap_nextval_(jmap_rawi((map), *(index)),	\
 				    (unsigned long *)(index)))
 
@@ -445,8 +454,8 @@ struct jmap {
  * See Also:
  *	jmap_last(), jmap_putval()
  */
-#define jmap_lastval(map, index)	      \
-	tcon_cast_ptr((map), ccanary,				\
+#define jmap_lastval(map, index)				\
+	tcon_cast_ptr(&(map)->jmap_, ccanary,			\
 		      jmap_lastval_(jmap_rawi((map), *(index)),	\
 				    (unsigned long *)(index)))
 
@@ -463,7 +472,7 @@ struct jmap {
  *	jmap_lastval(), jmap_putval()
  */
 #define jmap_prevval(map, index)				\
-	tcon_cast_ptr((map), ccanary,				\
+	tcon_cast_ptr(&(map)->jmap_, ccanary,			\
 		      jmap_prevval_(jmap_rawi((map), *(index)),	\
 				    (unsigned long *)(index)))
 
diff --git a/ccan/jmap/test/run.c b/ccan/jmap/test/run.c
index 3a4249dc..490b706f 100644
--- a/ccan/jmap/test/run.c
+++ b/ccan/jmap/test/run.c
@@ -38,7 +38,7 @@ int main(int argc, char *argv[])
 		jmap_add(map, (i << 4) + 1, (i << 5) + 1);
 
 	/* This only take 6.3MB on my 32-bit system. */
-	diag("%u bytes memory used\n", (unsigned)JudyLMemUsed(map->raw.judy));
+	diag("%u bytes memory used\n", (unsigned)JudyLMemUsed(jmap_raw_(map)));
 
 	ok1(jmap_get(map, 1) == 1);
 	ok1(jmap_get(map, (999999 << 4) + 1) == (999999 << 5) + 1);
@@ -102,13 +102,13 @@ int main(int argc, char *argv[])
 	jmap_putval(map, &value);
 
 	/* Test error handling */
-	JU_ERRNO(&map->raw.err) = 100;
-	JU_ERRID(&map->raw.err) = 991;
+	JU_ERRNO(&jmap_raw_(map)->err) = 100;
+	JU_ERRID(&jmap_raw_(map)->err) = 991;
 	err = jmap_error(map);
 	ok1(err);
 	ok1(strstr(err, "100"));
 	ok1(strstr(err, "991"));
-	ok1(err == map->raw.errstr);
+	ok1(err == jmap_raw_(map)->errstr);
 	jmap_free(map);
 
 	return exit_status();
-- 
2.13.3

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

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

* [PATCH 4/4] objset: Use TCON_WRAP instead of TCON
  2017-07-23 12:29 [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
                   ` (2 preceding siblings ...)
  2017-07-23 12:29 ` [PATCH 3/4] jmap: " David Gibson
@ 2017-07-23 12:29 ` David Gibson
  2017-07-27  7:27 ` [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
  4 siblings, 0 replies; 8+ messages in thread
From: David Gibson @ 2017-07-23 12:29 UTC (permalink / raw)
  To: ccan

TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension.  TCON_WRAP() avoids this and so
is more portable.

This doesn't change the objset interface, only its internals.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 ccan/objset/objset.h | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/ccan/objset/objset.h b/ccan/objset/objset.h
index 2492a520..9444d48c 100644
--- a/ccan/objset/objset.h
+++ b/ccan/objset/objset.h
@@ -36,8 +36,10 @@ HTABLE_DEFINE_TYPE(void, objset_key_, objset_hashfn_, objset_eqfn_, objset_h);
  *	};
  */
 #define OBJSET_MEMBERS(type)			\
-	struct objset_h raw;			\
-	TCON(type canary)
+	TCON_WRAP(struct objset_h, type canary) objset_
+
+#define objset_raw(set)				\
+	tcon_unwrap(&(set)->objset_)
 
 /**
  * objset_init - initialize an empty objset
@@ -48,7 +50,7 @@ HTABLE_DEFINE_TYPE(void, objset_key_, objset_hashfn_, objset_eqfn_, objset_h);
  *
  *	objset_init(&set);
  */
-#define objset_init(set) objset_h_init(&(set)->raw)
+#define objset_init(set) objset_h_init(objset_raw(set))
 
 /**
  * objset_empty - is this set empty?
@@ -58,7 +60,7 @@ HTABLE_DEFINE_TYPE(void, objset_key_, objset_hashfn_, objset_eqfn_, objset_h);
  *	if (!objset_empty(&set))
  *		abort();
  */
-#define objset_empty(set) objset_empty_(&(set)->raw)
+#define objset_empty(set) objset_empty_(objset_raw(set))
 
 static inline bool objset_empty_(const struct objset_h *set)
 {
@@ -83,7 +85,7 @@ static inline bool objset_empty_(const struct objset_h *set)
  *		printf("Impossible: value was already in the set?\n");
  */
 #define objset_add(set, value)						\
-	objset_h_add(&tcon_check((set), canary, (value))->raw, (void *)(value))
+	objset_h_add(tcon_unwrap(tcon_check(&(set)->objset_, canary, (value))), (void *)(value))
 
 /**
  * objset_get - get a value from a set
@@ -96,8 +98,9 @@ static inline bool objset_empty_(const struct objset_h *set)
  *	if (objset_get(&set, val));
  *		printf("hello => %i\n", *val);
  */
-#define objset_get(set, member) \
-	tcon_cast((set), canary, objset_h_get(&(set)->raw, (member)))
+#define objset_get(set, member)					\
+	tcon_cast(&(set)->objset_, canary,			\
+		  objset_h_get(objset_raw(set), (member)))
 
 /**
  * objset_del - remove a member from the set.
@@ -112,7 +115,7 @@ static inline bool objset_empty_(const struct objset_h *set)
  *		printf("val was not in the set?\n");
  */
 #define objset_del(set, value)						\
-	objset_h_del(&tcon_check((set), canary, value)->raw,		\
+	objset_h_del(tcon_unwrap(tcon_check(&(set)->objset_, canary, value)), \
 		     (const void *)value)
 
 /**
@@ -124,7 +127,7 @@ static inline bool objset_empty_(const struct objset_h *set)
  * Example:
  *	objset_clear(&set);
  */
-#define objset_clear(set) objset_h_clear(&(set)->raw)
+#define objset_clear(set) objset_h_clear(objset_raw(set))
 
 /**
  * objset_iter - iterator reference.
@@ -149,8 +152,9 @@ struct objset_iter {
  *	if (v)
  *		printf("One value is %i\n", *v);
  */
-#define objset_first(set, i) \
-	tcon_cast((set), canary, objset_h_first(&(set)->raw, &(i)->iter))
+#define objset_first(set, i)					\
+	tcon_cast(&(set)->objset_, canary,			\
+		  objset_h_first(objset_raw(set), &(i)->iter))
 
 /**
  * objset_next - get the another element in the set
@@ -167,7 +171,8 @@ struct objset_iter {
  *			printf("Another value is %i\n", *v);
  *	}
  */
-#define objset_next(set, i) \
-	tcon_cast((set), canary, objset_h_next(&(set)->raw, &(i)->iter))
+#define objset_next(set, i)					\
+	tcon_cast(&(set)->objset_, canary,			\
+		  objset_h_next(objset_raw(set), &(i)->iter))
 
 #endif /* CCAN_OBJSET_H */
-- 
2.13.3

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

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

* Re: [PATCH 0/4] Replace TCON() with TCON_WRAP()
  2017-07-23 12:29 [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
                   ` (3 preceding siblings ...)
  2017-07-23 12:29 ` [PATCH 4/4] objset: " David Gibson
@ 2017-07-27  7:27 ` David Gibson
  4 siblings, 0 replies; 8+ messages in thread
From: David Gibson @ 2017-07-27  7:27 UTC (permalink / raw)
  To: ccan


[-- Attachment #1.1: Type: text/plain, Size: 1855 bytes --]

On Sun, Jul 23, 2017 at 10:29:03PM +1000, David Gibson wrote:
> ccan/tcon has two basic ways to be used.  With TCON() which adds a
> type canary to a user's existing structure, or with TCON_WRAP(), which
> wraps a user's structure creating a new structure with the canaries.
> 
> TCON() is older and has several modules already using it.
> Unfortunately, it uses flexible-array members which aren't allowed in
> the middle of structures, except as a gcc extension.  That means with
> a "normal" compiler TCON() invocations can only appear at the end of a
> structure.  More importantly it also means a structure including them
> can never be embedded in another structure except at the end.
> 
> In short, using TCON() is either non-portable, or badly violates
> principle of least surprise.  So, this series removes use of it from
> all existing ccan modules.

Rusty,  any thoughts on this?

Unless there's an objection, I'm inclined to commit it.

> David Gibson (4):
>   tlist: Use TCON_WRAP instead of TCON
>   jset: Use TCON_WRAP instead of TCON
>   jmap: Use TCON_WRAP instead of TCON
>   objset: Use TCON_WRAP instead of TCON
> 
>  ccan/jmap/jmap.h      | 59 +++++++++++++++++++++++++++++----------------------
>  ccan/jmap/test/run.c  |  8 +++----
>  ccan/jset/jset.h      | 45 ++++++++++++++++++++++++---------------
>  ccan/jset/test/run.c  |  9 ++++----
>  ccan/objset/objset.h  | 31 +++++++++++++++------------
>  ccan/tlist/_info      |  6 +-----
>  ccan/tlist/test/run.c | 24 ++++++++++-----------
>  ccan/tlist/tlist.h    | 25 +++++++++++-----------
>  8 files changed, 114 insertions(+), 93 deletions(-)
> 

-- 
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: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 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] 8+ messages in thread

* Re: [PATCH 2/4] jset: Use TCON_WRAP instead of TCON
  2017-07-23 12:29 ` [PATCH 2/4] jset: " David Gibson
@ 2017-07-31  6:25   ` Rusty Russell
  2017-08-01  5:17     ` David Gibson
  0 siblings, 1 reply; 8+ messages in thread
From: Rusty Russell @ 2017-07-31  6:25 UTC (permalink / raw)
  To: David Gibson, ccan

OK, these all look good.

Please apply!
Rusty.

David Gibson <david@gibson.dropbear.id.au> writes:
> TCON() uses flexible-array members which aren't allowed in the middle
> of structures, except as a gcc extension.  TCON_WRAP() avoids this and so
> is more portable.
>
> This doesn't change the jset interface, only its internals.
>
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  ccan/jset/jset.h     | 45 ++++++++++++++++++++++++++++-----------------
>  ccan/jset/test/run.c |  9 +++++----
>  2 files changed, 33 insertions(+), 21 deletions(-)
>
> diff --git a/ccan/jset/jset.h b/ccan/jset/jset.h
> index ba72f097..904982d6 100644
> --- a/ccan/jset/jset.h
> +++ b/ccan/jset/jset.h
> @@ -30,8 +30,7 @@ struct jset {
>   *	};
>   */
>  #define JSET_MEMBERS(type)			\
> -	struct jset raw;			\
> -	TCON(type canary)
> +	TCON_WRAP(struct jset, type canary) jset_
>  
>  /**
>   * jset_new - create a new, empty jset.
> @@ -47,6 +46,14 @@ struct jset {
>   */
>  #define jset_new(type) ((type *)jset_new_(sizeof(type)))
>  
> +
> +/**
> + * jset_raw_ - unwrap the typed set (without type checking)
> + * @set: the typed jset
> + */
> +#define jset_raw_(set)	(tcon_unwrap(&(set)->jset_))
> +
> +
>  /**
>   * jset_free - destroy a jset.
>   * @set: the set returned from jset_new.
> @@ -54,7 +61,7 @@ struct jset {
>   * Example:
>   *	jset_free(set);
>   */
> -#define jset_free(set) jset_free_(&(set)->raw)
> +#define jset_free(set) jset_free_(jset_raw_(set))
>  
>  /**
>   * jset_error - test for an error in the a previous jset_ operation.
> @@ -74,8 +81,8 @@ struct jset {
>   *	if (errstr)
>   *		errx(1, "Woah, error on newly created set?! %s", errstr);
>   */
> -#define jset_error(set) \
> -	jset_error_(&(set)->raw)
> +#define jset_error(set) jset_error_(jset_raw_(set))
> +
>  
>  /**
>   * jset_raw - unwrap the typed set and check the type
> @@ -86,7 +93,9 @@ struct jset {
>   * variable is of an unexpected type.  It is used internally where we
>   * need to access the raw underlying jset.
>   */
> -#define jset_raw(set, expr) (&tcon_check((set), canary, (expr))->raw)
> +#define jset_raw(set, expr) \
> +	(tcon_unwrap(tcon_check(&(set)->jset_, canary, (expr))))
> +
>  
>  /**
>   * jset_test - test a bit in the bitset.
> @@ -137,8 +146,8 @@ struct jset {
>   *	// We expect 1000 entries.
>   *	assert(jset_count(set) == 1000);
>   */
> -#define jset_count(set)				\
> -	jset_popcount_(&(set)->raw, 0, -1UL)
> +#define jset_count(set)	\
> +	jset_popcount_(jset_raw_(set), 0, -1UL)
>  
>  /**
>   * jset_popcount - get population of (some part of) bitset.
> @@ -186,7 +195,7 @@ struct jset {
>   *	}
>   */
>  #define jset_nth(set, n, invalid)					\
> -	tcon_cast((set), canary,					\
> +	tcon_cast(&(set)->jset_, canary,				\
>  		  jset_nth_(jset_raw((set), (invalid)),			\
>  			    (n), (unsigned long)(invalid)))
>  
> @@ -205,7 +214,7 @@ struct jset {
>   *	printf("\n");
>   */
>  #define jset_first(set)						\
> -	tcon_cast((set), canary, jset_first_(&(set)->raw))
> +	tcon_cast(&(set)->jset_, canary, jset_first_(jset_raw_(set)))
>  
>  /**
>   * jset_next - return the next bit which is set (must not contain 0).
> @@ -216,7 +225,8 @@ struct jset {
>   * jset_first.
>   */
>  #define jset_next(set, prev)						\
> -	tcon_cast((set), canary, jset_next_(&(set)->raw, (unsigned long)(prev)))
> +	tcon_cast(&(set)->jset_, canary,				\
> +		  jset_next_(jset_raw_(set), (unsigned long)(prev)))
>  
>  /**
>   * jset_last - return the last bit which is set (must not contain 0).
> @@ -230,7 +240,7 @@ struct jset {
>   *	printf("\n");
>   */
>  #define jset_last(set)						\
> -	tcon_cast((set), canary, jset_last_(&(set)->raw))
> +	tcon_cast(&(set)->jset_, canary, jset_last_(jset_raw_(set)))
>  
>  /**
>   * jset_prev - return the previous bit which is set (must not contain 0).
> @@ -241,7 +251,8 @@ struct jset {
>   * jset_last.
>   */
>  #define jset_prev(set, prev)						\
> -	tcon_cast((set), canary, jset_prev_(&(set)->raw, (unsigned long)(prev)))
> +	tcon_cast(&(set)->jset_, canary,				\
> +		  jset_prev_(jset_raw_(set), (unsigned long)(prev)))
>  
>  /**
>   * jset_first_clear - return the first bit which is unset
> @@ -251,17 +262,17 @@ struct jset {
>   * set is full.
>   */
>  #define jset_first_clear(set)						\
> -	tcon_cast((set), canary, jset_next_clear_(&(set)->raw, 0))
> +	tcon_cast(&(set)->jset_, canary, jset_next_clear_(jset_raw_(set), 0))
>  
>  #define jset_next_clear(set, prev)					\
> -	tcon_cast((set), canary, jset_next_clear_(&(set)->raw,		\
> +	tcon_cast(&(set)->jset_, canary, jset_next_clear_(jset_raw_(set), \
>  						  (unsigned long)(prev)))
>  
>  #define jset_last_clear(set)					\
> -	tcon_cast((set), canary, jset_last_clear_(&(set)->raw))
> +	tcon_cast(&(set)->jset_, canary, jset_last_clear_(jset_raw_(set)))
>  
>  #define jset_prev_clear(set, prev)					\
> -	tcon_cast((set), canary, jset_prev_clear_(&(set)->raw,		\
> +	tcon_cast(&(set)->jset_, canary, jset_prev_clear_(jset_raw_(set), \
>  						  (unsigned long)(prev)))
>  
>  /* Raw functions */
> diff --git a/ccan/jset/test/run.c b/ccan/jset/test/run.c
> index a0fb8a8c..425546fd 100644
> --- a/ccan/jset/test/run.c
> +++ b/ccan/jset/test/run.c
> @@ -34,7 +34,8 @@ int main(int argc, char *argv[])
>  		jset_set(set, 1 + (i << 4));
>  
>  	/* This only take 1.7MB on my 32-bit system. */
> -	diag("%u bytes memory used\n", (unsigned)Judy1MemUsed(set->raw.judy));
> +	diag("%u bytes memory used\n",
> +	     (unsigned)Judy1MemUsed(jset_raw_(set)->judy));
>  
>  	ok1(jset_popcount(set, 0, -1) == 1000000);
>  	ok1(jset_nth(set, 0, -1) == 1);
> @@ -53,13 +54,13 @@ int main(int argc, char *argv[])
>  	ok1(jset_error(set) == NULL);
>  
>  	/* Test error handling */
> -	JU_ERRNO(&set->raw.err) = 100;
> -	JU_ERRID(&set->raw.err) = 991;
> +	JU_ERRNO(&jset_raw_(set)->err) = 100;
> +	JU_ERRID(&jset_raw_(set)->err) = 991;
>  	err = jset_error(set);
>  	ok1(err);
>  	ok1(strstr(err, "100"));
>  	ok1(strstr(err, "991"));
> -	ok1(err == set->raw.errstr);
> +	ok1(err == jset_raw_(set)->errstr);
>  	jset_free(set);
>  
>  	return exit_status();
> -- 
> 2.13.3
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

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

* Re: [PATCH 2/4] jset: Use TCON_WRAP instead of TCON
  2017-07-31  6:25   ` Rusty Russell
@ 2017-08-01  5:17     ` David Gibson
  0 siblings, 0 replies; 8+ messages in thread
From: David Gibson @ 2017-08-01  5:17 UTC (permalink / raw)
  To: Rusty Russell; +Cc: ccan


[-- Attachment #1.1: Type: text/plain, Size: 6941 bytes --]

On Mon, Jul 31, 2017 at 03:55:14PM +0930, Paul 'Rusty' Russell wrote:
> OK, these all look good.
> 
> Please apply!

Done.

> Rusty.
> 
> David Gibson <david@gibson.dropbear.id.au> writes:
> > TCON() uses flexible-array members which aren't allowed in the middle
> > of structures, except as a gcc extension.  TCON_WRAP() avoids this and so
> > is more portable.
> >
> > This doesn't change the jset interface, only its internals.
> >
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  ccan/jset/jset.h     | 45 ++++++++++++++++++++++++++++-----------------
> >  ccan/jset/test/run.c |  9 +++++----
> >  2 files changed, 33 insertions(+), 21 deletions(-)
> >
> > diff --git a/ccan/jset/jset.h b/ccan/jset/jset.h
> > index ba72f097..904982d6 100644
> > --- a/ccan/jset/jset.h
> > +++ b/ccan/jset/jset.h
> > @@ -30,8 +30,7 @@ struct jset {
> >   *	};
> >   */
> >  #define JSET_MEMBERS(type)			\
> > -	struct jset raw;			\
> > -	TCON(type canary)
> > +	TCON_WRAP(struct jset, type canary) jset_
> >  
> >  /**
> >   * jset_new - create a new, empty jset.
> > @@ -47,6 +46,14 @@ struct jset {
> >   */
> >  #define jset_new(type) ((type *)jset_new_(sizeof(type)))
> >  
> > +
> > +/**
> > + * jset_raw_ - unwrap the typed set (without type checking)
> > + * @set: the typed jset
> > + */
> > +#define jset_raw_(set)	(tcon_unwrap(&(set)->jset_))
> > +
> > +
> >  /**
> >   * jset_free - destroy a jset.
> >   * @set: the set returned from jset_new.
> > @@ -54,7 +61,7 @@ struct jset {
> >   * Example:
> >   *	jset_free(set);
> >   */
> > -#define jset_free(set) jset_free_(&(set)->raw)
> > +#define jset_free(set) jset_free_(jset_raw_(set))
> >  
> >  /**
> >   * jset_error - test for an error in the a previous jset_ operation.
> > @@ -74,8 +81,8 @@ struct jset {
> >   *	if (errstr)
> >   *		errx(1, "Woah, error on newly created set?! %s", errstr);
> >   */
> > -#define jset_error(set) \
> > -	jset_error_(&(set)->raw)
> > +#define jset_error(set) jset_error_(jset_raw_(set))
> > +
> >  
> >  /**
> >   * jset_raw - unwrap the typed set and check the type
> > @@ -86,7 +93,9 @@ struct jset {
> >   * variable is of an unexpected type.  It is used internally where we
> >   * need to access the raw underlying jset.
> >   */
> > -#define jset_raw(set, expr) (&tcon_check((set), canary, (expr))->raw)
> > +#define jset_raw(set, expr) \
> > +	(tcon_unwrap(tcon_check(&(set)->jset_, canary, (expr))))
> > +
> >  
> >  /**
> >   * jset_test - test a bit in the bitset.
> > @@ -137,8 +146,8 @@ struct jset {
> >   *	// We expect 1000 entries.
> >   *	assert(jset_count(set) == 1000);
> >   */
> > -#define jset_count(set)				\
> > -	jset_popcount_(&(set)->raw, 0, -1UL)
> > +#define jset_count(set)	\
> > +	jset_popcount_(jset_raw_(set), 0, -1UL)
> >  
> >  /**
> >   * jset_popcount - get population of (some part of) bitset.
> > @@ -186,7 +195,7 @@ struct jset {
> >   *	}
> >   */
> >  #define jset_nth(set, n, invalid)					\
> > -	tcon_cast((set), canary,					\
> > +	tcon_cast(&(set)->jset_, canary,				\
> >  		  jset_nth_(jset_raw((set), (invalid)),			\
> >  			    (n), (unsigned long)(invalid)))
> >  
> > @@ -205,7 +214,7 @@ struct jset {
> >   *	printf("\n");
> >   */
> >  #define jset_first(set)						\
> > -	tcon_cast((set), canary, jset_first_(&(set)->raw))
> > +	tcon_cast(&(set)->jset_, canary, jset_first_(jset_raw_(set)))
> >  
> >  /**
> >   * jset_next - return the next bit which is set (must not contain 0).
> > @@ -216,7 +225,8 @@ struct jset {
> >   * jset_first.
> >   */
> >  #define jset_next(set, prev)						\
> > -	tcon_cast((set), canary, jset_next_(&(set)->raw, (unsigned long)(prev)))
> > +	tcon_cast(&(set)->jset_, canary,				\
> > +		  jset_next_(jset_raw_(set), (unsigned long)(prev)))
> >  
> >  /**
> >   * jset_last - return the last bit which is set (must not contain 0).
> > @@ -230,7 +240,7 @@ struct jset {
> >   *	printf("\n");
> >   */
> >  #define jset_last(set)						\
> > -	tcon_cast((set), canary, jset_last_(&(set)->raw))
> > +	tcon_cast(&(set)->jset_, canary, jset_last_(jset_raw_(set)))
> >  
> >  /**
> >   * jset_prev - return the previous bit which is set (must not contain 0).
> > @@ -241,7 +251,8 @@ struct jset {
> >   * jset_last.
> >   */
> >  #define jset_prev(set, prev)						\
> > -	tcon_cast((set), canary, jset_prev_(&(set)->raw, (unsigned long)(prev)))
> > +	tcon_cast(&(set)->jset_, canary,				\
> > +		  jset_prev_(jset_raw_(set), (unsigned long)(prev)))
> >  
> >  /**
> >   * jset_first_clear - return the first bit which is unset
> > @@ -251,17 +262,17 @@ struct jset {
> >   * set is full.
> >   */
> >  #define jset_first_clear(set)						\
> > -	tcon_cast((set), canary, jset_next_clear_(&(set)->raw, 0))
> > +	tcon_cast(&(set)->jset_, canary, jset_next_clear_(jset_raw_(set), 0))
> >  
> >  #define jset_next_clear(set, prev)					\
> > -	tcon_cast((set), canary, jset_next_clear_(&(set)->raw,		\
> > +	tcon_cast(&(set)->jset_, canary, jset_next_clear_(jset_raw_(set), \
> >  						  (unsigned long)(prev)))
> >  
> >  #define jset_last_clear(set)					\
> > -	tcon_cast((set), canary, jset_last_clear_(&(set)->raw))
> > +	tcon_cast(&(set)->jset_, canary, jset_last_clear_(jset_raw_(set)))
> >  
> >  #define jset_prev_clear(set, prev)					\
> > -	tcon_cast((set), canary, jset_prev_clear_(&(set)->raw,		\
> > +	tcon_cast(&(set)->jset_, canary, jset_prev_clear_(jset_raw_(set), \
> >  						  (unsigned long)(prev)))
> >  
> >  /* Raw functions */
> > diff --git a/ccan/jset/test/run.c b/ccan/jset/test/run.c
> > index a0fb8a8c..425546fd 100644
> > --- a/ccan/jset/test/run.c
> > +++ b/ccan/jset/test/run.c
> > @@ -34,7 +34,8 @@ int main(int argc, char *argv[])
> >  		jset_set(set, 1 + (i << 4));
> >  
> >  	/* This only take 1.7MB on my 32-bit system. */
> > -	diag("%u bytes memory used\n", (unsigned)Judy1MemUsed(set->raw.judy));
> > +	diag("%u bytes memory used\n",
> > +	     (unsigned)Judy1MemUsed(jset_raw_(set)->judy));
> >  
> >  	ok1(jset_popcount(set, 0, -1) == 1000000);
> >  	ok1(jset_nth(set, 0, -1) == 1);
> > @@ -53,13 +54,13 @@ int main(int argc, char *argv[])
> >  	ok1(jset_error(set) == NULL);
> >  
> >  	/* Test error handling */
> > -	JU_ERRNO(&set->raw.err) = 100;
> > -	JU_ERRID(&set->raw.err) = 991;
> > +	JU_ERRNO(&jset_raw_(set)->err) = 100;
> > +	JU_ERRID(&jset_raw_(set)->err) = 991;
> >  	err = jset_error(set);
> >  	ok1(err);
> >  	ok1(strstr(err, "100"));
> >  	ok1(strstr(err, "991"));
> > -	ok1(err == set->raw.errstr);
> > +	ok1(err == jset_raw_(set)->errstr);
> >  	jset_free(set);
> >  
> >  	return exit_status();
> 

-- 
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: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 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] 8+ messages in thread

end of thread, other threads:[~2017-08-01  5:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-23 12:29 [PATCH 0/4] Replace TCON() with TCON_WRAP() David Gibson
2017-07-23 12:29 ` [PATCH 1/4] tlist: Use TCON_WRAP instead of TCON David Gibson
2017-07-23 12:29 ` [PATCH 2/4] jset: " David Gibson
2017-07-31  6:25   ` Rusty Russell
2017-08-01  5:17     ` David Gibson
2017-07-23 12:29 ` [PATCH 3/4] jmap: " David Gibson
2017-07-23 12:29 ` [PATCH 4/4] objset: " David Gibson
2017-07-27  7:27 ` [PATCH 0/4] Replace TCON() with TCON_WRAP() 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).