commit a58644c

Michael Forney  ·  2013-07-04 04:49:38 +0000 UTC
parent 7bb6813
Use our own container_of macro

wl_container_of doesn't work when the sample argument is uninitialized
because the compiler optimizes it away as a garbage value.
8 files changed,  +29, -23
M seat.c
M util.h
+8, -7
 1@@ -10,6 +10,7 @@
 2 #include "event.h"
 3 #include "region.h"
 4 #include "data_device_manager.h"
 5+#include "util.h"
 6 
 7 static const char default_seat[] = "seat0";
 8 
 9@@ -58,8 +59,8 @@ static bool handle_key(struct swc_keyboard * keyboard, uint32_t time,
10     struct swc_compositor * compositor;
11     char keysym_name[64];
12 
13-    seat = wl_container_of(keyboard, seat, keyboard);
14-    compositor = wl_container_of(seat, compositor, seat);
15+    seat = swc_container_of(keyboard, typeof(*seat), keyboard);
16+    compositor = swc_container_of(seat, typeof(*compositor), seat);
17 
18     if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
19     {
20@@ -139,8 +140,8 @@ static bool handle_motion(struct swc_pointer * pointer, uint32_t time)
21     struct swc_seat * seat;
22     struct swc_compositor * compositor;
23 
24-    seat = wl_container_of(pointer, seat, pointer);
25-    compositor = wl_container_of(seat, compositor, seat);
26+    seat = swc_container_of(pointer, typeof(*seat), pointer);
27+    compositor = swc_container_of(seat, typeof(*compositor), seat);
28 
29     return false;
30 }
31@@ -156,7 +157,7 @@ static void handle_tty_event(struct wl_listener * listener, void * data)
32     struct swc_event * event = data;
33     struct swc_compositor * compositor;
34 
35-    compositor = wl_container_of(listener, compositor, tty_listener);
36+    compositor = swc_container_of(listener, typeof(*compositor), tty_listener);
37 
38     switch (event->type)
39     {
40@@ -201,7 +202,7 @@ static void handle_drm_event(struct wl_listener * listener, void * data)
41     struct swc_event * event = data;
42     struct swc_compositor * compositor;
43 
44-    compositor = wl_container_of(listener, compositor, drm_listener);
45+    compositor = swc_container_of(listener, typeof(*compositor), drm_listener);
46 
47     switch (event->type)
48     {
49@@ -275,7 +276,7 @@ static void create_surface(struct wl_client * client,
50 
51     printf("compositor_create_surface: %p\n", surface);
52 
53-    output = wl_container_of(compositor->outputs.next, output, link);
54+    output = swc_container_of(compositor->outputs.next, typeof(*output), link);
55 
56     /* Initialize compositor state */
57     surface->compositor_state = (struct swc_compositor_surface_state) {
+2, -2
 1@@ -70,8 +70,8 @@ struct wl_data_device_interface data_device_implementation = {
 2 
 3 static void handle_selection_destroy(struct wl_listener * listener, void * data)
 4 {
 5-    struct swc_data_device * data_device
 6-        = wl_container_of(listener, data_device, selection_destroy_listener);
 7+    struct swc_data_device * data_device = swc_container_of
 8+        (listener, typeof(*data_device), selection_destroy_listener);
 9     struct swc_event event;
10 
11     event.type = SWC_DATA_DEVICE_EVENT_SELECTION_CHANGED;
+2, -6
 1@@ -58,12 +58,8 @@ static inline void unfocus(struct swc_input_focus * input_focus)
 2 static void handle_focus_surface_destroy(struct wl_listener * listener,
 3                                          void * data)
 4 {
 5-    struct swc_input_focus * input_focus;
 6-
 7-    printf("focus surface destroy\n");
 8-
 9-    input_focus = wl_container_of(listener, input_focus,
10-                                  surface_destroy_listener);
11+    struct swc_input_focus * input_focus = swc_container_of
12+        (listener, typeof(*input_focus), surface_destroy_listener);
13 
14     input_focus->surface = NULL;
15     input_focus->resource = NULL;
+1, -1
1@@ -11,7 +11,7 @@ static void enter(struct swc_input_focus_handler * handler,
2     struct wl_display * display;
3     uint32_t serial;
4 
5-    keyboard = wl_container_of(handler, keyboard, focus_handler);
6+    keyboard = swc_container_of(handler, typeof(*keyboard), focus_handler);
7     client = wl_resource_get_client(resource);
8     display = wl_client_get_display(client);
9     serial = wl_display_next_serial(display);
+1, -1
1@@ -13,7 +13,7 @@ static void enter(struct swc_input_focus_handler * handler,
2     uint32_t serial;
3     wl_fixed_t surface_x, surface_y;
4 
5-    pointer = wl_container_of(handler, pointer, focus_handler);
6+    pointer = swc_container_of(handler, typeof(*pointer), focus_handler);
7     client = wl_resource_get_client(resource);
8     display = wl_client_get_display(client);
9     serial = wl_display_next_serial(display);
M seat.c
+5, -5
 1@@ -134,7 +134,7 @@ static void handle_evdev_event(struct wl_listener * listener, void * data)
 2     struct swc_event * event = data;
 3     struct swc_evdev_device_event_data * evdev_data = event->data;
 4 
 5-    entry = wl_container_of(listener, entry, event_listener);
 6+    entry = swc_container_of(listener, typeof(*entry), event_listener);
 7 
 8     switch (event->type)
 9     {
10@@ -159,8 +159,8 @@ static void handle_evdev_event(struct wl_listener * listener, void * data)
11 static void handle_keyboard_focus_event(struct wl_listener * listener,
12                                         void * data)
13 {
14-    struct swc_seat * seat
15-        = wl_container_of(listener, seat, keyboard_focus_listener);
16+    struct swc_seat * seat = swc_container_of
17+        (listener, typeof(*seat), keyboard_focus_listener);
18     struct swc_event * event = data;
19     struct swc_input_focus_event_data * event_data = event->data;
20 
21@@ -181,8 +181,8 @@ static void handle_keyboard_focus_event(struct wl_listener * listener,
22 
23 static void handle_data_device_event(struct wl_listener * listener, void * data)
24 {
25-    struct swc_seat * seat
26-        = wl_container_of(listener, seat, data_device_listener);
27+    struct swc_seat * seat = swc_container_of
28+        (listener, typeof(*seat), data_device_listener);
29     struct swc_event * event = data;
30 
31     switch (event->type)
+1, -1
1@@ -18,7 +18,7 @@ static void handle_buffer_destroy(struct wl_listener * listener, void * data)
2 {
3     struct swc_surface_state * state;
4 
5-    state = wl_container_of(listener, state, buffer_destroy_listener);
6+    state = swc_container_of(listener, typeof(*state), buffer_destroy_listener);
7     state->buffer = NULL;
8 }
9 
M util.h
+9, -0
 1@@ -4,6 +4,15 @@
 2 #include <stdbool.h>
 3 #include <wayland-server.h>
 4 
 5+#ifndef offsetof
 6+#   define offsetof __builtin_offsetof
 7+#endif
 8+
 9+#define swc_container_of(ptr, type, member) ({                              \
10+    const typeof(((type *) 0)->member) *__mptr = (ptr);                     \
11+    ((type *) ((uintptr_t) __mptr - offsetof(type, member)));               \
12+})
13+
14 void swc_remove_resource(struct wl_resource * resource);
15 
16 int swc_launch_open_input_device(int socket, const char * path, int flags);