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
This commit is contained in:
Aidan MacDonald 2022-10-15 23:30:38 +01:00
parent ebebef5566
commit 879888b158

View file

@ -90,6 +90,7 @@ extern struct font sysfont;
struct buflib_alloc_data {
struct font font; /* must be the first member! */
char path[MAX_PATH]; /* font path and filename */
int refcount; /* how many times has this font been loaded? */
unsigned char buffer[];
};
@ -331,8 +332,13 @@ static int find_font_index(const char* path)
while (index < MAXFONTS)
{
handle = buflib_allocations[index];
if (handle > 0 && !strcmp(core_get_name(handle), path))
return index;
if (handle > 0)
{
struct buflib_alloc_data *data = core_get_data(handle);
if(!strcmp(data->path, path))
return index;
}
index++;
}
return FONT_SYSFIXED;
@ -344,7 +350,11 @@ const char* font_filename(int font_id)
return NULL;
int handle = buflib_allocations[font_id];
if (handle > 0)
return core_get_name(handle);
{
struct buflib_alloc_data *data = core_get_data(handle);
return data->path;
}
return NULL;
}
@ -402,6 +412,11 @@ static struct font* font_load_header(int fd, struct font *pheader,
/* load a font with room for glyphs, limited to bufsize if not zero */
int font_load_ex( const char *path, size_t buf_size, int glyphs )
{
/* needed to handle the font properly after it's loaded */
size_t path_len = strlen(path);
if ( path_len >= MAX_PATH )
return -1;
//printf("\nfont_load_ex(%s, %d, %d)\n", path, buf_size, glyphs);
int fd = open(path, O_RDONLY|O_BINARY);
if ( fd < 0 )
@ -510,7 +525,7 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs )
font_id = open_slot;
/* allocate mem */
int handle = core_alloc_ex( path,
int handle = core_alloc_ex( NULL,
bufsize + sizeof( struct buflib_alloc_data ),
&buflibops );
if ( handle <= 0 )
@ -522,6 +537,9 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs )
pdata = core_get_data(handle);
pdata->refcount = 1;
/* save load path so we can recognize this font later */
memcpy(pdata->path, path, path_len+1);
/* load and init */
struct font *pf = &pdata->font;
memcpy(pf, &f, sizeof( struct font) );
@ -594,7 +612,6 @@ void font_unload(int font_id)
pdata->refcount--;
if (pdata->refcount < 1)
{
//printf("freeing id: %d %s\n", font_id, core_get_name(*handle));
if (pf && pf->fd >= 0)
{
glyph_cache_save(font_id);