diff options
| author | Jason Rumney | 1999-06-13 17:49:12 +0000 |
|---|---|---|
| committer | Jason Rumney | 1999-06-13 17:49:12 +0000 |
| commit | 9c332a804d6c9508659ad2f4c7a5ec0ea7dcce7d (patch) | |
| tree | 4bdf582f637bc244053555fb586d91e98b6a63a7 | |
| parent | bcc6796524f5432fe1d9f889e4b4cb6860982902 (diff) | |
| download | emacs-9c332a804d6c9508659ad2f4c7a5ec0ea7dcce7d.tar.gz emacs-9c332a804d6c9508659ad2f4c7a5ec0ea7dcce7d.zip | |
(clear_cached_bitmap_slots): Remove.
(get_bitmap_with_cache): Check if CreateBitmap failed.
Adjust cache size dynamically so cache is never larger than the
system limit of GDI resources.
Do cache clearing inline. Move global variables to local scope.
| -rw-r--r-- | src/w32bdf.c | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/src/w32bdf.c b/src/w32bdf.c index 5b51db8e012..e287f499ec1 100644 --- a/src/w32bdf.c +++ b/src/w32bdf.c | |||
| @@ -34,15 +34,15 @@ Boston, MA 02111-1307, USA. */ | |||
| 34 | #define min(a, b) ((a) < (b) ? (a) : (b)) | 34 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 35 | #define max(a, b) ((a) > (b) ? (a) : (b)) | 35 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 36 | 36 | ||
| 37 | /* Portion of GDI Objects which the font cache is allowed to use. This | ||
| 38 | can be quite high, since the font cache is the only part of Emacs | ||
| 39 | that uses a large number of GDI objects, but there should still be | ||
| 40 | some GDI objects reserved for other uses. */ | ||
| 41 | #define CACHE_GDI_ALLOWANCE 9 / 10 | ||
| 42 | |||
| 37 | void w32_free_bdf_font(bdffont *fontp); | 43 | void w32_free_bdf_font(bdffont *fontp); |
| 38 | bdffont *w32_init_bdf_font(char *filename); | 44 | bdffont *w32_init_bdf_font(char *filename); |
| 39 | 45 | ||
| 40 | cache_bitmap cached_bitmap_slots[BDF_FONT_CACHE_SIZE]; | ||
| 41 | cache_bitmap *pcached_bitmap_latest = cached_bitmap_slots; | ||
| 42 | |||
| 43 | #define FONT_CACHE_SLOT_OVER_P(p) \ | ||
| 44 | ((p) >= cached_bitmap_slots + BDF_FONT_CACHE_SIZE) | ||
| 45 | |||
| 46 | static int | 46 | static int |
| 47 | search_file_line(char *key, char *start, int len, char **val, char **next) | 47 | search_file_line(char *key, char *start, int len, char **val, char **next) |
| 48 | { | 48 | { |
| @@ -368,27 +368,6 @@ seek_char(bdffont *fontp, int index) | |||
| 368 | return result; | 368 | return result; |
| 369 | } | 369 | } |
| 370 | 370 | ||
| 371 | void | ||
| 372 | clear_cached_bitmap_slots() | ||
| 373 | { | ||
| 374 | int i; | ||
| 375 | cache_bitmap *p; | ||
| 376 | |||
| 377 | p = pcached_bitmap_latest; | ||
| 378 | for (i = 0;i < BDF_FONT_CLEAR_SIZE;i++) | ||
| 379 | { | ||
| 380 | if (p->psrc) | ||
| 381 | { | ||
| 382 | DeleteObject(p->hbmp); | ||
| 383 | p->psrc->pcbmp = NULL; | ||
| 384 | p->psrc = NULL; | ||
| 385 | } | ||
| 386 | p++; | ||
| 387 | if (FONT_CACHE_SLOT_OVER_P(p)) | ||
| 388 | p = cached_bitmap_slots; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 392 | #define GET_HEX_VAL(x) ((isdigit(x)) ? ((x) - '0') : \ | 371 | #define GET_HEX_VAL(x) ((isdigit(x)) ? ((x) - '0') : \ |
| 393 | (((x) >= 'A') && ((x) <= 'Z')) ? ((x) - 'A' + 10) : \ | 372 | (((x) >= 'A') && ((x) <= 'Z')) ? ((x) - 'A' + 10) : \ |
| 394 | (((x) >= 'a') && ((x) <= 'z')) ? ((x) - 'a' + 10) : \ | 373 | (((x) >= 'a') && ((x) <= 'z')) ? ((x) - 'a' + 10) : \ |
| @@ -474,6 +453,8 @@ w32_get_bdf_glyph(bdffont *fontp, int index, int size, glyph_struct *glyph) | |||
| 474 | return 1; | 453 | return 1; |
| 475 | } | 454 | } |
| 476 | 455 | ||
| 456 | #define NEXT_CACHE_SLOT(n) (((n) + 1 >= BDF_FONT_CACHE_SIZE) ? 0 : ((n) + 1)) | ||
| 457 | |||
| 477 | static | 458 | static |
| 478 | cache_bitmap* | 459 | cache_bitmap* |
| 479 | get_bitmap_with_cache(bdffont *fontp, int index) | 460 | get_bitmap_with_cache(bdffont *fontp, int index) |
| @@ -483,6 +464,11 @@ get_bitmap_with_cache(bdffont *fontp, int index) | |||
| 483 | cache_bitmap* pcb; | 464 | cache_bitmap* pcb; |
| 484 | HBITMAP hbmp; | 465 | HBITMAP hbmp; |
| 485 | glyph_struct glyph; | 466 | glyph_struct glyph; |
| 467 | static cache_bitmap cached_bitmap_slots[BDF_FONT_CACHE_SIZE]; | ||
| 468 | static int cache_in_slot = 0; /* the next slot to use */ | ||
| 469 | static int cache_out_slot = 0; /* the last slot allocated */ | ||
| 470 | static int cache_occupancy = 0; /* current cache occupancy */ | ||
| 471 | static int cache_limit = BDF_FONT_CACHE_SIZE; /* allowed maximum occupancy */ | ||
| 486 | 472 | ||
| 487 | pch = get_cached_font_char(fontp, index); | 473 | pch = get_cached_font_char(fontp, index); |
| 488 | if (pch) | 474 | if (pch) |
| @@ -503,9 +489,35 @@ get_bitmap_with_cache(bdffont *fontp, int index) | |||
| 503 | 489 | ||
| 504 | hbmp = CreateBitmap(glyph.metric.bbw, glyph.metric.bbh, 1, 1, glyph.bitmap); | 490 | hbmp = CreateBitmap(glyph.metric.bbw, glyph.metric.bbh, 1, 1, glyph.bitmap); |
| 505 | 491 | ||
| 506 | pcb = pcached_bitmap_latest; | 492 | /* if bitmap allocation fails reduce the limit of the occupancy so |
| 507 | if (pcb->psrc) | 493 | that we can hope it will not happen again. */ |
| 508 | clear_cached_bitmap_slots(); | 494 | if (hbmp == NULL) |
| 495 | cache_limit = cache_occupancy * CACHE_GDI_ALLOWANCE; | ||
| 496 | |||
| 497 | /* if cache occupancy reaches at the limit release some cache slots */ | ||
| 498 | if (cache_occupancy >= cache_limit) | ||
| 499 | { | ||
| 500 | register int size_to_clear = cache_limit * BDF_FONT_CLEAR_SIZE | ||
| 501 | / BDF_FONT_CACHE_SIZE; | ||
| 502 | for (; size_to_clear; size_to_clear--, | ||
| 503 | cache_out_slot = NEXT_CACHE_SLOT(cache_out_slot)) | ||
| 504 | { | ||
| 505 | register cache_bitmap *p = &cached_bitmap_slots[cache_out_slot]; | ||
| 506 | if (p->psrc) | ||
| 507 | { | ||
| 508 | DeleteObject(p->hbmp); | ||
| 509 | p->psrc->pcbmp = NULL; | ||
| 510 | p->psrc = NULL; | ||
| 511 | cache_occupancy--; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | if (hbmp == NULL) | ||
| 517 | hbmp = CreateBitmap (glyph.metric.bbw, glyph.metric.bbh, | ||
| 518 | 1, 1, glyph.bitmap); | ||
| 519 | |||
| 520 | pcb = &cached_bitmap_slots[cache_in_slot]; | ||
| 509 | 521 | ||
| 510 | pcb->psrc = pch; | 522 | pcb->psrc = pch; |
| 511 | pcb->metric = glyph.metric; | 523 | pcb->metric = glyph.metric; |
| @@ -513,9 +525,8 @@ get_bitmap_with_cache(bdffont *fontp, int index) | |||
| 513 | 525 | ||
| 514 | pch->pcbmp = pcb; | 526 | pch->pcbmp = pcb; |
| 515 | 527 | ||
| 516 | pcached_bitmap_latest++; | 528 | cache_in_slot = NEXT_CACHE_SLOT(cache_in_slot); |
| 517 | if (FONT_CACHE_SLOT_OVER_P(pcached_bitmap_latest)) | 529 | cache_occupancy++; |
| 518 | pcached_bitmap_latest = cached_bitmap_slots; | ||
| 519 | 530 | ||
| 520 | return pcb; | 531 | return pcb; |
| 521 | } | 532 | } |