1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2012 Marek Olšák <maraeo@gmail.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 /* A simple allocator that suballocates memory from a large buffer. */
31 #include "pipe/p_defines.h"
32 #include "util/u_inlines.h"
33 #include "pipe/p_context.h"
34 #include "util/u_memory.h"
35 #include "util/u_math.h"
37 #include "u_suballoc.h"
40 struct u_suballocator
{
41 struct pipe_context
*pipe
;
43 unsigned size
; /* Size of the whole buffer, in bytes. */
44 unsigned bind
; /* Bitmask of PIPE_BIND_* flags. */
45 enum pipe_resource_usage usage
;
46 unsigned flags
; /* bitmask of PIPE_RESOURCE_FLAG_x */
47 boolean zero_buffer_memory
; /* If the buffer contents should be zeroed. */
49 struct pipe_resource
*buffer
; /* The buffer we suballocate from. */
50 unsigned offset
; /* Aligned offset pointing at the first unused byte. */
55 * Create a suballocator.
57 * \param flags bitmask of PIPE_RESOURCE_FLAG_x
58 * \param zero_buffer_memory determines whether the buffer contents should be
59 * cleared to 0 after the allocation.
62 struct u_suballocator
*
63 u_suballocator_create(struct pipe_context
*pipe
, unsigned size
, unsigned bind
,
64 enum pipe_resource_usage usage
, unsigned flags
,
65 boolean zero_buffer_memory
)
67 struct u_suballocator
*allocator
= CALLOC_STRUCT(u_suballocator
);
71 allocator
->pipe
= pipe
;
72 allocator
->size
= size
;
73 allocator
->bind
= bind
;
74 allocator
->usage
= usage
;
75 allocator
->flags
= flags
;
76 allocator
->zero_buffer_memory
= zero_buffer_memory
;
81 u_suballocator_destroy(struct u_suballocator
*allocator
)
83 pipe_resource_reference(&allocator
->buffer
, NULL
);
88 u_suballocator_alloc(struct u_suballocator
*allocator
, unsigned size
,
89 unsigned alignment
, unsigned *out_offset
,
90 struct pipe_resource
**outbuf
)
92 allocator
->offset
= align(allocator
->offset
, alignment
);
94 /* Don't allow allocations larger than the buffer size. */
95 if (size
> allocator
->size
)
98 /* Make sure we have enough space in the buffer. */
99 if (!allocator
->buffer
||
100 allocator
->offset
+ size
> allocator
->size
) {
101 /* Allocate a new buffer. */
102 pipe_resource_reference(&allocator
->buffer
, NULL
);
103 allocator
->offset
= 0;
105 struct pipe_resource templ
;
106 memset(&templ
, 0, sizeof(templ
));
107 templ
.target
= PIPE_BUFFER
;
108 templ
.format
= PIPE_FORMAT_R8_UNORM
;
109 templ
.bind
= allocator
->bind
;
110 templ
.usage
= allocator
->usage
;
111 templ
.flags
= allocator
->flags
;
112 templ
.width0
= allocator
->size
;
115 templ
.array_size
= 1;
117 struct pipe_screen
*screen
= allocator
->pipe
->screen
;
118 allocator
->buffer
= screen
->resource_create(screen
, &templ
);
119 if (!allocator
->buffer
)
122 /* Clear the memory if needed. */
123 if (allocator
->zero_buffer_memory
) {
124 struct pipe_context
*pipe
= allocator
->pipe
;
126 if (pipe
->clear_buffer
) {
127 unsigned clear_value
= 0;
129 pipe
->clear_buffer(pipe
, allocator
->buffer
, 0, allocator
->size
,
132 struct pipe_transfer
*transfer
= NULL
;
133 void *ptr
= pipe_buffer_map(pipe
, allocator
->buffer
,
134 PIPE_TRANSFER_WRITE
, &transfer
);
135 memset(ptr
, 0, allocator
->size
);
136 pipe_buffer_unmap(pipe
, transfer
);
141 assert(allocator
->offset
% alignment
== 0);
142 assert(allocator
->offset
< allocator
->buffer
->width0
);
143 assert(allocator
->offset
+ size
<= allocator
->buffer
->width0
);
145 /* Return the buffer. */
146 *out_offset
= allocator
->offset
;
147 pipe_resource_reference(outbuf
, allocator
->buffer
);
149 allocator
->offset
+= size
;
153 pipe_resource_reference(outbuf
, NULL
);