summaryrefslogtreecommitdiff
path: root/src/cairo.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2010-05-06 17:21:59 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2010-05-06 17:21:59 +0100
commit506636e19edcdb656c94f61c915c9d49829dade8 (patch)
tree1ce506cd9351633aff35f8561089e35fb94b6299 /src/cairo.c
parent91dfee420c3e9d85e5cd4f2cec6dc708c0e471a2 (diff)
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.
Diffstat (limited to 'src/cairo.c')
-rw-r--r--src/cairo.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/cairo.c b/src/cairo.c
index 4246ab74..b417f55b 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;