]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-event: extend comment about a flex member 37790/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 9 Jun 2025 14:11:17 +0000 (16:11 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 3 Jul 2025 14:35:40 +0000 (16:35 +0200)
Follow-up for dbef4dd4f23517abfc73b35f0bdf004d2f8f4805. Everything that that
commit says is true, but — at least for me — it wasn't obvious why the code is
correct and we can do fixed-size allocations like new(struct inotify_data, 1).

src/libsystemd/sd-event/event-source.h

index d25a0958edc4c32a8018cf72762e29e87fb88fd3..7f6d5b8b50eff6e423dfd63cfe26000cb0618e17 100644 (file)
@@ -242,6 +242,12 @@ struct inotify_data {
 
         /* The buffer we read inotify events into */
         size_t buffer_filled; /* fill level of the buffer */
-        union inotify_event_buffer buffer; /* struct inotify_event in union inotify_event_buffer has flex
-                                            * array. Hence, this must be at the end. */
+        union inotify_event_buffer buffer; /* We use a union to allow type-punning. One of the variants in
+                                            * the union — struct inotify_event — has a flex array, so C99
+                                            * only allows this union to be used at the end of the structure.
+                                            * The other variant in the union defines a fixed-size buffer that
+                                            * covers the maximum size that can be used for the flex variant,
+                                            * so in fact this field has a fixed size and could be safely
+                                            * placed in the middle of the struct. Unfortunately, there is no
+                                            * mechanism to let the compiler know that. */
 };