blob: ed4acbc05a2bad88144d359d88d69710db298cf9 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050023#ifndef WAYLAND_UTIL_H
24#define WAYLAND_UTIL_H
25
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -050026#include <inttypes.h>
27
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050028/* GCC visibility */
29#if defined(__GNUC__) && __GNUC__ >= 4
30#define WL_EXPORT __attribute__ ((visibility("default")))
31#else
32#define WL_EXPORT
33#endif
34
35#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050036#define ALIGN(n, a) ( ((n) + ((a) - 1)) & ~((a) - 1) )
37#define DIV_ROUNDUP(n, a) ( ((n) + ((a) - 1)) / (a) )
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050038
39#define container_of(ptr, type, member) ({ \
40 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
41 (type *)( (char *)__mptr - offsetof(type,member) );})
42
Kristian Høgsbergfabd4392008-12-22 18:06:49 -050043struct wl_argument {
44 uint32_t type;
45 void *data;
46};
47
48struct wl_message {
49 const char *name;
50 const char *signature;
51 const void **types;
52};
53
54struct wl_interface {
55 const char *name;
56 int version;
57 int method_count;
58 const struct wl_message *methods;
59 int event_count;
60 const struct wl_message *events;
61};
62
63struct wl_object {
64 const struct wl_interface *interface;
65 void (**implementation)(void);
66 uint32_t id;
67};
68
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -050069struct wl_hash_table;
70struct wl_hash_table *wl_hash_table_create(void);
71void wl_hash_table_destroy(struct wl_hash_table *ht);
72void *wl_hash_table_lookup(struct wl_hash_table *ht, uint32_t hash);
73int wl_hash_table_insert(struct wl_hash_table *ht, uint32_t hash, void *data);
74void wl_hash_table_remove(struct wl_hash_table *ht, uint32_t hash);
75
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050076
77struct wl_list {
78 struct wl_list *prev;
79 struct wl_list *next;
80};
81
82void wl_list_init(struct wl_list *list);
83void wl_list_insert(struct wl_list *list, struct wl_list *elm);
84void wl_list_remove(struct wl_list *elm);
85int wl_list_length(struct wl_list *list);
Kristian Høgsberg4a298902008-11-28 18:35:25 -050086int wl_list_empty(struct wl_list *list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050087
Kristian Høgsberga5db5892010-02-26 10:28:44 -050088#define __container_of(ptr, sample, member) \
89 (void *)((char *)(ptr) - \
90 ((char *)&(sample)->member - (char *)(sample)))
91
92#define wl_list_for_each(pos, head, member) \
93 for (pos = __container_of((head)->next, pos, member); \
94 &pos->member != (head); \
95 pos = __container_of(pos->member.next, pos, member))
96
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050097struct wl_array {
98 uint32_t size;
99 uint32_t alloc;
100 void *data;
101};
102
103void wl_array_init(struct wl_array *array);
104void wl_array_release(struct wl_array *array);
105void *wl_array_add(struct wl_array *array, int size);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500106
107#endif