1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * \file
30 * Implementation of malloc-based buffers to store data that can't be processed
31 * by the hardware.
32 *
33 * \author Jose Fonseca <jrfonseca@tungstengraphics.com>
34 */
35
36
37 #include "util/u_debug.h"
38 #include "util/u_memory.h"
39 #include "pb_buffer.h"
40 #include "pb_bufmgr.h"
41
42
43 struct malloc_buffer
44 {
45 struct pb_buffer base;
46 void *data;
47 };
48
49
50 extern const struct pb_vtbl malloc_buffer_vtbl;
51
52 static INLINE struct malloc_buffer *
malloc_buffer(struct pb_buffer * buf)53 malloc_buffer(struct pb_buffer *buf)
54 {
55 assert(buf);
56 if (!buf)
57 return NULL;
58 assert(buf->vtbl == &malloc_buffer_vtbl);
59 return (struct malloc_buffer *)buf;
60 }
61
62
63 static void
malloc_buffer_destroy(struct pb_buffer * buf)64 malloc_buffer_destroy(struct pb_buffer *buf)
65 {
66 align_free(malloc_buffer(buf)->data);
67 FREE(buf);
68 }
69
70
71 static void *
malloc_buffer_map(struct pb_buffer * buf,unsigned flags,void * flush_ctx)72 malloc_buffer_map(struct pb_buffer *buf,
73 unsigned flags,
74 void *flush_ctx)
75 {
76 return malloc_buffer(buf)->data;
77 }
78
79
80 static void
malloc_buffer_unmap(struct pb_buffer * buf)81 malloc_buffer_unmap(struct pb_buffer *buf)
82 {
83 /* No-op */
84 }
85
86
87 static enum pipe_error
malloc_buffer_validate(struct pb_buffer * buf,struct pb_validate * vl,unsigned flags)88 malloc_buffer_validate(struct pb_buffer *buf,
89 struct pb_validate *vl,
90 unsigned flags)
91 {
92 assert(0);
93 return PIPE_ERROR;
94 }
95
96
97 static void
malloc_buffer_fence(struct pb_buffer * buf,struct pipe_fence_handle * fence)98 malloc_buffer_fence(struct pb_buffer *buf,
99 struct pipe_fence_handle *fence)
100 {
101 assert(0);
102 }
103
104
105 static void
malloc_buffer_get_base_buffer(struct pb_buffer * buf,struct pb_buffer ** base_buf,pb_size * offset)106 malloc_buffer_get_base_buffer(struct pb_buffer *buf,
107 struct pb_buffer **base_buf,
108 pb_size *offset)
109 {
110 *base_buf = buf;
111 *offset = 0;
112 }
113
114
115 const struct pb_vtbl
116 malloc_buffer_vtbl = {
117 malloc_buffer_destroy,
118 malloc_buffer_map,
119 malloc_buffer_unmap,
120 malloc_buffer_validate,
121 malloc_buffer_fence,
122 malloc_buffer_get_base_buffer
123 };
124
125
126 struct pb_buffer *
pb_malloc_buffer_create(pb_size size,const struct pb_desc * desc)127 pb_malloc_buffer_create(pb_size size,
128 const struct pb_desc *desc)
129 {
130 struct malloc_buffer *buf;
131
132 /* TODO: do a single allocation */
133
134 buf = CALLOC_STRUCT(malloc_buffer);
135 if(!buf)
136 return NULL;
137
138 pipe_reference_init(&buf->base.reference, 1);
139 buf->base.usage = desc->usage;
140 buf->base.size = size;
141 buf->base.alignment = desc->alignment;
142 buf->base.vtbl = &malloc_buffer_vtbl;
143
144 buf->data = align_malloc(size, desc->alignment < sizeof(void*) ? sizeof(void*) : desc->alignment);
145 if(!buf->data) {
146 FREE(buf);
147 return NULL;
148 }
149
150 return &buf->base;
151 }
152
153
154 static struct pb_buffer *
pb_malloc_bufmgr_create_buffer(struct pb_manager * mgr,pb_size size,const struct pb_desc * desc)155 pb_malloc_bufmgr_create_buffer(struct pb_manager *mgr,
156 pb_size size,
157 const struct pb_desc *desc)
158 {
159 return pb_malloc_buffer_create(size, desc);
160 }
161
162
163 static void
pb_malloc_bufmgr_flush(struct pb_manager * mgr)164 pb_malloc_bufmgr_flush(struct pb_manager *mgr)
165 {
166 /* No-op */
167 }
168
169
170 static void
pb_malloc_bufmgr_destroy(struct pb_manager * mgr)171 pb_malloc_bufmgr_destroy(struct pb_manager *mgr)
172 {
173 /* No-op */
174 }
175
176
177 static boolean
pb_malloc_bufmgr_is_buffer_busy(struct pb_manager * mgr,struct pb_buffer * buf)178 pb_malloc_bufmgr_is_buffer_busy( struct pb_manager *mgr,
179 struct pb_buffer *buf )
180 {
181 return FALSE;
182 }
183
184
185 static struct pb_manager
186 pb_malloc_bufmgr = {
187 pb_malloc_bufmgr_destroy,
188 pb_malloc_bufmgr_create_buffer,
189 pb_malloc_bufmgr_flush,
190 pb_malloc_bufmgr_is_buffer_busy
191 };
192
193
194 struct pb_manager *
pb_malloc_bufmgr_create(void)195 pb_malloc_bufmgr_create(void)
196 {
197 return &pb_malloc_bufmgr;
198 }
199