michael@0: commit dfec2c249915560cedd2b49326c6629ad8a0b0f2 michael@0: Author: Jeff Muizelaar michael@0: Date: Tue Mar 2 16:01:41 2010 -0500 michael@0: michael@0: add a stash of cairo_t's michael@0: michael@0: diff --git a/src/cairo.c b/src/cairo.c michael@0: index 3c9d892..4b27b83 100644 michael@0: --- a/src/cairo.c michael@0: +++ b/src/cairo.c michael@0: @@ -119,7 +119,63 @@ _cairo_set_error (cairo_t *cr, cairo_status_t status) michael@0: _cairo_status_set_error (&cr->status, _cairo_error (status)); michael@0: } michael@0: michael@0: -#if HAS_ATOMIC_OPS michael@0: +#if defined(_MSC_VER) michael@0: +#pragma intrinsic(_BitScanForward) michael@0: +static __forceinline int michael@0: +ffs(int x) michael@0: +{ michael@0: + unsigned long i; michael@0: + michael@0: + if (_BitScanForward(&i, x) != 0) michael@0: + return i + 1; michael@0: + michael@0: + return 0; michael@0: +} michael@0: +#endif michael@0: + michael@0: + michael@0: +#if CAIRO_NO_MUTEX michael@0: +/* We keep a small stash of contexts to reduce malloc pressure */ michael@0: +#define CAIRO_STASH_SIZE 4 michael@0: +static struct { michael@0: + cairo_t pool[CAIRO_STASH_SIZE]; michael@0: + int occupied; michael@0: +} _context_stash; michael@0: + michael@0: +static cairo_t * michael@0: +_context_get (void) michael@0: +{ michael@0: + int avail, old, new; michael@0: + michael@0: + old = _context_stash.occupied; michael@0: + avail = ffs (~old) - 1; michael@0: + if (avail >= CAIRO_STASH_SIZE) michael@0: + return malloc (sizeof (cairo_t)); michael@0: + michael@0: + new = old | (1 << avail); michael@0: + _context_stash.occupied = new; michael@0: + michael@0: + return &_context_stash.pool[avail]; michael@0: +} michael@0: + michael@0: +static void michael@0: +_context_put (cairo_t *cr) michael@0: +{ michael@0: + int old, new, avail; michael@0: + michael@0: + if (cr < &_context_stash.pool[0] || michael@0: + cr >= &_context_stash.pool[CAIRO_STASH_SIZE]) michael@0: + { michael@0: + free (cr); michael@0: + return; michael@0: + } michael@0: + michael@0: + avail = ~(1 << (cr - &_context_stash.pool[0])); michael@0: + old = _context_stash.occupied; michael@0: + new = old & avail; michael@0: + _context_stash.occupied = new; michael@0: +} michael@0: +#elif HAS_ATOMIC_OPS michael@0: /* We keep a small stash of contexts to reduce malloc pressure */ michael@0: #define CAIRO_STASH_SIZE 4 michael@0: static struct {