Revert "pipebuffer: Implement proper buffer validation."
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_bufmgr_pool.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 /**
30 * \file
31 * Batch buffer pool management.
32 *
33 * \author José Fonseca <jrfonseca-at-tungstengraphics-dot-com>
34 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
35 */
36
37
38 #include "pipe/p_compiler.h"
39 #include "pipe/p_debug.h"
40 #include "pipe/p_thread.h"
41 #include "pipe/p_defines.h"
42 #include "util/u_memory.h"
43 #include "util/u_double_list.h"
44
45 #include "pb_buffer.h"
46 #include "pb_bufmgr.h"
47
48
49 /**
50 * Convenience macro (type safe).
51 */
52 #define SUPER(__derived) (&(__derived)->base)
53
54
55 struct pool_pb_manager
56 {
57 struct pb_manager base;
58
59 pipe_mutex mutex;
60
61 size_t bufSize;
62 size_t bufAlign;
63
64 size_t numFree;
65 size_t numTot;
66
67 struct list_head free;
68
69 struct pb_buffer *buffer;
70 void *map;
71
72 struct pool_buffer *bufs;
73 };
74
75
76 static INLINE struct pool_pb_manager *
77 pool_pb_manager(struct pb_manager *mgr)
78 {
79 assert(mgr);
80 return (struct pool_pb_manager *)mgr;
81 }
82
83
84 struct pool_buffer
85 {
86 struct pb_buffer base;
87
88 struct pool_pb_manager *mgr;
89
90 struct list_head head;
91
92 size_t start;
93 };
94
95
96 static INLINE struct pool_buffer *
97 pool_buffer(struct pb_buffer *buf)
98 {
99 assert(buf);
100 return (struct pool_buffer *)buf;
101 }
102
103
104
105 static void
106 pool_buffer_destroy(struct pb_buffer *buf)
107 {
108 struct pool_buffer *pool_buf = pool_buffer(buf);
109 struct pool_pb_manager *pool = pool_buf->mgr;
110
111 assert(pool_buf->base.base.refcount == 0);
112
113 pipe_mutex_lock(pool->mutex);
114 LIST_ADD(&pool_buf->head, &pool->free);
115 pool->numFree++;
116 pipe_mutex_unlock(pool->mutex);
117 }
118
119
120 static void *
121 pool_buffer_map(struct pb_buffer *buf, unsigned flags)
122 {
123 struct pool_buffer *pool_buf = pool_buffer(buf);
124 struct pool_pb_manager *pool = pool_buf->mgr;
125 void *map;
126
127 pipe_mutex_lock(pool->mutex);
128 map = (unsigned char *) pool->map + pool_buf->start;
129 pipe_mutex_unlock(pool->mutex);
130 return map;
131 }
132
133
134 static void
135 pool_buffer_unmap(struct pb_buffer *buf)
136 {
137 /* No-op */
138 }
139
140
141 static void
142 pool_buffer_get_base_buffer(struct pb_buffer *buf,
143 struct pb_buffer **base_buf,
144 unsigned *offset)
145 {
146 struct pool_buffer *pool_buf = pool_buffer(buf);
147 struct pool_pb_manager *pool = pool_buf->mgr;
148 pb_get_base_buffer(pool->buffer, base_buf, offset);
149 *offset += pool_buf->start;
150 }
151
152
153 static const struct pb_vtbl
154 pool_buffer_vtbl = {
155 pool_buffer_destroy,
156 pool_buffer_map,
157 pool_buffer_unmap,
158 pool_buffer_get_base_buffer
159 };
160
161
162 static struct pb_buffer *
163 pool_bufmgr_create_buffer(struct pb_manager *mgr,
164 size_t size,
165 const struct pb_desc *desc)
166 {
167 struct pool_pb_manager *pool = pool_pb_manager(mgr);
168 struct pool_buffer *pool_buf;
169 struct list_head *item;
170
171 assert(size == pool->bufSize);
172 assert(pool->bufAlign % desc->alignment == 0);
173
174 pipe_mutex_lock(pool->mutex);
175
176 if (pool->numFree == 0) {
177 pipe_mutex_unlock(pool->mutex);
178 debug_printf("warning: out of fixed size buffer objects\n");
179 return NULL;
180 }
181
182 item = pool->free.next;
183
184 if (item == &pool->free) {
185 pipe_mutex_unlock(pool->mutex);
186 debug_printf("error: fixed size buffer pool corruption\n");
187 return NULL;
188 }
189
190 LIST_DEL(item);
191 --pool->numFree;
192
193 pipe_mutex_unlock(pool->mutex);
194
195 pool_buf = LIST_ENTRY(struct pool_buffer, item, head);
196 assert(pool_buf->base.base.refcount == 0);
197 pool_buf->base.base.refcount = 1;
198 pool_buf->base.base.alignment = desc->alignment;
199 pool_buf->base.base.usage = desc->usage;
200
201 return SUPER(pool_buf);
202 }
203
204
205 static void
206 pool_bufmgr_flush(struct pb_manager *mgr)
207 {
208 /* No-op */
209 }
210
211
212 static void
213 pool_bufmgr_destroy(struct pb_manager *mgr)
214 {
215 struct pool_pb_manager *pool = pool_pb_manager(mgr);
216 pipe_mutex_lock(pool->mutex);
217
218 FREE(pool->bufs);
219
220 pb_unmap(pool->buffer);
221 pb_reference(&pool->buffer, NULL);
222
223 pipe_mutex_unlock(pool->mutex);
224
225 FREE(mgr);
226 }
227
228
229 struct pb_manager *
230 pool_bufmgr_create(struct pb_manager *provider,
231 size_t numBufs,
232 size_t bufSize,
233 const struct pb_desc *desc)
234 {
235 struct pool_pb_manager *pool;
236 struct pool_buffer *pool_buf;
237 size_t i;
238
239 if(!provider)
240 return NULL;
241
242 pool = CALLOC_STRUCT(pool_pb_manager);
243 if (!pool)
244 return NULL;
245
246 pool->base.destroy = pool_bufmgr_destroy;
247 pool->base.create_buffer = pool_bufmgr_create_buffer;
248 pool->base.flush = pool_bufmgr_flush;
249
250 LIST_INITHEAD(&pool->free);
251
252 pool->numTot = numBufs;
253 pool->numFree = numBufs;
254 pool->bufSize = bufSize;
255 pool->bufAlign = desc->alignment;
256
257 pipe_mutex_init(pool->mutex);
258
259 pool->buffer = provider->create_buffer(provider, numBufs*bufSize, desc);
260 if (!pool->buffer)
261 goto failure;
262
263 pool->map = pb_map(pool->buffer,
264 PIPE_BUFFER_USAGE_CPU_READ |
265 PIPE_BUFFER_USAGE_CPU_WRITE);
266 if(!pool->map)
267 goto failure;
268
269 pool->bufs = (struct pool_buffer *)CALLOC(numBufs, sizeof(*pool->bufs));
270 if (!pool->bufs)
271 goto failure;
272
273 pool_buf = pool->bufs;
274 for (i = 0; i < numBufs; ++i) {
275 pool_buf->base.base.refcount = 0;
276 pool_buf->base.base.alignment = 0;
277 pool_buf->base.base.usage = 0;
278 pool_buf->base.base.size = bufSize;
279 pool_buf->base.vtbl = &pool_buffer_vtbl;
280 pool_buf->mgr = pool;
281 pool_buf->start = i * bufSize;
282 LIST_ADDTAIL(&pool_buf->head, &pool->free);
283 pool_buf++;
284 }
285
286 return SUPER(pool);
287
288 failure:
289 if(pool->bufs)
290 FREE(pool->bufs);
291 if(pool->map)
292 pb_unmap(pool->buffer);
293 if(pool->buffer)
294 pb_reference(&pool->buffer, NULL);
295 if(pool)
296 FREE(pool);
297 return NULL;
298 }