diff options
author | Aidan MacDonald <amachronic@protonmail.com> | 2022-10-15 23:30:38 +0100 |
---|---|---|
committer | Aidan MacDonald <amachronic@protonmail.com> | 2023-01-13 04:54:05 -0500 |
commit | 879888b158376f1ea2c92dd49e0c7617d07fd5b2 (patch) | |
tree | 8b84b11823fb2692e022fca11e6a1d11dc1e1e9b | |
parent | ebebef556652bd271000727484b72e19970477e1 (diff) | |
download | rockbox-879888b158376f1ea2c92dd49e0c7617d07fd5b2.tar.gz rockbox-879888b158376f1ea2c92dd49e0c7617d07fd5b2.zip |
Avoid using buflib names for storing font paths
Naming your allocations is more of a debugging feature; it's a
bad idea to abuse this feature for storing some random string.
The font code does exactly that to remember the path used to
load a font, and it's the only code that uses buflib names in
this way.
Store the path inside the font allocation instead as a regular
char array. For simplicity it's MAX_PATH sized, so this'll use
a bit more memory than the buflib name method, maybe 1-2 KiB if
a few fonts are loaded.
Change-Id: Ie286a1a273d6477af9e5d3f76e0534b62f72e8f7
-rw-r--r-- | firmware/font.c | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/firmware/font.c b/firmware/font.c index d7473346da..8f0808ba87 100644 --- a/firmware/font.c +++ b/firmware/font.c | |||
@@ -90,6 +90,7 @@ extern struct font sysfont; | |||
90 | 90 | ||
91 | struct buflib_alloc_data { | 91 | struct buflib_alloc_data { |
92 | struct font font; /* must be the first member! */ | 92 | struct font font; /* must be the first member! */ |
93 | char path[MAX_PATH]; /* font path and filename */ | ||
93 | int refcount; /* how many times has this font been loaded? */ | 94 | int refcount; /* how many times has this font been loaded? */ |
94 | unsigned char buffer[]; | 95 | unsigned char buffer[]; |
95 | }; | 96 | }; |
@@ -331,8 +332,13 @@ static int find_font_index(const char* path) | |||
331 | while (index < MAXFONTS) | 332 | while (index < MAXFONTS) |
332 | { | 333 | { |
333 | handle = buflib_allocations[index]; | 334 | handle = buflib_allocations[index]; |
334 | if (handle > 0 && !strcmp(core_get_name(handle), path)) | 335 | if (handle > 0) |
335 | return index; | 336 | { |
337 | struct buflib_alloc_data *data = core_get_data(handle); | ||
338 | if(!strcmp(data->path, path)) | ||
339 | return index; | ||
340 | } | ||
341 | |||
336 | index++; | 342 | index++; |
337 | } | 343 | } |
338 | return FONT_SYSFIXED; | 344 | return FONT_SYSFIXED; |
@@ -344,7 +350,11 @@ const char* font_filename(int font_id) | |||
344 | return NULL; | 350 | return NULL; |
345 | int handle = buflib_allocations[font_id]; | 351 | int handle = buflib_allocations[font_id]; |
346 | if (handle > 0) | 352 | if (handle > 0) |
347 | return core_get_name(handle); | 353 | { |
354 | struct buflib_alloc_data *data = core_get_data(handle); | ||
355 | return data->path; | ||
356 | } | ||
357 | |||
348 | return NULL; | 358 | return NULL; |
349 | } | 359 | } |
350 | 360 | ||
@@ -402,6 +412,11 @@ static struct font* font_load_header(int fd, struct font *pheader, | |||
402 | /* load a font with room for glyphs, limited to bufsize if not zero */ | 412 | /* load a font with room for glyphs, limited to bufsize if not zero */ |
403 | int font_load_ex( const char *path, size_t buf_size, int glyphs ) | 413 | int font_load_ex( const char *path, size_t buf_size, int glyphs ) |
404 | { | 414 | { |
415 | /* needed to handle the font properly after it's loaded */ | ||
416 | size_t path_len = strlen(path); | ||
417 | if ( path_len >= MAX_PATH ) | ||
418 | return -1; | ||
419 | |||
405 | //printf("\nfont_load_ex(%s, %d, %d)\n", path, buf_size, glyphs); | 420 | //printf("\nfont_load_ex(%s, %d, %d)\n", path, buf_size, glyphs); |
406 | int fd = open(path, O_RDONLY|O_BINARY); | 421 | int fd = open(path, O_RDONLY|O_BINARY); |
407 | if ( fd < 0 ) | 422 | if ( fd < 0 ) |
@@ -510,7 +525,7 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) | |||
510 | font_id = open_slot; | 525 | font_id = open_slot; |
511 | 526 | ||
512 | /* allocate mem */ | 527 | /* allocate mem */ |
513 | int handle = core_alloc_ex( path, | 528 | int handle = core_alloc_ex( NULL, |
514 | bufsize + sizeof( struct buflib_alloc_data ), | 529 | bufsize + sizeof( struct buflib_alloc_data ), |
515 | &buflibops ); | 530 | &buflibops ); |
516 | if ( handle <= 0 ) | 531 | if ( handle <= 0 ) |
@@ -522,6 +537,9 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) | |||
522 | pdata = core_get_data(handle); | 537 | pdata = core_get_data(handle); |
523 | pdata->refcount = 1; | 538 | pdata->refcount = 1; |
524 | 539 | ||
540 | /* save load path so we can recognize this font later */ | ||
541 | memcpy(pdata->path, path, path_len+1); | ||
542 | |||
525 | /* load and init */ | 543 | /* load and init */ |
526 | struct font *pf = &pdata->font; | 544 | struct font *pf = &pdata->font; |
527 | memcpy(pf, &f, sizeof( struct font) ); | 545 | memcpy(pf, &f, sizeof( struct font) ); |
@@ -594,7 +612,6 @@ void font_unload(int font_id) | |||
594 | pdata->refcount--; | 612 | pdata->refcount--; |
595 | if (pdata->refcount < 1) | 613 | if (pdata->refcount < 1) |
596 | { | 614 | { |
597 | //printf("freeing id: %d %s\n", font_id, core_get_name(*handle)); | ||
598 | if (pf && pf->fd >= 0) | 615 | if (pf && pf->fd >= 0) |
599 | { | 616 | { |
600 | glyph_cache_save(font_id); | 617 | glyph_cache_save(font_id); |