[RFC wayland] Make wl_buffer use a wl_resource pointer instead of including it directly

Jason Ekstrand jason at jlekstrand.net
Tue Mar 19 12:18:05 PDT 2013


---
 src/wayland-server.h |  3 ++-
 src/wayland-shm.c    | 49 +++++++++++++++++++++++--------------------------
 2 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/src/wayland-server.h b/src/wayland-server.h
index 2c6efc9..01ef7b5 100644
--- a/src/wayland-server.h
+++ b/src/wayland-server.h
@@ -215,7 +215,8 @@ wl_resource_init(struct wl_resource *resource,
 }
 
 struct wl_buffer {
-	struct wl_resource resource;
+	struct wl_resource *resource;
+	const void * implementation;
 	int32_t width, height;
 	uint32_t busy_count;
 };
diff --git a/src/wayland-shm.c b/src/wayland-shm.c
index 776c0f2..c2da7c5 100644
--- a/src/wayland-shm.c
+++ b/src/wayland-shm.c
@@ -37,7 +37,7 @@
 #include "wayland-server.h"
 
 struct wl_shm_pool {
-	struct wl_resource resource;
+	struct wl_resource *resource;
 	int refcount;
 	char *data;
 	int size;
@@ -65,8 +65,7 @@ shm_pool_unref(struct wl_shm_pool *pool)
 static void
 destroy_buffer(struct wl_resource *resource)
 {
-	struct wl_shm_buffer *buffer =
-		container_of(resource, struct wl_shm_buffer, buffer.resource);
+	struct wl_shm_buffer *buffer = wl_resource_get_user_data(resource);
 
 	if (buffer->pool)
 		shm_pool_unref(buffer->pool);
@@ -89,7 +88,7 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
 		       int32_t width, int32_t height,
 		       int32_t stride, uint32_t format)
 {
-	struct wl_shm_pool *pool = resource->data;
+	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
 	struct wl_shm_buffer *buffer;
 
 	switch (format) {
@@ -119,6 +118,7 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
 		return;
 	}
 
+	buffer->buffer.implementation = &shm_buffer_interface;
 	buffer->buffer.width = width;
 	buffer->buffer.height = height;
 	buffer->buffer.busy_count = 0;
@@ -128,18 +128,17 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
 	buffer->pool = pool;
 	pool->refcount++;
 
-	wl_resource_init(&buffer->buffer.resource, &wl_buffer_interface,
-			 &shm_buffer_interface, id, buffer);
-	buffer->buffer.resource.client = resource->client;
-	buffer->buffer.resource.destroy = destroy_buffer;
-
-	wl_client_add_resource(client, &buffer->buffer.resource);
+	buffer->buffer.resource = wl_client_add_object(client,
+						       &wl_buffer_interface,
+						       &shm_buffer_interface,
+						       id, buffer);
+	wl_resource_set_destructor(buffer->buffer.resource, destroy_buffer);
 }
 
 static void
 destroy_pool(struct wl_resource *resource)
 {
-	struct wl_shm_pool *pool = resource->data;
+	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
 
 	shm_pool_unref(pool);
 }
@@ -154,7 +153,7 @@ static void
 shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
 		int32_t size)
 {
-	struct wl_shm_pool *pool = resource->data;
+	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
 	void *data;
 
 	data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
@@ -207,12 +206,11 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource,
 	}
 	close(fd);
 
-	wl_resource_init(&pool->resource, &wl_shm_pool_interface,
-			 &shm_pool_interface, id, pool);
-	pool->resource.client = client;
-	pool->resource.destroy = destroy_pool;
-
-	wl_client_add_resource(client, &pool->resource);
+	pool->resource = wl_client_add_object(client,
+					      &wl_shm_pool_interface,
+					      &shm_pool_interface,
+					      id, pool);
+	wl_resource_set_destructor(pool->resource, destroy_pool);
 
 	return;
 
@@ -267,6 +265,7 @@ wl_shm_buffer_create(struct wl_client *client,
 	if (buffer == NULL)
 		return NULL;
 
+	buffer->buffer.implementation = &shm_buffer_interface;
 	buffer->buffer.width = width;
 	buffer->buffer.height = height;
 	buffer->buffer.busy_count = 0;
@@ -275,12 +274,11 @@ wl_shm_buffer_create(struct wl_client *client,
 	buffer->offset = 0;
 	buffer->pool = NULL;
 
-	wl_resource_init(&buffer->buffer.resource, &wl_buffer_interface,
-			 &shm_buffer_interface, id, buffer);
-	buffer->buffer.resource.client = client;
-	buffer->buffer.resource.destroy = destroy_buffer;
-
-	wl_client_add_resource(client, &buffer->buffer.resource);
+	buffer->buffer.resource = wl_client_add_object(client,
+						       &wl_buffer_interface,
+						       &shm_buffer_interface,
+						       id, buffer);
+	wl_resource_set_destructor(buffer->buffer.resource, destroy_buffer);
 
 	return &buffer->buffer;
 }
@@ -288,8 +286,7 @@ wl_shm_buffer_create(struct wl_client *client,
 WL_EXPORT int
 wl_buffer_is_shm(struct wl_buffer *buffer)
 {
-	return buffer->resource.object.implementation == 
-		(void (**)(void)) &shm_buffer_interface;
+	return buffer->implementation == &shm_buffer_interface;
 }
 
 WL_EXPORT int32_t
-- 
1.8.1.4



More information about the wayland-devel mailing list