revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / pipebuffer / pb_bufmgr_slab.c
blob176f9aa38aa421dd3de022cf59d6fd2c0bd469f3
1 /**************************************************************************
3 * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
4 * All Rights Reserved.
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:
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.
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.
27 **************************************************************************/
29 /**
30 * @file
31 * S-lab pool implementation.
33 * @sa http://en.wikipedia.org/wiki/Slab_allocation
35 * @author Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
36 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
39 #include "pipe/p_compiler.h"
40 #include "util/u_debug.h"
41 #include "os/os_thread.h"
42 #include "pipe/p_defines.h"
43 #include "util/u_memory.h"
44 #include "util/u_double_list.h"
45 #include "util/u_time.h"
47 #include "pb_buffer.h"
48 #include "pb_bufmgr.h"
51 struct pb_slab;
54 /**
55 * Buffer in a slab.
57 * Sub-allocation of a contiguous buffer.
59 struct pb_slab_buffer
61 struct pb_buffer base;
63 struct pb_slab *slab;
65 struct list_head head;
67 unsigned mapCount;
69 /** Offset relative to the start of the slab buffer. */
70 pb_size start;
72 /** Use when validating, to signal that all mappings are finished */
73 /* TODO: Actually validation does not reach this stage yet */
74 pipe_condvar event;
78 /**
79 * Slab -- a contiguous piece of memory.
81 struct pb_slab
83 struct list_head head;
84 struct list_head freeBuffers;
85 pb_size numBuffers;
86 pb_size numFree;
88 struct pb_slab_buffer *buffers;
89 struct pb_slab_manager *mgr;
91 /** Buffer from the provider */
92 struct pb_buffer *bo;
94 void *virtual;
98 /**
99 * It adds/removes slabs as needed in order to meet the allocation/destruction
100 * of individual buffers.
102 struct pb_slab_manager
104 struct pb_manager base;
106 /** From where we get our buffers */
107 struct pb_manager *provider;
109 /** Size of the buffers we hand on downstream */
110 pb_size bufSize;
112 /** Size of the buffers we request upstream */
113 pb_size slabSize;
115 /**
116 * Alignment, usage to be used to allocate the slab buffers.
118 * We can only provide buffers which are consistent (in alignment, usage)
119 * with this description.
121 struct pb_desc desc;
123 /**
124 * Partial slabs
126 * Full slabs are not stored in any list. Empty slabs are destroyed
127 * immediatly.
129 struct list_head slabs;
131 pipe_mutex mutex;
136 * Wrapper around several slabs, therefore capable of handling buffers of
137 * multiple sizes.
139 * This buffer manager just dispatches buffer allocations to the appropriate slab
140 * manager, according to the requested buffer size, or by passes the slab
141 * managers altogether for even greater sizes.
143 * The data of this structure remains constant after
144 * initialization and thus needs no mutex protection.
146 struct pb_slab_range_manager
148 struct pb_manager base;
150 struct pb_manager *provider;
152 pb_size minBufSize;
153 pb_size maxBufSize;
155 /** @sa pb_slab_manager::desc */
156 struct pb_desc desc;
158 unsigned numBuckets;
159 pb_size *bucketSizes;
161 /** Array of pb_slab_manager, one for each bucket size */
162 struct pb_manager **buckets;
166 static INLINE struct pb_slab_buffer *
167 pb_slab_buffer(struct pb_buffer *buf)
169 assert(buf);
170 return (struct pb_slab_buffer *)buf;
174 static INLINE struct pb_slab_manager *
175 pb_slab_manager(struct pb_manager *mgr)
177 assert(mgr);
178 return (struct pb_slab_manager *)mgr;
182 static INLINE struct pb_slab_range_manager *
183 pb_slab_range_manager(struct pb_manager *mgr)
185 assert(mgr);
186 return (struct pb_slab_range_manager *)mgr;
191 * Delete a buffer from the slab delayed list and put
192 * it on the slab FREE list.
194 static void
195 pb_slab_buffer_destroy(struct pb_buffer *_buf)
197 struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
198 struct pb_slab *slab = buf->slab;
199 struct pb_slab_manager *mgr = slab->mgr;
200 struct list_head *list = &buf->head;
202 pipe_mutex_lock(mgr->mutex);
204 assert(!pipe_is_referenced(&buf->base.base.reference));
206 buf->mapCount = 0;
208 LIST_DEL(list);
209 LIST_ADDTAIL(list, &slab->freeBuffers);
210 slab->numFree++;
212 if (slab->head.next == &slab->head)
213 LIST_ADDTAIL(&slab->head, &mgr->slabs);
215 /* If the slab becomes totally empty, free it */
216 if (slab->numFree == slab->numBuffers) {
217 list = &slab->head;
218 LIST_DELINIT(list);
219 pb_reference(&slab->bo, NULL);
220 FREE(slab->buffers);
221 FREE(slab);
224 pipe_mutex_unlock(mgr->mutex);
228 static void *
229 pb_slab_buffer_map(struct pb_buffer *_buf,
230 unsigned flags,
231 void *flush_ctx)
233 struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
235 /* XXX: it will be necessary to remap here to propagate flush_ctx */
237 ++buf->mapCount;
238 return (void *) ((uint8_t *) buf->slab->virtual + buf->start);
242 static void
243 pb_slab_buffer_unmap(struct pb_buffer *_buf)
245 struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
247 --buf->mapCount;
248 if (buf->mapCount == 0)
249 pipe_condvar_broadcast(buf->event);
253 static enum pipe_error
254 pb_slab_buffer_validate(struct pb_buffer *_buf,
255 struct pb_validate *vl,
256 unsigned flags)
258 struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
259 return pb_validate(buf->slab->bo, vl, flags);
263 static void
264 pb_slab_buffer_fence(struct pb_buffer *_buf,
265 struct pipe_fence_handle *fence)
267 struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
268 pb_fence(buf->slab->bo, fence);
272 static void
273 pb_slab_buffer_get_base_buffer(struct pb_buffer *_buf,
274 struct pb_buffer **base_buf,
275 pb_size *offset)
277 struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
278 pb_get_base_buffer(buf->slab->bo, base_buf, offset);
279 *offset += buf->start;
283 static const struct pb_vtbl
284 pb_slab_buffer_vtbl = {
285 pb_slab_buffer_destroy,
286 pb_slab_buffer_map,
287 pb_slab_buffer_unmap,
288 pb_slab_buffer_validate,
289 pb_slab_buffer_fence,
290 pb_slab_buffer_get_base_buffer
295 * Create a new slab.
297 * Called when we ran out of free slabs.
299 static enum pipe_error
300 pb_slab_create(struct pb_slab_manager *mgr)
302 struct pb_slab *slab;
303 struct pb_slab_buffer *buf;
304 unsigned numBuffers;
305 unsigned i;
306 enum pipe_error ret;
308 slab = CALLOC_STRUCT(pb_slab);
309 if (!slab)
310 return PIPE_ERROR_OUT_OF_MEMORY;
312 slab->bo = mgr->provider->create_buffer(mgr->provider, mgr->slabSize, &mgr->desc);
313 if(!slab->bo) {
314 ret = PIPE_ERROR_OUT_OF_MEMORY;
315 goto out_err0;
318 /* Note down the slab virtual address. All mappings are accessed directly
319 * through this address so it is required that the buffer is pinned. */
320 slab->virtual = pb_map(slab->bo,
321 PB_USAGE_CPU_READ |
322 PB_USAGE_CPU_WRITE, NULL);
323 if(!slab->virtual) {
324 ret = PIPE_ERROR_OUT_OF_MEMORY;
325 goto out_err1;
327 pb_unmap(slab->bo);
329 numBuffers = slab->bo->base.size / mgr->bufSize;
331 slab->buffers = CALLOC(numBuffers, sizeof(*slab->buffers));
332 if (!slab->buffers) {
333 ret = PIPE_ERROR_OUT_OF_MEMORY;
334 goto out_err1;
337 LIST_INITHEAD(&slab->head);
338 LIST_INITHEAD(&slab->freeBuffers);
339 slab->numBuffers = numBuffers;
340 slab->numFree = 0;
341 slab->mgr = mgr;
343 buf = slab->buffers;
344 for (i=0; i < numBuffers; ++i) {
345 pipe_reference_init(&buf->base.base.reference, 0);
346 buf->base.base.size = mgr->bufSize;
347 buf->base.base.alignment = 0;
348 buf->base.base.usage = 0;
349 buf->base.vtbl = &pb_slab_buffer_vtbl;
350 buf->slab = slab;
351 buf->start = i* mgr->bufSize;
352 buf->mapCount = 0;
353 pipe_condvar_init(buf->event);
354 LIST_ADDTAIL(&buf->head, &slab->freeBuffers);
355 slab->numFree++;
356 buf++;
359 /* Add this slab to the list of partial slabs */
360 LIST_ADDTAIL(&slab->head, &mgr->slabs);
362 return PIPE_OK;
364 out_err1:
365 pb_reference(&slab->bo, NULL);
366 out_err0:
367 FREE(slab);
368 return ret;
372 static struct pb_buffer *
373 pb_slab_manager_create_buffer(struct pb_manager *_mgr,
374 pb_size size,
375 const struct pb_desc *desc)
377 struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
378 static struct pb_slab_buffer *buf;
379 struct pb_slab *slab;
380 struct list_head *list;
382 /* check size */
383 assert(size <= mgr->bufSize);
384 if(size > mgr->bufSize)
385 return NULL;
387 /* check if we can provide the requested alignment */
388 assert(pb_check_alignment(desc->alignment, mgr->desc.alignment));
389 if(!pb_check_alignment(desc->alignment, mgr->desc.alignment))
390 return NULL;
391 assert(pb_check_alignment(desc->alignment, mgr->bufSize));
392 if(!pb_check_alignment(desc->alignment, mgr->bufSize))
393 return NULL;
395 assert(pb_check_usage(desc->usage, mgr->desc.usage));
396 if(!pb_check_usage(desc->usage, mgr->desc.usage))
397 return NULL;
399 pipe_mutex_lock(mgr->mutex);
401 /* Create a new slab, if we run out of partial slabs */
402 if (mgr->slabs.next == &mgr->slabs) {
403 (void) pb_slab_create(mgr);
404 if (mgr->slabs.next == &mgr->slabs) {
405 pipe_mutex_unlock(mgr->mutex);
406 return NULL;
410 /* Allocate the buffer from a partial (or just created) slab */
411 list = mgr->slabs.next;
412 slab = LIST_ENTRY(struct pb_slab, list, head);
414 /* If totally full remove from the partial slab list */
415 if (--slab->numFree == 0)
416 LIST_DELINIT(list);
418 list = slab->freeBuffers.next;
419 LIST_DELINIT(list);
421 pipe_mutex_unlock(mgr->mutex);
422 buf = LIST_ENTRY(struct pb_slab_buffer, list, head);
424 pipe_reference_init(&buf->base.base.reference, 1);
425 buf->base.base.alignment = desc->alignment;
426 buf->base.base.usage = desc->usage;
428 return &buf->base;
432 static void
433 pb_slab_manager_flush(struct pb_manager *_mgr)
435 struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
437 assert(mgr->provider->flush);
438 if(mgr->provider->flush)
439 mgr->provider->flush(mgr->provider);
443 static void
444 pb_slab_manager_destroy(struct pb_manager *_mgr)
446 struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
448 /* TODO: cleanup all allocated buffers */
449 FREE(mgr);
453 struct pb_manager *
454 pb_slab_manager_create(struct pb_manager *provider,
455 pb_size bufSize,
456 pb_size slabSize,
457 const struct pb_desc *desc)
459 struct pb_slab_manager *mgr;
461 mgr = CALLOC_STRUCT(pb_slab_manager);
462 if (!mgr)
463 return NULL;
465 mgr->base.destroy = pb_slab_manager_destroy;
466 mgr->base.create_buffer = pb_slab_manager_create_buffer;
467 mgr->base.flush = pb_slab_manager_flush;
469 mgr->provider = provider;
470 mgr->bufSize = bufSize;
471 mgr->slabSize = slabSize;
472 mgr->desc = *desc;
474 LIST_INITHEAD(&mgr->slabs);
476 pipe_mutex_init(mgr->mutex);
478 return &mgr->base;
482 static struct pb_buffer *
483 pb_slab_range_manager_create_buffer(struct pb_manager *_mgr,
484 pb_size size,
485 const struct pb_desc *desc)
487 struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
488 pb_size bufSize;
489 pb_size reqSize = size;
490 unsigned i;
492 if(desc->alignment > reqSize)
493 reqSize = desc->alignment;
495 bufSize = mgr->minBufSize;
496 for (i = 0; i < mgr->numBuckets; ++i) {
497 if(bufSize >= reqSize)
498 return mgr->buckets[i]->create_buffer(mgr->buckets[i], size, desc);
499 bufSize *= 2;
502 /* Fall back to allocate a buffer object directly from the provider. */
503 return mgr->provider->create_buffer(mgr->provider, size, desc);
507 static void
508 pb_slab_range_manager_flush(struct pb_manager *_mgr)
510 struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
512 /* Individual slabs don't hold any temporary buffers so no need to call them */
514 assert(mgr->provider->flush);
515 if(mgr->provider->flush)
516 mgr->provider->flush(mgr->provider);
520 static void
521 pb_slab_range_manager_destroy(struct pb_manager *_mgr)
523 struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
524 unsigned i;
526 for (i = 0; i < mgr->numBuckets; ++i)
527 mgr->buckets[i]->destroy(mgr->buckets[i]);
528 FREE(mgr->buckets);
529 FREE(mgr->bucketSizes);
530 FREE(mgr);
534 struct pb_manager *
535 pb_slab_range_manager_create(struct pb_manager *provider,
536 pb_size minBufSize,
537 pb_size maxBufSize,
538 pb_size slabSize,
539 const struct pb_desc *desc)
541 struct pb_slab_range_manager *mgr;
542 pb_size bufSize;
543 unsigned i;
545 if(!provider)
546 return NULL;
548 mgr = CALLOC_STRUCT(pb_slab_range_manager);
549 if (!mgr)
550 goto out_err0;
552 mgr->base.destroy = pb_slab_range_manager_destroy;
553 mgr->base.create_buffer = pb_slab_range_manager_create_buffer;
554 mgr->base.flush = pb_slab_range_manager_flush;
556 mgr->provider = provider;
557 mgr->minBufSize = minBufSize;
558 mgr->maxBufSize = maxBufSize;
560 mgr->numBuckets = 1;
561 bufSize = minBufSize;
562 while(bufSize < maxBufSize) {
563 bufSize *= 2;
564 ++mgr->numBuckets;
567 mgr->buckets = CALLOC(mgr->numBuckets, sizeof(*mgr->buckets));
568 if (!mgr->buckets)
569 goto out_err1;
571 bufSize = minBufSize;
572 for (i = 0; i < mgr->numBuckets; ++i) {
573 mgr->buckets[i] = pb_slab_manager_create(provider, bufSize, slabSize, desc);
574 if(!mgr->buckets[i])
575 goto out_err2;
576 bufSize *= 2;
579 return &mgr->base;
581 out_err2:
582 for (i = 0; i < mgr->numBuckets; ++i)
583 if(mgr->buckets[i])
584 mgr->buckets[i]->destroy(mgr->buckets[i]);
585 FREE(mgr->buckets);
586 out_err1:
587 FREE(mgr);
588 out_err0:
589 return NULL;