1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2009 Chris Wilson
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it either under the terms of the GNU Lesser General Public
7
 * License version 2.1 as published by the Free Software Foundation
8
 * (the "LGPL") or, at your option, under the terms of the Mozilla
9
 * Public License Version 1.1 (the "MPL"). If you do not alter this
10
 * notice, a recipient may use your version of this file under either
11
 * the MPL or the LGPL.
12
 *
13
 * You should have received a copy of the LGPL along with this library
14
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16
 * You should have received a copy of the MPL along with this library
17
 * in the file COPYING-MPL-1.1
18
 *
19
 * The contents of this file are subject to the Mozilla Public License
20
 * Version 1.1 (the "License"); you may not use this file except in
21
 * compliance with the License. You may obtain a copy of the License at
22
 * http://www.mozilla.org/MPL/
23
 *
24
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26
 * the specific language governing rights and limitations.
27
 *
28
 * The Original Code is the cairo graphics library.
29
 *
30
 * The Initial Developer of the Original Code is University of Southern
31
 * California.
32
 *
33
 * Contributor(s):
34
 *	Chris Wilson <chris@chris-wilson.co.uk>
35
 */
36

            
37
#ifndef CAIRO_FREED_POOL_H
38
#define CAIRO_FREED_POOL_H
39

            
40
#include "cairoint.h"
41
#include "cairo-atomic-private.h"
42

            
43
CAIRO_BEGIN_DECLS
44

            
45
#define DISABLE_FREED_POOLS 0
46

            
47
#if HAS_ATOMIC_OPS && ! DISABLE_FREED_POOLS
48
/* Keep a stash of recently freed clip_paths, since we need to
49
 * reallocate them frequently.
50
 */
51
#define MAX_FREED_POOL_SIZE 16
52
typedef struct {
53
    cairo_atomic_intptr_t pool[MAX_FREED_POOL_SIZE];
54
    cairo_atomic_int_t top;
55
} freed_pool_t;
56

            
57
static cairo_always_inline void *
58
_atomic_fetch (cairo_atomic_intptr_t *slot)
59
{
60
    void *ptr;
61

            
62
    do {
63
2867567
        ptr = _cairo_atomic_ptr_get (slot);
64
2867567
    } while (! _cairo_atomic_ptr_cmpxchg (slot, ptr, NULL));
65

            
66
2863986
    return ptr;
67
}
68

            
69
static cairo_always_inline cairo_bool_t
70
_atomic_store (cairo_atomic_intptr_t *slot, void *ptr)
71
{
72
2517198
    return _cairo_atomic_ptr_cmpxchg (slot, NULL, ptr);
73
}
74

            
75
cairo_private void *
76
_freed_pool_get_search (freed_pool_t *pool);
77

            
78
static inline void *
79
2493811
_freed_pool_get (freed_pool_t *pool)
80
{
81
    void *ptr;
82
    int i;
83

            
84
2493811
    i = _cairo_atomic_int_get_relaxed (&pool->top) - 1;
85
2493811
    if (i < 0)
86
13046
	i = 0;
87

            
88
2493811
    ptr = _atomic_fetch (&pool->pool[i]);
89
2493811
    if (likely (ptr != NULL)) {
90
2473806
	_cairo_atomic_int_set_relaxed (&pool->top, i);
91
2473806
	return ptr;
92
    }
93

            
94
    /* either empty or contended */
95
20005
    return _freed_pool_get_search (pool);
96
}
97

            
98
cairo_private void
99
_freed_pool_put_search (freed_pool_t *pool, void *ptr);
100

            
101
static inline void
102
2493500
_freed_pool_put (freed_pool_t *pool, void *ptr)
103
{
104
    int i;
105

            
106
2493500
    i = _cairo_atomic_int_get_relaxed (&pool->top);
107
4986975
    if (likely (i < ARRAY_LENGTH (pool->pool) &&
108
		_atomic_store (&pool->pool[i], ptr)))
109
    {
110
2488115
	_cairo_atomic_int_set_relaxed (&pool->top, i + 1);
111
2488115
	return;
112
    }
113

            
114
    /* either full or contended */
115
5385
    _freed_pool_put_search (pool, ptr);
116
}
117

            
118
cairo_private void
119
_freed_pool_reset (freed_pool_t *pool);
120

            
121
#define HAS_FREED_POOL 1
122

            
123
#else
124

            
125
/* A warning about an unused freed-pool in a build without atomics
126
 * enabled usually indicates a missing _freed_pool_reset() in the
127
 * static reset function */
128

            
129
typedef int freed_pool_t;
130

            
131
#define _freed_pool_get(pool) NULL
132
#define _freed_pool_put(pool, ptr) free(ptr)
133
#define _freed_pool_reset(ptr)
134

            
135
#endif
136

            
137
CAIRO_END_DECLS
138

            
139
#endif /* CAIRO_FREED_POOL_PRIVATE_H */