[cairo] [PATCH] Add an support for the cairo_t stash with CAIRO_NO_MUTEX

Jeff Muizelaar jeff at infidigm.net
Thu May 6 08:53:44 PDT 2010


     The implemenation is the same as the HAS_ATOMIC_OPS stash
     without the use of atomics.

diff --git a/src/cairo.c b/src/cairo.c
index 4246ab7..ba1c146 100644
--- a/src/cairo.c
+++ b/src/cairo.c
@@ -122,7 +122,48 @@ _cairo_set_error (cairo_t *cr, cairo_status_t status)
      _cairo_status_set_error (&cr->status, _cairo_error (status));
  }

-#if HAS_ATOMIC_OPS
+#ifdef CAIRO_NO_MUTEX
+/* We keep a small stash of contexts to reduce malloc pressure */
+#define CAIRO_STASH_SIZE 4
+static struct {
+    cairo_t pool[CAIRO_STASH_SIZE];
+    int occupied;
+} _context_stash;
+
+static cairo_t *
+_context_get (void)
+{
+    int avail, old, new;
+
+    old = _context_stash.occupied;
+    avail = ffs (~old) - 1;
+    if (avail >= CAIRO_STASH_SIZE)
+    return malloc (sizeof (cairo_t));
+
+    new = old | (1 << avail);
+    _context_stash.occupied = new;
+
+    return &_context_stash.pool[avail];
+}
+
+static void
+_context_put (cairo_t *cr)
+{
+    int old, new, avail;
+
+    if (cr < &_context_stash.pool[0] ||
+    cr >= &_context_stash.pool[CAIRO_STASH_SIZE])
+    {
+    free (cr);
+    return;
+    }
+
+    avail = ~(1 << (cr - &_context_stash.pool[0]));
+    old = _context_stash.occupied;
+    new = old & avail;
+    _context_stash.occupied = new;
+}
+#elif HAS_ATOMIC_OPS
  /* We keep a small stash of contexts to reduce malloc pressure */
  #define CAIRO_STASH_SIZE 4
  static struct {



More information about the cairo mailing list