pb_buffer_malloc.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  *
00026  **************************************************************************/
00027 
00037 #include "pipe/p_debug.h"
00038 #include "util/u_memory.h"
00039 #include "pb_buffer.h"
00040 #include "pb_bufmgr.h"
00041 
00042 
00043 struct malloc_buffer 
00044 {
00045    struct pb_buffer base;
00046    void *data;
00047 };
00048 
00049 
00050 extern const struct pb_vtbl malloc_buffer_vtbl;
00051 
00052 static INLINE struct malloc_buffer *
00053 malloc_buffer(struct pb_buffer *buf)
00054 {
00055    assert(buf);
00056    assert(buf->vtbl == &malloc_buffer_vtbl);
00057    return (struct malloc_buffer *)buf;
00058 }
00059 
00060 
00061 static void
00062 malloc_buffer_destroy(struct pb_buffer *buf)
00063 {
00064    align_free(malloc_buffer(buf)->data);
00065    FREE(buf);
00066 }
00067 
00068 
00069 static void *
00070 malloc_buffer_map(struct pb_buffer *buf, 
00071                   unsigned flags)
00072 {
00073    return malloc_buffer(buf)->data;
00074 }
00075 
00076 
00077 static void
00078 malloc_buffer_unmap(struct pb_buffer *buf)
00079 {
00080    /* No-op */
00081 }
00082 
00083 
00084 static void
00085 malloc_buffer_get_base_buffer(struct pb_buffer *buf,
00086                               struct pb_buffer **base_buf,
00087                               unsigned *offset)
00088 {
00089    *base_buf = buf;
00090    *offset = 0;
00091 }
00092 
00093 
00094 const struct pb_vtbl 
00095 malloc_buffer_vtbl = {
00096       malloc_buffer_destroy,
00097       malloc_buffer_map,
00098       malloc_buffer_unmap,
00099       malloc_buffer_get_base_buffer
00100 };
00101 
00102 
00103 struct pb_buffer *
00104 pb_malloc_buffer_create(size_t size,
00105                         const struct pb_desc *desc) 
00106 {
00107    struct malloc_buffer *buf;
00108    
00109    /* TODO: do a single allocation */
00110    
00111    buf = CALLOC_STRUCT(malloc_buffer);
00112    if(!buf)
00113       return NULL;
00114 
00115    buf->base.base.refcount = 1;
00116    buf->base.base.alignment = desc->alignment;
00117    buf->base.base.usage = desc->usage;
00118    buf->base.base.size = size;
00119    buf->base.vtbl = &malloc_buffer_vtbl;
00120 
00121    buf->data = align_malloc(size, desc->alignment < sizeof(void*) ? sizeof(void*) : desc->alignment);
00122    if(!buf->data) {
00123       FREE(buf);
00124       return NULL;
00125    }
00126 
00127    return &buf->base;
00128 }
00129 
00130 
00131 static struct pb_buffer *
00132 pb_malloc_bufmgr_create_buffer(struct pb_manager *mgr, 
00133                                size_t size,
00134                                const struct pb_desc *desc) 
00135 {
00136    return pb_malloc_buffer_create(size, desc);
00137 }
00138 
00139 
00140 static void
00141 pb_malloc_bufmgr_flush(struct pb_manager *mgr) 
00142 {
00143    /* No-op */
00144 }
00145 
00146 
00147 static void
00148 pb_malloc_bufmgr_destroy(struct pb_manager *mgr) 
00149 {
00150    /* No-op */
00151 }
00152 
00153 
00154 static struct pb_manager 
00155 pb_malloc_bufmgr = {
00156    pb_malloc_bufmgr_destroy,
00157    pb_malloc_bufmgr_create_buffer,
00158    pb_malloc_bufmgr_flush
00159 };
00160 
00161 
00162 struct pb_manager *
00163 pb_malloc_bufmgr_create(void) 
00164 {
00165   return &pb_malloc_bufmgr;
00166 }

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