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 * Create a suballocator.
41 *
42 * \param flags               bitmask of PIPE_RESOURCE_FLAG_x
43 * \param zero_buffer_memory  determines whether the buffer contents should be
44 *                            cleared to 0 after the allocation.
45 *
46 */
47void
48u_suballocator_init(struct u_suballocator *allocator,
49                    struct pipe_context *pipe,
50                    unsigned size, unsigned bind,
51                    enum pipe_resource_usage usage, unsigned flags,
52                    boolean zero_buffer_memory)
53{
54   memset(allocator, 0, sizeof(*allocator));
55
56   allocator->pipe = pipe;
57   allocator->size = size;
58   allocator->bind = bind;
59   allocator->usage = usage;
60   allocator->flags = flags;
61   allocator->zero_buffer_memory = zero_buffer_memory;
62}
63
64void
65u_suballocator_destroy(struct u_suballocator *allocator)
66{
67   pipe_resource_reference(&allocator->buffer, NULL);
68}
69
70void
71u_suballocator_alloc(struct u_suballocator *allocator, unsigned size,
72                     unsigned alignment, unsigned *out_offset,
73                     struct pipe_resource **outbuf)
74{
75   allocator->offset = align(allocator->offset, alignment);
76
77   /* Don't allow allocations larger than the buffer size. */
78   if (size > allocator->size)
79      goto fail;
80
81   /* Make sure we have enough space in the buffer. */
82   if (!allocator->buffer ||
83       allocator->offset + size > allocator->size) {
84      /* Allocate a new buffer. */
85      pipe_resource_reference(&allocator->buffer, NULL);
86      allocator->offset = 0;
87
88      struct pipe_resource templ;
89      memset(&templ, 0, sizeof(templ));
90      templ.target = PIPE_BUFFER;
91      templ.format = PIPE_FORMAT_R8_UNORM;
92      templ.bind = allocator->bind;
93      templ.usage = allocator->usage;
94      templ.flags = allocator->flags;
95      templ.width0 = allocator->size;
96      templ.height0 = 1;
97      templ.depth0 = 1;
98      templ.array_size = 1;
99
100      struct pipe_screen *screen = allocator->pipe->screen;
101      allocator->buffer = screen->resource_create(screen, &templ);
102      if (!allocator->buffer)
103         goto fail;
104
105      /* Clear the memory if needed. */
106      if (allocator->zero_buffer_memory) {
107         struct pipe_context *pipe = allocator->pipe;
108
109         if (pipe->clear_buffer) {
110            unsigned clear_value = 0;
111
112            pipe->clear_buffer(pipe, allocator->buffer, 0, allocator->size,
113                               &clear_value, 4);
114         } else {
115            struct pipe_transfer *transfer = NULL;
116            void *ptr = pipe_buffer_map(pipe, allocator->buffer,
117                                        PIPE_MAP_WRITE, &transfer);
118            memset(ptr, 0, allocator->size);
119            pipe_buffer_unmap(pipe, transfer);
120         }
121      }
122   }
123
124   assert(allocator->offset % alignment == 0);
125   assert(allocator->offset < allocator->buffer->width0);
126   assert(allocator->offset + size <= allocator->buffer->width0);
127
128   /* Return the buffer. */
129   *out_offset = allocator->offset;
130   pipe_resource_reference(outbuf, allocator->buffer);
131
132   allocator->offset += size;
133   return;
134
135fail:
136   pipe_resource_reference(outbuf, NULL);
137}
138