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 boolean zero_buffer_memory
; /* If the buffer contents should be zeroed. */
48 struct pipe_resource
*buffer
; /* The buffer we suballocate from. */
49 unsigned offset
; /* Aligned offset pointing at the first unused byte. */
54 * Create a suballocator.
56 * \p zero_buffer_memory determines whether the buffer contents should be
57 * cleared to 0 after the allocation.
59 struct u_suballocator
*
60 u_suballocator_create(struct pipe_context
*pipe
, unsigned size
, unsigned bind
,
61 enum pipe_resource_usage usage
,
62 boolean zero_buffer_memory
)
64 struct u_suballocator
*allocator
= CALLOC_STRUCT(u_suballocator
);
68 allocator
->pipe
= pipe
;
69 allocator
->size
= size
;
70 allocator
->bind
= bind
;
71 allocator
->usage
= usage
;
72 allocator
->zero_buffer_memory
= zero_buffer_memory
;
77 u_suballocator_destroy(struct u_suballocator
*allocator
)
79 pipe_resource_reference(&allocator
->buffer
, NULL
);
84 u_suballocator_alloc(struct u_suballocator
*allocator
, unsigned size
,
85 unsigned alignment
, unsigned *out_offset
,
86 struct pipe_resource
**outbuf
)
88 allocator
->offset
= align(allocator
->offset
, alignment
);
90 /* Don't allow allocations larger than the buffer size. */
91 if (size
> allocator
->size
)
94 /* Make sure we have enough space in the buffer. */
95 if (!allocator
->buffer
||
96 allocator
->offset
+ size
> allocator
->size
) {
97 /* Allocate a new buffer. */
98 pipe_resource_reference(&allocator
->buffer
, NULL
);
99 allocator
->offset
= 0;
101 pipe_buffer_create(allocator
->pipe
->screen
, allocator
->bind
,
102 allocator
->usage
, allocator
->size
);
103 if (!allocator
->buffer
)
106 /* Clear the memory if needed. */
107 if (allocator
->zero_buffer_memory
) {
108 struct pipe_transfer
*transfer
= NULL
;
111 ptr
= pipe_buffer_map(allocator
->pipe
, allocator
->buffer
,
112 PIPE_TRANSFER_WRITE
, &transfer
);
113 memset(ptr
, 0, allocator
->size
);
114 pipe_buffer_unmap(allocator
->pipe
, transfer
);
118 assert(allocator
->offset
% alignment
== 0);
119 assert(allocator
->offset
< allocator
->buffer
->width0
);
120 assert(allocator
->offset
+ size
<= allocator
->buffer
->width0
);
122 /* Return the buffer. */
123 *out_offset
= allocator
->offset
;
124 pipe_resource_reference(outbuf
, allocator
->buffer
);
126 allocator
->offset
+= size
;
130 pipe_resource_reference(outbuf
, NULL
);