From 41c53a7b2f558c7c8f1a3a1490831006aa21b572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 15 Jan 2019 21:01:18 +0100 Subject: [PATCH] font: ref-count font objects --- font.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/font.c b/font.c index 5faeec1..dba1a5b 100644 --- a/font.c +++ b/font.c @@ -15,6 +15,7 @@ struct font { char *name; cairo_scaled_font_t *scaled_font; + int ref_counter; }; static tll(const struct font *) cache = tll_init(); @@ -113,20 +114,18 @@ font_new(const char *name) struct font *font = malloc(sizeof(*font)); font->name = strdup(name); font->scaled_font = scaled_font; + font->ref_counter = 1; tll_push_back(cache, font); return font; } struct font * -font_clone(const struct font *font) +font_clone(const struct font *_font) { - struct font *clone = malloc(sizeof(*font)); - clone->name = strdup(font->name); - clone->scaled_font = font->scaled_font; - - cairo_scaled_font_reference(clone->scaled_font); - return clone; + struct font *font = (struct font *)_font; + font->ref_counter++; + return font; } void @@ -135,6 +134,10 @@ font_destroy(struct font *font) if (font == NULL) return; + assert(font->ref_counter > 0); + if (--font->ref_counter > 0) + return; + tll_foreach(cache, it) { if (it->item == font) { tll_remove(cache, it);