summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/pipebuffer/pb_bufmgr_slab.c
blob: 2a801549202309aa79666a070456b0eb139666ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**************************************************************************
 *
 * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
 * All Rights Reserved.
 *
 * Permission is hereby granted, FREE of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 *
 **************************************************************************/

/**
 * @file
 * S-lab pool implementation.
 * 
 * @sa http://en.wikipedia.org/wiki/Slab_allocation
 * 
 * @author Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
 */

#include "pipe/p_compiler.h"
#include "pipe/p_error.h"
#include "pipe/p_debug.h"
#include "pipe/p_thread.h"
#include "pipe/p_defines.h"
#include "util/u_memory.h"
#include "util/u_double_list.h"
#include "util/u_time.h"

#include "pb_buffer.h"
#include "pb_bufmgr.h"


struct pb_slab;


/**
 * Buffer in a slab.
 * 
 * Sub-allocation of a contiguous buffer.
 */
struct pb_slab_buffer
{
   struct pb_buffer base;
   
   struct pb_slab *slab;
   
   struct list_head head;
   
   unsigned mapCount;
   
   /** Offset relative to the start of the slab buffer. */
   size_t start;
   
   /** Use when validating, to signal that all mappings are finished */
   /* TODO: Actually validation does not reach this stage yet */
   pipe_condvar event;
};


/**
 * Slab -- a contiguous piece of memory. 
 */
struct pb_slab
{
   struct list_head head;
   struct list_head freeBuffers;
   size_t numBuffers;
   size_t numFree;
   
   struct pb_slab_buffer *buffers;
   struct pb_slab_manager *mgr;
   
   /** Buffer from the provider */
   struct pb_buffer *bo;
   
   void *virtual;   
};


/**
 * It adds/removes slabs as needed in order to meet the allocation/destruction 
 * of individual buffers.
 */
struct pb_slab_manager 
{
   struct pb_manager base;
   
   /** From where we get our buffers */
   struct pb_manager *provider;
   
   /** Size of the buffers we hand on downstream */
   size_t bufSize;
   
   /** Size of the buffers we request upstream */
   size_t slabSize;
   
   /** 
    * Alignment, usage to be used to allocate the slab buffers.
    * 
    * We can only provide buffers which are consistent (in alignment, usage) 
    * with this description.   
    */
   struct pb_desc desc;

   /** 
    * Partial slabs
    * 
    * Full slabs are not stored in any list. Empty slabs are destroyed 
    * immediatly.
    */
   struct list_head slabs;
   
   pipe_mutex mutex;
};


/**
 * Wrapper around several slabs, therefore capable of handling buffers of 
 * multiple sizes. 
 * 
 * This buffer manager just dispatches buffer allocations to the appropriate slab
 * manager, according to the requested buffer size, or by passes the slab 
 * managers altogether for even greater sizes.
 * 
 * The data of this structure remains constant after
 * initialization and thus needs no mutex protection.
 */
struct pb_slab_range_manager 
{
   struct pb_manager base;

   struct pb_manager *provider;
   
   size_t minBufSize;
   size_t maxBufSize;
   
   /** @sa pb_slab_manager::desc */ 
   struct pb_desc desc;
   
   unsigned numBuckets;
   size_t *bucketSizes;
   
   /** Array of pb_slab_manager, one for each bucket size */
   struct pb_manager **buckets;
};


static INLINE struct pb_slab_buffer *
pb_slab_buffer(struct pb_buffer *buf)
{
   assert(buf);
   return (struct pb_slab_buffer *)buf;
}


static INLINE struct pb_slab_manager *
pb_slab_manager(struct pb_manager *mgr)
{
   assert(mgr);
   return (struct pb_slab_manager *)mgr;
}


static INLINE struct pb_slab_range_manager *
pb_slab_range_manager(struct pb_manager *mgr)
{
   assert(mgr);
   return (struct pb_slab_range_manager *)mgr;
}


/**
 * Delete a buffer from the slab delayed list and put
 * it on the slab FREE list.
 */
static void
pb_slab_buffer_destroy(struct pb_buffer *_buf)
{
   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
   struct pb_slab *slab = buf->slab;
   struct pb_slab_manager *mgr = slab->mgr;
   struct list_head *list = &buf->head;

   pipe_mutex_lock(mgr->mutex);
   
   assert(buf->base.base.refcount == 0);
   
   buf->mapCount = 0;

   LIST_DEL(list);
   LIST_ADDTAIL(list, &slab->freeBuffers);
   slab->numFree++;

   if (slab->head.next == &slab->head)
      LIST_ADDTAIL(&slab->head, &mgr->slabs);

   /* If the slab becomes totally empty, free it */
   if (slab->numFree == slab->numBuffers) {
      list = &slab->head;
      LIST_DELINIT(list);
      pb_reference(&slab->bo, NULL);
      FREE(slab->buffers);
      FREE(slab);
   }

   pipe_mutex_unlock(mgr->mutex);
}


static void *
pb_slab_buffer_map(struct pb_buffer *_buf, 
                   unsigned flags)
{
   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);

   ++buf->mapCount;
   return (void *) ((uint8_t *) buf->slab->virtual + buf->start);
}


static void
pb_slab_buffer_unmap(struct pb_buffer *_buf)
{
   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);

   --buf->mapCount;
   if (buf->mapCount == 0) 
       pipe_condvar_broadcast(buf->event);
}


static void
pb_slab_buffer_get_base_buffer(struct pb_buffer *_buf,
                               struct pb_buffer **base_buf,
                               unsigned *offset)
{
   struct pb_slab_buffer *buf = pb_slab_buffer(_buf);
   pb_get_base_buffer(buf->slab->bo, base_buf, offset);
   *offset += buf->start;
}


static const struct pb_vtbl 
pb_slab_buffer_vtbl = {
      pb_slab_buffer_destroy,
      pb_slab_buffer_map,
      pb_slab_buffer_unmap,
      pb_slab_buffer_get_base_buffer
};


/**
 * Create a new slab.
 * 
 * Called when we ran out of free slabs.
 */
static enum pipe_error
pb_slab_create(struct pb_slab_manager *mgr)
{
   struct pb_slab *slab;
   struct pb_slab_buffer *buf;
   unsigned numBuffers;
   unsigned i;
   enum pipe_error ret;

   slab = CALLOC_STRUCT(pb_slab);
   if (!slab)
      return PIPE_ERROR_OUT_OF_MEMORY;

   slab->bo = mgr->provider->create_buffer(mgr->provider, mgr->slabSize, &mgr->desc);
   if(!slab->bo) {
      ret = PIPE_ERROR_OUT_OF_MEMORY;
      goto out_err0;
   }

   /* Note down the slab virtual address. All mappings are accessed directly 
    * through this address so it is required that the buffer is pinned. */
   slab->virtual = pb_map(slab->bo, 
                          PIPE_BUFFER_USAGE_CPU_READ |
                          PIPE_BUFFER_USAGE_CPU_WRITE);
   if(!slab->virtual) {
      ret = PIPE_ERROR_OUT_OF_MEMORY;
      goto out_err1;
   }
   pb_unmap(slab->bo);

   numBuffers = slab->bo->base.size / mgr->bufSize;

   slab->buffers = CALLOC(numBuffers, sizeof(*slab->buffers));
   if (!slab->buffers) {
      ret = PIPE_ERROR_OUT_OF_MEMORY;
      goto out_err1;
   }

   LIST_INITHEAD(&slab->head);
   LIST_INITHEAD(&slab->freeBuffers);
   slab->numBuffers = numBuffers;
   slab->numFree = 0;
   slab->mgr = mgr;

   buf = slab->buffers;
   for (i=0; i < numBuffers; ++i) {
      buf->base.base.refcount = 0;
      buf->base.base.size = mgr->bufSize;
      buf->base.base.alignment = 0;
      buf->base.base.usage = 0;
      buf->base.vtbl = &pb_slab_buffer_vtbl;
      buf->slab = slab;
      buf->start = i* mgr->bufSize;
      buf->mapCount = 0;
      pipe_condvar_init(buf->event);
      LIST_ADDTAIL(&buf->head, &slab->freeBuffers);
      slab->numFree++;
      buf++;
   }

   /* Add this slab to the list of partial slabs */
   LIST_ADDTAIL(&slab->head, &mgr->slabs);

   return PIPE_OK;

out_err1: 
   pb_reference(&slab->bo, NULL);
out_err0: 
   FREE(slab);
   return ret;
}


static struct pb_buffer *
pb_slab_manager_create_buffer(struct pb_manager *_mgr,
                              size_t size,
                              const struct pb_desc *desc)
{
   struct pb_slab_manager *mgr = pb_slab_manager(_mgr);
   static struct pb_slab_buffer *buf;
   struct pb_slab *slab;
   struct list_head *list;

   /* check size */
   assert(size <= mgr->bufSize);
   if(size > mgr->bufSize)
      return NULL;
   
   /* check if we can provide the requested alignment */
   assert(pb_check_alignment(desc->alignment, mgr->desc.alignment));
   if(!pb_check_alignment(desc->alignment, mgr->desc.alignment))
      return NULL;
   assert(pb_check_alignment(desc->alignment, mgr->bufSize));
   if(!pb_check_alignment(desc->alignment, mgr->bufSize))
      return NULL;

   assert(pb_check_usage(desc->usage, mgr->desc.usage));
   if(!pb_check_usage(desc->usage, mgr->desc.usage))
      return NULL;

   pipe_mutex_lock(mgr->mutex);
   
   /* Create a new slab, if we run out of partial slabs */
   if (mgr->slabs.next == &mgr->slabs) {
      (void) pb_slab_create(mgr);
      if (mgr->slabs.next == &mgr->slabs) {
	 pipe_mutex_unlock(mgr->mutex);
	 return NULL;
      }
   }
   
   /* Allocate the buffer from a partial (or just created) slab */
   list = mgr->slabs.next;
   slab = LIST_ENTRY(struct pb_slab, list, head);
   
   /* If totally full remove from the partial slab list */
   if (--slab->numFree == 0)
      LIST_DELINIT(list);

   list = slab->freeBuffers.next;
   LIST_DELINIT(list);

   pipe_mutex_unlock(mgr->mutex);
   buf = LIST_ENTRY(struct pb_slab_buffer, list, head);
   
   ++buf->base.base.refcount;
   buf->base.base.alignment = desc->alignment;
   buf->base.base.usage = desc->usage;
   
   return &buf->base;
}


static void
pb_slab_manager_flush(struct pb_manager *_mgr)
{
   struct pb_slab_manager *mgr = pb_slab_manager(_mgr);

   assert(mgr->provider->flush);
   if(mgr->provider->flush)
      mgr->provider->flush(mgr->provider);
}


static void
pb_slab_manager_destroy(struct pb_manager *_mgr)
{
   struct pb_slab_manager *mgr = pb_slab_manager(_mgr);

   /* TODO: cleanup all allocated buffers */
   FREE(mgr);
}


struct pb_manager *
pb_slab_manager_create(struct pb_manager *provider,
                       size_t bufSize,
                       size_t slabSize,
                       const struct pb_desc *desc)
{
   struct pb_slab_manager *mgr;

   mgr = CALLOC_STRUCT(pb_slab_manager);
   if (!mgr)
      return NULL;

   mgr->base.destroy = pb_slab_manager_destroy;
   mgr->base.create_buffer = pb_slab_manager_create_buffer;
   mgr->base.flush = pb_slab_manager_flush;

   mgr->provider = provider;
   mgr->bufSize = bufSize;
   mgr->slabSize = slabSize;
   mgr->desc = *desc;

   LIST_INITHEAD(&mgr->slabs);
   
   pipe_mutex_init(mgr->mutex);

   return &mgr->base;
}


static struct pb_buffer *
pb_slab_range_manager_create_buffer(struct pb_manager *_mgr,
                                    size_t size,
                                    const struct pb_desc *desc)
{
   struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
   size_t bufSize;
   unsigned i;

   bufSize = mgr->minBufSize;
   for (i = 0; i < mgr->numBuckets; ++i) {
      if(bufSize >= size)
	 return mgr->buckets[i]->create_buffer(mgr->buckets[i], size, desc);
      bufSize *= 2;
   }

   /* Fall back to allocate a buffer object directly from the provider. */
   return mgr->provider->create_buffer(mgr->provider, size, desc);
}


static void
pb_slab_range_manager_flush(struct pb_manager *_mgr)
{
   struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);

   /* Individual slabs don't hold any temporary buffers so no need to call them */
   
   assert(mgr->provider->flush);
   if(mgr->provider->flush)
      mgr->provider->flush(mgr->provider);
}


static void
pb_slab_range_manager_destroy(struct pb_manager *_mgr)
{
   struct pb_slab_range_manager *mgr = pb_slab_range_manager(_mgr);
   unsigned i;
   
   for (i = 0; i < mgr->numBuckets; ++i)
      mgr->buckets[i]->destroy(mgr->buckets[i]);
   FREE(mgr->buckets);
   FREE(mgr->bucketSizes);
   FREE(mgr);
}


struct pb_manager *
pb_slab_range_manager_create(struct pb_manager *provider,
                             size_t minBufSize,
                             size_t maxBufSize,
                             size_t slabSize,
                             const struct pb_desc *desc)
{
   struct pb_slab_range_manager *mgr;
   size_t bufSize;
   unsigned i;

   if(!provider)
      return NULL;
   
   mgr = CALLOC_STRUCT(pb_slab_range_manager);
   if (!mgr)
      goto out_err0;

   mgr->base.destroy = pb_slab_range_manager_destroy;
   mgr->base.create_buffer = pb_slab_range_manager_create_buffer;
   mgr->base.flush = pb_slab_range_manager_flush;

   mgr->provider = provider;
   mgr->minBufSize = minBufSize;
   mgr->maxBufSize = maxBufSize;

   mgr->numBuckets = 1;
   bufSize = minBufSize;
   while(bufSize < maxBufSize) {
      bufSize *= 2;
      ++mgr->numBuckets;
   }
   
   mgr->buckets = CALLOC(mgr->numBuckets, sizeof(*mgr->buckets));
   if (!mgr->buckets)
      goto out_err1;

   bufSize = minBufSize;
   for (i = 0; i < mgr->numBuckets; ++i) {
      mgr->buckets[i] = pb_slab_manager_create(provider, bufSize, slabSize, desc);
      if(!mgr->buckets[i])
	 goto out_err2;
      bufSize *= 2;
   }

   return &mgr->base;

out_err2: 
   for (i = 0; i < mgr->numBuckets; ++i)
      if(mgr->buckets[i])
	    mgr->buckets[i]->destroy(mgr->buckets[i]);
   FREE(mgr->buckets);
out_err1: 
   FREE(mgr);
out_err0:
   return NULL;
}