From fb46d7390e0776e888d75533304fb5ce26dca04f Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 30 Oct 2003 10:55:04 +0000 Subject: [PATCH] A few cleanups to eliminate a memory leak. --- ChangeLog | 2 ++ src/cairo-font.c | 40 +++++----------------------------------- src/cairo-ft-font.c | 4 ++-- src/cairo-gstate.c | 8 ++++---- src/cairo_font.c | 40 +++++----------------------------------- src/cairo_ft_font.c | 4 ++-- src/cairo_gstate.c | 8 ++++---- src/cairoint.h | 8 +------- 8 files changed, 25 insertions(+), 89 deletions(-) diff --git a/ChangeLog b/ChangeLog index f03e16ddc..5fd2b378c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2003-10-30 Carl Worth + * cairo_font: A few cleanups to eliminate a memory leak. + * src/cairo_gstate.c (_cairo_gstate_init_copy): (_cairo_gstate_fini): (_cairo_gstate_select_font): diff --git a/src/cairo-font.c b/src/cairo-font.c index ed2394fd3..f6bf390a9 100644 --- a/src/cairo-font.c +++ b/src/cairo-font.c @@ -41,14 +41,8 @@ cairo_status_t _cairo_font_init (cairo_font_t *font, const struct cairo_font_backend *backend) { - font->family = (unsigned char *) strdup (CAIRO_FONT_FAMILY_DEFAULT); - if (font->family == NULL) - return CAIRO_STATUS_NO_MEMORY; - cairo_matrix_set_identity (&font->matrix); font->refcount = 1; - font->weight = CAIRO_FONT_WEIGHT_NORMAL; - font->slant = CAIRO_FONT_SLANT_NORMAL; font->backend = backend; return CAIRO_STATUS_SUCCESS; @@ -63,12 +57,6 @@ _cairo_font_copy (cairo_font_t *font) if (font == NULL || font->backend->copy == NULL) return NULL; - if (font->family) { - tmp = (unsigned char *) strdup ((char *) font->family); - if (tmp == NULL) - return NULL; - } - newfont = font->backend->copy (font); if (newfont == NULL) { free (tmp); @@ -76,33 +64,11 @@ _cairo_font_copy (cairo_font_t *font) } newfont->refcount = 1; - newfont->family = tmp; cairo_matrix_copy(&newfont->matrix, &font->matrix); - newfont->slant = font->slant; - newfont->weight = font->weight; newfont->backend = font->backend; return newfont; } -void -_cairo_font_fini (cairo_font_t *font) -{ - if (font == NULL) - return; - - if (--(font->refcount) > 0) - return; - - if (font->family) - free (font->family); - font->family = NULL; - - _cairo_matrix_fini (&font->matrix); - - if (font->backend->close) - font->backend->close (font); -} - cairo_status_t _cairo_font_scale (cairo_font_t *font, double scale) { @@ -197,7 +163,11 @@ cairo_font_reference (cairo_font_t *font) void cairo_font_destroy (cairo_font_t *font) { - _cairo_font_fini (font); + if (--(font->refcount) > 0) + return; + + if (font->backend->destroy) + font->backend->destroy (font); } void diff --git a/src/cairo-ft-font.c b/src/cairo-ft-font.c index dd90a4757..35d30b1b9 100644 --- a/src/cairo-ft-font.c +++ b/src/cairo-ft-font.c @@ -207,7 +207,7 @@ _cairo_ft_font_copy (cairo_font_t *font) } static void -_cairo_ft_font_close (cairo_font_t *font) +_cairo_ft_font_destroy (cairo_font_t *font) { cairo_ft_font_t * ft_font = NULL; @@ -600,5 +600,5 @@ const struct cairo_font_backend cairo_ft_font_backend = { glyph_path: (void *) _cairo_ft_font_glyph_path, create: (void *) _cairo_ft_font_create, copy: (void *) _cairo_ft_font_copy, - close: (void *) _cairo_ft_font_close + destroy: (void *) _cairo_ft_font_destroy }; diff --git a/src/cairo-gstate.c b/src/cairo-gstate.c index 8696ed5f0..296308643 100644 --- a/src/cairo-gstate.c +++ b/src/cairo-gstate.c @@ -147,7 +147,7 @@ _cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other) CLEANUP_PATH: _cairo_path_fini (&gstate->path); CLEANUP_FONT: - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); CLEANUP_DASHES: free (gstate->dash); gstate->dash = NULL; @@ -158,7 +158,7 @@ _cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other) void _cairo_gstate_fini (cairo_gstate_t *gstate) { - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); cairo_surface_destroy (gstate->surface); gstate->surface = NULL; @@ -1426,7 +1426,7 @@ _cairo_gstate_select_font (cairo_gstate_t *gstate, cairo_font_weight_t weight) { if (gstate->font != NULL) - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); gstate->font = _cairo_font_create (family, slant, weight); @@ -1478,7 +1478,7 @@ _cairo_gstate_set_font (cairo_gstate_t *gstate, cairo_font_t *font) { if (gstate->font != NULL) - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); gstate->font = font; cairo_font_reference (gstate->font); return CAIRO_STATUS_SUCCESS; diff --git a/src/cairo_font.c b/src/cairo_font.c index ed2394fd3..f6bf390a9 100644 --- a/src/cairo_font.c +++ b/src/cairo_font.c @@ -41,14 +41,8 @@ cairo_status_t _cairo_font_init (cairo_font_t *font, const struct cairo_font_backend *backend) { - font->family = (unsigned char *) strdup (CAIRO_FONT_FAMILY_DEFAULT); - if (font->family == NULL) - return CAIRO_STATUS_NO_MEMORY; - cairo_matrix_set_identity (&font->matrix); font->refcount = 1; - font->weight = CAIRO_FONT_WEIGHT_NORMAL; - font->slant = CAIRO_FONT_SLANT_NORMAL; font->backend = backend; return CAIRO_STATUS_SUCCESS; @@ -63,12 +57,6 @@ _cairo_font_copy (cairo_font_t *font) if (font == NULL || font->backend->copy == NULL) return NULL; - if (font->family) { - tmp = (unsigned char *) strdup ((char *) font->family); - if (tmp == NULL) - return NULL; - } - newfont = font->backend->copy (font); if (newfont == NULL) { free (tmp); @@ -76,33 +64,11 @@ _cairo_font_copy (cairo_font_t *font) } newfont->refcount = 1; - newfont->family = tmp; cairo_matrix_copy(&newfont->matrix, &font->matrix); - newfont->slant = font->slant; - newfont->weight = font->weight; newfont->backend = font->backend; return newfont; } -void -_cairo_font_fini (cairo_font_t *font) -{ - if (font == NULL) - return; - - if (--(font->refcount) > 0) - return; - - if (font->family) - free (font->family); - font->family = NULL; - - _cairo_matrix_fini (&font->matrix); - - if (font->backend->close) - font->backend->close (font); -} - cairo_status_t _cairo_font_scale (cairo_font_t *font, double scale) { @@ -197,7 +163,11 @@ cairo_font_reference (cairo_font_t *font) void cairo_font_destroy (cairo_font_t *font) { - _cairo_font_fini (font); + if (--(font->refcount) > 0) + return; + + if (font->backend->destroy) + font->backend->destroy (font); } void diff --git a/src/cairo_ft_font.c b/src/cairo_ft_font.c index dd90a4757..35d30b1b9 100644 --- a/src/cairo_ft_font.c +++ b/src/cairo_ft_font.c @@ -207,7 +207,7 @@ _cairo_ft_font_copy (cairo_font_t *font) } static void -_cairo_ft_font_close (cairo_font_t *font) +_cairo_ft_font_destroy (cairo_font_t *font) { cairo_ft_font_t * ft_font = NULL; @@ -600,5 +600,5 @@ const struct cairo_font_backend cairo_ft_font_backend = { glyph_path: (void *) _cairo_ft_font_glyph_path, create: (void *) _cairo_ft_font_create, copy: (void *) _cairo_ft_font_copy, - close: (void *) _cairo_ft_font_close + destroy: (void *) _cairo_ft_font_destroy }; diff --git a/src/cairo_gstate.c b/src/cairo_gstate.c index 8696ed5f0..296308643 100644 --- a/src/cairo_gstate.c +++ b/src/cairo_gstate.c @@ -147,7 +147,7 @@ _cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other) CLEANUP_PATH: _cairo_path_fini (&gstate->path); CLEANUP_FONT: - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); CLEANUP_DASHES: free (gstate->dash); gstate->dash = NULL; @@ -158,7 +158,7 @@ _cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other) void _cairo_gstate_fini (cairo_gstate_t *gstate) { - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); cairo_surface_destroy (gstate->surface); gstate->surface = NULL; @@ -1426,7 +1426,7 @@ _cairo_gstate_select_font (cairo_gstate_t *gstate, cairo_font_weight_t weight) { if (gstate->font != NULL) - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); gstate->font = _cairo_font_create (family, slant, weight); @@ -1478,7 +1478,7 @@ _cairo_gstate_set_font (cairo_gstate_t *gstate, cairo_font_t *font) { if (gstate->font != NULL) - _cairo_font_fini (gstate->font); + cairo_font_destroy (gstate->font); gstate->font = font; cairo_font_reference (gstate->font); return CAIRO_STATUS_SUCCESS; diff --git a/src/cairoint.h b/src/cairoint.h index 0c0e85fa7..612964add 100644 --- a/src/cairoint.h +++ b/src/cairoint.h @@ -274,7 +274,7 @@ struct cairo_font_backend { cairo_font_t *(*copy) (cairo_font_t *other); - void (*close) (cairo_font_t *font); + void (*destroy) (cairo_font_t *font); }; @@ -399,10 +399,7 @@ typedef struct cairo_traps { struct cairo_font { int refcount; - unsigned char *family; cairo_matrix_t matrix; - cairo_font_slant_t slant; - cairo_font_weight_t weight; const struct cairo_font_backend *backend; }; @@ -789,9 +786,6 @@ _cairo_font_init (cairo_font_t *font, extern cairo_font_t * __internal_linkage _cairo_font_copy (cairo_font_t *font); -extern void __internal_linkage -_cairo_font_fini (cairo_font_t *font); - extern cairo_status_t __internal_linkage _cairo_font_scale (cairo_font_t *font, double scale);