File: imgcache.c

package info (click to toggle)
links2 2.1pre37-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,556 kB
  • ctags: 6,227
  • sloc: ansic: 115,566; sh: 3,335; cpp: 530; makefile: 116; awk: 47; perl: 34
file content (149 lines) | stat: -rw-r--r-- 2,971 bytes parent folder | download | duplicates (3)
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
/* imgcache.c
 * Image cache
 * (c) 2002 Mikulas Patocka
 * This file is a part of the Links program, released under GPL.
 */

#include "cfg.h"

#ifdef G

#include "links.h"

struct list_head image_cache = { &image_cache, &image_cache };

/* prototypes */
int image_size(struct cached_image *);
int shrink_image_cache(int);

/* xyw_meaning either MEANING_DIMS or MEANING_AUTOSCALE. */
struct cached_image *find_cached_image(int bg, unsigned char *url, int xw, int
		yw, int xyw_meaning, int scale, int aspect)
{
	struct cached_image *i;
	if (xw>=0&&yw>=0&&xyw_meaning==MEANING_DIMS){
		/* The xw and yw is already scaled so that scale and
		 * aspect don't matter.
		 */
		foreach (i, image_cache) {
			if (i->background_color == bg
				&& !strcmp(i->url, url)
				&& i->wanted_xw==xw
				&& i->wanted_yw==yw
				&& i->wanted_xyw_meaning==xyw_meaning
				) goto hit;
		}
	}else{
		foreach (i, image_cache) {
			if (i->background_color == bg
				&& !strcmp(i->url, url)
				&& i->wanted_xw==xw
				&& i->wanted_yw==yw
				&& i->wanted_xyw_meaning==xyw_meaning 
				&& i->scale==scale
				&& i->aspect==aspect) goto hit;
		}
	}
	return NULL;

hit:		
	i->refcount++;
	del_from_list(i);
	add_to_list(image_cache, i);
	return i;
}

void add_image_to_cache(struct cached_image *ci)
{
	add_to_list(image_cache, ci);
}

int image_size(struct cached_image *cimg)
{
	int siz = 100;
	switch(cimg->state){
		case 0:
		case 1:
		case 2:
		case 3:
		case 8:
		case 9:
		case 10:
		case 11:			
		break;

		case 12:
		case 14:
		siz+=cimg->width*cimg->height*cimg->buffer_bytes_per_pixel;
		if (cimg->bmp.user){
			case 13:
			case 15:	
			siz+=cimg->bmp.x*cimg->bmp.y*(drv->depth&7);
		}
		break;

#ifdef DEBUG
		default:
		fprintf(stderr,"cimg->state=%d\n",cimg->state);
		internal("Invalid cimg->state in image_size\n");
		break;
#endif /* #ifdef DEBUG */
	}
	return siz;
}

int shrink_image_cache(int u)
{
	struct cached_image *i;
	int si = 0;
	int r = 0;
	foreach(i, image_cache) if (!i->refcount) si += image_size(i);
	if (u == SH_FREE_SOMETHING) si = image_cache_size + si * 1 / 4;
	while ((si >= image_cache_size || u == SH_FREE_ALL) && !list_empty(image_cache)) {
		i = image_cache.prev;
		while (i->refcount) {
			i = i->prev;
			if ((void *)i == &image_cache) goto no;
		}
		r |= ST_SOMETHING_FREED;
		si -= image_size(i);
		del_from_list(i);
		img_destruct_cached_image(i);
	}
	no:
	return r | (list_empty(image_cache) ? ST_CACHE_EMPTY : 0);
}

int imgcache_info(int type)
{
	struct cached_image *i;
	int n = 0;
	foreach(i, image_cache) {
		switch (type) {
			case CI_BYTES:
				n += image_size(i);
				break;
			case CI_LOCKED:
				if (!i->refcount) break;
				/* fall through */
			case CI_FILES:
				n++;
				break;
			default:
				internal("imgcache_info: query %d", type);
		}
	}
	return n;
}

void init_imgcache(void)
{
	register_cache_upcall(shrink_image_cache, "imgcache");
}

/*
del_from_list ...
void img_destruct_cached_image(sturct cached_image *img);
*/

#endif