[PATCH 2/3] wayland-client: Introduce shm pool helper function

Ander Conselvan de Oliveira conselvan2 at gmail.com
Tue Aug 7 05:03:10 PDT 2012


On 08/04/2012 04:22 PM, Andre Heider wrote:
> Every client dealing with shm buffers requires code to set up pools.
> Wayland already implements this platform independent, so make public
> helper function available instead of putting the burden on every client.
> ---
>   src/Makefile.am      |    6 ++-
>   src/wayland-client.c |  110 ++++++++++++++++++++++++++++++++++++++++++++++++++
>   src/wayland-client.h |   23 +++++++++++
>   3 files changed, 138 insertions(+), 1 deletion(-)
>
[...]

> diff --git a/src/wayland-client.c b/src/wayland-client.c
> index 631ec5a..569501d 100644
> --- a/src/wayland-client.c
> +++ b/src/wayland-client.c
> @@ -28,6 +28,7 @@
>   #include <errno.h>
>   #include <string.h>
>   #include <unistd.h>
> +#include <sys/mman.h>
>   #include <sys/socket.h>
>   #include <sys/un.h>
>   #include <ctype.h>
> @@ -39,6 +40,7 @@
>   #include "wayland-os.h"
>   #include "wayland-client.h"
>   #include "wayland-private.h"
> +#include "../shared/os-compatibility.h"

You should have the Makefile set -I$(top_srcdir)/shared so you can do just

   #include "os-compatibility.h",

but I think we don't need a libshared in this particular case.


>   struct wl_global_listener {
>   	wl_display_global_func_t handler;

[...]

> @@ -643,3 +653,103 @@ wl_log_set_handler_client(wl_log_func_t handler)
>   {
>   	wl_log_handler = handler;
>   }
> +

[...]

> +
> +WL_EXPORT int
> +wl_shm_pool_helper_resize(struct wl_shm_pool_helper *pool, int32_t size)
> +{
> +	if (ftruncate(pool->fd, size) < 0)
> +		return 0;
> +
> +	wl_shm_pool_resize(pool->pool, size);
> +
> +	munmap(pool->data, pool->size);
> +
> +	pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
> +			  pool->fd, 0);
> +	pool->size = size;
> +
> +	return 1;
> +}
> +
> +WL_EXPORT void *
> +wl_shm_pool_helper_allocate(struct wl_shm_pool_helper *pool, int32_t size,
> +			    int32_t *offset)

I don't think any one would want to allocate a pool bigger than 4GB, but 
we should use size_t for size and offset.

> +{
> +	void *p;
> +
> +	if (pool->used + size > pool->size) {
> +		if (offset)
> +			*offset = 0;

If the allocation fails offset is undefined but zero is a valid offset. 
The user should just check for NULL and we leave offset alone if the 
allocation fails.

Cheers,
Ander

> +
> +		return NULL;
> +	}
> +
> +	if (offset)
> +		*offset = pool->used;
> +
> +	p = (char *) pool->data + pool->used;
> +	pool->used += size;
> +
> +	return p;
> +}
> +
> +WL_EXPORT void
> +wl_shm_pool_helper_reset(struct wl_shm_pool_helper *pool)
> +{
> +	pool->used = 0;
> +}
> +
> +WL_EXPORT struct wl_shm_pool *
> +wl_shm_pool_helper_get_pool(struct wl_shm_pool_helper *pool)
> +{
> +	return pool->pool;
> +}
> +
> +WL_EXPORT int32_t
> +wl_shm_pool_helper_get_size(struct wl_shm_pool_helper *pool)
> +{
> +	return pool->size;
> +}
> +
> +WL_EXPORT void
> +wl_shm_pool_helper_destroy(struct wl_shm_pool_helper *pool, int32_t unmap)
> +{
> +	if (unmap)
> +		munmap(pool->data, pool->size);
> +
> +	wl_shm_pool_destroy(pool->pool);
> +	close(pool->fd);
> +	free(pool);
> +}
> diff --git a/src/wayland-client.h b/src/wayland-client.h
> index 06dc6fe..2a6e869 100644
> --- a/src/wayland-client.h
> +++ b/src/wayland-client.h
> @@ -32,6 +32,7 @@ extern "C" {
>
>   struct wl_proxy;
>   struct wl_display;
> +struct wl_shm_pool_helper;
>
>   void wl_proxy_marshal(struct wl_proxy *p, uint32_t opcode, ...);
>   struct wl_proxy *wl_proxy_create(struct wl_proxy *factory,
> @@ -98,6 +99,28 @@ wl_display_get_global(struct wl_display *display,
>
>   void wl_log_set_handler_client(wl_log_func_t handler);
>
> +struct wl_shm_pool_helper *
> +wl_shm_pool_helper_create(struct wl_shm *shm, int32_t size);
> +
> +int
> +wl_shm_pool_helper_resize(struct wl_shm_pool_helper *pool, int32_t size);
> +
> +void *
> +wl_shm_pool_helper_allocate(struct wl_shm_pool_helper *pool, int32_t size,
> +			    int32_t *offset);
> +
> +void
> +wl_shm_pool_helper_reset(struct wl_shm_pool_helper *pool);
> +
> +struct wl_shm_pool *
> +wl_shm_pool_helper_get_pool(struct wl_shm_pool_helper *pool);
> +
> +int32_t
> +wl_shm_pool_helper_get_size(struct wl_shm_pool_helper *pool);
> +
> +void
> +wl_shm_pool_helper_destroy(struct wl_shm_pool_helper *pool, int32_t unmap);
> +
>   #ifdef  __cplusplus
>   }
>   #endif
>



More information about the wayland-devel mailing list