diff mbox

[01/13] ptrlist: let all pointer lists have the same parametrized structure

Message ID 20180611015119.94067-2-luc.vanoostenryck@gmail.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Luc Van Oostenryck June 11, 2018, 1:51 a.m. UTC
Currently, all pointer lists have the same concrete structure,
struct ptr_list, which is also used as a generic pointer list.
But they're each declared with a phony type which just allows
to get back the type of the pointer:
	struct <name> { <TYPE> list[0]; };
vs.
	struct ptr_list {
		....
		void * list[NR];
	};

Since the declaration doesn't match the real representation
every operation done on pointer lists must cast forth & back the
list pointer to the generic type. OTOH, type safety is assured
by phony extra statements which are NOP but would produce warnings
if, for example, the type of the list doesn't correspond to
the element to be stored or retrieved.

This patch and the following ones takes another approach where
all pointer lists are declared *and* defined to essentially the
same structure but kinda parametrized with the type of the pointers
to be stored in the list. This has the advantage that most operations
are done on the real types and thus to eliminate all the type checking
statements and most of the casts (the ones remaining are the ones
needed to interface with the generic type used for the functionalities
implemented as functions).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 ptrlist.h | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
diff mbox

Patch

diff --git a/ptrlist.h b/ptrlist.h
index e3a84ea0a..6cec720e0 100644
--- a/ptrlist.h
+++ b/ptrlist.h
@@ -10,7 +10,6 @@ 
  */
 
 /* Silly type-safety check ;) */
-#define DECLARE_PTR_LIST(listname,type)	struct listname { type *list[1]; }
 #define CHECK_TYPE(head,ptr)		(void)(&(ptr) == &(head)->list[0])
 #define TYPEOF(head)			__typeof__(&(head)->list[0])
 #define VRFY_PTR_LIST(head)		(void)(sizeof((head)->list[0]))
@@ -24,13 +23,16 @@ 
 
 #define LIST_NODE_NR (13)
 
-struct ptr_list {
-	int nr:8;
-	int rm:8;
-	struct ptr_list *prev;
-	struct ptr_list *next;
-	void *list[LIST_NODE_NR];
-};
+#define DECLARE_PTR_LIST(listname, type)	\
+	struct listname {			\
+		int nr:8;			\
+		int rm:8;			\
+		struct listname *prev;		\
+		struct listname *next;		\
+		type *list[LIST_NODE_NR];	\
+	}
+
+DECLARE_PTR_LIST(ptr_list, void);
 
 #define ptr_list_empty(x) ((x) == NULL)