commit 09023ff

Michael Forney  ·  2014-07-21 04:48:41 +0000 UTC
parent 22c4fee
Use wl_container_of instead of CONTAINER_OF

Since wayland-1.5, wl_container_of has defined behavior when called with
an uninitialized pointer for the "sample" argument.
16 files changed,  +44, -63
+2, -1
 1@@ -22,7 +22,8 @@ TARGETS         := swc.pc
 2 SUBDIRS         := launch libswc protocol cursor example
 3 CLEAN_FILES     := $(TARGETS)
 4 
 5-libinput_CONSTRAINTS := >= 0.4
 6+libinput_CONSTRAINTS        := >= 0.4
 7+wayland-server_CONSTRAINTS  := >= 1.5.0
 8 
 9 include config.mk
10 
+3, -5
 1@@ -98,7 +98,7 @@ static void handle_screen_event(struct wl_listener * listener, void * data)
 2     if (event->type == SWC_SCREEN_DESTROYED)
 3     {
 4         struct target * target
 5-            = CONTAINER_OF(listener, typeof(*target), screen_listener);
 6+            = wl_container_of(listener, target, screen_listener);
 7 
 8         wld_destroy_surface(target->surface);
 9         free(target);
10@@ -111,16 +111,14 @@ static struct target * target_get(struct screen * screen)
11         = wl_signal_get(&screen->base.event_signal, &handle_screen_event);
12     struct target * target;
13 
14-    return listener ? CONTAINER_OF(listener, typeof(*target), screen_listener)
15-                    : NULL;
16+    return listener ? wl_container_of(listener, target, screen_listener) : NULL;
17 }
18 
19 static void handle_screen_view_event(struct wl_listener * listener, void * data)
20 {
21     struct swc_event * event = data;
22     struct view_event_data * event_data = event->data;
23-    struct target * target
24-        = CONTAINER_OF(listener, typeof(*target), view_listener);
25+    struct target * target = wl_container_of(listener, target, view_listener);
26 
27     switch (event->type)
28     {
+3, -3
 1@@ -40,7 +40,7 @@ static bool update(struct view * view)
 2 
 3 static bool attach(struct view * view, struct wld_buffer * buffer)
 4 {
 5-    struct cursor_plane * plane = CONTAINER_OF(view, typeof(*plane), view);
 6+    struct cursor_plane * plane = wl_container_of(view, plane, view);
 7 
 8     if (buffer)
 9     {
10@@ -75,7 +75,7 @@ static bool attach(struct view * view, struct wld_buffer * buffer)
11 
12 static bool move(struct view * view, int32_t x, int32_t y)
13 {
14-    struct cursor_plane * plane = CONTAINER_OF(view, typeof(*plane), view);
15+    struct cursor_plane * plane = wl_container_of(view, plane, view);
16 
17     if (drmModeMoveCursor(swc.drm->fd, plane->crtc,
18                           x - plane->origin->x, y - plane->origin->y) != 0)
19@@ -99,7 +99,7 @@ static void handle_launch_event(struct wl_listener * listener, void * data)
20 {
21     struct swc_event * event = data;
22     struct cursor_plane * plane
23-        = CONTAINER_OF(listener, typeof(*plane), launch_listener);
24+        = wl_container_of(listener, plane, launch_listener);
25 
26     switch (event->type)
27     {
+2, -2
 1@@ -68,8 +68,8 @@ static struct wl_data_device_interface data_device_implementation = {
 2 
 3 static void handle_selection_destroy(struct wl_listener * listener, void * data)
 4 {
 5-    struct data_device * data_device = CONTAINER_OF
 6-        (listener, typeof(*data_device), selection_destroy_listener);
 7+    struct data_device * data_device
 8+        = wl_container_of(listener, data_device, selection_destroy_listener);
 9 
10     data_device->selection = NULL;
11     swc_send_event(&data_device->event_signal,
+5, -6
 1@@ -50,7 +50,7 @@ static bool framebuffer_export(struct wld_exporter * exporter,
 2                                uint32_t type, union wld_object * object)
 3 {
 4     struct framebuffer * framebuffer
 5-        = CONTAINER_OF(exporter, typeof(*framebuffer), exporter);
 6+        = wl_container_of(exporter, framebuffer, exporter);
 7 
 8     switch (type)
 9     {
10@@ -66,7 +66,7 @@ static bool framebuffer_export(struct wld_exporter * exporter,
11 static void framebuffer_destroy(struct wld_destructor * destructor)
12 {
13     struct framebuffer * framebuffer
14-        = CONTAINER_OF(destructor, typeof(*framebuffer), destructor);
15+        = wl_container_of(destructor, framebuffer, destructor);
16 
17     drmModeRmFB(swc.drm->fd, framebuffer->id);
18     free(framebuffer);
19@@ -86,8 +86,7 @@ static void send_frame(void * data)
20 
21 static bool attach(struct view * view, struct wld_buffer * buffer)
22 {
23-    struct framebuffer_plane * plane
24-        = CONTAINER_OF(view, typeof(*plane), view);
25+    struct framebuffer_plane * plane = wl_container_of(view, plane, view);
26     union wld_object object;
27 
28     if (!wld_export(buffer, WLD_USER_OBJECT_FRAMEBUFFER, &object))
29@@ -163,7 +162,7 @@ const static struct view_impl view_impl = {
30 static void handle_page_flip(struct swc_drm_handler * handler, uint32_t time)
31 {
32     struct framebuffer_plane * plane
33-        = CONTAINER_OF(handler, typeof(*plane), drm_handler);
34+        = wl_container_of(handler, plane, drm_handler);
35 
36     view_frame(&plane->view, time);
37 }
38@@ -172,7 +171,7 @@ static void handle_launch_event(struct wl_listener * listener, void * data)
39 {
40     struct swc_event * event = data;
41     struct framebuffer_plane * plane
42-        = CONTAINER_OF(listener, typeof(*plane), launch_listener);
43+        = wl_container_of(listener, plane, launch_listener);
44 
45     switch (event->type)
46     {
+1, -1
1@@ -66,7 +66,7 @@ static void handle_focus_view_destroy(struct wl_listener * listener,
2                                       void * data)
3 {
4     struct input_focus * input_focus
5-        = CONTAINER_OF(listener, typeof(*input_focus), view_destroy_listener);
6+        = wl_container_of(listener, input_focus, view_destroy_listener);
7 
8     input_focus->resource = NULL;
9     input_focus->view = NULL;
+1, -1
1@@ -38,7 +38,7 @@ static void enter(struct input_focus_handler * handler,
2                   struct wl_resource * resource, struct compositor_view * view)
3 {
4     struct keyboard * keyboard
5-        = CONTAINER_OF(handler, typeof(*keyboard), focus_handler);
6+        = wl_container_of(handler, keyboard, focus_handler);
7     uint32_t serial;
8 
9     serial = wl_display_next_serial(swc.display);
+5, -7
 1@@ -89,7 +89,7 @@ static void dock(struct wl_client * client, struct wl_resource * resource,
 2     struct swc_output * output = output_resource
 3         ? wl_resource_get_user_data(output_resource) : NULL;
 4     struct screen * screen = output
 5-        ? output->screen : CONTAINER_OF(swc.screens.next, struct screen, link);
 6+        ? output->screen : wl_container_of(swc.screens.next, screen, link);
 7     bool screen_changed = screen != panel->screen;
 8     uint32_t length;
 9 
10@@ -169,7 +169,7 @@ static void modify(struct screen_modifier * modifier,
11                    const struct swc_rectangle * geometry,
12                    pixman_region32_t * usable)
13 {
14-    struct panel * panel = CONTAINER_OF(modifier, typeof(*panel), modifier);
15+    struct panel * panel = wl_container_of(modifier, panel, modifier);
16     pixman_box32_t box = {
17         .x1 = geometry->x, .y1 = geometry->y,
18         .x2 = geometry->x + geometry->width,
19@@ -222,11 +222,9 @@ static void destroy_panel(struct wl_resource * resource)
20 
21 static void handle_view_event(struct wl_listener * listener, void * data)
22 {
23-    struct panel * panel;
24+    struct panel * panel = wl_container_of(listener, panel, view_listener);
25     struct swc_event * event = data;
26 
27-    panel = CONTAINER_OF(listener, typeof(*panel), view_listener);
28-
29     switch (event->type)
30     {
31         case VIEW_EVENT_RESIZED:
32@@ -237,9 +235,9 @@ static void handle_view_event(struct wl_listener * listener, void * data)
33 
34 static void handle_surface_destroy(struct wl_listener * listener, void * data)
35 {
36-    struct panel * panel;
37+    struct panel * panel
38+        = wl_container_of(listener, panel, surface_destroy_listener);
39 
40-    panel = CONTAINER_OF(listener, typeof(*panel), surface_destroy_listener);
41     wl_resource_destroy(panel->resource);
42 }
43 
+7, -11
 1@@ -37,16 +37,13 @@
 2 static void enter(struct input_focus_handler * handler,
 3                   struct wl_resource * resource, struct compositor_view * view)
 4 {
 5-    struct pointer * pointer;
 6+    struct pointer * pointer = wl_container_of(handler, pointer, focus_handler);
 7     uint32_t serial;
 8     wl_fixed_t surface_x, surface_y;
 9 
10-    pointer = CONTAINER_OF(handler, typeof(*pointer), focus_handler);
11     serial = wl_display_next_serial(swc.display);
12-
13     surface_x = pointer->x - wl_fixed_from_int(view->base.geometry.x);
14     surface_y = pointer->y - wl_fixed_from_int(view->base.geometry.y);
15-
16     wl_pointer_send_enter(resource, serial, view->surface->resource,
17                           surface_x, surface_y);
18 }
19@@ -65,7 +62,7 @@ static void handle_cursor_surface_destroy(struct wl_listener * listener,
20                                           void * data)
21 {
22     struct pointer * pointer
23-        = CONTAINER_OF(listener, typeof(*pointer), cursor.destroy_listener);
24+        = wl_container_of(listener, pointer, cursor.destroy_listener);
25 
26     view_attach(&pointer->cursor.view, NULL);
27     pointer->cursor.surface = NULL;
28@@ -78,8 +75,7 @@ static bool update(struct view * view)
29 
30 static bool attach(struct view * view, struct wld_buffer * buffer)
31 {
32-    struct pointer * pointer
33-        = CONTAINER_OF(view, typeof(*pointer), cursor.view);
34+    struct pointer * pointer = wl_container_of(view, pointer, cursor.view);
35     struct swc_surface * surface = pointer->cursor.surface;
36 
37     if (surface && !pixman_region32_not_empty(&surface->state.damage))
38@@ -160,7 +156,7 @@ static bool client_handle_button
39      struct press * press, uint32_t state)
40 {
41     struct pointer * pointer
42-        = CONTAINER_OF(handler, typeof(*pointer), client_handler);
43+        = wl_container_of(handler, pointer, client_handler);
44 
45     if (!pointer->focus.resource)
46         return false;
47@@ -175,7 +171,7 @@ static bool client_handle_axis(struct pointer_handler * handler,
48                                uint32_t time, uint32_t axis, wl_fixed_t amount)
49 {
50     struct pointer * pointer
51-        = CONTAINER_OF(handler, typeof(*pointer), client_handler);
52+        = wl_container_of(handler, pointer, client_handler);
53 
54     if (!pointer->focus.resource)
55         return false;
56@@ -189,7 +185,7 @@ static bool client_handle_motion(struct pointer_handler * handler,
57                                  uint32_t time, wl_fixed_t x, wl_fixed_t y)
58 {
59     struct pointer * pointer
60-        = CONTAINER_OF(handler, typeof(*pointer), client_handler);
61+        = wl_container_of(handler, pointer, client_handler);
62 
63     if (!pointer->focus.resource)
64         return false;
65@@ -207,7 +203,7 @@ bool pointer_initialize(struct pointer * pointer)
66     struct screen * screen;
67 
68     /* Center cursor in the geometry of the first screen. */
69-    screen = CONTAINER_OF(swc.screens.next, typeof(*screen), link);
70+    screen = wl_container_of(swc.screens.next, screen, link);
71     pointer->x = wl_fixed_from_int
72         (screen->base.geometry.x + screen->base.geometry.width / 2);
73     pointer->y = wl_fixed_from_int
+4, -4
 1@@ -114,7 +114,7 @@ static void set_fullscreen(struct wl_client * client,
 2     struct screen * screen;
 3 
 4     screen = output ? output->screen
 5-                    : CONTAINER_OF(swc.screens.next, typeof(*screen), link);
 6+                    : wl_container_of(swc.screens.next, screen, link);
 7 
 8     /* TODO: Handle fullscreen windows. */
 9 
10@@ -188,7 +188,7 @@ static void configure(struct window * window,
11                       const struct swc_rectangle * geometry)
12 {
13     struct shell_surface * shell_surface
14-        = CONTAINER_OF(window, typeof(*shell_surface), window);
15+        = wl_container_of(window, shell_surface, window);
16 
17     wl_shell_surface_send_configure(shell_surface->resource,
18                                     WL_SHELL_SURFACE_RESIZE_NONE,
19@@ -201,8 +201,8 @@ static const struct window_impl shell_window_impl = {
20 
21 static void handle_surface_destroy(struct wl_listener * listener, void * data)
22 {
23-    struct shell_surface * shell_surface = CONTAINER_OF
24-        (listener, typeof(*shell_surface), surface_destroy_listener);
25+    struct shell_surface * shell_surface
26+        = wl_container_of(listener, shell_surface, surface_destroy_listener);
27 
28     wl_resource_destroy(shell_surface->resource);
29 }
+1, -1
1@@ -77,7 +77,7 @@ static void destroy_pool_resource(struct wl_resource * resource)
2 static void handle_buffer_destroy(struct wld_destructor * destructor)
3 {
4     struct pool_reference * reference
5-        = CONTAINER_OF(destructor, typeof(*reference), destructor);
6+        = wl_container_of(destructor, reference, destructor);
7 
8     unref_pool(reference->pool);
9 }
+2, -2
 1@@ -42,7 +42,7 @@ static void handle_buffer_destroy(struct wl_listener * listener, void * data)
 2 {
 3     struct swc_surface_state * state;
 4 
 5-    state = CONTAINER_OF(listener, typeof(*state), buffer_destroy_listener);
 6+    state = wl_container_of(listener, state, buffer_destroy_listener);
 7     state->buffer = NULL;
 8 }
 9 
10@@ -299,7 +299,7 @@ static void surface_destroy(struct wl_resource * resource)
11 static void handle_view_event(struct wl_listener * listener, void * data)
12 {
13     struct swc_surface * surface
14-        = CONTAINER_OF(listener, typeof(*surface), view_listener);
15+        = wl_container_of(listener, surface, view_listener);
16     struct swc_event * event = data;
17     struct view_event_data * event_data = event->data;
18 
+0, -11
 1@@ -58,17 +58,6 @@
 2 # define DEBUG(format, ...)
 3 #endif
 4 
 5-#ifdef offsetof
 6-# define OFFSET_OF offsetof
 7-#else
 8-# define OFFSET_OF __builtin_offsetof
 9-#endif
10-
11-#define CONTAINER_OF(ptr, type, member) ({                                  \
12-    const typeof(((type *) 0)->member) *__mptr = (ptr);                     \
13-    ((type *) ((uintptr_t) __mptr - OFFSET_OF(type, member)));              \
14-})
15-
16 #define ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
17 
18 struct wl_resource;
+2, -2
 1@@ -187,7 +187,7 @@ static bool move_motion(struct pointer_handler * handler, uint32_t time,
 2                         wl_fixed_t fx, wl_fixed_t fy)
 3 {
 4     struct window * window
 5-        = CONTAINER_OF(handler, typeof(*window), move.interaction.handler);
 6+        = wl_container_of(handler, window, move.interaction.handler);
 7 
 8     view_move(&window->view->base, wl_fixed_to_int(fx) + window->move.offset.x,
 9                                    wl_fixed_to_int(fy) + window->move.offset.y);
10@@ -207,7 +207,7 @@ static bool handle_button(struct pointer_handler * handler, uint32_t time,
11                           struct press * press, uint32_t state)
12 {
13     struct window_pointer_interaction * interaction
14-        = CONTAINER_OF(handler, typeof(*interaction), handler);
15+        = wl_container_of(handler, interaction, handler);
16 
17     if (state != WL_POINTER_BUTTON_STATE_RELEASED
18         || !interaction->original_handler)
+1, -1
1@@ -58,7 +58,7 @@ struct swc_xserver swc_xserver;
2 static int open_socket(struct sockaddr_un * addr, size_t path_size)
3 {
4     int fd;
5-    socklen_t size = OFFSET_OF(typeof(*addr), sun_path) + path_size + 1;
6+    socklen_t size = offsetof(typeof(*addr), sun_path) + path_size + 1;
7 
8     if ((fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0)
9         goto error0;
+5, -5
 1@@ -128,7 +128,7 @@ static void configure(struct window * window,
 2 {
 3     uint32_t mask, values[4];
 4     struct xwl_window * xwl_window
 5-        = CONTAINER_OF(window, typeof(*xwl_window), window);
 6+        = wl_container_of(window, xwl_window, window);
 7 
 8     mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
 9          | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
10@@ -143,9 +143,9 @@ static void configure(struct window * window,
11 
12 static void focus(struct window * window)
13 {
14-    xcb_window_t id = window ? CONTAINER_OF(window, struct xwl_window,
15-                                            window)->id
16-                             : XCB_NONE;
17+    struct xwl_window * xwl_window
18+        = wl_container_of(window, xwl_window, window);
19+    xcb_window_t id = window ? xwl_window->id : XCB_NONE;
20 
21     xcb_set_input_focus(xwm.connection, XCB_INPUT_FOCUS_NONE,
22                         id, XCB_CURRENT_TIME);
23@@ -160,7 +160,7 @@ static const struct window_impl xwl_window_handler = {
24 static void handle_surface_destroy(struct wl_listener * listener, void * data)
25 {
26     struct xwl_window * xwl_window
27-        = CONTAINER_OF(listener, typeof(*xwl_window), surface_destroy_listener);
28+        = wl_container_of(listener, xwl_window, surface_destroy_listener);
29 
30     window_finalize(&xwl_window->window);
31     wl_list_remove(&xwl_window->link);