[PATCH wayland 7/8] scanner: initialize .{method, event}_count via ARRAY_SIZE

Emil Velikov emil.l.velikov at gmail.com
Thu Jun 14 15:49:44 UTC 2018


From: Emil Velikov <emil.velikov at collabora.com>

Demystify the final magic value within the generated files.

Signed-off-by: Emil Velikov <emil.velikov at collabora.com>
---
 src/scanner.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/scanner.c b/src/scanner.c
index 22b1daa..8f469e9 100644
--- a/src/scanner.c
+++ b/src/scanner.c
@@ -1779,6 +1779,7 @@ emit_code(struct protocol *protocol, enum visibility vis)
 	}
 	printf("};\n\n");
 
+	printf("#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))\n");
 	wl_list_for_each_safe(i, next, &protocol->interface_list, link) {
 
 		emit_messages(&i->request_list, i, "requests");
@@ -1790,12 +1791,12 @@ emit_code(struct protocol *protocol, enum visibility vis)
 		       symbol_visibility, i->name, i->name, i->version);
 
 		if (!wl_list_empty(&i->request_list))
-			printf("\t.method_count = %d, .methods = %s_requests,\n",
-			       wl_list_length(&i->request_list), i->name);
+			printf("\t.method_count = ARRAY_SIZE(%s_requests), .methods = %s_requests,\n",
+			       i->name, i->name);
 
 		if (!wl_list_empty(&i->event_list))
-			printf("\t.event_count = %d, .events = %s_events,\n",
-			       wl_list_length(&i->event_list), i->name);
+			printf("\t.event_count = ARRAY_SIZE(%s_events), .events = %s_events,\n",
+			       i->name, i->name);
 
 		printf("};\n\n");
 
-- 
2.16.0



More information about the wayland-devel mailing list