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 | |
Kristian Høgsberg | da6c6b1 | 2010-06-10 13:48:44 -0400 | [diff] [blame] | 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
Kristian Høgsberg | f52e03f | 2010-02-26 11:42:59 -0500 | [diff] [blame] | 30 | #include <inttypes.h> |
| 31 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 32 | /* GCC visibility */ |
| 33 | #if defined(__GNUC__) && __GNUC__ >= 4 |
| 34 | #define WL_EXPORT __attribute__ ((visibility("default"))) |
| 35 | #else |
| 36 | #define WL_EXPORT |
| 37 | #endif |
| 38 | |
| 39 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
Kristian Høgsberg | bf967b4 | 2008-12-21 20:25:16 -0500 | [diff] [blame] | 40 | #define ALIGN(n, a) ( ((n) + ((a) - 1)) & ~((a) - 1) ) |
| 41 | #define DIV_ROUNDUP(n, a) ( ((n) + ((a) - 1)) / (a) ) |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 42 | |
| 43 | #define container_of(ptr, type, member) ({ \ |
| 44 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 45 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 46 | |
Kristian Høgsberg | fabd439 | 2008-12-22 18:06:49 -0500 | [diff] [blame] | 47 | struct wl_argument { |
| 48 | uint32_t type; |
| 49 | void *data; |
| 50 | }; |
| 51 | |
| 52 | struct wl_message { |
| 53 | const char *name; |
| 54 | const char *signature; |
| 55 | const void **types; |
| 56 | }; |
| 57 | |
| 58 | struct wl_interface { |
| 59 | const char *name; |
| 60 | int version; |
| 61 | int method_count; |
| 62 | const struct wl_message *methods; |
| 63 | int event_count; |
| 64 | const struct wl_message *events; |
| 65 | }; |
| 66 | |
| 67 | struct wl_object { |
| 68 | const struct wl_interface *interface; |
| 69 | void (**implementation)(void); |
| 70 | uint32_t id; |
| 71 | }; |
| 72 | |
Kristian Høgsberg | f52e03f | 2010-02-26 11:42:59 -0500 | [diff] [blame] | 73 | struct wl_hash_table; |
| 74 | struct wl_hash_table *wl_hash_table_create(void); |
| 75 | void wl_hash_table_destroy(struct wl_hash_table *ht); |
| 76 | void *wl_hash_table_lookup(struct wl_hash_table *ht, uint32_t hash); |
| 77 | int wl_hash_table_insert(struct wl_hash_table *ht, uint32_t hash, void *data); |
| 78 | void wl_hash_table_remove(struct wl_hash_table *ht, uint32_t hash); |
| 79 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 80 | |
| 81 | struct wl_list { |
| 82 | struct wl_list *prev; |
| 83 | struct wl_list *next; |
| 84 | }; |
| 85 | |
| 86 | void wl_list_init(struct wl_list *list); |
| 87 | void wl_list_insert(struct wl_list *list, struct wl_list *elm); |
| 88 | void wl_list_remove(struct wl_list *elm); |
| 89 | int wl_list_length(struct wl_list *list); |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 90 | int wl_list_empty(struct wl_list *list); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 91 | |
Kristian Høgsberg | a5db589 | 2010-02-26 10:28:44 -0500 | [diff] [blame] | 92 | #define __container_of(ptr, sample, member) \ |
| 93 | (void *)((char *)(ptr) - \ |
| 94 | ((char *)&(sample)->member - (char *)(sample))) |
| 95 | |
| 96 | #define wl_list_for_each(pos, head, member) \ |
| 97 | for (pos = __container_of((head)->next, pos, member); \ |
| 98 | &pos->member != (head); \ |
| 99 | pos = __container_of(pos->member.next, pos, member)) |
| 100 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 101 | #define wl_list_for_each_safe(pos, tmp, head, member) \ |
| 102 | for (pos = __container_of((head)->next, pos, member), \ |
| 103 | tmp = __container_of((pos)->member.next, tmp, member); \ |
| 104 | &pos->member != (head); \ |
| 105 | pos = tmp, \ |
| 106 | tmp = __container_of(pos->member.next, tmp, member)) |
| 107 | |
Kristian Høgsberg | 747638b | 2010-07-12 17:06:06 -0400 | [diff] [blame] | 108 | #define wl_list_for_each_reverse(pos, head, member) \ |
| 109 | for (pos = __container_of((head)->prev, pos, member); \ |
| 110 | &pos->member != (head); \ |
| 111 | pos = __container_of(pos->member.prev, pos, member)) |
| 112 | |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 113 | struct wl_array { |
| 114 | uint32_t size; |
| 115 | uint32_t alloc; |
| 116 | void *data; |
| 117 | }; |
| 118 | |
| 119 | void wl_array_init(struct wl_array *array); |
| 120 | void wl_array_release(struct wl_array *array); |
| 121 | void *wl_array_add(struct wl_array *array, int size); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 122 | |
Kristian Høgsberg | da6c6b1 | 2010-06-10 13:48:44 -0400 | [diff] [blame] | 123 | #ifdef __cplusplus |
| 124 | } |
| 125 | #endif |
| 126 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 127 | #endif |