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 alignment
; /* Alignment of each sub-allocation. */
45 unsigned bind
; /* Bitmask of PIPE_BIND_* flags. */
46 unsigned usage
; /* One of PIPE_USAGE_* flags. */
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 * \p zero_buffer_memory determines whether the buffer contents should be
58 * cleared to 0 after the allocation.
60 struct u_suballocator
*
61 u_suballocator_create(struct pipe_context
*pipe
, unsigned size
,
62 unsigned alignment
, unsigned bind
, unsigned usage
,
63 boolean zero_buffer_memory
)
65 struct u_suballocator
*allocator
= CALLOC_STRUCT(u_suballocator
);
69 allocator
->pipe
= pipe
;
70 allocator
->size
= align(size
, alignment
);
71 allocator
->alignment
= alignment
;
72 allocator
->bind
= bind
;
73 allocator
->usage
= usage
;
74 allocator
->zero_buffer_memory
= zero_buffer_memory
;
79 u_suballocator_destroy(struct u_suballocator
*allocator
)
81 pipe_resource_reference(&allocator
->buffer
, NULL
);
86 u_suballocator_alloc(struct u_suballocator
*allocator
, unsigned size
,
87 unsigned *out_offset
, struct pipe_resource
**outbuf
)
89 unsigned alloc_size
= align(size
, allocator
->alignment
);
91 /* Don't allow allocations larger than the buffer size. */
92 if (alloc_size
> allocator
->size
)
95 /* Make sure we have enough space in the buffer. */
96 if (!allocator
->buffer
||
97 allocator
->offset
+ alloc_size
> allocator
->size
) {
98 /* Allocate a new buffer. */
99 pipe_resource_reference(&allocator
->buffer
, NULL
);
100 allocator
->offset
= 0;
102 pipe_buffer_create(allocator
->pipe
->screen
, allocator
->bind
,
103 allocator
->usage
, allocator
->size
);
104 if (!allocator
->buffer
)
107 /* Clear the memory if needed. */
108 if (allocator
->zero_buffer_memory
) {
109 struct pipe_transfer
*transfer
= NULL
;
112 ptr
= pipe_buffer_map(allocator
->pipe
, allocator
->buffer
,
113 PIPE_TRANSFER_WRITE
, &transfer
);
114 memset(ptr
, 0, allocator
->size
);
115 pipe_buffer_unmap(allocator
->pipe
, transfer
);
119 assert(allocator
->offset
% allocator
->alignment
== 0);
120 assert(allocator
->offset
< allocator
->buffer
->width0
);
121 assert(allocator
->offset
+ alloc_size
<= allocator
->buffer
->width0
);
123 /* Return the buffer. */
124 *out_offset
= allocator
->offset
;
125 pipe_resource_reference(outbuf
, allocator
->buffer
);
127 allocator
->offset
+= alloc_size
;
131 pipe_resource_reference(outbuf
, NULL
);