[cairo-commit] cairo/src cairo-font.c, 1.66, 1.67 cairoint.h, 1.192, 1.193

Carl Worth commit at pdx.freedesktop.org
Thu Aug 11 14:59:31 PDT 2005


Committed by: cworth

Update of /cvs/cairo/cairo/src
In directory gabe:/tmp/cvs-serv9853/src

Modified Files:
	cairo-font.c cairoint.h 
Log Message:

2005-08-08  Carl Worth  <cworth at cworth.org>

        * src/cairoint.h: Add hash_entry field to cairo_font_face_t so
        that it can (optionally) be hashed.

        * src/cairo-font.c: (_cairo_toy_font_face_hash_table_lock),
        (_cairo_toy_font_face_hash_table_unlock),
        (_cairo_toy_font_face_init_key), (_cairo_toy_font_face_init),
        (_cairo_toy_font_face_fini), (_cairo_toy_font_face_keys_equal),
        (_cairo_toy_font_face_create), (_cairo_toy_font_face_destroy),
        (_cairo_toy_font_face_scaled_font_create),
        (_cairo_font_reset_static_data): Complete the remainder of the
        simple -> toy renaming. Convert the family/slant/weight ->
        cairo_toy_font_face_t mapping to use cairo-hash.c rather
        than cairo-cache.c.


Index: cairo-font.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-font.c,v
retrieving revision 1.66
retrieving revision 1.67
diff -u -d -r1.66 -r1.67
--- cairo-font.c	11 Aug 2005 13:35:21 -0000	1.66
+++ cairo-font.c	11 Aug 2005 21:59:29 -0000	1.67
@@ -47,6 +47,7 @@
 /* cairo_font_face_t */
 
 const cairo_font_face_t _cairo_font_face_nil = {
+    { 0 },			/* hash_entry */
     CAIRO_STATUS_NO_MEMORY,	/* status */
     -1,		                /* ref_count */
     { 0, 0, 0, NULL },		/* user_data */
@@ -188,219 +189,122 @@
 					    key, user_data, destroy);
 }
 
-/* We maintain a global cache from family/weight/slant => cairo_font_face_t
- * for cairo_simple_font_t. The primary purpose of this cache is to provide
- * unique cairo_font_face_t values so that our cache from
- * cairo_font_face_t => cairo_scaled_font_t works. For this reason, we don't need
- * this cache to keep font faces alive; we just add them to the cache and
- * remove them again when freed.
- */
-
-typedef struct {
-    cairo_cache_entry_base_t base;
-    const char *family;
-    cairo_font_slant_t slant;
-    cairo_font_weight_t weight;
-} cairo_simple_cache_key_t;
+static const cairo_font_face_backend_t _cairo_toy_font_face_backend;
 
-typedef struct {
-    cairo_simple_cache_key_t key;
-    cairo_toy_font_face_t *font_face;
-} cairo_simple_cache_entry_t;
+static int
+_cairo_toy_font_face_keys_equal (void *key_a,
+				 void *key_b);
 
-static const cairo_cache_backend_t _cairo_simple_font_cache_backend;
+/* We maintain a hash table from family/weight/slant =>
+ * cairo_font_face_t for cairo_toy_font_t. The primary purpose of
+ * this mapping is to provide unique cairo_font_face_t values so that
+ * our cache and mapping from cairo_font_face_t => cairo_scaled_font_t
+ * works. Once the corresponding cairo_font_face_t objects fall out of
+ * downstream caches, we don't need them in this hash table anymore.
+ */
 
-CAIRO_MUTEX_DECLARE(_global_simple_cache_mutex);
+static cairo_hash_table_t *cairo_toy_font_face_hash_table = NULL;
 
-static void
-_lock_global_simple_cache (void)
-{
-    CAIRO_MUTEX_LOCK (_global_simple_cache_mutex);
-}
+CAIRO_MUTEX_DECLARE (cairo_toy_font_face_hash_table_mutex);
 
-static void
-_unlock_global_simple_cache (void)
+static cairo_hash_table_t *
+_cairo_toy_font_face_hash_table_lock (void)
 {
-    CAIRO_MUTEX_UNLOCK (_global_simple_cache_mutex);
-}
-
-static cairo_cache_t *global_simple_cache = NULL;
+    CAIRO_MUTEX_LOCK (cairo_toy_font_face_hash_table_mutex);
 
-static cairo_cache_t *
-_get_global_simple_cache (void)
-{
-    if (global_simple_cache == NULL)
+    if (cairo_toy_font_face_hash_table == NULL)
     {
-	global_simple_cache = malloc (sizeof (cairo_cache_t));	
-	if (!global_simple_cache)
-	    goto FAIL;
+	cairo_toy_font_face_hash_table =
+	    _cairo_hash_table_create (_cairo_toy_font_face_keys_equal);
 
-	if (_cairo_cache_init (global_simple_cache,
-			       &_cairo_simple_font_cache_backend,
-			       0)) /* No memory limit */
-	    goto FAIL;
+	if (cairo_toy_font_face_hash_table == NULL) {
+	    CAIRO_MUTEX_UNLOCK (cairo_toy_font_face_hash_table_mutex);
+	    return NULL;
+	}
     }
-    return global_simple_cache;
 
- FAIL:
-    if (global_simple_cache)
-	free (global_simple_cache);
-    global_simple_cache = NULL;
-    return NULL;
+    return cairo_toy_font_face_hash_table;
 }
 
-static unsigned long
-_cairo_simple_font_cache_hash (void *cache, void *key)
+static void
+_cairo_toy_font_face_hash_table_unlock (void)
 {
-    cairo_simple_cache_key_t *k = (cairo_simple_cache_key_t *) key;
-    unsigned long hash;
-
-    /* 1607 and 1451 are just a couple random primes. */
-    hash = _cairo_hash_string (k->family);
-    hash += ((unsigned long) k->slant) * 1607;
-    hash += ((unsigned long) k->weight) * 1451;
-    
-    return hash;
+    CAIRO_MUTEX_UNLOCK (cairo_toy_font_face_hash_table_mutex);
 }
 
-static int
-_cairo_simple_font_cache_keys_equal (void *cache,
-				     void *k1,
-				     void *k2)
+/**
+ * _cairo_toy_font_face_init_key:
+ * 
+ * Initialize those portions of cairo_toy_font_face_t needed to use
+ * it as a hash table key, including the hash code buried away in
+ * font_face->base.hash_entry. No memory allocation is performed here
+ * so that no fini call is needed. We do this to make it easier to use
+ * an automatic cairo_toy_font_face_t variable as a key.
+ **/
+static void
+_cairo_toy_font_face_init_key (cairo_toy_font_face_t *key,
+			       const char	     *family,
+			       cairo_font_slant_t     slant,
+			       cairo_font_weight_t    weight)
 {
-    cairo_simple_cache_key_t *a;
-    cairo_simple_cache_key_t *b;
-    a = (cairo_simple_cache_key_t *) k1;
-    b = (cairo_simple_cache_key_t *) k2;
+    unsigned long hash;
 
-    return strcmp (a->family, b->family) == 0 &&
-	a->slant == b->slant &&
-	a->weight == b->weight;
-}
+    key->family = family;
+    key->owns_family = FALSE;
 
-static cairo_toy_font_face_t *
-_cairo_toy_font_face_create_from_cache_key (cairo_simple_cache_key_t *key)
-{
-    cairo_toy_font_face_t *simple_face;
+    key->slant = slant;
+    key->weight = weight;
 
-    simple_face = malloc (sizeof (cairo_toy_font_face_t));
-    if (!simple_face)
-	return NULL;
+    /* 1607 and 1451 are just a couple of arbitrary primes. */
+    hash = _cairo_hash_string (family);
+    hash += ((unsigned long) slant) * 1607;
+    hash += ((unsigned long) weight) * 1451;
     
-    simple_face->family = strdup (key->family);
-    if (!simple_face->family) {
-	free (simple_face);
-	return NULL;
-    }
-
-    simple_face->slant = key->slant;
-    simple_face->weight = key->weight;
-
-    _cairo_font_face_init (&simple_face->base, &_cairo_toy_font_face_backend);
-
-    return simple_face;
+    key->base.hash_entry.hash = hash;
 }
 
 static cairo_status_t
-_cairo_simple_font_cache_create_entry (void  *cache,
-				       void  *key,
-				       void **return_entry)
+_cairo_toy_font_face_init (cairo_toy_font_face_t *font_face,
+			   const char	         *family,
+			   cairo_font_slant_t	  slant,
+			   cairo_font_weight_t	  weight)
 {
-    cairo_simple_cache_key_t *k = (cairo_simple_cache_key_t *) key;
-    cairo_simple_cache_entry_t *entry;
-
-    entry = malloc (sizeof (cairo_simple_cache_entry_t));
-    if (entry == NULL)
-	return CAIRO_STATUS_NO_MEMORY;
+    char *family_copy;
 
-    entry->font_face = _cairo_toy_font_face_create_from_cache_key (k);
-    if (!entry->font_face) {
-	free (entry);
+    family_copy = strdup (family);
+    if (family_copy == NULL)
 	return CAIRO_STATUS_NO_MEMORY;
-    }
-    
-    entry->key.base.memory = 0;
-    entry->key.family = entry->font_face->family;
-    entry->key.slant = entry->font_face->slant;
-    entry->key.weight = entry->font_face->weight;
-    
-    *return_entry = entry;
-
-    return CAIRO_STATUS_SUCCESS;
-}
 
-/* Entries are never spontaneously destroyed; but only when
- * we remove them from the cache specifically. We free entry->font_face
- * in the code that removes the entry from the cache
- */
-static void
-_cairo_simple_font_cache_destroy_entry (void *cache,
-					void *entry)
-{    
-    cairo_simple_cache_entry_t *e = (cairo_simple_cache_entry_t *) entry;
+    _cairo_toy_font_face_init_key (font_face, family_copy,
+				      slant, weight);
+    font_face->owns_family = TRUE;
 
-    free (e);
-}
+    _cairo_font_face_init (&font_face->base, &_cairo_toy_font_face_backend);
 
-static void 
-_cairo_simple_font_cache_destroy_cache (void *cache)
-{
-    free (cache);
+    return CAIRO_STATUS_SUCCESS;
 }
 
-static const cairo_cache_backend_t _cairo_simple_font_cache_backend = {
-    _cairo_simple_font_cache_hash,
-    _cairo_simple_font_cache_keys_equal,
-    _cairo_simple_font_cache_create_entry,
-    _cairo_simple_font_cache_destroy_entry,
-    _cairo_simple_font_cache_destroy_cache
-};
-
 static void
-_cairo_toy_font_face_destroy (void *abstract_face)
+_cairo_toy_font_face_fini (cairo_toy_font_face_t *font_face)
 {
-    cairo_toy_font_face_t *simple_face = abstract_face;
-    cairo_cache_t *cache;
-    cairo_simple_cache_key_t key;
-
-    if (simple_face == NULL)
-	return;
-
-    _lock_global_simple_cache ();
-    cache = _get_global_simple_cache ();
-    assert (cache);
-    
-    key.family = simple_face->family;
-    key.slant = simple_face->slant;
-    key.weight = simple_face->weight;
-	
-    _cairo_cache_remove (cache, &key);
-    
-    _unlock_global_simple_cache ();
-    
-    free (simple_face->family);
+    /* We assert here that we own font_face->family before casting
+     * away the const qualifer. */
+    assert (! font_face->owns_family);
+    free ((char*) font_face->family);
 }
 
-static cairo_status_t
-_cairo_toy_font_face_scaled_font_create (void			  *abstract_face,
-					 const cairo_matrix_t       *font_matrix,
-					 const cairo_matrix_t       *ctm,
-					 const cairo_font_options_t *options,
-					 cairo_scaled_font_t    **scaled_font)
+static int
+_cairo_toy_font_face_keys_equal (void *key_a,
+				 void *key_b)
 {
-    const cairo_scaled_font_backend_t * backend = CAIRO_SCALED_FONT_BACKEND_DEFAULT;
-
-    cairo_toy_font_face_t *simple_face = abstract_face;
+    cairo_toy_font_face_t *face_a = key_a;
+    cairo_toy_font_face_t *face_b = key_b;
 
-    return backend->create_toy (simple_face,
-				font_matrix, ctm, options, scaled_font);
+    return (strcmp (face_a->family, face_b->family) == 0 &&
+	    face_a->slant == face_b->slant &&
+	    face_a->weight == face_b->weight);
 }
 
-static const cairo_font_face_backend_t _cairo_toy_font_face_backend = {
-    _cairo_toy_font_face_destroy,
-    _cairo_toy_font_face_scaled_font_create
-};
-
 /**
  * _cairo_toy_font_face_create:
  * @family: a font family name, encoded in UTF-8
@@ -419,36 +323,90 @@
 			     cairo_font_slant_t   slant, 
 			     cairo_font_weight_t  weight)
 {
-    cairo_simple_cache_entry_t *entry;
-    cairo_simple_cache_key_t key;
-    cairo_cache_t *cache;
     cairo_status_t status;
-    cairo_bool_t created_entry;
+    cairo_toy_font_face_t key, *font_face;
+    cairo_hash_table_t *hash_table;
 
-    key.family = family;
-    key.slant = slant;
-    key.weight = weight;
+    hash_table = _cairo_toy_font_face_hash_table_lock ();
+    if (hash_table == NULL)
+	goto UNWIND;
+
+    _cairo_toy_font_face_init_key (&key, family, slant, weight);
     
-    _lock_global_simple_cache ();
-    cache = _get_global_simple_cache ();
-    if (cache == NULL) {
-	_unlock_global_simple_cache ();
-	_cairo_error (CAIRO_STATUS_NO_MEMORY);
-	return (cairo_font_face_t *)&_cairo_font_face_nil;
+    /* Return existing font_face if it exists in the hash table. */
+    if (_cairo_hash_table_lookup (hash_table,
+				  &key.base.hash_entry,
+				  (cairo_hash_entry_t **) &font_face))
+    {
+	_cairo_toy_font_face_hash_table_unlock ();
+	return cairo_font_face_reference (&font_face->base);
     }
-    status = _cairo_cache_lookup (cache, &key, (void **) &entry, &created_entry);
-    if (status == CAIRO_STATUS_SUCCESS && !created_entry)
-	cairo_font_face_reference (&entry->font_face->base);
+
+    /* Otherwise create it and insert into hash table. */
+    font_face = malloc (sizeof (cairo_toy_font_face_t));
+    if (font_face == NULL)
+	goto UNWIND_HASH_TABLE_LOCK;
+
+    status = _cairo_toy_font_face_init (font_face, family, slant, weight);
+    if (status)
+	goto UNWIND_FONT_FACE_MALLOC;
+
+    status = _cairo_hash_table_insert (hash_table, &font_face->base.hash_entry);
+    if (status)
+	goto UNWIND_FONT_FACE_INIT;
+
+    _cairo_toy_font_face_hash_table_unlock ();
+
+    return &font_face->base;
+
+ UNWIND_FONT_FACE_INIT:
+ UNWIND_FONT_FACE_MALLOC:
+    free (font_face);
+ UNWIND_HASH_TABLE_LOCK:
+    _cairo_toy_font_face_hash_table_unlock ();
+ UNWIND:
+    return (cairo_font_face_t*) &_cairo_font_face_nil;
+}
+
+static void
+_cairo_toy_font_face_destroy (void *abstract_face)
+{
+    cairo_toy_font_face_t *font_face = abstract_face;
+    cairo_hash_table_t *hash_table;
+
+    if (font_face == NULL)
+	return;
+
+    hash_table = _cairo_toy_font_face_hash_table_lock ();
+    /* All created objects must have been mapped in the hash table. */
+    assert (hash_table != NULL);
+
+    _cairo_hash_table_remove (hash_table, &font_face->base.hash_entry);
     
-    _unlock_global_simple_cache ();
-    if (status) {
-	_cairo_error (status);
-	return (cairo_font_face_t *)&_cairo_font_face_nil;
-    }
+    _cairo_toy_font_face_hash_table_unlock ();
+    
+    _cairo_toy_font_face_fini (font_face);
+}
 
-    return &entry->font_face->base;
+static cairo_status_t
+_cairo_toy_font_face_scaled_font_create (void                *abstract_font_face,
+					 const cairo_matrix_t       *font_matrix,
+					 const cairo_matrix_t       *ctm,
+					 const cairo_font_options_t *options,
+					 cairo_scaled_font_t	   **scaled_font)
+{
+    cairo_toy_font_face_t *font_face = abstract_font_face;
+    const cairo_scaled_font_backend_t * backend = CAIRO_SCALED_FONT_BACKEND_DEFAULT;
+
+    return backend->create_toy (font_face,
+				font_matrix, ctm, options, scaled_font);
 }
 
+static const cairo_font_face_backend_t _cairo_toy_font_face_backend = {
+    _cairo_toy_font_face_destroy,
+    _cairo_toy_font_face_scaled_font_create
+};
+
 /* cairo_scaled_font_t */
 
 static const cairo_scaled_font_t _cairo_scaled_font_nil = {
@@ -1391,8 +1349,8 @@
     _global_image_glyph_cache = NULL;
     _cairo_unlock_global_image_glyph_cache();
 
-    _lock_global_simple_cache ();
-    _cairo_cache_destroy (global_simple_cache);
-    global_simple_cache = NULL;
-    _unlock_global_simple_cache ();
+    CAIRO_MUTEX_LOCK (cairo_toy_font_face_hash_table_mutex);
+    _cairo_hash_table_destroy (cairo_toy_font_face_hash_table);
+    cairo_toy_font_face_hash_table = NULL;
+    CAIRO_MUTEX_UNLOCK (cairo_toy_font_face_hash_table_mutex);
 }

Index: cairoint.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairoint.h,v
retrieving revision 1.192
retrieving revision 1.193
diff -u -d -r1.192 -r1.193
--- cairoint.h	11 Aug 2005 21:49:13 -0000	1.192
+++ cairoint.h	11 Aug 2005 21:59:29 -0000	1.193
@@ -490,6 +490,7 @@
 };
 
 struct _cairo_font_face {
+    cairo_hash_entry_t hash_entry;
     cairo_status_t status;
     int ref_count;
     cairo_user_data_array_t user_data;
@@ -566,7 +567,8 @@
 
 typedef struct _cairo_toy_font_face {
     cairo_font_face_t base;
-    char *family;
+    const char *family;
+    cairo_bool_t owns_family;
     cairo_font_slant_t slant;
     cairo_font_weight_t weight;
 } cairo_toy_font_face_t;




More information about the cairo-commit mailing list