• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * Copyright 2012 Marek Olšák <maraeo@gmail.com>
5  * All Rights Reserved.
6  *
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:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
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.
26  *
27  **************************************************************************/
28 
29 /* A simple allocator that suballocates memory from a large buffer. */
30 
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"
36 
37 #include "u_suballoc.h"
38 
39 
40 struct u_suballocator {
41    struct pipe_context *pipe;
42 
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. */
47 
48    struct pipe_resource *buffer;   /* The buffer we suballocate from. */
49    unsigned offset; /* Aligned offset pointing at the first unused byte. */
50 };
51 
52 
53 /**
54  * Create a suballocator.
55  *
56  * \p zero_buffer_memory determines whether the buffer contents should be
57  * cleared to 0 after the allocation.
58  */
59 struct u_suballocator *
u_suballocator_create(struct pipe_context * pipe,unsigned size,unsigned bind,enum pipe_resource_usage usage,boolean zero_buffer_memory)60 u_suballocator_create(struct pipe_context *pipe, unsigned size, unsigned bind,
61                       enum pipe_resource_usage usage,
62 		      boolean zero_buffer_memory)
63 {
64    struct u_suballocator *allocator = CALLOC_STRUCT(u_suballocator);
65    if (!allocator)
66       return NULL;
67 
68    allocator->pipe = pipe;
69    allocator->size = size;
70    allocator->bind = bind;
71    allocator->usage = usage;
72    allocator->zero_buffer_memory = zero_buffer_memory;
73    return allocator;
74 }
75 
76 void
u_suballocator_destroy(struct u_suballocator * allocator)77 u_suballocator_destroy(struct u_suballocator *allocator)
78 {
79    pipe_resource_reference(&allocator->buffer, NULL);
80    FREE(allocator);
81 }
82 
83 void
u_suballocator_alloc(struct u_suballocator * allocator,unsigned size,unsigned alignment,unsigned * out_offset,struct pipe_resource ** outbuf)84 u_suballocator_alloc(struct u_suballocator *allocator, unsigned size,
85                      unsigned alignment, unsigned *out_offset,
86                      struct pipe_resource **outbuf)
87 {
88    allocator->offset = align(allocator->offset, alignment);
89 
90    /* Don't allow allocations larger than the buffer size. */
91    if (size > allocator->size)
92       goto fail;
93 
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;
100       allocator->buffer =
101          pipe_buffer_create(allocator->pipe->screen, allocator->bind,
102                             allocator->usage, allocator->size);
103       if (!allocator->buffer)
104          goto fail;
105 
106       /* Clear the memory if needed. */
107       if (allocator->zero_buffer_memory) {
108          struct pipe_transfer *transfer = NULL;
109          void *ptr;
110 
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);
115       }
116    }
117 
118    assert(allocator->offset % alignment == 0);
119    assert(allocator->offset < allocator->buffer->width0);
120    assert(allocator->offset + size <= allocator->buffer->width0);
121 
122    /* Return the buffer. */
123    *out_offset = allocator->offset;
124    pipe_resource_reference(outbuf, allocator->buffer);
125 
126    allocator->offset += size;
127    return;
128 
129 fail:
130    pipe_resource_reference(outbuf, NULL);
131 }
132