pb_bufmgr_pool.c File Reference

Batch buffer pool management. More...

Include dependency graph for pb_bufmgr_pool.c:

Go to the source code of this file.

Data Structures

struct  pool_pb_manager
struct  pool_buffer

Defines

#define SUPER(__derived)   (&(__derived)->base)
 Convenience macro (type safe).

Functions

static struct pool_pb_managerpool_pb_manager (struct pb_manager *mgr)
static struct pool_bufferpool_buffer (struct pb_buffer *buf)
static void pool_buffer_destroy (struct pb_buffer *buf)
static void * pool_buffer_map (struct pb_buffer *buf, unsigned flags)
static void pool_buffer_unmap (struct pb_buffer *buf)
static void pool_buffer_get_base_buffer (struct pb_buffer *buf, struct pb_buffer **base_buf, unsigned *offset)
static struct pb_bufferpool_bufmgr_create_buffer (struct pb_manager *mgr, size_t size, const struct pb_desc *desc)
static void pool_bufmgr_flush (struct pb_manager *mgr)
static void pool_bufmgr_destroy (struct pb_manager *mgr)
struct pb_managerpool_bufmgr_create (struct pb_manager *provider, size_t numBufs, size_t bufSize, const struct pb_desc *desc)
 Static buffer pool sub-allocator.

Variables

static struct pb_vtbl pool_buffer_vtbl


Detailed Description

Batch buffer pool management.

Author:
José Fonseca <jrfonseca-at-tungstengraphics-dot-com>

Thomas Hellström <thomas-at-tungstengraphics-dot-com>

Definition in file pb_bufmgr_pool.c.


Define Documentation

#define SUPER ( __derived   )     (&(__derived)->base)

Convenience macro (type safe).

Definition at line 52 of file pb_bufmgr_pool.c.


Function Documentation

static struct pool_buffer* pool_buffer ( struct pb_buffer buf  )  [static, read]

Definition at line 97 of file pb_bufmgr_pool.c.

References assert.

00098 {
00099    assert(buf);
00100    return (struct pool_buffer *)buf;
00101 }

static void pool_buffer_destroy ( struct pb_buffer buf  )  [static]

Definition at line 106 of file pb_bufmgr_pool.c.

References assert, pb_buffer::base, pool_buffer::base, pool_pb_manager::free, pool_buffer::head, LIST_ADD, pool_buffer::mgr, pool_pb_manager::mutex, pool_pb_manager::numFree, pipe_mutex_lock, pipe_mutex_unlock, pool_buffer(), and pipe_buffer::refcount.

00107 {
00108    struct pool_buffer *pool_buf = pool_buffer(buf);
00109    struct pool_pb_manager *pool = pool_buf->mgr;
00110    
00111    assert(pool_buf->base.base.refcount == 0);
00112 
00113    pipe_mutex_lock(pool->mutex);
00114    LIST_ADD(&pool_buf->head, &pool->free);
00115    pool->numFree++;
00116    pipe_mutex_unlock(pool->mutex);
00117 }

static void pool_buffer_get_base_buffer ( struct pb_buffer buf,
struct pb_buffer **  base_buf,
unsigned *  offset 
) [static]

Definition at line 142 of file pb_bufmgr_pool.c.

References pool_pb_manager::buffer, pool_buffer::mgr, pb_get_base_buffer(), pool_buffer(), and pool_buffer::start.

00145 {
00146    struct pool_buffer *pool_buf = pool_buffer(buf);
00147    struct pool_pb_manager *pool = pool_buf->mgr;
00148    pb_get_base_buffer(pool->buffer, base_buf, offset);
00149    *offset += pool_buf->start;
00150 }

static void* pool_buffer_map ( struct pb_buffer buf,
unsigned  flags 
) [static]

Definition at line 121 of file pb_bufmgr_pool.c.

References pool_pb_manager::map, pool_buffer::mgr, pool_pb_manager::mutex, pipe_mutex_lock, pipe_mutex_unlock, pool_buffer(), and pool_buffer::start.

00122 {
00123    struct pool_buffer *pool_buf = pool_buffer(buf);
00124    struct pool_pb_manager *pool = pool_buf->mgr;
00125    void *map;
00126 
00127    pipe_mutex_lock(pool->mutex);
00128    map = (unsigned char *) pool->map + pool_buf->start;
00129    pipe_mutex_unlock(pool->mutex);
00130    return map;
00131 }

static void pool_buffer_unmap ( struct pb_buffer buf  )  [static]

Definition at line 135 of file pb_bufmgr_pool.c.

00136 {
00137    /* No-op */
00138 }

struct pb_manager* pool_bufmgr_create ( struct pb_manager provider,
size_t  n,
size_t  size,
const struct pb_desc desc 
) [read]

Static buffer pool sub-allocator.

Manages the allocation of equally sized buffers. It does so by allocating a single big buffer and divide it equally sized buffers.

It is meant to manage the allocation of batch buffer pools.

Definition at line 230 of file pb_bufmgr_pool.c.

References pipe_buffer::alignment, pb_desc::alignment, pb_buffer::base, pool_buffer::base, pool_pb_manager::base, pool_pb_manager::bufAlign, pool_pb_manager::buffer, pool_pb_manager::bufs, pool_pb_manager::bufSize, CALLOC, CALLOC_STRUCT, pb_manager::create_buffer, pb_manager::destroy, pb_manager::flush, FREE, pool_pb_manager::free, pool_buffer::head, LIST_ADDTAIL, LIST_INITHEAD, pool_pb_manager::map, pool_buffer::mgr, pool_pb_manager::mutex, pool_pb_manager::numFree, pool_pb_manager::numTot, pb_map(), pb_reference(), pb_unmap(), PIPE_BUFFER_USAGE_CPU_READ, PIPE_BUFFER_USAGE_CPU_WRITE, pipe_mutex_init, pool_bufmgr_create_buffer(), pool_bufmgr_destroy(), pool_bufmgr_flush(), pipe_buffer::refcount, pipe_buffer::size, pool_buffer::start, SUPER, pipe_buffer::usage, and pb_buffer::vtbl.

00234 {
00235    struct pool_pb_manager *pool;
00236    struct pool_buffer *pool_buf;
00237    size_t i;
00238 
00239    if(!provider)
00240       return NULL;
00241    
00242    pool = CALLOC_STRUCT(pool_pb_manager);
00243    if (!pool)
00244       return NULL;
00245 
00246    pool->base.destroy = pool_bufmgr_destroy;
00247    pool->base.create_buffer = pool_bufmgr_create_buffer;
00248    pool->base.flush = pool_bufmgr_flush;
00249 
00250    LIST_INITHEAD(&pool->free);
00251 
00252    pool->numTot = numBufs;
00253    pool->numFree = numBufs;
00254    pool->bufSize = bufSize;
00255    pool->bufAlign = desc->alignment; 
00256    
00257    pipe_mutex_init(pool->mutex);
00258 
00259    pool->buffer = provider->create_buffer(provider, numBufs*bufSize, desc); 
00260    if (!pool->buffer)
00261       goto failure;
00262 
00263    pool->map = pb_map(pool->buffer,
00264                           PIPE_BUFFER_USAGE_CPU_READ |
00265                           PIPE_BUFFER_USAGE_CPU_WRITE);
00266    if(!pool->map)
00267       goto failure;
00268 
00269    pool->bufs = (struct pool_buffer *)CALLOC(numBufs, sizeof(*pool->bufs));
00270    if (!pool->bufs)
00271       goto failure;
00272 
00273    pool_buf = pool->bufs;
00274    for (i = 0; i < numBufs; ++i) {
00275       pool_buf->base.base.refcount = 0;
00276       pool_buf->base.base.alignment = 0;
00277       pool_buf->base.base.usage = 0;
00278       pool_buf->base.base.size = bufSize;
00279       pool_buf->base.vtbl = &pool_buffer_vtbl;
00280       pool_buf->mgr = pool;
00281       pool_buf->start = i * bufSize;
00282       LIST_ADDTAIL(&pool_buf->head, &pool->free);
00283       pool_buf++;
00284    }
00285 
00286    return SUPER(pool);
00287    
00288 failure:
00289    if(pool->bufs)
00290       FREE(pool->bufs);
00291    if(pool->map)
00292       pb_unmap(pool->buffer);
00293    if(pool->buffer)
00294       pb_reference(&pool->buffer, NULL);
00295    if(pool)
00296       FREE(pool);
00297    return NULL;
00298 }

static struct pb_buffer* pool_bufmgr_create_buffer ( struct pb_manager mgr,
size_t  size,
const struct pb_desc desc 
) [static, read]

Definition at line 163 of file pb_bufmgr_pool.c.

References pipe_buffer::alignment, pb_desc::alignment, assert, pb_buffer::base, pool_buffer::base, pool_pb_manager::bufAlign, pool_pb_manager::bufSize, debug_printf(), pool_pb_manager::free, LIST_DEL, LIST_ENTRY, pool_pb_manager::mutex, list_head::next, pool_pb_manager::numFree, pipe_mutex_lock, pipe_mutex_unlock, pool_pb_manager(), pipe_buffer::refcount, SUPER, pb_desc::usage, and pipe_buffer::usage.

00166 {
00167    struct pool_pb_manager *pool = pool_pb_manager(mgr);
00168    struct pool_buffer *pool_buf;
00169    struct list_head *item;
00170 
00171    assert(size == pool->bufSize);
00172    assert(pool->bufAlign % desc->alignment == 0);
00173    
00174    pipe_mutex_lock(pool->mutex);
00175 
00176    if (pool->numFree == 0) {
00177       pipe_mutex_unlock(pool->mutex);
00178       debug_printf("warning: out of fixed size buffer objects\n");
00179       return NULL;
00180    }
00181 
00182    item = pool->free.next;
00183 
00184    if (item == &pool->free) {
00185       pipe_mutex_unlock(pool->mutex);
00186       debug_printf("error: fixed size buffer pool corruption\n");
00187       return NULL;
00188    }
00189 
00190    LIST_DEL(item);
00191    --pool->numFree;
00192 
00193    pipe_mutex_unlock(pool->mutex);
00194    
00195    pool_buf = LIST_ENTRY(struct pool_buffer, item, head);
00196    assert(pool_buf->base.base.refcount == 0);
00197    pool_buf->base.base.refcount = 1;
00198    pool_buf->base.base.alignment = desc->alignment;
00199    pool_buf->base.base.usage = desc->usage;
00200    
00201    return SUPER(pool_buf);
00202 }

static void pool_bufmgr_destroy ( struct pb_manager mgr  )  [static]

Definition at line 213 of file pb_bufmgr_pool.c.

References pool_pb_manager::buffer, pool_pb_manager::bufs, FREE, pool_pb_manager::mutex, pb_reference(), pb_unmap(), pipe_mutex_lock, pipe_mutex_unlock, and pool_pb_manager().

00214 {
00215    struct pool_pb_manager *pool = pool_pb_manager(mgr);
00216    pipe_mutex_lock(pool->mutex);
00217 
00218    FREE(pool->bufs);
00219    
00220    pb_unmap(pool->buffer);
00221    pb_reference(&pool->buffer, NULL);
00222    
00223    pipe_mutex_unlock(pool->mutex);
00224    
00225    FREE(mgr);
00226 }

static void pool_bufmgr_flush ( struct pb_manager mgr  )  [static]

Definition at line 206 of file pb_bufmgr_pool.c.

00207 {
00208    /* No-op */
00209 }

static struct pool_pb_manager* pool_pb_manager ( struct pb_manager mgr  )  [static, read]

Definition at line 77 of file pb_bufmgr_pool.c.

References assert.

00078 {
00079    assert(mgr);
00080    return (struct pool_pb_manager *)mgr;
00081 }


Variable Documentation

struct pb_vtbl pool_buffer_vtbl [static]

Initial value:

Definition at line 154 of file pb_bufmgr_pool.c.


Generated on Tue Sep 29 06:25:24 2009 for Gallium3D by  doxygen 1.5.4