ttf.c: glyph raster cache, fixed slots, no allocation (item 4.3.7d)

Punch list §25 item 4.3.7d complete. ttf_raster_cache_get() looks up
(font, codepoint, size_px) in a caller-owned fixed slot array, evicting
round-robin once full, rasterizing into a slot on a miss.

Verified live in tools/ttftest.c: an identical (font, 'A', 24px) call
made twice returns was_hit=0 then was_hit=1, and the slot's own hits
counter reads exactly 1 afterward -- checked programmatically. A
different-codepoint call misses again, proving the key actually
discriminates. Wall-clock timing (miss 0.040ms vs hit 0.001ms) is
printed as informational corroboration only, not the load-bearing
check.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Robert Allan James
2026-08-10 22:31:56 -04:00
co-authored by Claude Sonnet 5
parent 095860251a
commit 2db4603d04
5 changed files with 219 additions and 2 deletions
+51
View File
@@ -23,6 +23,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../include/starkernel/ttf.h"
@@ -221,6 +222,54 @@ static void test_rasterize(const ttf_font_t *font, uint32_t codepoint, const cha
free(pixels);
}
/* Raster cache (4.3.7d): first ttf_raster_cache_get() for a given
* (font, codepoint, size) must miss and rasterize; the second, identical
* call must hit -- checked both via the returned *was_hit flag and the
* slot's own hits counter (the item's own "measurable... counter or
* timing difference" bar), plus a wall-clock comparison as corroborating
* (not load-bearing -- host timing is noisy) evidence. A third call for
* a different codepoint at the same size must miss again, proving the
* cache actually discriminates on key, not just "always says hit". */
static void test_raster_cache(const ttf_font_t *font) {
enum { SLOTS = 4 };
ttf_raster_cache_slot_t slot_storage[SLOTS];
ttf_raster_cache_t cache;
ttf_bitmap_t bmp;
int was_hit, rc;
clock_t t0, t1, t2;
ttf_raster_cache_init(&cache, slot_storage, SLOTS);
t0 = clock();
rc = ttf_raster_cache_get(&cache, font, 0x0041, 24, &bmp, &was_hit);
t1 = clock();
if (rc != TTF_OK || was_hit) {
printf(" FAIL raster_cache: first call rc=%d was_hit=%d (want TTF_OK, miss)\n",
rc, was_hit);
failures++;
}
rc = ttf_raster_cache_get(&cache, font, 0x0041, 24, &bmp, &was_hit);
t2 = clock();
if (rc != TTF_OK || !was_hit) {
printf(" FAIL raster_cache: second (identical) call rc=%d was_hit=%d (want TTF_OK, hit)\n",
rc, was_hit);
failures++;
}
check_eq_i("raster_cache hits", 0x0041, slot_storage[0].hits, 1);
printf("raster_cache: miss=%.3fms hit=%.3fms (hit expected <= miss on a real cache;"
" host clock() is coarse/noisy, informational only)\n",
1000.0 * (t1 - t0) / CLOCKS_PER_SEC, 1000.0 * (t2 - t1) / CLOCKS_PER_SEC);
rc = ttf_raster_cache_get(&cache, font, 0x0061, 24, &bmp, &was_hit);
if (rc != TTF_OK || was_hit) {
printf(" FAIL raster_cache: different codepoint call rc=%d was_hit=%d (want TTF_OK, miss)\n",
rc, was_hit);
failures++;
}
}
int main(int argc, char **argv) {
FILE *fp;
long size;
@@ -287,6 +336,8 @@ int main(int argc, char **argv) {
test_rasterize(&font, 0x002E, "'.'");
test_rasterize(&font, 0x0061, "'a'");
test_raster_cache(&font);
free(buf);
if (failures) {