font: ref-count font objects

This commit is contained in:
Daniel Eklöf 2019-01-15 21:01:18 +01:00
parent 7525ae99eb
commit 41c53a7b2f

17
font.c
View file

@ -15,6 +15,7 @@
struct font { struct font {
char *name; char *name;
cairo_scaled_font_t *scaled_font; cairo_scaled_font_t *scaled_font;
int ref_counter;
}; };
static tll(const struct font *) cache = tll_init(); static tll(const struct font *) cache = tll_init();
@ -113,20 +114,18 @@ font_new(const char *name)
struct font *font = malloc(sizeof(*font)); struct font *font = malloc(sizeof(*font));
font->name = strdup(name); font->name = strdup(name);
font->scaled_font = scaled_font; font->scaled_font = scaled_font;
font->ref_counter = 1;
tll_push_back(cache, font); tll_push_back(cache, font);
return font; return font;
} }
struct font * struct font *
font_clone(const struct font *font) font_clone(const struct font *_font)
{ {
struct font *clone = malloc(sizeof(*font)); struct font *font = (struct font *)_font;
clone->name = strdup(font->name); font->ref_counter++;
clone->scaled_font = font->scaled_font; return font;
cairo_scaled_font_reference(clone->scaled_font);
return clone;
} }
void void
@ -135,6 +134,10 @@ font_destroy(struct font *font)
if (font == NULL) if (font == NULL)
return; return;
assert(font->ref_counter > 0);
if (--font->ref_counter > 0)
return;
tll_foreach(cache, it) { tll_foreach(cache, it) {
if (it->item == font) { if (it->item == font) {
tll_remove(cache, it); tll_remove(cache, it);