"Fossies" - the Fresh Open Source Software Archive

Member "mesa-20.1.8/src/gallium/auxiliary/pipebuffer/pb_bufmgr_slab.c" (16 Sep 2020, 14772 Bytes) of package /linux/misc/mesa-20.1.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pb_bufmgr_slab.c" see the Fossies "Dox" file reference documentation.

    1 /**************************************************************************
    2  *
    3  * Copyright 2006-2008 VMware, Inc., USA
    4  * All Rights Reserved.
    5  *
    6  * Permission is hereby granted, FREE of charge, to any person obtaining a
    7  * copy of this software and associated documentation files (the
    8  * "Software"), to deal in the Software without restriction, including
    9  * without limitation the rights to use, copy, modify, merge, publish,
   10  * distribute, sub license, and/or sell copies of the Software, and to
   11  * permit persons to whom the Software is furnished to do so, subject to
   12  * the following conditions:
   13  *
   14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
   17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
   18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
   19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
   20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
   21  *
   22  * The above copyright notice and this permission notice (including the
   23  * next paragraph) shall be included in all copies or substantial portions
   24  * of the Software.
   25  *
   26  *
   27  **************************************************************************/
   28 
   29 /**
   30  * @file
   31  * S-lab pool implementation.
   32  * 
   33  * @sa http://en.wikipedia.org/wiki/Slab_allocation
   34  * 
   35  * @author Thomas Hellstrom <thellstrom-at-vmware-dot-com>
   36  * @author Jose Fonseca <jfonseca@vmware.com>
   37  */
   38 
   39 #include "pipe/p_compiler.h"
   40 #include "util/u_debug.h"
   41 #include "os/os_thread.h"
   42 #include "pipe/p_defines.h"
   43 #include "util/u_memory.h"
   44 #include "util/list.h"
   45 
   46 #include "pb_buffer.h"
   47 #include "pb_bufmgr.h"
   48 
   49 
   50 struct pb_slab;
   51 
   52 
   53 /**
   54  * Buffer in a slab.
   55  * 
   56  * Sub-allocation of a contiguous buffer.
   57  */
   58 struct pb_slab_buffer
   59 {
   60    struct pb_buffer base;
   61    
   62    struct pb_slab *slab;
   63    
   64    struct list_head head;
   65    
   66    unsigned mapCount;
   67    
   68    /** Offset relative to the start of the slab buffer. */
   69    pb_size start;
   70    
   71    /** Use when validating, to signal that all mappings are finished */
   72    /* TODO: Actually validation does not reach this stage yet */
   73    cnd_t event;
   74 };
   75 
   76 
   77 /**
   78  * Slab -- a contiguous piece of memory. 
   79  */
   80 struct pb_slab
   81 {
   82    struct list_head head;
   83    struct list_head freeBuffers;
   84    pb_size numBuffers;
   85    pb_size numFree;
   86    
   87    struct pb_slab_buffer *buffers;
   88    struct pb_slab_manager *mgr;
   89    
   90    /** Buffer from the provider */
   91    struct pb_buffer *bo;
   92    
   93    void *virtual;   
   94 };
   95 
   96 
   97 /**
   98  * It adds/removes slabs as needed in order to meet the allocation/destruction 
   99  * of individual buffers.
  100  */
  101 struct pb_slab_manager 
  102 {
  103    struct pb_manager base;
  104    
  105    /** From where we get our buffers */
  106    struct pb_manager *provider;
  107    
  108    /** Size of the buffers we hand on downstream */
  109    pb_size bufSize;
  110    
  111    /** Size of the buffers we request upstream */
  112    pb_size slabSize;
  113    
  114    /** 
  115     * Alignment, usage to be used to allocate the slab buffers.
  116     * 
  117     * We can only provide buffers which are consistent (in alignment, usage) 
  118     * with this description.   
  119     */
  120    struct pb_desc desc;
  121 
  122    /** 
  123     * Partial slabs
  124     * 
  125     * Full slabs are not stored in any list. Empty slabs are destroyed 
  126     * immediatly.
  127     */
  128    struct list_head slabs;
  129    
  130    mtx_t mutex;
  131 };
  132 
  133 
  134 /**
  135  * Wrapper around several slabs, therefore capable of handling buffers of 
  136  * multiple sizes. 
  137  * 
  138  * This buffer manager just dispatches buffer allocations to the appropriate slab
  139  * manager, according to the requested buffer size, or by passes the slab 
  140  * managers altogether for even greater sizes.
  141  * 
  142  * The data of this structure remains constant after
  143  * initialization and thus needs no mutex protection.
  144  */
  145 struct pb_slab_range_manager 
  146 {
  147    struct pb_manager base;
  148 
  149    struct pb_manager *provider;
  150    
  151    pb_size minBufSize;
  152    pb_size maxBufSize;
  153    
  154    /** @sa pb_slab_manager::desc */ 
  155    struct pb_desc desc;
  156    
  157    unsigned numBuckets;
  158    pb_size *bucketSizes;
  159    
  160    /** Array of pb_slab_manager, one for each bucket size */
  161    struct pb_manager **buckets;
  162 };
  163 
  164 
  165 static inline struct pb_slab_buffer *
  166 pb_slab_buffer(struct pb_buffer *buf)
  167 {
  168    assert(buf);
  169    return (struct pb_slab_buffer *)buf;
  170 }
  171 
  172 
  173 static inline struct pb_slab_manager *
  174 pb_slab_manager(struct pb_manager *mgr)
  175 {
  176    assert(mgr);
  177    return (struct pb_slab_manager *)mgr;
  178 }
  179 
  180 
  181 static inline struct pb_slab_range_manager *
  182 pb_slab_range_manager(struct pb_manager *mgr)
  183 {
  184    assert(mgr);
  185    return (struct pb_slab_range_manager *)mgr;
  186 }
  187 
  188 
  189 /**
  190  * Delete a buffer from the slab delayed list and put
  191  * it on the slab FREE list.
  192  */
  193 static void
  194 pb_slab_buffer_destroy(struct pb_buffer *_buf)
  195 {
  196    struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
  197    struct pb_slab *slab = buf->slab;
  198    struct pb_slab_manager *mgr = slab->mgr;
  199    struct list_head *list = &buf->head;
  200 
  201    mtx_lock(&mgr->mutex);
  202    
  203    assert(!pipe_is_referenced(&buf->base.reference));
  204    
  205    buf->mapCount = 0;
  206 
  207    list_del(list);
  208    list_addtail(list, &slab->freeBuffers);
  209    slab->numFree++;
  210 
  211    if (slab->head.next == &slab->head)
  212       list_addtail(&slab->head, &mgr->slabs);
  213 
  214    /* If the slab becomes totally empty, free it */
  215    if (slab->numFree == slab->numBuffers) {
  216       list = &slab->head;
  217       list_delinit(list);
  218       pb_unmap(slab->bo);
  219       pb_reference(&slab->bo, NULL);
  220       FREE(slab->buffers);
  221       FREE(slab);
  222    }
  223 
  224    mtx_unlock(&mgr->mutex);
  225 }
  226 
  227 
  228 static void *
  229 pb_slab_buffer_map(struct pb_buffer *_buf, 
  230                    enum pb_usage_flags flags,
  231                    void *flush_ctx)
  232 {
  233    struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
  234 
  235    /* XXX: it will be necessary to remap here to propagate flush_ctx */
  236 
  237    ++buf->mapCount;
  238    return (void *) ((uint8_t *) buf->slab->virtual + buf->start);
  239 }
  240 
  241 
  242 static void
  243 pb_slab_buffer_unmap(struct pb_buffer *_buf)
  244 {
  245    struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
  246 
  247    --buf->mapCount;
  248    if (buf->mapCount == 0) 
  249        cnd_broadcast(&buf->event);
  250 }
  251 
  252 
  253 static enum pipe_error 
  254 pb_slab_buffer_validate(struct pb_buffer *_buf, 
  255                          struct pb_validate *vl,
  256                          enum pb_usage_flags flags)
  257 {
  258    struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
  259    return pb_validate(buf->slab->bo, vl, flags);
  260 }
  261 
  262 
  263 static void
  264 pb_slab_buffer_fence(struct pb_buffer *_buf, 
  265                       struct pipe_fence_handle *fence)
  266 {
  267    struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
  268    pb_fence(buf->slab->bo, fence);
  269 }
  270 
  271 
  272 static void
  273 pb_slab_buffer_get_base_buffer(struct pb_buffer *_buf,
  274                                struct pb_buffer **base_buf,
  275                                pb_size *offset)
  276 {
  277    struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
  278    pb_get_base_buffer(buf->slab->bo, base_buf, offset);
  279    *offset += buf->start;
  280 }
  281 
  282 
  283 static const struct pb_vtbl 
  284 pb_slab_buffer_vtbl = {
  285       pb_slab_buffer_destroy,
  286       pb_slab_buffer_map,
  287       pb_slab_buffer_unmap,
  288       pb_slab_buffer_validate,
  289       pb_slab_buffer_fence,
  290       pb_slab_buffer_get_base_buffer
  291 };
  292 
  293 
  294 /**
  295  * Create a new slab.
  296  * 
  297  * Called when we ran out of free slabs.
  298  */
  299 static enum pipe_error
  300 pb_slab_create(struct pb_slab_manager *mgr)
  301 {
  302    struct pb_slab *slab;
  303    struct pb_slab_buffer *buf;
  304    unsigned numBuffers;
  305    unsigned i;
  306    enum pipe_error ret;
  307 
  308    slab = CALLOC_STRUCT(pb_slab);
  309    if (!slab)
  310       return PIPE_ERROR_OUT_OF_MEMORY;
  311 
  312    slab->bo = mgr->provider->create_buffer(mgr->provider, mgr->slabSize, &mgr->desc);
  313    if(!slab->bo) {
  314       ret = PIPE_ERROR_OUT_OF_MEMORY;
  315       goto out_err0;
  316    }
  317 
  318    /* Note down the slab virtual address. All mappings are accessed directly 
  319     * through this address so it is required that the buffer is mapped
  320     * persistent */
  321    slab->virtual = pb_map(slab->bo, 
  322                           PB_USAGE_CPU_READ |
  323                           PB_USAGE_CPU_WRITE |
  324                           PB_USAGE_PERSISTENT, NULL);
  325    if(!slab->virtual) {
  326       ret = PIPE_ERROR_OUT_OF_MEMORY;
  327       goto out_err1;
  328    }
  329 
  330    numBuffers = slab->bo->size / mgr->bufSize;
  331 
  332    slab->buffers = CALLOC(numBuffers, sizeof(*slab->buffers));
  333    if (!slab->buffers) {
  334       ret = PIPE_ERROR_OUT_OF_MEMORY;
  335       goto out_err1;
  336    }
  337 
  338    list_inithead(&slab->head);
  339    list_inithead(&slab->freeBuffers);
  340    slab->numBuffers = numBuffers;
  341    slab->numFree = 0;
  342    slab->mgr = mgr;
  343 
  344    buf = slab->buffers;
  345    for (i=0; i < numBuffers; ++i) {
  346       pipe_reference_init(&buf->base.reference, 0);
  347       buf->base.size = mgr->bufSize;
  348       buf->base.alignment = 0;
  349       buf->base.usage = 0;
  350       buf->base.vtbl = &pb_slab_buffer_vtbl;
  351       buf->slab = slab;
  352       buf->start = i* mgr->bufSize;
  353       buf->mapCount = 0;
  354       cnd_init(&buf->event);
  355       list_addtail(&buf->head, &slab->freeBuffers);
  356       slab->numFree++;
  357       buf++;
  358    }
  359 
  360    /* Add this slab to the list of partial slabs */
  361    list_addtail(&slab->head, &mgr->slabs);
  362 
  363    return PIPE_OK;
  364 
  365 out_err1: 
  366    pb_reference(&slab->bo, NULL);
  367 out_err0: 
  368    FREE(slab);
  369    return ret;
  370 }
  371 
  372 
  373 static struct pb_buffer *
  374 pb_slab_manager_create_buffer(struct pb_manager *_mgr,
  375                               pb_size size,
  376                               const struct pb_desc *desc)
  377 {
  378    struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
  379    static struct pb_slab_buffer *buf;
  380    struct pb_slab *slab;
  381    struct list_head *list;
  382 
  383    /* check size */
  384    assert(size <= mgr->bufSize);
  385    if(size > mgr->bufSize)
  386       return NULL;
  387    
  388    /* check if we can provide the requested alignment */
  389    assert(pb_check_alignment(desc->alignment, mgr->desc.alignment));
  390    if(!pb_check_alignment(desc->alignment, mgr->desc.alignment))
  391       return NULL;
  392    assert(pb_check_alignment(desc->alignment, mgr->bufSize));
  393    if(!pb_check_alignment(desc->alignment, mgr->bufSize))
  394       return NULL;
  395 
  396    assert(pb_check_usage(desc->usage, mgr->desc.usage));
  397    if(!pb_check_usage(desc->usage, mgr->desc.usage))
  398       return NULL;
  399 
  400    mtx_lock(&mgr->mutex);
  401    
  402    /* Create a new slab, if we run out of partial slabs */
  403    if (mgr->slabs.next == &mgr->slabs) {
  404       (void) pb_slab_create(mgr);
  405       if (mgr->slabs.next == &mgr->slabs) {
  406      mtx_unlock(&mgr->mutex);
  407      return NULL;
  408       }
  409    }
  410    
  411    /* Allocate the buffer from a partial (or just created) slab */
  412    list = mgr->slabs.next;
  413    slab = LIST_ENTRY(struct pb_slab, list, head);
  414    
  415    /* If totally full remove from the partial slab list */
  416    if (--slab->numFree == 0)
  417       list_delinit(list);
  418 
  419    list = slab->freeBuffers.next;
  420    list_delinit(list);
  421 
  422    mtx_unlock(&mgr->mutex);
  423    buf = LIST_ENTRY(struct pb_slab_buffer, list, head);
  424    
  425    pipe_reference_init(&buf->base.reference, 1);
  426    buf->base.alignment = desc->alignment;
  427    buf->base.usage = desc->usage;
  428    
  429    return &buf->base;
  430 }
  431 
  432 
  433 static void
  434 pb_slab_manager_flush(struct pb_manager *_mgr)
  435 {
  436    struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
  437 
  438    assert(mgr->provider->flush);
  439    if(mgr->provider->flush)
  440       mgr->provider->flush(mgr->provider);
  441 }
  442 
  443 
  444 static void
  445 pb_slab_manager_destroy(struct pb_manager *_mgr)
  446 {
  447    struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
  448 
  449    /* TODO: cleanup all allocated buffers */
  450    FREE(mgr);
  451 }
  452 
  453 
  454 struct pb_manager *
  455 pb_slab_manager_create(struct pb_manager *provider,
  456                        pb_size bufSize,
  457                        pb_size slabSize,
  458                        const struct pb_desc *desc)
  459 {
  460    struct pb_slab_manager *mgr;
  461 
  462    mgr = CALLOC_STRUCT(pb_slab_manager);
  463    if (!mgr)
  464       return NULL;
  465 
  466    mgr->base.destroy = pb_slab_manager_destroy;
  467    mgr->base.create_buffer = pb_slab_manager_create_buffer;
  468    mgr->base.flush = pb_slab_manager_flush;
  469 
  470    mgr->provider = provider;
  471    mgr->bufSize = bufSize;
  472    mgr->slabSize = slabSize;
  473    mgr->desc = *desc;
  474 
  475    list_inithead(&mgr->slabs);
  476    
  477    (void) mtx_init(&mgr->mutex, mtx_plain);
  478 
  479    return &mgr->base;
  480 }
  481 
  482 
  483 static struct pb_buffer *
  484 pb_slab_range_manager_create_buffer(struct pb_manager *_mgr,
  485                                     pb_size size,
  486                                     const struct pb_desc *desc)
  487 {
  488    struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
  489    pb_size bufSize;
  490    pb_size reqSize = size;
  491    enum pb_usage_flags i;
  492 
  493    if(desc->alignment > reqSize)
  494        reqSize = desc->alignment;
  495 
  496    bufSize = mgr->minBufSize;
  497    for (i = 0; i < mgr->numBuckets; ++i) {
  498       if(bufSize >= reqSize)
  499      return mgr->buckets[i]->create_buffer(mgr->buckets[i], size, desc);
  500       bufSize *= 2;
  501    }
  502 
  503    /* Fall back to allocate a buffer object directly from the provider. */
  504    return mgr->provider->create_buffer(mgr->provider, size, desc);
  505 }
  506 
  507 
  508 static void
  509 pb_slab_range_manager_flush(struct pb_manager *_mgr)
  510 {
  511    struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
  512 
  513    /* Individual slabs don't hold any temporary buffers so no need to call them */
  514    
  515    assert(mgr->provider->flush);
  516    if(mgr->provider->flush)
  517       mgr->provider->flush(mgr->provider);
  518 }
  519 
  520 
  521 static void
  522 pb_slab_range_manager_destroy(struct pb_manager *_mgr)
  523 {
  524    struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
  525    unsigned i;
  526    
  527    for (i = 0; i < mgr->numBuckets; ++i)
  528       mgr->buckets[i]->destroy(mgr->buckets[i]);
  529    FREE(mgr->buckets);
  530    FREE(mgr->bucketSizes);
  531    FREE(mgr);
  532 }
  533 
  534 
  535 struct pb_manager *
  536 pb_slab_range_manager_create(struct pb_manager *provider,
  537                              pb_size minBufSize,
  538                              pb_size maxBufSize,
  539                              pb_size slabSize,
  540                              const struct pb_desc *desc)
  541 {
  542    struct pb_slab_range_manager *mgr;
  543    pb_size bufSize;
  544    unsigned i;
  545 
  546    if (!provider)
  547       return NULL;
  548    
  549    mgr = CALLOC_STRUCT(pb_slab_range_manager);
  550    if (!mgr)
  551       goto out_err0;
  552 
  553    mgr->base.destroy = pb_slab_range_manager_destroy;
  554    mgr->base.create_buffer = pb_slab_range_manager_create_buffer;
  555    mgr->base.flush = pb_slab_range_manager_flush;
  556 
  557    mgr->provider = provider;
  558    mgr->minBufSize = minBufSize;
  559    mgr->maxBufSize = maxBufSize;
  560 
  561    mgr->numBuckets = 1;
  562    bufSize = minBufSize;
  563    while(bufSize < maxBufSize) {
  564       bufSize *= 2;
  565       ++mgr->numBuckets;
  566    }
  567    
  568    mgr->buckets = CALLOC(mgr->numBuckets, sizeof(*mgr->buckets));
  569    if (!mgr->buckets)
  570       goto out_err1;
  571 
  572    bufSize = minBufSize;
  573    for (i = 0; i < mgr->numBuckets; ++i) {
  574       mgr->buckets[i] = pb_slab_manager_create(provider, bufSize, slabSize, desc);
  575       if(!mgr->buckets[i])
  576      goto out_err2;
  577       bufSize *= 2;
  578    }
  579 
  580    return &mgr->base;
  581 
  582 out_err2: 
  583    for (i = 0; i < mgr->numBuckets; ++i)
  584       if(mgr->buckets[i])
  585         mgr->buckets[i]->destroy(mgr->buckets[i]);
  586    FREE(mgr->buckets);
  587 out_err1: 
  588    FREE(mgr);
  589 out_err0:
  590    return NULL;
  591 }