summaryrefslogtreecommitdiffstats
path: root/gfx/cairo/add-a-stash-of-cairo_t-s.patch
blob: c6fcdd9d881ad2ec56c8a2d088d81a39f6310ab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
commit dfec2c249915560cedd2b49326c6629ad8a0b0f2
Author: Jeff Muizelaar <jmuizelaar@mozilla.com>
Date:   Tue Mar 2 16:01:41 2010 -0500

    add a stash of cairo_t's

diff --git a/src/cairo.c b/src/cairo.c
index 3c9d892..4b27b83 100644
--- a/src/cairo.c
+++ b/src/cairo.c
@@ -119,7 +119,63 @@ _cairo_set_error (cairo_t *cr, cairo_status_t status)
     _cairo_status_set_error (&cr->status, _cairo_error (status));
 }
 
-#if HAS_ATOMIC_OPS
+#if defined(_MSC_VER)
+#pragma intrinsic(_BitScanForward)
+static __forceinline int
+ffs(int x)
+{
+    unsigned long i;
+
+    if (_BitScanForward(&i, x) != 0)
+	return i + 1;
+
+    return 0;
+}
+#endif
+
+
+#if 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 {