[cairo-commit] src/cairo.c

Chris Wilson ickle at kemper.freedesktop.org
Thu May 6 09:24:17 PDT 2010


 src/cairo.c |   33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

New commits:
commit 506636e19edcdb656c94f61c915c9d49829dade8
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Thu May 6 17:21:59 2010 +0100

    cairo: stash a number of contexts for use with NO_MUTEX
    
    The implementation is the same as the atomic one (bar the use of atomic
    primitives to manipulate the occupancy!).
    
    Patch based on the original by Jeff Muizelaar.

diff --git a/src/cairo.c b/src/cairo.c
index 4246ab7..b417f55 100644
--- a/src/cairo.c
+++ b/src/cairo.c
@@ -122,9 +122,40 @@ _cairo_set_error (cairo_t *cr, cairo_status_t status)
     _cairo_status_set_error (&cr->status, _cairo_error (status));
 }
 
-#if HAS_ATOMIC_OPS
 /* We keep a small stash of contexts to reduce malloc pressure */
 #define CAIRO_STASH_SIZE 4
+#if CAIRO_NO_MUTEX
+static struct {
+    cairo_t pool[CAIRO_STASH_SIZE];
+    int occupied;
+} _context_stash;
+
+static cairo_t *
+_context_get (void)
+{
+    int avail;
+
+    avail = ffs (~_context_stash.occupied) - 1;
+    if (avail >= CAIRO_STASH_SIZE)
+	return malloc (sizeof (cairo_t));
+
+    _context_stash.occupied |= 1 << avail;
+    return &_context_stash.pool[avail];
+}
+
+static void
+_context_put (cairo_t *cr)
+{
+    if (cr < &_context_stash.pool[0] ||
+	cr >= &_context_stash.pool[CAIRO_STASH_SIZE])
+    {
+	free (cr);
+	return;
+    }
+
+    _context_stash.occupied &= ~(1 << (cr - &_context_stash.pool[0]));
+}
+#elif HAS_ATOMIC_OPS
 static struct {
     cairo_t pool[CAIRO_STASH_SIZE];
     cairo_atomic_int_t occupied;


More information about the cairo-commit mailing list