summaryrefslogtreecommitdiff
path: root/sys/src/libcontrol/cache.c
blob: ab38d3084a98eff1f9a8b0fc6b626d02b48aa774 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>
#include <control.h>

typedef struct Cache Cache;

struct Cache
{
	char		*name;
	CCache	**cache;
	int		ncache;
};

static struct Cache imagecache = {"image"};
static struct Cache fontcache = {"font"};

static CCache*
getcacheitem(Cache *c, char *name)
{
	int i;

	for(i=0; i<c->ncache; i++)
		if(c->cache[i]!=nil && strcmp(c->cache[i]->name, name)==0){
			c->cache[i]->ref++;
			return c->cache[i];
		}
	return nil;
}

static int
namecacheitem(Cache *c, void *image, char *name)
{
	int i, free;
	CCache *cc;

	free = -1;
	for(i=0; i<c->ncache; i++){
		if(c->cache[i] == nil){
			free = i;
			continue;
		}
		if(strcmp(c->cache[i]->name, name) == 0){
			werrstr("%s name %q already in use", c->name, name);
			return -1;
		}
	}
	cc = ctlmalloc(sizeof(CCache));
	cc->image = image;
	cc->name = ctlstrdup(name);
	if(free >= 0){
		cc->index = free;
		c->cache[free] = cc;
	}else{
		cc->index = c->ncache;
		c->cache = ctlrealloc(c->cache, (c->ncache+1)*sizeof(CCache*));
		c->cache[c->ncache++] = cc;
	}
	cc->ref = 1;
	return 1;
}

static int
freecacheitem(Cache *c, char *name)
{
	CCache	*cc;

	cc = getcacheitem(c, name);
	if(cc == nil){
		werrstr("%s name %q not in use", c->name, name);
		return -1;
	}
	cc->ref--;	/* getcacheitem increments ref */
	if(cc->ref-- == 1){
		/* client must free object itself */
		free(cc->name);
		c->cache[cc->index] = nil;
		free(cc);
	}
	return 0;
}

static void
putcacheitem(CCache *cc)
{
	if(cc == nil)
		return;
	cc->ref--;
}

static void
setcacheitemptr(Cache *c, Control *ctl, CCache **cp, char *s)
{
	CCache *ci;

	ci = getcacheitem(c, s);
	if(ci == nil)
		ctlerror("%q: %s name %q not defined", ctl->name, c->name, s);
	putcacheitem(*cp);
	*cp = ci;
}

/* Images */

CImage*
_getctlimage(char *name)
{
	return getcacheitem(&imagecache, name);
}

void
_putctlimage(CImage *c)
{
	putcacheitem(c);
}

int
namectlimage(Image *image, char *name)
{
	return namecacheitem(&imagecache, image, name);
}

int
freectlimage(char *name)
{
	return freecacheitem(&imagecache, name);
}

void
_setctlimage(Control *c, CImage **cp, char *s)
{
	setcacheitemptr(&imagecache, c, cp, s);
}

/* Fonts */

CFont*
_getctlfont(char *name)
{
	return getcacheitem(&fontcache, name);
}

void
_putctlfont(CFont *c)
{
	putcacheitem(c);
}

int
namectlfont(Font *font, char *name)
{
	return namecacheitem(&fontcache, font, name);
}

int
freectlfont(char *name)
{
	return freecacheitem(&fontcache, name);
}

void
_setctlfont(Control *c, CFont **cp, char *s)
{
	setcacheitemptr(&fontcache, c, cp, s);
}