1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Implementation of malloc-based buffers to store data that can't be processed
33 * \author Jose Fonseca <jrfonseca@tungstengraphics.com>
37 #include "pipe/p_debug.h"
38 #include "util/u_memory.h"
39 #include "pb_buffer.h"
40 #include "pb_bufmgr.h"
45 struct pb_buffer base
;
50 extern const struct pb_vtbl malloc_buffer_vtbl
;
52 static INLINE
struct malloc_buffer
*
53 malloc_buffer(struct pb_buffer
*buf
)
56 assert(buf
->vtbl
== &malloc_buffer_vtbl
);
57 return (struct malloc_buffer
*)buf
;
62 malloc_buffer_destroy(struct pb_buffer
*buf
)
64 align_free(malloc_buffer(buf
)->data
);
70 malloc_buffer_map(struct pb_buffer
*buf
,
73 return malloc_buffer(buf
)->data
;
78 malloc_buffer_unmap(struct pb_buffer
*buf
)
85 malloc_buffer_get_base_buffer(struct pb_buffer
*buf
,
86 struct pb_buffer
**base_buf
,
95 malloc_buffer_vtbl
= {
96 malloc_buffer_destroy
,
99 malloc_buffer_get_base_buffer
104 pb_malloc_buffer_create(size_t size
,
105 const struct pb_desc
*desc
)
107 struct malloc_buffer
*buf
;
109 /* TODO: do a single allocation */
111 buf
= CALLOC_STRUCT(malloc_buffer
);
115 buf
->base
.base
.refcount
= 1;
116 buf
->base
.base
.alignment
= desc
->alignment
;
117 buf
->base
.base
.usage
= desc
->usage
;
118 buf
->base
.base
.size
= size
;
119 buf
->base
.vtbl
= &malloc_buffer_vtbl
;
121 buf
->data
= align_malloc(size
, desc
->alignment
< sizeof(void*) ? sizeof(void*) : desc
->alignment
);
131 static struct pb_buffer
*
132 pb_malloc_bufmgr_create_buffer(struct pb_manager
*mgr
,
134 const struct pb_desc
*desc
)
136 return pb_malloc_buffer_create(size
, desc
);
141 pb_malloc_bufmgr_flush(struct pb_manager
*mgr
)
148 pb_malloc_bufmgr_destroy(struct pb_manager
*mgr
)
154 static struct pb_manager
156 pb_malloc_bufmgr_destroy
,
157 pb_malloc_bufmgr_create_buffer
,
158 pb_malloc_bufmgr_flush
163 pb_malloc_bufmgr_create(void)
165 return &pb_malloc_bufmgr
;