[cairo-commit] cairo/src cairo-cache-private.h, 1.2, 1.3 cairo-cache.c, 1.16, 1.17 cairo-scaled-font.c, 1.3, 1.4

Carl Worth commit at pdx.freedesktop.org
Mon Sep 12 11:15:54 PDT 2005


Committed by: cworth

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

Modified Files:
	cairo-cache-private.h cairo-cache.c cairo-scaled-font.c 
Log Message:

2005-09-12  Carl Worth  <cworth at cworth.org>

        Originally 2005-09-06  Carl Worth  <cworth at cworth.org>:

        * src/cairo-cache-private.h: Rename:

                _cairo_cache_preserve -> _cairo_cache_freeze
                _cairo_cache_release  -> _cairo_cache_thaw

        * src/cairo-scaled-font.c: (_cairo_scaled_font_show_glyphs): Track
        rename of _cairo_cache_freeze/thaw.

        * src/cairo-cache.c: (_cairo_cache_init), (_cairo_cache_freeze),
        (_cairo_cache_thaw), (_cairo_cache_shrink_to_accomodate),
        (_cairo_cache_insert): Track rename. Add stacking behavior to
        _cairo_cache_freeze/thaw. Abstract out shrinking from
        _cairo_cache_insert so that _cairo_cache_thaw will also shrink as
        necessary.

        * src/cairo-cache.c: (_cairo_cache_remove): Make this function
        static since its current limitation to accept an entry rather than
        a key makes it not as externally useful as would be
        desirable. Document this limitation.


Index: cairo-cache-private.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-cache-private.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- cairo-cache-private.h	2 Sep 2005 18:22:40 -0000	1.2
+++ cairo-cache-private.h	12 Sep 2005 18:15:52 -0000	1.3
@@ -103,10 +103,10 @@
 _cairo_cache_destroy (cairo_cache_t *cache);
 
 cairo_private void
-_cairo_cache_preserve (cairo_cache_t *cache);
+_cairo_cache_freeze (cairo_cache_t *cache);
 
 cairo_private void
-_cairo_cache_release (cairo_cache_t *cache);
+_cairo_cache_thaw (cairo_cache_t *cache);
 
 cairo_private cairo_bool_t
 _cairo_cache_lookup (cairo_cache_t	  *cache,
@@ -118,10 +118,6 @@
 		     cairo_cache_entry_t *entry);
 
 cairo_private void
-_cairo_cache_remove (cairo_cache_t	 *cache,
-		     cairo_cache_entry_t *key);
-
-cairo_private void
 _cairo_cache_foreach (cairo_cache_t 	      	 *cache,
 		      cairo_cache_callback_func_t cache_callback,
 		      void			 *closure);

Index: cairo-cache.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-cache.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- cairo-cache.c	2 Sep 2005 18:22:40 -0000	1.16
+++ cairo-cache.c	12 Sep 2005 18:15:52 -0000	1.17
@@ -46,9 +46,18 @@
     unsigned long max_size;
     unsigned long size;
 
-    cairo_bool_t preserve_entries;
+    int freeze_count;
 };
 
+static void
+_cairo_cache_remove (cairo_cache_t	 *cache,
+		     cairo_cache_entry_t *entry);
+
+static void
+_cairo_cache_shrink_to_accomodate (cairo_cache_t *cache,
+				   unsigned long  additional);
+
+
 static cairo_status_t
 _cairo_cache_init (cairo_cache_t		*cache,
 		   cairo_cache_keys_equal_func_t keys_equal,
@@ -64,7 +73,7 @@
     cache->max_size = max_size;
     cache->size = 0;
 
-    cache->preserve_entries = FALSE;
+    cache->freeze_count = 0;
 
     return CAIRO_STATUS_SUCCESS;
 }
@@ -103,7 +112,7 @@
  * of cairo_cache_entry_t. A cache entry must be able to hold hash
  * code, a size, and the key/value pair being stored in the
  * cache. Sometimes only the key will be necessary, (as in
- * _cairo_cache_remove()), and in these cases the value portion of the
+ * _cairo_cache_lookup()), and in these cases the value portion of the
  * entry need not be initialized.
  *
  * The units for max_size can be chosen by the caller, but should be
@@ -119,7 +128,7 @@
  * continue to live even after being ejected from the cache. However,
  * in some cases the memory overhead of adding a reference count to
  * the entry would be objectionable. In such cases, the
- * _cairo_cache_preserve() and _cairo_cache_release() calls can be
+ * _cairo_cache_freeze() and _cairo_cache_thaw() calls can be
  * used to establish a window during which no automatic removal of
  * entries will occur.
  * 
@@ -164,37 +173,50 @@
 }
 
 /**
- * _cairo_cache_preserve:
+ * _cairo_cache_freeze:
  * @cache: a cache with some precious entries in it (or about to be
  * added)
  * 
- * Disable the automatic ejection of entries from the cache. Future
- * calls to _cairo_cache_insert() will add new entries to the cache
- * regardless of how large the cache grows. See
- * _cairo_cache_release().
+ * Disable the automatic ejection of entries from the cache. For as
+ * long as the cache is "frozen", calls to _cairo_cache_insert() will
+ * add new entries to the cache regardless of how large the cache
+ * grows. See _cairo_cache_thaw().
+ *
+ * NOTE: Multiple calls to _cairo_cache_freeze() will stack, in that
+ * the cache will remain "frozen" until a corresponding number of
+ * calls are made to _cairo_cache_thaw().
  **/
 void
-_cairo_cache_preserve (cairo_cache_t *cache)
+_cairo_cache_freeze (cairo_cache_t *cache)
 {
-    cache->preserve_entries = TRUE;
+    assert (cache->freeze_count >= 0);
+
+    cache->freeze_count++;
 }
 
 /**
- * _cairo_cache_release:
+ * _cairo_cache_thaw:
  * @cache: a cache, just after the entries in it have become less
- * previous
+ * precious
  * 
- * Cancel the effects of _cairo_cache_preserve(). That is, allow the
- * cache to resume ejecting entries when it is larger than max_size as
- * passed to cairo_cache_create(). If the cache is already larger than
- * max_size, no entries will be immediately removed, but the cache
- * will be brought down to size at the time of the next call to
- * _cairo_cache_insert().
+ * Cancels the effects of _cairo_cache_freeze().
+ *
+ * When a number of calls to _cairo_cache_thaw() is made corresponding
+ * to the number of calls to _cairo_cache_freeze() the cache will no
+ * longer be "frozen". If the cache had grown larger than max_size
+ * while frozen, entries will immediately be ejected (by random) from
+ * the cache until the cache is smaller than max_size. Also, the
+ * automatic ejection of entries on _cairo_cache_insert() will resume.
  **/
 void
-_cairo_cache_release (cairo_cache_t *cache)
+_cairo_cache_thaw (cairo_cache_t *cache)
 {
-    cache->preserve_entries = FALSE;
+    assert (cache->freeze_count > 0);
+
+    cache->freeze_count--;
+
+    if (cache->freeze_count == 0)
+	_cairo_cache_shrink_to_accomodate (cache, 0);
 }
 
 /**
@@ -211,7 +233,7 @@
  * @key, (which will now be in *entry_return). %FALSE otherwise, (in
  * which case *entry_return will be %NULL).
  **/
-cairo_private cairo_bool_t
+cairo_bool_t
 _cairo_cache_lookup (cairo_cache_t	  *cache,
 		     cairo_cache_entry_t  *key,
 		     cairo_cache_entry_t **entry_return)
@@ -246,6 +268,35 @@
 }
 
 /**
+ * _cairo_cache_shrink_to_accomodate:
+ * @cache: a cache
+ * @additional: additional size requested in bytes
+ * 
+ * If cache is not frozen, eject entries randomly until the size of
+ * the cache is at least @additional bytes less than
+ * cache->max_size. That is, make enough room to accomodate a new
+ * entry of size @additional.
+ **/
+static void
+_cairo_cache_shrink_to_accomodate (cairo_cache_t *cache,
+				   unsigned long  additional)
+{
+    cairo_int_status_t status;
+
+    if (cache->freeze_count)
+	return;
+
+    while (cache->size + additional > cache->max_size) {
+	status = _cairo_cache_remove_random (cache);
+	if (status) {
+	    if (status == CAIRO_INT_STATUS_CACHE_EMPTY)
+		return;
+	    ASSERT_NOT_REACHED;
+	}
+    }
+}
+
+/**
  * _cairo_cache_insert:
  * @cache: a cache
  * @entry: an entry to be inserted
@@ -257,22 +308,13 @@
  * Return value: CAIRO_STATUS_SUCCESS if successful or
  * CAIRO_STATUS_NO_MEMORY if insufficient memory is available.
  **/
-cairo_private cairo_status_t
+cairo_status_t
 _cairo_cache_insert (cairo_cache_t	 *cache,
 		     cairo_cache_entry_t *entry)
 {
     cairo_status_t status;
 
-    if (! cache->preserve_entries) {
-	while (cache->size + entry->size > cache->max_size) {
-	    status = _cairo_cache_remove_random (cache);
-	    if (status) {
-		if (status == CAIRO_INT_STATUS_CACHE_EMPTY)
-		    break;
-		return status;
-	    }
-	}
-    }
+    _cairo_cache_shrink_to_accomodate (cache, entry->size);
 
     status = _cairo_hash_table_insert (cache->hash_table,
 				       (cairo_hash_entry_t *) entry);
@@ -287,13 +329,16 @@
 /**
  * _cairo_cache_remove:
  * @cache: a cache
- * @entry: key of entry to be removed
+ * @entry: an entry that exists in the cache
  * 
- * Remove an entry from the cache which has a key that matches @key,
- * if any (as determined by the keys_equal() function passed to
- * _cairo_cache_create()).
+ * Remove an existing entry from the cache.
+ *
+ * (NOTE: If any caller wanted access to a non-static version of this
+ * function, an improved version would require only a key rather than
+ * an entry. Fixing that would require fixing _cairo_hash_table_remove
+ * to return (a copy of?) the entry being removed.)
  **/
-cairo_private void
+static void
 _cairo_cache_remove (cairo_cache_t	 *cache,
 		     cairo_cache_entry_t *entry)
 {

Index: cairo-scaled-font.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-scaled-font.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- cairo-scaled-font.c	1 Sep 2005 13:13:46 -0000	1.3
+++ cairo-scaled-font.c	12 Sep 2005 18:15:52 -0000	1.4
@@ -804,7 +804,7 @@
     
     status = CAIRO_STATUS_SUCCESS;
 
-    _cairo_cache_preserve (scaled_font->glyphs);
+    _cairo_cache_freeze (scaled_font->glyphs);
 
     for (i = 0; i < num_glyphs; i++) {
 	int x, y;
@@ -886,8 +886,7 @@
     }
 	
 CLEANUP_MASK:
-    
-    _cairo_cache_release (scaled_font->glyphs);
+    _cairo_cache_thaw (scaled_font->glyphs);
     
     if (mask != NULL)
 	cairo_surface_destroy (mask);



More information about the cairo-commit mailing list