[go: up one dir, main page]

Menu

[r664]: / tags / 0.3 / cache.c  Maximize  Restore  History

Download this file

401 lines (338 with data), 9.4 kB

  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
/*
rdesktop: A Remote Desktop Protocol client.
Cache routines
Copyright (C) Matthew Chapman 1999-2005
Copyright (C) Jeroen Meijer 2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "rdesktop.h"
#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
#define IS_PERSISTENT(id) (conn->pstcacheFd[id] > 0)
#define TO_TOP -1
#define CACHE_IS_SET(idx) (idx >= 0)
/*
* TODO: Test for optimal value of BUMP_COUNT. TO_TOP gives lowest cpu utilisation but using
* a positive value will hopefully result in less frequently used bitmaps having a greater chance
* of being evicted from the cache, and therby reducing the need to load bitmaps from disk.
* (Jeroen)
*/
#define BUMP_COUNT 40
static void cache_bump_bitmap(rdcConnection conn, uint8 id, uint16 idx, int bump);
static void cache_evict_bitmap(rdcConnection conn, uint8 id);
/* Setup the bitmap cache lru/mru linked list */
void
cache_rebuild_bmpcache_linked_list(rdcConnection conn, uint8 id, sint16 * idx, int count)
{
int n = count, c = 0;
sint16 n_idx;
/* find top, skip evicted bitmaps */
while (--n >= 0 && conn->bmpcache[id][idx[n]].bitmap == NULL);
if (n < 0)
{
conn->bmpcacheMru[id] = conn->bmpcacheLru[id] = NOT_SET;
return;
}
conn->bmpcacheMru[id] = idx[n];
conn->bmpcache[id][idx[n]].next = NOT_SET;
n_idx = idx[n];
c++;
/* link list */
while (n >= 0)
{
/* skip evicted bitmaps */
while (--n >= 0 && conn->bmpcache[id][idx[n]].bitmap == NULL);
if (n < 0)
break;
conn->bmpcache[id][n_idx].previous = idx[n];
conn->bmpcache[id][idx[n]].next = n_idx;
n_idx = idx[n];
c++;
}
conn->bmpcache[id][n_idx].previous = NOT_SET;
conn->bmpcacheLru[id] = n_idx;
if (c != conn->bmpcacheCount[id])
{
error("Oops. %d in bitmap cache linked list, %d in ui cache...\n", c,
conn->bmpcacheCount[id]);
exit(1);
}
}
/* Move a bitmap to a new position in the linked list. */
static void
cache_bump_bitmap(rdcConnection conn, uint8 id, uint16 idx, int bump)
{
int p_idx, n_idx, n;
if (!IS_PERSISTENT(id))
return;
if (conn->bmpcacheMru[id] == idx)
return;
DEBUG_RDP5(("bump bitmap: id=%d, idx=%d, bump=%d\n", id, idx, bump));
n_idx = conn->bmpcache[id][idx].next;
p_idx = conn->bmpcache[id][idx].previous;
if (CACHE_IS_SET(n_idx))
{
/* remove */
--conn->bmpcacheCount[id];
if (CACHE_IS_SET(p_idx))
conn->bmpcache[id][p_idx].next = n_idx;
else
conn->bmpcacheLru[id] = n_idx;
if (CACHE_IS_SET(n_idx))
conn->bmpcache[id][n_idx].previous = p_idx;
else
conn->bmpcacheMru[id] = p_idx;
}
else
{
p_idx = NOT_SET;
n_idx = conn->bmpcacheLru[id];
}
if (bump >= 0)
{
for (n = 0; n < bump && CACHE_IS_SET(n_idx); n++)
{
p_idx = n_idx;
n_idx = conn->bmpcache[id][p_idx].next;
}
}
else
{
p_idx = conn->bmpcacheMru[id];
n_idx = NOT_SET;
}
/* insert */
++conn->bmpcacheCount[id];
conn->bmpcache[id][idx].previous = p_idx;
conn->bmpcache[id][idx].next = n_idx;
if (p_idx >= 0)
conn->bmpcache[id][p_idx].next = idx;
else
conn->bmpcacheLru[id] = idx;
if (n_idx >= 0)
conn->bmpcache[id][n_idx].previous = idx;
else
conn->bmpcacheMru[id] = idx;
}
/* Evict the least-recently used bitmap from the cache */
static void
cache_evict_bitmap(rdcConnection conn, uint8 id)
{
uint16 idx;
int n_idx;
if (!IS_PERSISTENT(id))
return;
idx = conn->bmpcacheLru[id];
n_idx = conn->bmpcache[id][idx].next;
DEBUG_RDP5(("evict bitmap: id=%d idx=%d n_idx=%d bmp=0x%x\n", id, idx, n_idx,
conn->bmpcache[id][idx].bitmap));
ui_destroy_bitmap(conn->bmpcache[id][idx].bitmap);
--conn->bmpcacheCount[id];
conn->bmpcache[id][idx].bitmap = 0;
conn->bmpcacheLru[id] = n_idx;
conn->bmpcache[id][n_idx].previous = NOT_SET;
pstcache_touch_bitmap(conn, id, idx, 0);
}
/* Retrieve a bitmap from the cache */
HBITMAP
cache_get_bitmap(rdcConnection conn, uint8 id, uint16 idx)
{
if ((id < NUM_ELEMENTS(conn->bmpcache)) && (idx < NUM_ELEMENTS(conn->bmpcache[0])))
{
if (conn->bmpcache[id][idx].bitmap || pstcache_load_bitmap(conn, id, idx))
{
if (IS_PERSISTENT(id))
cache_bump_bitmap(conn, id, idx, BUMP_COUNT);
return conn->bmpcache[id][idx].bitmap;
}
}
else if ((id < NUM_ELEMENTS(conn->volatileBc)) && (idx == 0x7fff))
{
return conn->volatileBc[id];
}
error("get bitmap %d:%d\n", id, idx);
return NULL;
}
/* Store a bitmap in the cache */
void
cache_put_bitmap(rdcConnection conn, uint8 id, uint16 idx, HBITMAP bitmap)
{
HBITMAP old;
if ((id < NUM_ELEMENTS(conn->bmpcache)) && (idx < NUM_ELEMENTS(conn->bmpcache[0])))
{
old = conn->bmpcache[id][idx].bitmap;
if (old != NULL)
ui_destroy_bitmap(old);
conn->bmpcache[id][idx].bitmap = bitmap;
if (IS_PERSISTENT(id))
{
if (old == NULL)
conn->bmpcache[id][idx].previous = conn->bmpcache[id][idx].next = NOT_SET;
cache_bump_bitmap(conn, id, idx, TO_TOP);
if (conn->bmpcacheCount[id] > BMPCACHE2_C2_CELLS)
cache_evict_bitmap(conn, id);
}
}
else if ((id < NUM_ELEMENTS(conn->volatileBc)) && (idx == 0x7fff))
{
old = conn->volatileBc[id];
if (old != NULL)
ui_destroy_bitmap(old);
conn->volatileBc[id] = bitmap;
}
else
{
error("put bitmap %d:%d\n", id, idx);
}
}
/* Updates the persistent bitmap cache MRU information on exit */
void
cache_save_state(rdcConnection conn)
{
uint32 id = 0, t = 0;
int idx;
for (id = 0; id < NUM_ELEMENTS(conn->bmpcache); id++)
if (IS_PERSISTENT(id))
{
DEBUG_RDP5(("Saving cache state for bitmap cache %d...", id));
idx = conn->bmpcacheLru[id];
while (idx >= 0)
{
pstcache_touch_bitmap(conn, id, idx, ++t);
idx = conn->bmpcache[id][idx].next;
}
DEBUG_RDP5((" %d stamps written.\n", t));
}
}
/* Retrieve a glyph from the font cache */
FONTGLYPH *
cache_get_font(rdcConnection conn, uint8 font, uint16 character)
{
FONTGLYPH *glyph;
if ((font < NUM_ELEMENTS(conn->fontCache)) && (character < NUM_ELEMENTS(conn->fontCache[0])))
{
glyph = &conn->fontCache[font][character];
if (glyph->pixmap != NULL)
return glyph;
}
error("get font %d:%d\n", font, character);
return NULL;
}
/* Store a glyph in the font cache */
void
cache_put_font(rdcConnection conn, uint8 font, uint16 character, uint16 offset,
uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap)
{
FONTGLYPH *glyph;
if ((font < NUM_ELEMENTS(conn->fontCache)) && (character < NUM_ELEMENTS(conn->fontCache[0])))
{
glyph = &conn->fontCache[font][character];
if (glyph->pixmap != NULL)
ui_destroy_glyph(glyph->pixmap);
glyph->offset = offset;
glyph->baseline = baseline;
glyph->width = width;
glyph->height = height;
glyph->pixmap = pixmap;
}
else
{
error("put font %d:%d\n", font, character);
}
}
/* Retrieve a text item from the cache */
DATABLOB *
cache_get_text(rdcConnection conn, uint8 cache_id)
{
DATABLOB *text;
text = &conn->textCache[cache_id];
return text;
}
/* Store a text item in the cache */
void
cache_put_text(rdcConnection conn, uint8 cache_id, void *data, int length)
{
DATABLOB *text;
text = &conn->textCache[cache_id];
if (text->data != NULL)
xfree(text->data);
text->data = xmalloc(length);
text->size = length;
memcpy(text->data, data, length);
}
/* Retrieve desktop data from the cache */
uint8 *
cache_get_desktop(rdcConnection conn, uint32 offset, int cx, int cy, int bytes_per_pixel)
{
int length = cx * cy * bytes_per_pixel;
if (offset > sizeof(conn->deskCache))
offset = 0;
if ((offset + length) <= sizeof(conn->deskCache))
{
return &conn->deskCache[offset];
}
error("get desktop %d:%d\n", offset, length);
return NULL;
}
/* Store desktop data in the cache */
void
cache_put_desktop(rdcConnection conn, uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 * data)
{
int length = cx * cy * bytes_per_pixel;
if (offset > sizeof(conn->deskCache))
offset = 0;
if ((offset + length) <= sizeof(conn->deskCache))
{
cx *= bytes_per_pixel;
while (cy--)
{
memcpy(&conn->deskCache[offset], data, cx);
data += scanline;
offset += cx;
}
}
else
{
error("put desktop %d:%d\n", offset, length);
}
}
/* Retrieve cursor from cache */
HCURSOR
cache_get_cursor(rdcConnection conn, uint16 cache_idx)
{
HCURSOR cursor;
if (cache_idx < NUM_ELEMENTS(conn->cursorCache))
{
cursor = conn->cursorCache[cache_idx];
if (cursor != NULL)
return cursor;
}
error("get cursor %d\n", cache_idx);
return NULL;
}
/* Store cursor in cache */
void
cache_put_cursor(rdcConnection conn, uint16 cache_idx, HCURSOR cursor)
{
HCURSOR old;
if (cache_idx < NUM_ELEMENTS(conn->cursorCache))
{
old = conn->cursorCache[cache_idx];
if (old != NULL)
ui_destroy_cursor(old);
conn->cursorCache[cache_idx] = cursor;
}
else
{
error("put cursor %d\n", cache_idx);
}
}