Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_buffer_malloc.c
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 *
53 malloc_buffer(struct pb_buffer *buf)
54 {
55 assert(buf);
56 assert(buf->vtbl == &malloc_buffer_vtbl);
57 return (struct malloc_buffer *)buf;
58 }
59
60
61 static void
62 malloc_buffer_destroy(struct pb_buffer *buf)
63 {
64 align_free(malloc_buffer(buf)->data);
65 FREE(buf);
66 }
67
68
69 static void *
70 malloc_buffer_map(struct pb_buffer *buf,
71 unsigned flags)
72 {
73 return malloc_buffer(buf)->data;
74 }
75
76
77 static void
78 malloc_buffer_unmap(struct pb_buffer *buf)
79 {
80 /* No-op */
81 }
82
83
84 static enum pipe_error
85 malloc_buffer_validate(struct pb_buffer *buf,
86 struct pb_validate *vl,
87 unsigned flags)
88 {
89 assert(0);
90 return PIPE_ERROR;
91 }
92
93
94 static void
95 malloc_buffer_fence(struct pb_buffer *buf,
96 struct pipe_fence_handle *fence)
97 {
98 assert(0);
99 }
100
101
102 static void
103 malloc_buffer_get_base_buffer(struct pb_buffer *buf,
104 struct pb_buffer **base_buf,
105 unsigned *offset)
106 {
107 *base_buf = buf;
108 *offset = 0;
109 }
110
111
112 const struct pb_vtbl
113 malloc_buffer_vtbl = {
114 malloc_buffer_destroy,
115 malloc_buffer_map,
116 malloc_buffer_unmap,
117 malloc_buffer_validate,
118 malloc_buffer_fence,
119 malloc_buffer_get_base_buffer
120 };
121
122
123 struct pb_buffer *
124 pb_malloc_buffer_create(size_t size,
125 const struct pb_desc *desc)
126 {
127 struct malloc_buffer *buf;
128
129 /* TODO: do a single allocation */
130
131 buf = CALLOC_STRUCT(malloc_buffer);
132 if(!buf)
133 return NULL;
134
135 pipe_reference_init(&buf->base.base.reference, 1);
136 buf->base.base.alignment = desc->alignment;
137 buf->base.base.usage = desc->usage;
138 buf->base.base.size = size;
139 buf->base.vtbl = &malloc_buffer_vtbl;
140
141 buf->data = align_malloc(size, desc->alignment < sizeof(void*) ? sizeof(void*) : desc->alignment);
142 if(!buf->data) {
143 FREE(buf);
144 return NULL;
145 }
146
147 return &buf->base;
148 }
149
150
151 static struct pb_buffer *
152 pb_malloc_bufmgr_create_buffer(struct pb_manager *mgr,
153 size_t size,
154 const struct pb_desc *desc)
155 {
156 return pb_malloc_buffer_create(size, desc);
157 }
158
159
160 static void
161 pb_malloc_bufmgr_flush(struct pb_manager *mgr)
162 {
163 /* No-op */
164 }
165
166
167 static void
168 pb_malloc_bufmgr_destroy(struct pb_manager *mgr)
169 {
170 /* No-op */
171 }
172
173
174 static struct pb_manager
175 pb_malloc_bufmgr = {
176 pb_malloc_bufmgr_destroy,
177 pb_malloc_bufmgr_create_buffer,
178 pb_malloc_bufmgr_flush
179 };
180
181
182 struct pb_manager *
183 pb_malloc_bufmgr_create(void)
184 {
185 return &pb_malloc_bufmgr;
186 }