Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 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øgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 23 | #ifndef WAYLAND_UTIL_H |
| 24 | #define WAYLAND_UTIL_H |
| 25 | |
| 26 | /* GCC visibility */ |
| 27 | #if defined(__GNUC__) && __GNUC__ >= 4 |
| 28 | #define WL_EXPORT __attribute__ ((visibility("default"))) |
| 29 | #else |
| 30 | #define WL_EXPORT |
| 31 | #endif |
| 32 | |
| 33 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
Kristian Høgsberg | bf967b4 | 2008-12-21 20:25:16 -0500 | [diff] [blame] | 34 | #define ALIGN(n, a) ( ((n) + ((a) - 1)) & ~((a) - 1) ) |
| 35 | #define DIV_ROUNDUP(n, a) ( ((n) + ((a) - 1)) / (a) ) |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 36 | |
| 37 | #define container_of(ptr, type, member) ({ \ |
| 38 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 39 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 40 | |
Kristian Høgsberg | fabd439 | 2008-12-22 18:06:49 -0500 | [diff] [blame] | 41 | struct wl_argument { |
| 42 | uint32_t type; |
| 43 | void *data; |
| 44 | }; |
| 45 | |
| 46 | struct wl_message { |
| 47 | const char *name; |
| 48 | const char *signature; |
| 49 | const void **types; |
| 50 | }; |
| 51 | |
| 52 | struct wl_interface { |
| 53 | const char *name; |
| 54 | int version; |
| 55 | int method_count; |
| 56 | const struct wl_message *methods; |
| 57 | int event_count; |
| 58 | const struct wl_message *events; |
| 59 | }; |
| 60 | |
| 61 | struct wl_object { |
| 62 | const struct wl_interface *interface; |
| 63 | void (**implementation)(void); |
| 64 | uint32_t id; |
| 65 | }; |
| 66 | |
Kristian Høgsberg | 864c468 | 2008-12-12 11:05:17 -0500 | [diff] [blame] | 67 | struct wl_hash; |
| 68 | struct wl_hash *wl_hash_create(void); |
| 69 | void wl_hash_destroy(struct wl_hash *hash); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 70 | int wl_hash_insert(struct wl_hash *hash, struct wl_object *object); |
| 71 | struct wl_object *wl_hash_lookup(struct wl_hash *hash, uint32_t id); |
Kristian Høgsberg | f15ce9f | 2009-09-18 16:57:55 -0400 | [diff] [blame] | 72 | void wl_hash_remove(struct wl_hash *hash, struct wl_object *object); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 73 | |
| 74 | struct wl_list { |
| 75 | struct wl_list *prev; |
| 76 | struct wl_list *next; |
| 77 | }; |
| 78 | |
| 79 | void wl_list_init(struct wl_list *list); |
| 80 | void wl_list_insert(struct wl_list *list, struct wl_list *elm); |
| 81 | void wl_list_remove(struct wl_list *elm); |
| 82 | int wl_list_length(struct wl_list *list); |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 83 | int wl_list_empty(struct wl_list *list); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 84 | |
Kristian Høgsberg | a5db589 | 2010-02-26 10:28:44 -0500 | [diff] [blame^] | 85 | #define __container_of(ptr, sample, member) \ |
| 86 | (void *)((char *)(ptr) - \ |
| 87 | ((char *)&(sample)->member - (char *)(sample))) |
| 88 | |
| 89 | #define wl_list_for_each(pos, head, member) \ |
| 90 | for (pos = __container_of((head)->next, pos, member); \ |
| 91 | &pos->member != (head); \ |
| 92 | pos = __container_of(pos->member.next, pos, member)) |
| 93 | |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 94 | struct wl_array { |
| 95 | uint32_t size; |
| 96 | uint32_t alloc; |
| 97 | void *data; |
| 98 | }; |
| 99 | |
| 100 | void wl_array_init(struct wl_array *array); |
| 101 | void wl_array_release(struct wl_array *array); |
| 102 | void *wl_array_add(struct wl_array *array, int size); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 103 | |
| 104 | #endif |