121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca/**************************************************************************
221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca *
321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * All Rights Reserved.
521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca *
621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * Permission is hereby granted, FREE of charge, to any person obtaining a
721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * copy of this software and associated documentation files (the
821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * "Software"), to deal in the Software without restriction, including
921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * without limitation the rights to use, copy, modify, merge, publish,
1021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * distribute, sub license, and/or sell copies of the Software, and to
1121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * permit persons to whom the Software is furnished to do so, subject to
1221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * the following conditions:
1321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca *
1421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
1721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
1821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * USE OR OTHER DEALINGS IN THE SOFTWARE.
2121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca *
2221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * The above copyright notice and this permission notice (including the
2321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * next paragraph) shall be included in all copies or substantial portions
2421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * of the Software.
2521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca *
2621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca *
2721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca **************************************************************************/
2821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
2921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca/**
3021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * @file
3121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * S-lab pool implementation.
3221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca *
335c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * @sa http://en.wikipedia.org/wiki/Slab_allocation
345c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca *
3521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * @author Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
3621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
3721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca */
3821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
3921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca#include "pipe/p_compiler.h"
40ea4bf267e4b023b08043f91ac44592fed1736e7fJosé Fonseca#include "util/u_debug.h"
412aaca1df9df6980ec88180c8866c8987b31db91aJosé Fonseca#include "os/os_thread.h"
4221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca#include "pipe/p_defines.h"
434f25420bdd834e81a3e22733304efc5261c2998aBrian Paul#include "util/u_memory.h"
4421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca#include "util/u_double_list.h"
4521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca#include "util/u_time.h"
4621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
4721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca#include "pb_buffer.h"
4821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca#include "pb_bufmgr.h"
4921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
5021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
5121c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastruct pb_slab;
5221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
535c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
545c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca/**
555c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * Buffer in a slab.
565c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca *
575c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * Sub-allocation of a contiguous buffer.
585c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca */
5921c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastruct pb_slab_buffer
6021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
6121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_buffer base;
6221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
6321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab *slab;
645c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
6521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct list_head head;
665c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
6721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   unsigned mapCount;
685c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
695c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** Offset relative to the start of the slab buffer. */
702af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size start;
715c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
725c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** Use when validating, to signal that all mappings are finished */
735c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /* TODO: Actually validation does not reach this stage yet */
740bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul   pipe_condvar event;
7521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca};
7621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
775c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
785c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca/**
795c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * Slab -- a contiguous piece of memory.
805c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca */
8121c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastruct pb_slab
8221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
8321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct list_head head;
8421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct list_head freeBuffers;
852af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size numBuffers;
862af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size numFree;
875c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
8821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab_buffer *buffers;
89a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_manager *mgr;
9021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
915c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** Buffer from the provider */
9221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_buffer *bo;
935c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
9421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   void *virtual;
9521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca};
9621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
975c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
985c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca/**
995c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * It adds/removes slabs as needed in order to meet the allocation/destruction
1005c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * of individual buffers.
1015c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca */
102a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecastruct pb_slab_manager
10321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
104a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_manager base;
105a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
1065c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** From where we get our buffers */
107a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_manager *provider;
1085c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
1095c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** Size of the buffers we hand on downstream */
1102af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size bufSize;
1115c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
1125c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** Size of the buffers we request upstream */
1132af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size slabSize;
1145c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
1155c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /**
1165c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    * Alignment, usage to be used to allocate the slab buffers.
1175c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    *
1185c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    * We can only provide buffers which are consistent (in alignment, usage)
1195c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    * with this description.
1205c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    */
121a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_desc desc;
122a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
1235c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /**
1245c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    * Partial slabs
1255c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    *
1265c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    * Full slabs are not stored in any list. Empty slabs are destroyed
1275c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    * immediatly.
1285c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    */
12921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct list_head slabs;
130a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
1310bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul   pipe_mutex mutex;
13221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca};
13321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
1345c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
13521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca/**
1365c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * Wrapper around several slabs, therefore capable of handling buffers of
1375c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * multiple sizes.
1385c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca *
1395c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * This buffer manager just dispatches buffer allocations to the appropriate slab
1405c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * manager, according to the requested buffer size, or by passes the slab
1415c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * managers altogether for even greater sizes.
1425c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca *
14321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * The data of this structure remains constant after
14421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * initialization and thus needs no mutex protection.
14521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca */
146a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecastruct pb_slab_range_manager
14721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
14821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_manager base;
14921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
150a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_manager *provider;
1515c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
1522af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size minBufSize;
1532af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size maxBufSize;
1545c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
1555c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** @sa pb_slab_manager::desc */
15621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_desc desc;
157a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
158a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   unsigned numBuckets;
1592af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size *bucketSizes;
1605c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
1615c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /** Array of pb_slab_manager, one for each bucket size */
162a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_manager **buckets;
16321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca};
16421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
16521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
16621c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic INLINE struct pb_slab_buffer *
16721c302b0ec39480a7eaab7827cce5b609d196606José Fonsecapb_slab_buffer(struct pb_buffer *buf)
16821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
16921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   assert(buf);
17021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   return (struct pb_slab_buffer *)buf;
17121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
17221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
17321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
17421c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic INLINE struct pb_slab_manager *
17521c302b0ec39480a7eaab7827cce5b609d196606José Fonsecapb_slab_manager(struct pb_manager *mgr)
17621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
17721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   assert(mgr);
17821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   return (struct pb_slab_manager *)mgr;
17921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
18021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
18121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
182a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecastatic INLINE struct pb_slab_range_manager *
183a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_range_manager(struct pb_manager *mgr)
184a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca{
185a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   assert(mgr);
186a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   return (struct pb_slab_range_manager *)mgr;
187a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca}
188a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
189a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
19021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca/**
191a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca * Delete a buffer from the slab delayed list and put
19221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca * it on the slab FREE list.
19321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca */
19421c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic void
19521c302b0ec39480a7eaab7827cce5b609d196606José Fonsecapb_slab_buffer_destroy(struct pb_buffer *_buf)
19621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
19721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
19821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab *slab = buf->slab;
199a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_manager *mgr = slab->mgr;
20021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct list_head *list = &buf->head;
20121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
2020bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul   pipe_mutex_lock(mgr->mutex);
20321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
2044682e706012fe26627a2f827db01b5068cc62814Marek Olšák   assert(!pipe_is_referenced(&buf->base.reference));
20521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
20621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   buf->mapCount = 0;
20721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
20821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   LIST_DEL(list);
20921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   LIST_ADDTAIL(list, &slab->freeBuffers);
21021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   slab->numFree++;
21121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
21221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   if (slab->head.next == &slab->head)
213a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      LIST_ADDTAIL(&slab->head, &mgr->slabs);
21421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
2155c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /* If the slab becomes totally empty, free it */
21621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   if (slab->numFree == slab->numBuffers) {
21721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      list = &slab->head;
2185c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca      LIST_DELINIT(list);
2195c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca      pb_reference(&slab->bo, NULL);
2205c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca      FREE(slab->buffers);
2215c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca      FREE(slab);
22221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   }
22321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
2240bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul   pipe_mutex_unlock(mgr->mutex);
22521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
22621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
22721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
22821c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic void *
22921c302b0ec39480a7eaab7827cce5b609d196606José Fonsecapb_slab_buffer_map(struct pb_buffer *_buf,
23067763488b1fea01d6eb1c7d9e05ecb175918c3afJosé Fonseca                   unsigned flags,
23167763488b1fea01d6eb1c7d9e05ecb175918c3afJosé Fonseca                   void *flush_ctx)
23221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
23321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
23421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
23567763488b1fea01d6eb1c7d9e05ecb175918c3afJosé Fonseca   /* XXX: it will be necessary to remap here to propagate flush_ctx */
23667763488b1fea01d6eb1c7d9e05ecb175918c3afJosé Fonseca
23721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   ++buf->mapCount;
23821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   return (void *) ((uint8_t *) buf->slab->virtual + buf->start);
23921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
24021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
24121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
24221c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic void
24321c302b0ec39480a7eaab7827cce5b609d196606José Fonsecapb_slab_buffer_unmap(struct pb_buffer *_buf)
24421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
24521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
24621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
24721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   --buf->mapCount;
24821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   if (buf->mapCount == 0)
2490bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul       pipe_condvar_broadcast(buf->event);
25021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
25121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
25221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
253e06474dbae6979177629fb6187331291ff230c65José Fonsecastatic enum pipe_error
254e06474dbae6979177629fb6187331291ff230c65José Fonsecapb_slab_buffer_validate(struct pb_buffer *_buf,
255e06474dbae6979177629fb6187331291ff230c65José Fonseca                         struct pb_validate *vl,
256e06474dbae6979177629fb6187331291ff230c65José Fonseca                         unsigned flags)
257e06474dbae6979177629fb6187331291ff230c65José Fonseca{
258e06474dbae6979177629fb6187331291ff230c65José Fonseca   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
259e06474dbae6979177629fb6187331291ff230c65José Fonseca   return pb_validate(buf->slab->bo, vl, flags);
260e06474dbae6979177629fb6187331291ff230c65José Fonseca}
261e06474dbae6979177629fb6187331291ff230c65José Fonseca
262e06474dbae6979177629fb6187331291ff230c65José Fonseca
263e06474dbae6979177629fb6187331291ff230c65José Fonsecastatic void
264e06474dbae6979177629fb6187331291ff230c65José Fonsecapb_slab_buffer_fence(struct pb_buffer *_buf,
265e06474dbae6979177629fb6187331291ff230c65José Fonseca                      struct pipe_fence_handle *fence)
266e06474dbae6979177629fb6187331291ff230c65José Fonseca{
267e06474dbae6979177629fb6187331291ff230c65José Fonseca   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
268e06474dbae6979177629fb6187331291ff230c65José Fonseca   pb_fence(buf->slab->bo, fence);
269e06474dbae6979177629fb6187331291ff230c65José Fonseca}
270e06474dbae6979177629fb6187331291ff230c65José Fonseca
271e06474dbae6979177629fb6187331291ff230c65José Fonseca
27221c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic void
27321c302b0ec39480a7eaab7827cce5b609d196606José Fonsecapb_slab_buffer_get_base_buffer(struct pb_buffer *_buf,
27421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca                               struct pb_buffer **base_buf,
2752af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                               pb_size *offset)
27621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
27721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
27821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   pb_get_base_buffer(buf->slab->bo, base_buf, offset);
27921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   *offset += buf->start;
28021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
28121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
28221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
28321c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic const struct pb_vtbl
28421c302b0ec39480a7eaab7827cce5b609d196606José Fonsecapb_slab_buffer_vtbl = {
28521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      pb_slab_buffer_destroy,
28621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      pb_slab_buffer_map,
28721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      pb_slab_buffer_unmap,
288e06474dbae6979177629fb6187331291ff230c65José Fonseca      pb_slab_buffer_validate,
289e06474dbae6979177629fb6187331291ff230c65José Fonseca      pb_slab_buffer_fence,
29021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      pb_slab_buffer_get_base_buffer
29121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca};
29221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
29321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
2945c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca/**
2955c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * Create a new slab.
2965c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca *
2975c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca * Called when we ran out of free slabs.
2985c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca */
29921c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic enum pipe_error
300a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_create(struct pb_slab_manager *mgr)
30121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
30221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab *slab;
30321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab_buffer *buf;
304a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   unsigned numBuffers;
30521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   unsigned i;
306a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   enum pipe_error ret;
30721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
30821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   slab = CALLOC_STRUCT(pb_slab);
30921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   if (!slab)
31021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      return PIPE_ERROR_OUT_OF_MEMORY;
31121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
312a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   slab->bo = mgr->provider->create_buffer(mgr->provider, mgr->slabSize, &mgr->desc);
313a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   if(!slab->bo) {
314a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      ret = PIPE_ERROR_OUT_OF_MEMORY;
31521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      goto out_err0;
316a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   }
31721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
3185c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /* Note down the slab virtual address. All mappings are accessed directly
3195c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca    * through this address so it is required that the buffer is pinned. */
32021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   slab->virtual = pb_map(slab->bo,
321287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell                          PB_USAGE_CPU_READ |
322b5fcf0c8e07e666523b007fab1d0fc18c2c89241Dave Airlie                          PB_USAGE_CPU_WRITE, NULL);
323a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   if(!slab->virtual) {
324a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      ret = PIPE_ERROR_OUT_OF_MEMORY;
32521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      goto out_err1;
326a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   }
32721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   pb_unmap(slab->bo);
32821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
3294682e706012fe26627a2f827db01b5068cc62814Marek Olšák   numBuffers = slab->bo->size / mgr->bufSize;
33021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
33121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   slab->buffers = CALLOC(numBuffers, sizeof(*slab->buffers));
33221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   if (!slab->buffers) {
33321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      ret = PIPE_ERROR_OUT_OF_MEMORY;
33421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      goto out_err1;
33521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   }
33621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
33721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   LIST_INITHEAD(&slab->head);
33821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   LIST_INITHEAD(&slab->freeBuffers);
33921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   slab->numBuffers = numBuffers;
34021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   slab->numFree = 0;
341a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   slab->mgr = mgr;
34221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
34321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   buf = slab->buffers;
34421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   for (i=0; i < numBuffers; ++i) {
3454682e706012fe26627a2f827db01b5068cc62814Marek Olšák      pipe_reference_init(&buf->base.reference, 0);
3464682e706012fe26627a2f827db01b5068cc62814Marek Olšák      buf->base.size = mgr->bufSize;
3474682e706012fe26627a2f827db01b5068cc62814Marek Olšák      buf->base.alignment = 0;
3484682e706012fe26627a2f827db01b5068cc62814Marek Olšák      buf->base.usage = 0;
34921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      buf->base.vtbl = &pb_slab_buffer_vtbl;
35021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      buf->slab = slab;
351a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      buf->start = i* mgr->bufSize;
35221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      buf->mapCount = 0;
3530bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul      pipe_condvar_init(buf->event);
35421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      LIST_ADDTAIL(&buf->head, &slab->freeBuffers);
35521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      slab->numFree++;
35621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      buf++;
35721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   }
35821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
3595c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /* Add this slab to the list of partial slabs */
360a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   LIST_ADDTAIL(&slab->head, &mgr->slabs);
36121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
36221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   return PIPE_OK;
36321c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
36421c302b0ec39480a7eaab7827cce5b609d196606José Fonsecaout_err1:
36521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   pb_reference(&slab->bo, NULL);
36621c302b0ec39480a7eaab7827cce5b609d196606José Fonsecaout_err0:
36721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   FREE(slab);
36821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   return ret;
36921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
37021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
37121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
37221c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic struct pb_buffer *
373a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_manager_create_buffer(struct pb_manager *_mgr,
3742af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                              pb_size size,
37521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca                              const struct pb_desc *desc)
37621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
377a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
37821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   static struct pb_slab_buffer *buf;
37921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct pb_slab *slab;
38021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   struct list_head *list;
38121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
382a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   /* check size */
383a0e2955a16a8a04afe7f84b1c8551211542a6fbdJosé Fonseca   assert(size <= mgr->bufSize);
384a0e2955a16a8a04afe7f84b1c8551211542a6fbdJosé Fonseca   if(size > mgr->bufSize)
385a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      return NULL;
386a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
387a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   /* check if we can provide the requested alignment */
38895aeeb6d746e57473116ef4d72c05330902f68a5José Fonseca   assert(pb_check_alignment(desc->alignment, mgr->desc.alignment));
38995aeeb6d746e57473116ef4d72c05330902f68a5José Fonseca   if(!pb_check_alignment(desc->alignment, mgr->desc.alignment))
390a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      return NULL;
39195aeeb6d746e57473116ef4d72c05330902f68a5José Fonseca   assert(pb_check_alignment(desc->alignment, mgr->bufSize));
39295aeeb6d746e57473116ef4d72c05330902f68a5José Fonseca   if(!pb_check_alignment(desc->alignment, mgr->bufSize))
393a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      return NULL;
39421c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
395ea4ca10b1bec67c8a60db0e4e5581318ce9f62f9José Fonseca   assert(pb_check_usage(desc->usage, mgr->desc.usage));
396ea4ca10b1bec67c8a60db0e4e5581318ce9f62f9José Fonseca   if(!pb_check_usage(desc->usage, mgr->desc.usage))
397ea4ca10b1bec67c8a60db0e4e5581318ce9f62f9José Fonseca      return NULL;
398ea4ca10b1bec67c8a60db0e4e5581318ce9f62f9José Fonseca
3990bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul   pipe_mutex_lock(mgr->mutex);
4005c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
4015c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /* Create a new slab, if we run out of partial slabs */
402bd4eec0561fb021849ac4047fdbf40a616fb68b3José Fonseca   if (mgr->slabs.next == &mgr->slabs) {
403a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      (void) pb_slab_create(mgr);
404bd4eec0561fb021849ac4047fdbf40a616fb68b3José Fonseca      if (mgr->slabs.next == &mgr->slabs) {
4050bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul	 pipe_mutex_unlock(mgr->mutex);
406bd4eec0561fb021849ac4047fdbf40a616fb68b3José Fonseca	 return NULL;
407bd4eec0561fb021849ac4047fdbf40a616fb68b3José Fonseca      }
40821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   }
4095c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
4105c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /* Allocate the buffer from a partial (or just created) slab */
411a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   list = mgr->slabs.next;
41221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   slab = LIST_ENTRY(struct pb_slab, list, head);
4135c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca
4145c198f660a1812d9b3970408695d04bdd74a5d1eJosé Fonseca   /* If totally full remove from the partial slab list */
41521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   if (--slab->numFree == 0)
41621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      LIST_DELINIT(list);
41721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
41821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   list = slab->freeBuffers.next;
41921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   LIST_DELINIT(list);
42021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
4210bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul   pipe_mutex_unlock(mgr->mutex);
42221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   buf = LIST_ENTRY(struct pb_slab_buffer, list, head);
423a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
4244682e706012fe26627a2f827db01b5068cc62814Marek Olšák   pipe_reference_init(&buf->base.reference, 1);
4254682e706012fe26627a2f827db01b5068cc62814Marek Olšák   buf->base.alignment = desc->alignment;
4264682e706012fe26627a2f827db01b5068cc62814Marek Olšák   buf->base.usage = desc->usage;
427a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
42821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   return &buf->base;
42921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
43021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
43121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
43221c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastatic void
4331672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonsecapb_slab_manager_flush(struct pb_manager *_mgr)
4341672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca{
4351672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
4361672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca
4371672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   assert(mgr->provider->flush);
4381672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   if(mgr->provider->flush)
4391672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca      mgr->provider->flush(mgr->provider);
4401672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca}
4411672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca
4421672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca
4431672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonsecastatic void
444a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_manager_destroy(struct pb_manager *_mgr)
44521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
446a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
44721c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
448a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   /* TODO: cleanup all allocated buffers */
449a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   FREE(mgr);
45021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
45121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
45221c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
45321c302b0ec39480a7eaab7827cce5b609d196606José Fonsecastruct pb_manager *
454a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_manager_create(struct pb_manager *provider,
4552af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                       pb_size bufSize,
4562af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                       pb_size slabSize,
457a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca                       const struct pb_desc *desc)
458a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca{
459a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_manager *mgr;
460a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
461a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr = CALLOC_STRUCT(pb_slab_manager);
462a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   if (!mgr)
463a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      return NULL;
464a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
465a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->base.destroy = pb_slab_manager_destroy;
466a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->base.create_buffer = pb_slab_manager_create_buffer;
4671672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   mgr->base.flush = pb_slab_manager_flush;
468a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
469a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->provider = provider;
470a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->bufSize = bufSize;
471a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->slabSize = slabSize;
472a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->desc = *desc;
473a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
474a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   LIST_INITHEAD(&mgr->slabs);
475a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
4760bb852fa49e7f9a31036089ea4f5dfbd312a4a3aBrian Paul   pipe_mutex_init(mgr->mutex);
477a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
478a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   return &mgr->base;
479a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca}
480a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
481a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
482a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecastatic struct pb_buffer *
483a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_range_manager_create_buffer(struct pb_manager *_mgr,
4842af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                                    pb_size size,
485a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca                                    const struct pb_desc *desc)
48621c302b0ec39480a7eaab7827cce5b609d196606José Fonseca{
487a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
4882af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size bufSize;
4892f5beb4b530d47d6b4a7cf0effeaec0d2c1b6ea4Luca Barbieri   pb_size reqSize = size;
490a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   unsigned i;
49121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
4922f5beb4b530d47d6b4a7cf0effeaec0d2c1b6ea4Luca Barbieri   if(desc->alignment > reqSize)
4932f5beb4b530d47d6b4a7cf0effeaec0d2c1b6ea4Luca Barbieri	   reqSize = desc->alignment;
4942f5beb4b530d47d6b4a7cf0effeaec0d2c1b6ea4Luca Barbieri
495a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   bufSize = mgr->minBufSize;
496a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   for (i = 0; i < mgr->numBuckets; ++i) {
4972f5beb4b530d47d6b4a7cf0effeaec0d2c1b6ea4Luca Barbieri      if(bufSize >= reqSize)
498a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca	 return mgr->buckets[i]->create_buffer(mgr->buckets[i], size, desc);
499a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      bufSize *= 2;
500a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   }
501a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
502a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   /* Fall back to allocate a buffer object directly from the provider. */
503a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   return mgr->provider->create_buffer(mgr->provider, size, desc);
504a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca}
505a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
506a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
507a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecastatic void
5081672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonsecapb_slab_range_manager_flush(struct pb_manager *_mgr)
5091672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca{
5101672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
5111672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca
5121672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   /* Individual slabs don't hold any temporary buffers so no need to call them */
5131672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca
5141672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   assert(mgr->provider->flush);
5151672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   if(mgr->provider->flush)
5161672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca      mgr->provider->flush(mgr->provider);
5171672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca}
5181672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca
5191672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca
5201672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonsecastatic void
521a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_range_manager_destroy(struct pb_manager *_mgr)
522a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca{
523a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
524a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   unsigned i;
525a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
526a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   for (i = 0; i < mgr->numBuckets; ++i)
527a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      mgr->buckets[i]->destroy(mgr->buckets[i]);
528a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   FREE(mgr->buckets);
529a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   FREE(mgr->bucketSizes);
530a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   FREE(mgr);
531a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca}
532a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
533a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
534a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecastruct pb_manager *
535a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonsecapb_slab_range_manager_create(struct pb_manager *provider,
5362af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                             pb_size minBufSize,
5372af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                             pb_size maxBufSize,
5382af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca                             pb_size slabSize,
539a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca                             const struct pb_desc *desc)
540a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca{
541a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   struct pb_slab_range_manager *mgr;
5422af0173e9e4eefe910c6011038e7346091a9b2a4José Fonseca   pb_size bufSize;
543a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   unsigned i;
544a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
545ea4ca10b1bec67c8a60db0e4e5581318ce9f62f9José Fonseca   if(!provider)
546ea4ca10b1bec67c8a60db0e4e5581318ce9f62f9José Fonseca      return NULL;
547ea4ca10b1bec67c8a60db0e4e5581318ce9f62f9José Fonseca
548a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr = CALLOC_STRUCT(pb_slab_range_manager);
549a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   if (!mgr)
55021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca      goto out_err0;
55121c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
552a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->base.destroy = pb_slab_range_manager_destroy;
553a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->base.create_buffer = pb_slab_range_manager_create_buffer;
5541672e8e05996d48e51a1998bd7e9b08b78e012f5José Fonseca   mgr->base.flush = pb_slab_range_manager_flush;
55521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
556a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->provider = provider;
557a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->minBufSize = minBufSize;
558a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->maxBufSize = maxBufSize;
559a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
560a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->numBuckets = 1;
561a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   bufSize = minBufSize;
562a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   while(bufSize < maxBufSize) {
563a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      bufSize *= 2;
564a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      ++mgr->numBuckets;
56521c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   }
566a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca
567a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   mgr->buckets = CALLOC(mgr->numBuckets, sizeof(*mgr->buckets));
568a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   if (!mgr->buckets)
569a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      goto out_err1;
57021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
571a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   bufSize = minBufSize;
572a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   for (i = 0; i < mgr->numBuckets; ++i) {
573a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      mgr->buckets[i] = pb_slab_manager_create(provider, bufSize, slabSize, desc);
574a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      if(!mgr->buckets[i])
575a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca	 goto out_err2;
576a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      bufSize *= 2;
577a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   }
57821c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
579a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   return &mgr->base;
58021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca
58121c302b0ec39480a7eaab7827cce5b609d196606José Fonsecaout_err2:
582a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   for (i = 0; i < mgr->numBuckets; ++i)
583a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca      if(mgr->buckets[i])
584a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca	    mgr->buckets[i]->destroy(mgr->buckets[i]);
585a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   FREE(mgr->buckets);
58621c302b0ec39480a7eaab7827cce5b609d196606José Fonsecaout_err1:
587a175e15f20b2a231cc9d09099e7b6d8aea6c624eJosé Fonseca   FREE(mgr);
58821c302b0ec39480a7eaab7827cce5b609d196606José Fonsecaout_err0:
58921c302b0ec39480a7eaab7827cce5b609d196606José Fonseca   return NULL;
59021c302b0ec39480a7eaab7827cce5b609d196606José Fonseca}
591