Home | History | Annotate | Download | only in util
      1 /**************************************************************************
      2  *
      3  * Copyright 2009 VMware, Inc.
      4  * Copyright 2012 Marek Olk <maraeo (at) 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    unsigned flags;         /* bitmask of PIPE_RESOURCE_FLAG_x */
     47    boolean zero_buffer_memory; /* If the buffer contents should be zeroed. */
     48 
     49    struct pipe_resource *buffer;   /* The buffer we suballocate from. */
     50    unsigned offset; /* Aligned offset pointing at the first unused byte. */
     51 };
     52 
     53 
     54 /**
     55  * Create a suballocator.
     56  *
     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.
     60  *
     61  */
     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)
     66 {
     67    struct u_suballocator *allocator = CALLOC_STRUCT(u_suballocator);
     68    if (!allocator)
     69       return NULL;
     70 
     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;
     77    return allocator;
     78 }
     79 
     80 void
     81 u_suballocator_destroy(struct u_suballocator *allocator)
     82 {
     83    pipe_resource_reference(&allocator->buffer, NULL);
     84    FREE(allocator);
     85 }
     86 
     87 void
     88 u_suballocator_alloc(struct u_suballocator *allocator, unsigned size,
     89                      unsigned alignment, unsigned *out_offset,
     90                      struct pipe_resource **outbuf)
     91 {
     92    allocator->offset = align(allocator->offset, alignment);
     93 
     94    /* Don't allow allocations larger than the buffer size. */
     95    if (size > allocator->size)
     96       goto fail;
     97 
     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;
    104 
    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;
    113       templ.height0 = 1;
    114       templ.depth0 = 1;
    115       templ.array_size = 1;
    116 
    117       struct pipe_screen *screen = allocator->pipe->screen;
    118       allocator->buffer = screen->resource_create(screen, &templ);
    119       if (!allocator->buffer)
    120          goto fail;
    121 
    122       /* Clear the memory if needed. */
    123       if (allocator->zero_buffer_memory) {
    124          struct pipe_context *pipe = allocator->pipe;
    125 
    126          if (pipe->clear_buffer) {
    127             unsigned clear_value = 0;
    128 
    129             pipe->clear_buffer(pipe, allocator->buffer, 0, allocator->size,
    130                                &clear_value, 4);
    131          } else {
    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);
    137          }
    138       }
    139    }
    140 
    141    assert(allocator->offset % alignment == 0);
    142    assert(allocator->offset < allocator->buffer->width0);
    143    assert(allocator->offset + size <= allocator->buffer->width0);
    144 
    145    /* Return the buffer. */
    146    *out_offset = allocator->offset;
    147    pipe_resource_reference(outbuf, allocator->buffer);
    148 
    149    allocator->offset += size;
    150    return;
    151 
    152 fail:
    153    pipe_resource_reference(outbuf, NULL);
    154 }
    155