mirror of
https://gitlab.freedesktop.org/cairo/cairo.git
synced 2026-05-05 00:38:06 +02:00
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.
This commit is contained in:
parent
91dfee420c
commit
506636e19e
1 changed files with 32 additions and 1 deletions
33
src/cairo.c
33
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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue