summaryrefslogtreecommitdiff
path: root/fitz/res_store.c
blob: a76a9bb117b35b40aabba315fa9ebbe436f1c4c4 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "fitz.h"
#include "mupdf.h"

struct fz_item_s
{
	fz_obj *key;
	fz_storable *val;
	unsigned int size;
	fz_item *next;
	fz_item *prev;
	fz_store *store;
};

struct refkey
{
	fz_store_free_fn *free;
	int num;
	int gen;
};

struct fz_store_s
{
	/* Every item in the store is kept in a doubly linked list, ordered
	 * by usage (so LRU entries are at the end). */
	fz_item *head;
	fz_item *tail;

	/* We have a hash table that allows to quickly find a subset of the
	 * entries (those whose keys are indirect objects). */
	fz_hash_table *hash;

	/* We keep track of the size of the store, and keep it below max. */
	unsigned int max;
	unsigned int size;
};

void
fz_new_store_context(fz_context *ctx, unsigned int max)
{
	fz_store *store;
	store = fz_malloc(ctx, sizeof(fz_store));
	fz_try(ctx)
	{
		store->hash = fz_new_hash_table(ctx, 4096, sizeof(struct refkey));
	}
	fz_catch(ctx)
	{
		fz_free(ctx, store);
		fz_rethrow(ctx);
	}
	store->head = NULL;
	store->tail = NULL;
	store->size = 0;
	store->max = max;
	ctx->store = store;
}

void *
fz_keep_storable(fz_storable *s)
{
	if (s == NULL)
		return NULL;
	/* LOCK */
	if (s->refs > 0)
		s->refs++;
	/* UNLOCK */
	return s;
}

void
fz_drop_storable(fz_context *ctx, fz_storable *s)
{
	if (s == NULL)
		return;
	/* LOCK */
	if (s->refs < 0)
	{
		/* It's a static object. Dropping does nothing. */
	}
	else if (--s->refs == 0)
	{
		/* If we are dropping the last reference to an object, then
		 * it cannot possibly be in the store (as the store always
		 * keeps a ref to everything in it, and doesn't drop via
		 * this method. So we can simply drop the storable object
		 * itself without any operations on the fz_store. */
		s->free(ctx, s);
	}
	/* UNLOCK */
}

static void
evict(fz_context *ctx, fz_item *item)
{
	fz_store *store = ctx->store;

	store->size -= item->size;
	/* Unlink from the linked list */
	if (item->next)
		item->next->prev = item->prev;
	else
		store->tail = item->prev;
	if (item->prev)
		item->prev->next = item->next;
	else
		store->head = item->next;
	/* Remove from the hash table */
	if (fz_is_indirect(item->key))
	{
		struct refkey refkey;
		refkey.free = item->val->free;
		refkey.num = fz_to_num(item->key);
		refkey.gen = fz_to_gen(item->key);
		fz_hash_remove(store->hash, &refkey);
	}
	/* Drop a reference to the value (freeing if required) */
	if (item->val->refs > 0 && --item->val->refs == 0)
		item->val->free(ctx, item->val);
	/* Always drops the key and free the item */
	fz_drop_obj(item->key);
	fz_free(ctx, item);
}

static int
ensure_space(fz_context *ctx, unsigned int tofree)
{
	fz_item *item, *prev;
	unsigned int count;
	fz_store *store = ctx->store;

	/* First check that we *can* free tofree; if not, we'd rather not
	 * cache this. */
	count = 0;
	for (item = store->tail; item; item = item->prev)
	{
		if (item->val->refs == 1)
		{
			count += item->size;
			if (count >= tofree)
				break;
		}
	}

	/* If we ran out of items to search, then we can never free enough */
	if (item == NULL)
		return 1;

	/* Actually free the items */
	count = 0;
	for (item = store->tail; item; item = prev)
	{
		prev = item->prev;
		if (item->val->refs == 1)
		{
			/* Free this item */
			count += item->size;
			evict(ctx, item);

			if (count >= tofree)
				break;
		}
	}

	return 0;
}

void
fz_store_item(fz_context *ctx, fz_obj *key, void *val_, unsigned int itemsize)
{
	fz_item *item;
	unsigned int size;
	fz_storable *val = (fz_storable *)val_;
	fz_store *store = ctx->store;

	if (!store)
		return;

	item = fz_malloc(ctx, sizeof(*item));
	/* LOCK */
	size = store->size + itemsize;
	if (store->max != FZ_STORE_UNLIMITED && size > store->max && ensure_space(ctx, size - store->max))
	{
		fz_free(ctx, item);
		/* UNLOCK */
		return;
	}
	store->size += itemsize;

	item->key = fz_keep_obj(key);
	item->val = val;
	item->size = itemsize;
	item->next = NULL;
	if (val->refs > 0)
		val->refs++;

	/* If we can index it fast, put it into the hash table */
	if (fz_is_indirect(key))
	{
		struct refkey refkey;
		refkey.free = val->free;
		refkey.num = fz_to_num(key);
		refkey.gen = fz_to_gen(key);
		fz_hash_insert(store->hash, &refkey, item);
	}
	/* Regardless of whether it's indexed, it goes into the linked list */
	item->next = store->head;
	if (item->next)
		item->next->prev = item;
	else
		store->tail = item;
	store->head = item;
	item->prev = NULL;
	/* UNLOCK */
}

void *
fz_find_item(fz_context *ctx, fz_store_free_fn *free, fz_obj *key)
{
	struct refkey refkey;
	fz_item *item;
	fz_store *store = ctx->store;

	if (!store)
		return NULL;

	if (!key)
		return NULL;

	/* LOCK */
	if (fz_is_indirect(key))
	{
		/* We can find objects keyed on indirected objects quickly */
		refkey.free = free;
		refkey.num = fz_to_num(key);
		refkey.gen = fz_to_gen(key);
		item = fz_hash_find(store->hash, &refkey);
	}
	else
	{
		/* Others we have to hunt for slowly */
		for (item = store->head; item; item = item->next)
		{
			if (item->val->free == free && !fz_objcmp(item->key, key))
				break;
		}
	}
	if (item)
	{
		/* LRU: Move the block to the front */
		/* Unlink from present position */
		if (item->next)
			item->next->prev = item->prev;
		else
			store->tail = item->prev;
		if (item->prev)
			item->prev->next = item->next;
		else
			store->head = item->next;
		/* Insert at head */
		item->next = store->head;
		if (item->next)
			item->next->prev = item;
		else
			store->tail = item;
		item->prev = NULL;
		store->head = item;
		/* And bump the refcount before returning */
		if (item->val->refs > 0)
			item->val->refs++;
		/* UNLOCK */
		return (void *)item->val;
	}
	/* UNLOCK */

	return NULL;
}

void
fz_remove_item(fz_context *ctx, fz_store_free_fn *free, fz_obj *key)
{
	struct refkey refkey;
	fz_item *item;
	fz_store *store = ctx->store;

	/* LOCK */
	if (fz_is_indirect(key))
	{
		/* We can find objects keyed on indirect objects quickly */
		refkey.free = free;
		refkey.num = fz_to_num(key);
		refkey.gen = fz_to_gen(key);
		item = fz_hash_find(store->hash, &refkey);
		if (item)
			fz_hash_remove(store->hash, &refkey);
	}
	else
	{
		/* Others we have to hunt for slowly */
		for (item = store->head; item; item = item->next)
			if (item->val->free == free && !fz_objcmp(item->key, key))
				break;
	}
	if (item)
	{
		if (item->next)
			item->next->prev = item->prev;
		else
			store->tail = item->prev;
		if (item->prev)
			item->prev->next = item->next;
		else
			store->head = item->next;
		if (item->val->refs > 0 && --item->val->refs == 0)
			item->val->free(ctx, item->val);
		fz_drop_obj(item->key);
		fz_free(ctx, item);
	}
	/* UNLOCK */
}

void
fz_empty_store(fz_context *ctx)
{
	fz_item *item, *next;
	fz_store *store = ctx->store;

	if (store == NULL)
		return;

	/* LOCK */
	/* Run through all the items in the store */
	for (item = store->head; item; item = next)
	{
		next = item->next;
		evict(ctx, item);
	}
	/* UNLOCK */
}

void
fz_free_store_context(fz_context *ctx)
{
	if (ctx == NULL || ctx->store == NULL)
		return;
	fz_empty_store(ctx);
	fz_free_hash(ctx->store->hash);
	fz_free(ctx, ctx->store);
	ctx->store = NULL;
}

void
fz_debug_store(fz_context *ctx)
{
	fz_item *item;
	fz_store *store = ctx->store;

	printf("-- resource store contents --\n");

	/* LOCK */
	for (item = store->head; item; item = item->next)
	{
		printf("store[*][refs=%d][size=%d] ", item->val->refs, item->size);
		if (fz_is_indirect(item->key))
		{
			printf("(%d %d R) ", fz_to_num(item->key), fz_to_gen(item->key));
		} else
			fz_debug_obj(item->key);
		printf(" = %p\n", item->val);
	}
	/* UNLOCK */
}

static int
scavenge(fz_context *ctx, unsigned int tofree)
{
	fz_store *store = ctx->store;
	unsigned int count = 0;
	fz_item *item, *prev;

	/* Free the items */
	for (item = store->tail; item; item = prev)
	{
		prev = item->prev;
		if (item->val->refs == 1)
		{
			/* Free this item */
			count += item->size;
			evict(ctx, item);

			if (count >= tofree)
				break;
		}
	}
	/* Success is managing to evict any blocks */
	return count != 0;
}

int fz_store_scavenge(fz_context *ctx, unsigned int size, int *phase)
{
	fz_store *store;
	unsigned int max;

	if (ctx == NULL)
		return 0;
	store = ctx->store;
	if (store == NULL)
		return 0;

#ifdef DEBUG_SCAVENGING
	printf("Scavenging: store=%d size=%d phase=%d\n", store->size, size, *phase);
	fz_debug_store(ctx);
	Memento_stats();
#endif
	do
	{
		/* Calculate 'max' as the maximum size of the store for this phase */
		if (store->max != FZ_STORE_UNLIMITED)
			max = store->max / 16 * (16 - *phase);
		else
			max = store->size / (16 - *phase) * (15 - *phase);
		(*phase)++;

		if (size + store->size > max)
			if (scavenge(ctx, size + store->size - max))
			{
#ifdef DEBUG_SCAVENGING
				printf("scavenged: store=%d\n", store->size);
				fz_debug_store(ctx);
				Memento_stats();
#endif
				return 1;
			}
	}
	while (max > 0);

#ifdef DEBUG_SCAVENGING
	printf("scavenging failed\n");
	fz_debug_store(ctx);
	Memento_listBlocks();
#endif
	return 0;
}