From fe3433c7368cb9b237fc35c247254d6e8cc12bb5 Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Sun, 18 Jul 2010 18:41:47 +0200 Subject: [PATCH] Add reference counters and to FT_Library and FT_Face objects. * include/freetype/freetype.h (FT_Reference_Face): New function. * include/freetype/ftmodapi.h (FT_Rererence_Library): New function. * include/freetype/internal/ftobjs.h (FT_Face_InternalRec, FT_LibraryRec): New field `refcount'. * src/base/ftobjs.c (FT_Open_Face, FT_New_Library): Handle `refcount'. (FT_Reference_Face, FT_Reference_Library): Implement new functions. (FT_Done_Face, FT_Done_Library): Handle `refcount'. * docs/CHANGES: Updated. --- ChangeLog | 17 +++++++ docs/CHANGES | 15 ++++++ include/freetype/freetype.h | 34 ++++++++++++++ include/freetype/ftlcdfil.h | 2 +- include/freetype/ftmodapi.h | 35 ++++++++++++++ include/freetype/internal/ftobjs.h | 31 +++++++++++- src/base/ftobjs.c | 75 +++++++++++++++++++++++------- 7 files changed, 188 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 72d7dd7e6..54c49b0e7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2010-07-18 Werner Lemberg + + Add reference counters and to FT_Library and FT_Face objects. + + * include/freetype/freetype.h (FT_Reference_Face): New function. + * include/freetype/ftmodapi.h (FT_Rererence_Library): New function. + + * include/freetype/internal/ftobjs.h (FT_Face_InternalRec, + FT_LibraryRec): New field `refcount'. + + * src/base/ftobjs.c (FT_Open_Face, FT_New_Library): Handle + `refcount'. + (FT_Reference_Face, FT_Reference_Library): Implement new functions. + (FT_Done_Face, FT_Done_Library): Handle `refcount'. + + * docs/CHANGES: Updated. + 2010-07-18 Werner Lemberg * Version 2.4.1 released. diff --git a/docs/CHANGES b/docs/CHANGES index cc38a756c..6c952ac1b 100644 --- a/docs/CHANGES +++ b/docs/CHANGES @@ -1,3 +1,18 @@ +CHANGES BETWEEN 2.4.1 and 2.4.2 + + I. MISCELLANEOUS + + - Two new functions, `FT_Reference_Library' (in FT_MODULE_H) and + `FT_Reference_Face' (in FT_FREETYPE_H), have been added to + simplify life-cycle management. A counter gets initialized to 1 + at the time an FT_Library (or FT_Face) structure is created. + The two new functions increment the respective counter. + `FT_Done_Library' and `FT_Done_Face' then only destroy a library + or face if the counter is 1, otherwise they simply decrement the + counter. + + +====================================================================== CHANGES BETWEEN 2.4.0 and 2.4.1 diff --git a/include/freetype/freetype.h b/include/freetype/freetype.h index e2ab77437..17c8547ef 100644 --- a/include/freetype/freetype.h +++ b/include/freetype/freetype.h @@ -1952,6 +1952,9 @@ FT_BEGIN_HEADER /* Each new face object created with this function also owns a */ /* default @FT_Size object, accessible as `face->size'. */ /* */ + /* See the discussion of reference counters in the description of */ + /* @FT_Reference_Face. */ + /* */ FT_EXPORT( FT_Error ) FT_Open_Face( FT_Library library, const FT_Open_Args* args, @@ -2016,6 +2019,33 @@ FT_BEGIN_HEADER FT_Open_Args* parameters ); + /*************************************************************************/ + /* */ + /* */ + /* FT_Reference_Face */ + /* */ + /* */ + /* A counter gets initialized to~1 at the time an @FT_Face structure */ + /* is created. This function increments the counter. @FT_Done_Face */ + /* then only destroys a face if the counter is~1, otherwise it simply */ + /* decrements the counter. */ + /* */ + /* This function helps in managing life-cycles of structures which */ + /* reference @FT_Face objects. */ + /* */ + /* */ + /* face :: A handle to a target face object. */ + /* */ + /* */ + /* FreeType error code. 0~means success. */ + /* */ + /* */ + /* 2.4.2 */ + /* */ + FT_EXPORT( FT_Error ) + FT_Reference_Face( FT_Face face ); + + /*************************************************************************/ /* */ /* */ @@ -2031,6 +2061,10 @@ FT_BEGIN_HEADER /* */ /* FreeType error code. 0~means success. */ /* */ + /* */ + /* See the discussion of reference counters in the description of */ + /* @FT_Reference_Face. */ + /* */ FT_EXPORT( FT_Error ) FT_Done_Face( FT_Face face ); diff --git a/include/freetype/ftlcdfil.h b/include/freetype/ftlcdfil.h index e7f592714..0b55ebee6 100644 --- a/include/freetype/ftlcdfil.h +++ b/include/freetype/ftlcdfil.h @@ -58,7 +58,7 @@ FT_BEGIN_HEADER /**************************************************************************** * - * @func: + * @enum: * FT_LcdFilter * * @description: diff --git a/include/freetype/ftmodapi.h b/include/freetype/ftmodapi.h index 17868b2f8..8f2e01794 100644 --- a/include/freetype/ftmodapi.h +++ b/include/freetype/ftmodapi.h @@ -249,6 +249,33 @@ FT_BEGIN_HEADER FT_Module module ); + /*************************************************************************/ + /* */ + /* */ + /* FT_Reference_Library */ + /* */ + /* */ + /* A counter gets initialized to~1 at the time an @FT_Library */ + /* structure is created. This function increments the counter. */ + /* @FT_Done_Library then only destroys a library if the counter is~1, */ + /* otherwise it simply decrements the counter. */ + /* */ + /* This function helps in managing life-cycles of structures which */ + /* reference @FT_Library objects. */ + /* */ + /* */ + /* library :: A handle to a target library object. */ + /* */ + /* */ + /* FreeType error code. 0~means success. */ + /* */ + /* */ + /* 2.4.2 */ + /* */ + FT_EXPORT( FT_Error ) + FT_Reference_Library( FT_Library library ); + + /*************************************************************************/ /* */ /* */ @@ -275,6 +302,10 @@ FT_BEGIN_HEADER /* */ /* FreeType error code. 0~means success. */ /* */ + /* */ + /* See the discussion of reference counters in the description of */ + /* @FT_Reference_Library. */ + /* */ FT_EXPORT( FT_Error ) FT_New_Library( FT_Memory memory, FT_Library *alibrary ); @@ -295,6 +326,10 @@ FT_BEGIN_HEADER /* */ /* FreeType error code. 0~means success. */ /* */ + /* */ + /* See the discussion of reference counters in the description of */ + /* @FT_Reference_Library. */ + /* */ FT_EXPORT( FT_Error ) FT_Done_Library( FT_Library library ); diff --git a/include/freetype/internal/ftobjs.h b/include/freetype/internal/ftobjs.h index 574cf5829..670eb78a8 100644 --- a/include/freetype/internal/ftobjs.h +++ b/include/freetype/internal/ftobjs.h @@ -4,7 +4,7 @@ /* */ /* The FreeType private base classes (specification). */ /* */ -/* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2008 by */ +/* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2008, 2010 by */ /* David Turner, Robert Wilhelm, and Werner Lemberg. */ /* */ /* This file is part of the FreeType project, and may only be used, */ @@ -311,6 +311,12 @@ FT_BEGIN_HEADER /* in the case when the unpatented hinter is compiled within the */ /* library. */ /* */ + /* refcount :: */ + /* A counter initialized to~1 at the time an @FT_Face structure is */ + /* created. @FT_Reference_Face increments this counter, and */ + /* @FT_Done_Face only destroys a face if the counter is~1, */ + /* otherwise it simply decrements it. */ + /* */ typedef struct FT_Face_InternalRec_ { #ifdef FT_CONFIG_OPTION_OLD_INTERNALS @@ -328,6 +334,7 @@ FT_BEGIN_HEADER #endif FT_Bool ignore_unpatented_hinter; + FT_UInt refcount; } FT_Face_InternalRec; @@ -805,10 +812,28 @@ FT_BEGIN_HEADER /* */ /* debug_hooks :: XXX */ /* */ + /* lcd_filter :: If subpixel rendering is activated, the */ + /* selected LCD filter mode. */ + /* */ + /* lcd_extra :: If subpixel rendering is activated, the number */ + /* of extra pixels needed for the LCD filter. */ + /* */ + /* lcd_weights :: If subpixel rendering is activated, the LCD */ + /* filter weights, if any. */ + /* */ + /* lcd_filter_func :: If subpixel rendering is activated, the LCD */ + /* filtering callback function. */ + /* */ /* pic_container :: Contains global structs and tables, instead */ /* of defining them globallly. */ /* */ - + /* refcount :: A counter initialized to~1 at the time an */ + /* @FT_Library structure is created. */ + /* @FT_Reference_Library increments this counter, */ + /* and @FT_Done_Library only destroys a library */ + /* if the counter is~1, otherwise it simply */ + /* decrements it. */ + /* */ typedef struct FT_LibraryRec_ { FT_Memory memory; /* library's memory manager */ @@ -843,6 +868,8 @@ FT_BEGIN_HEADER FT_PIC_Container pic_container; #endif + FT_UInt refcount; + } FT_LibraryRec; diff --git a/src/base/ftobjs.c b/src/base/ftobjs.c index aa84cf16f..9dce576e1 100644 --- a/src/base/ftobjs.c +++ b/src/base/ftobjs.c @@ -142,7 +142,7 @@ if ( !args ) return FT_Err_Invalid_Argument; - memory = library->memory; + memory = library->memory; if ( FT_NEW( stream ) ) goto Exit; @@ -1960,9 +1960,9 @@ FT_Error error; FT_Driver driver; FT_Memory memory; - FT_Stream stream = 0; - FT_Face face = 0; - FT_ListNode node = 0; + FT_Stream stream = NULL; + FT_Face face = NULL; + FT_ListNode node = NULL; FT_Bool external_stream; FT_Module* cur; FT_Module* limit; @@ -2186,6 +2186,8 @@ internal->transform_delta.x = 0; internal->transform_delta.y = 0; + + internal->refcount = 1; } if ( aface ) @@ -2271,6 +2273,17 @@ } + /* documentation is in freetype.h */ + + FT_EXPORT_DEF( FT_Error ) + FT_Reference_Face( FT_Face face ) + { + face->internal->refcount++; + + return FT_Err_Ok; + } + + /* documentation is in freetype.h */ FT_EXPORT_DEF( FT_Error ) @@ -2285,22 +2298,29 @@ error = FT_Err_Invalid_Face_Handle; if ( face && face->driver ) { - driver = face->driver; - memory = driver->root.memory; - - /* find face in driver's list */ - node = FT_List_Find( &driver->faces_list, face ); - if ( node ) - { - /* remove face object from the driver's list */ - FT_List_Remove( &driver->faces_list, node ); - FT_FREE( node ); - - /* now destroy the object proper */ - destroy_face( memory, face, driver ); + face->internal->refcount--; + if ( face->internal->refcount > 0 ) error = FT_Err_Ok; + else + { + driver = face->driver; + memory = driver->root.memory; + + /* find face in driver's list */ + node = FT_List_Find( &driver->faces_list, face ); + if ( node ) + { + /* remove face object from the driver's list */ + FT_List_Remove( &driver->faces_list, node ); + FT_FREE( node ); + + /* now destroy the object proper */ + destroy_face( memory, face, driver ); + error = FT_Err_Ok; + } } } + return error; } @@ -4262,13 +4282,24 @@ /*************************************************************************/ + /* documentation is in ftmodapi.h */ + + FT_EXPORT_DEF( FT_Error ) + FT_Reference_Library( FT_Library library ) + { + library->refcount++; + + return FT_Err_Ok; + } + + /* documentation is in ftmodapi.h */ FT_EXPORT_DEF( FT_Error ) FT_New_Library( FT_Memory memory, FT_Library *alibrary ) { - FT_Library library = 0; + FT_Library library = NULL; FT_Error error; @@ -4304,6 +4335,8 @@ library->version_minor = FREETYPE_MINOR; library->version_patch = FREETYPE_PATCH; + library->refcount = 1; + /* That's ok now */ *alibrary = library; @@ -4360,6 +4393,10 @@ if ( !library ) return FT_Err_Invalid_Library_Handle; + library->refcount--; + if ( library->refcount > 0 ) + goto Exit; + memory = library->memory; /* Discard client-data */ @@ -4435,6 +4472,8 @@ #endif FT_FREE( library ); + + Exit: return FT_Err_Ok; }