1 /**************************************************************************
3 * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
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
27 **************************************************************************/
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"
57 * Sub-allocation of a contiguous buffer.
61 struct pb_buffer base
;
65 struct list_head head
;
69 /** Offset relative to the start of the slab buffer. */
72 /** Use when validating, to signal that all mappings are finished */
73 /* TODO: Actually validation does not reach this stage yet */
79 * Slab -- a contiguous piece of memory.
83 struct list_head head
;
84 struct list_head freeBuffers
;
88 struct pb_slab_buffer
*buffers
;
89 struct pb_slab_manager
*mgr
;
91 /** Buffer from the provider */
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 */
112 /** Size of the buffers we request upstream */
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.
126 * Full slabs are not stored in any list. Empty slabs are destroyed
129 struct list_head slabs
;
136 * Wrapper around several slabs, therefore capable of handling buffers of
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
;
155 /** @sa pb_slab_manager::desc */
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
)
170 return (struct pb_slab_buffer
*)buf
;
174 static INLINE
struct pb_slab_manager
*
175 pb_slab_manager(struct pb_manager
*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
)
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.
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
));
209 LIST_ADDTAIL(list
, &slab
->freeBuffers
);
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
) {
219 pb_reference(&slab
->bo
, NULL
);
224 pipe_mutex_unlock(mgr
->mutex
);
229 pb_slab_buffer_map(struct pb_buffer
*_buf
,
233 struct pb_slab_buffer
*buf
= pb_slab_buffer(_buf
);
235 /* XXX: it will be necessary to remap here to propagate flush_ctx */
238 return (void *) ((uint8_t *) buf
->slab
->virtual + buf
->start
);
243 pb_slab_buffer_unmap(struct pb_buffer
*_buf
)
245 struct pb_slab_buffer
*buf
= pb_slab_buffer(_buf
);
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
,
258 struct pb_slab_buffer
*buf
= pb_slab_buffer(_buf
);
259 return pb_validate(buf
->slab
->bo
, vl
, flags
);
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
);
273 pb_slab_buffer_get_base_buffer(struct pb_buffer
*_buf
,
274 struct pb_buffer
**base_buf
,
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
,
287 pb_slab_buffer_unmap
,
288 pb_slab_buffer_validate
,
289 pb_slab_buffer_fence
,
290 pb_slab_buffer_get_base_buffer
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
;
308 slab
= CALLOC_STRUCT(pb_slab
);
310 return PIPE_ERROR_OUT_OF_MEMORY
;
312 slab
->bo
= mgr
->provider
->create_buffer(mgr
->provider
, mgr
->slabSize
, &mgr
->desc
);
314 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
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
,
322 PB_USAGE_CPU_WRITE
, NULL
);
324 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
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
;
337 LIST_INITHEAD(&slab
->head
);
338 LIST_INITHEAD(&slab
->freeBuffers
);
339 slab
->numBuffers
= numBuffers
;
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
;
351 buf
->start
= i
* mgr
->bufSize
;
353 pipe_condvar_init(buf
->event
);
354 LIST_ADDTAIL(&buf
->head
, &slab
->freeBuffers
);
359 /* Add this slab to the list of partial slabs */
360 LIST_ADDTAIL(&slab
->head
, &mgr
->slabs
);
365 pb_reference(&slab
->bo
, NULL
);
372 static struct pb_buffer
*
373 pb_slab_manager_create_buffer(struct pb_manager
*_mgr
,
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
;
383 assert(size
<= mgr
->bufSize
);
384 if(size
> mgr
->bufSize
)
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
))
391 assert(pb_check_alignment(desc
->alignment
, mgr
->bufSize
));
392 if(!pb_check_alignment(desc
->alignment
, mgr
->bufSize
))
395 assert(pb_check_usage(desc
->usage
, mgr
->desc
.usage
));
396 if(!pb_check_usage(desc
->usage
, mgr
->desc
.usage
))
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
);
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)
418 list
= slab
->freeBuffers
.next
;
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
;
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
);
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 */
454 pb_slab_manager_create(struct pb_manager
*provider
,
457 const struct pb_desc
*desc
)
459 struct pb_slab_manager
*mgr
;
461 mgr
= CALLOC_STRUCT(pb_slab_manager
);
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
;
474 LIST_INITHEAD(&mgr
->slabs
);
476 pipe_mutex_init(mgr
->mutex
);
482 static struct pb_buffer
*
483 pb_slab_range_manager_create_buffer(struct pb_manager
*_mgr
,
485 const struct pb_desc
*desc
)
487 struct pb_slab_range_manager
*mgr
= pb_slab_range_manager(_mgr
);
489 pb_size reqSize
= size
;
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
);
502 /* Fall back to allocate a buffer object directly from the provider. */
503 return mgr
->provider
->create_buffer(mgr
->provider
, size
, desc
);
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
);
521 pb_slab_range_manager_destroy(struct pb_manager
*_mgr
)
523 struct pb_slab_range_manager
*mgr
= pb_slab_range_manager(_mgr
);
526 for (i
= 0; i
< mgr
->numBuckets
; ++i
)
527 mgr
->buckets
[i
]->destroy(mgr
->buckets
[i
]);
529 FREE(mgr
->bucketSizes
);
535 pb_slab_range_manager_create(struct pb_manager
*provider
,
539 const struct pb_desc
*desc
)
541 struct pb_slab_range_manager
*mgr
;
548 mgr
= CALLOC_STRUCT(pb_slab_range_manager
);
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
;
561 bufSize
= minBufSize
;
562 while(bufSize
< maxBufSize
) {
567 mgr
->buckets
= CALLOC(mgr
->numBuckets
, sizeof(*mgr
->buckets
));
571 bufSize
= minBufSize
;
572 for (i
= 0; i
< mgr
->numBuckets
; ++i
) {
573 mgr
->buckets
[i
] = pb_slab_manager_create(provider
, bufSize
, slabSize
, desc
);
582 for (i
= 0; i
< mgr
->numBuckets
; ++i
)
584 mgr
->buckets
[i
]->destroy(mgr
->buckets
[i
]);