2008-12-02 15:15:01 -05:00
|
|
|
/*
|
2011-11-18 13:11:58 -05:00
|
|
|
* Copyright © 2008-2011 Kristian Høgsberg
|
|
|
|
|
* Copyright © 2011 Intel Corporation
|
2008-12-02 15:15:01 -05:00
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
|
* the above copyright notice appear in all copies and that both that copyright
|
|
|
|
|
* notice and this permission notice appear in supporting documentation, and
|
|
|
|
|
* that the name of the copyright holders not be used in advertising or
|
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
|
*
|
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-09-30 09:46:10 -04:00
|
|
|
#include <stdlib.h>
|
2008-12-22 18:06:49 -05:00
|
|
|
#include <stdint.h>
|
2008-12-12 11:05:17 -05:00
|
|
|
#include <string.h>
|
2012-05-29 17:37:02 +02:00
|
|
|
#include <stdarg.h>
|
2011-11-18 13:46:56 -05:00
|
|
|
|
2008-12-22 18:06:49 -05:00
|
|
|
#include "wayland-util.h"
|
2011-11-18 13:46:56 -05:00
|
|
|
#include "wayland-private.h"
|
2008-09-30 09:46:10 -04:00
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_list_init(struct wl_list *list)
|
2008-11-23 23:41:08 -05:00
|
|
|
{
|
|
|
|
|
list->prev = list;
|
|
|
|
|
list->next = list;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT void
|
2008-11-23 23:41:08 -05:00
|
|
|
wl_list_insert(struct wl_list *list, struct wl_list *elm)
|
|
|
|
|
{
|
|
|
|
|
elm->prev = list;
|
|
|
|
|
elm->next = list->next;
|
|
|
|
|
list->next = elm;
|
|
|
|
|
elm->next->prev = elm;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT void
|
2008-11-23 23:41:08 -05:00
|
|
|
wl_list_remove(struct wl_list *elm)
|
|
|
|
|
{
|
|
|
|
|
elm->prev->next = elm->next;
|
|
|
|
|
elm->next->prev = elm->prev;
|
2011-11-09 16:27:07 +02:00
|
|
|
elm->next = NULL;
|
|
|
|
|
elm->prev = NULL;
|
2008-11-23 23:41:08 -05:00
|
|
|
}
|
|
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT int
|
2013-04-03 17:29:16 +02:00
|
|
|
wl_list_length(const struct wl_list *list)
|
2008-11-23 23:41:08 -05:00
|
|
|
{
|
|
|
|
|
struct wl_list *e;
|
|
|
|
|
int count;
|
|
|
|
|
|
2008-12-14 15:52:34 -05:00
|
|
|
count = 0;
|
2008-11-23 23:41:08 -05:00
|
|
|
e = list->next;
|
|
|
|
|
while (e != list) {
|
|
|
|
|
e = e->next;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2008-11-28 18:35:25 -05:00
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT int
|
2013-04-03 17:29:16 +02:00
|
|
|
wl_list_empty(const struct wl_list *list)
|
2008-11-28 18:35:25 -05:00
|
|
|
{
|
|
|
|
|
return list->next == list;
|
|
|
|
|
}
|
2009-02-23 22:30:29 -05:00
|
|
|
|
2011-11-15 13:48:41 -05:00
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_list_insert_list(struct wl_list *list, struct wl_list *other)
|
|
|
|
|
{
|
2012-06-12 23:57:32 +02:00
|
|
|
if (wl_list_empty(other))
|
|
|
|
|
return;
|
|
|
|
|
|
2011-11-15 13:48:41 -05:00
|
|
|
other->next->prev = list;
|
|
|
|
|
other->prev->next = list->next;
|
|
|
|
|
list->next->prev = other->prev;
|
|
|
|
|
list->next = other->next;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT void
|
2009-02-23 22:30:29 -05:00
|
|
|
wl_array_init(struct wl_array *array)
|
|
|
|
|
{
|
|
|
|
|
memset(array, 0, sizeof *array);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT void
|
2009-02-23 22:30:29 -05:00
|
|
|
wl_array_release(struct wl_array *array)
|
|
|
|
|
{
|
|
|
|
|
free(array->data);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-18 09:47:34 -04:00
|
|
|
WL_EXPORT void *
|
2012-03-30 10:41:38 -04:00
|
|
|
wl_array_add(struct wl_array *array, size_t size)
|
2009-02-23 22:30:29 -05:00
|
|
|
{
|
2012-03-30 10:41:38 -04:00
|
|
|
size_t alloc;
|
2009-02-23 22:30:29 -05:00
|
|
|
void *data, *p;
|
|
|
|
|
|
|
|
|
|
if (array->alloc > 0)
|
|
|
|
|
alloc = array->alloc;
|
|
|
|
|
else
|
|
|
|
|
alloc = 16;
|
|
|
|
|
|
|
|
|
|
while (alloc < array->size + size)
|
|
|
|
|
alloc *= 2;
|
|
|
|
|
|
|
|
|
|
if (array->alloc < alloc) {
|
|
|
|
|
if (array->alloc > 0)
|
|
|
|
|
data = realloc(array->data, alloc);
|
|
|
|
|
else
|
|
|
|
|
data = malloc(alloc);
|
|
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
array->data = data;
|
|
|
|
|
array->alloc = alloc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = array->data + array->size;
|
|
|
|
|
array->size += size;
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
2011-01-27 11:57:19 -05:00
|
|
|
|
2012-10-11 23:37:43 +02:00
|
|
|
WL_EXPORT int
|
2011-01-27 11:57:19 -05:00
|
|
|
wl_array_copy(struct wl_array *array, struct wl_array *source)
|
|
|
|
|
{
|
2012-10-11 23:37:43 +02:00
|
|
|
if (array->size < source->size) {
|
|
|
|
|
if (!wl_array_add(array, source->size - array->size))
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
array->size = source->size;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-27 11:57:19 -05:00
|
|
|
memcpy(array->data, source->data, source->size);
|
2012-10-11 23:37:43 +02:00
|
|
|
return 0;
|
2011-01-27 11:57:19 -05:00
|
|
|
}
|
2011-08-19 22:50:53 -04:00
|
|
|
|
|
|
|
|
union map_entry {
|
|
|
|
|
uintptr_t next;
|
|
|
|
|
void *data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_map_init(struct wl_map *map)
|
|
|
|
|
{
|
|
|
|
|
memset(map, 0, sizeof *map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_map_release(struct wl_map *map)
|
|
|
|
|
{
|
2011-11-18 21:59:36 -05:00
|
|
|
wl_array_release(&map->client_entries);
|
|
|
|
|
wl_array_release(&map->server_entries);
|
2011-08-19 22:50:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WL_EXPORT uint32_t
|
2011-11-18 21:59:36 -05:00
|
|
|
wl_map_insert_new(struct wl_map *map, uint32_t side, void *data)
|
2011-08-19 22:50:53 -04:00
|
|
|
{
|
|
|
|
|
union map_entry *start, *entry;
|
2011-11-18 21:59:36 -05:00
|
|
|
struct wl_array *entries;
|
|
|
|
|
uint32_t base;
|
|
|
|
|
|
|
|
|
|
if (side == WL_MAP_CLIENT_SIDE) {
|
|
|
|
|
entries = &map->client_entries;
|
|
|
|
|
base = 0;
|
|
|
|
|
} else {
|
|
|
|
|
entries = &map->server_entries;
|
|
|
|
|
base = WL_SERVER_ID_START;
|
|
|
|
|
}
|
2011-08-19 22:50:53 -04:00
|
|
|
|
|
|
|
|
if (map->free_list) {
|
2011-11-18 21:59:36 -05:00
|
|
|
start = entries->data;
|
2011-08-19 22:50:53 -04:00
|
|
|
entry = &start[map->free_list >> 1];
|
|
|
|
|
map->free_list = entry->next;
|
|
|
|
|
} else {
|
2011-11-18 21:59:36 -05:00
|
|
|
entry = wl_array_add(entries, sizeof *entry);
|
2012-10-11 23:37:44 +02:00
|
|
|
if (!entry)
|
|
|
|
|
return 0;
|
2011-11-18 21:59:36 -05:00
|
|
|
start = entries->data;
|
2011-08-19 22:50:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry->data = data;
|
|
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
return (entry - start) + base;
|
2011-08-19 22:50:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WL_EXPORT int
|
|
|
|
|
wl_map_insert_at(struct wl_map *map, uint32_t i, void *data)
|
|
|
|
|
{
|
|
|
|
|
union map_entry *start;
|
|
|
|
|
uint32_t count;
|
2011-11-18 21:59:36 -05:00
|
|
|
struct wl_array *entries;
|
2011-08-19 22:50:53 -04:00
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
if (i < WL_SERVER_ID_START) {
|
|
|
|
|
entries = &map->client_entries;
|
|
|
|
|
} else {
|
|
|
|
|
entries = &map->server_entries;
|
|
|
|
|
i -= WL_SERVER_ID_START;
|
|
|
|
|
}
|
2011-08-19 22:50:53 -04:00
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
count = entries->size / sizeof *start;
|
2011-08-19 22:50:53 -04:00
|
|
|
if (count < i)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (count == i)
|
2011-11-18 21:59:36 -05:00
|
|
|
wl_array_add(entries, sizeof *start);
|
2011-08-19 22:50:53 -04:00
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
start = entries->data;
|
2011-08-19 22:50:53 -04:00
|
|
|
start[i].data = data;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-18 15:52:51 +02:00
|
|
|
WL_EXPORT int
|
|
|
|
|
wl_map_reserve_new(struct wl_map *map, uint32_t i)
|
|
|
|
|
{
|
|
|
|
|
union map_entry *start;
|
|
|
|
|
uint32_t count;
|
|
|
|
|
struct wl_array *entries;
|
|
|
|
|
|
|
|
|
|
if (i < WL_SERVER_ID_START) {
|
|
|
|
|
entries = &map->client_entries;
|
|
|
|
|
} else {
|
|
|
|
|
entries = &map->server_entries;
|
|
|
|
|
i -= WL_SERVER_ID_START;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count = entries->size / sizeof *start;
|
|
|
|
|
|
|
|
|
|
if (count < i)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (count == i) {
|
|
|
|
|
wl_array_add(entries, sizeof *start);
|
|
|
|
|
start = entries->data;
|
|
|
|
|
start[i].data = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
start = entries->data;
|
|
|
|
|
if (start[i].data != NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-19 22:50:53 -04:00
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_map_remove(struct wl_map *map, uint32_t i)
|
|
|
|
|
{
|
|
|
|
|
union map_entry *start;
|
2011-11-18 21:59:36 -05:00
|
|
|
struct wl_array *entries;
|
2011-08-19 22:50:53 -04:00
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
if (i < WL_SERVER_ID_START) {
|
|
|
|
|
entries = &map->client_entries;
|
|
|
|
|
} else {
|
|
|
|
|
entries = &map->server_entries;
|
|
|
|
|
i -= WL_SERVER_ID_START;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = entries->data;
|
2011-08-19 22:50:53 -04:00
|
|
|
start[i].next = map->free_list;
|
|
|
|
|
map->free_list = (i << 1) | 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WL_EXPORT void *
|
|
|
|
|
wl_map_lookup(struct wl_map *map, uint32_t i)
|
|
|
|
|
{
|
|
|
|
|
union map_entry *start;
|
|
|
|
|
uint32_t count;
|
2011-11-18 21:59:36 -05:00
|
|
|
struct wl_array *entries;
|
|
|
|
|
|
|
|
|
|
if (i < WL_SERVER_ID_START) {
|
|
|
|
|
entries = &map->client_entries;
|
|
|
|
|
} else {
|
|
|
|
|
entries = &map->server_entries;
|
|
|
|
|
i -= WL_SERVER_ID_START;
|
|
|
|
|
}
|
2011-08-19 22:50:53 -04:00
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
start = entries->data;
|
|
|
|
|
count = entries->size / sizeof *start;
|
2011-08-19 22:50:53 -04:00
|
|
|
|
|
|
|
|
if (i < count && !(start[i].next & 1))
|
|
|
|
|
return start[i].data;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
static void
|
|
|
|
|
for_each_helper(struct wl_array *entries, wl_iterator_func_t func, void *data)
|
2011-08-19 22:50:53 -04:00
|
|
|
{
|
|
|
|
|
union map_entry *start, *end, *p;
|
|
|
|
|
|
2011-11-18 21:59:36 -05:00
|
|
|
start = entries->data;
|
|
|
|
|
end = (union map_entry *) ((char *) entries->data + entries->size);
|
2011-08-19 22:50:53 -04:00
|
|
|
|
|
|
|
|
for (p = start; p < end; p++)
|
|
|
|
|
if (p->data && !(p->next & 1))
|
|
|
|
|
func(p->data, data);
|
|
|
|
|
}
|
2011-11-18 21:59:36 -05:00
|
|
|
|
|
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
|
|
|
|
|
{
|
|
|
|
|
for_each_helper(&map->client_entries, func, data);
|
|
|
|
|
for_each_helper(&map->server_entries, func, data);
|
|
|
|
|
}
|
2012-05-29 17:37:02 +02:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
wl_log_noop_handler(const char *fmt, va_list arg)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wl_log_func_t wl_log_handler = wl_log_noop_handler;
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
wl_log(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list argp;
|
|
|
|
|
|
|
|
|
|
va_start(argp, fmt);
|
|
|
|
|
wl_log_handler(fmt, argp);
|
|
|
|
|
va_end(argp);
|
|
|
|
|
}
|