summaryrefslogtreecommitdiff
path: root/resource.c
blob: 38f16be8eaf605ccf57b8e1eac36f41997dd3995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <stdint.h>
#include <stdlib.h>
#include <wayland-client.h>

#include <X11/Xlib.h>

#include "private.h"

union entry {
	uintptr_t next;
	void *data;
};

XID
csx_display_add_resource(struct csx_display *display, void *p)
{
	union entry *e, *map;
	int size;

	if (display->free_resource > 0) {
		e = display->resource_map + display->free_resource;
		display->free_resource = e->next;
	} else {
		if (display->resource_count == display->resource_size) {
			if (display->resource_size == 0)
				size = 256;
			else
				size = display->resource_size * 2;
			map = realloc(display->resource_map,
				      size * sizeof *e);
			if (map == NULL)
				return None;
			display->resource_map = map;
			display->resource_size = size;
		}

		e = display->resource_map + display->resource_count;
		display->resource_count++;
	}

	e->data = p;

	return e - display->resource_map;
}

void
csx_display_remove_resource(struct csx_display *display, XID id)
{
	union entry *e;

	e = display->resource_map + id;
	e->next = display->free_resource;
	display->free_resource = id;
}

void *
csx_display_lookup_resource(struct csx_display *display, XID id)
{
	/* FIXME: verify that we're looking up a valid resource:
	 * inside valid range and not in free-list. */

	if (id >= display->resource_size)
		return NULL;

	return display->resource_map[id].data;
}