PostgreSQL Source Code  git master
slab.c File Reference
#include "postgres.h"
#include "lib/ilist.h"
#include "utils/memdebug.h"
#include "utils/memutils.h"
Include dependency graph for slab.c:

Go to the source code of this file.

Data Structures

struct  SlabContext
 
struct  SlabBlock
 
struct  SlabChunk
 

Macros

#define SlabPointerGetChunk(ptr)   ((SlabChunk *)(((char *)(ptr)) - sizeof(SlabChunk)))
 
#define SlabChunkGetPointer(chk)   ((void *)(((char *)(chk)) + sizeof(SlabChunk)))
 
#define SlabBlockGetChunk(slab, block, idx)
 
#define SlabBlockStart(block)   ((char *) block + sizeof(SlabBlock))
 
#define SlabChunkIndex(slab, block, chunk)   (((char *) chunk - SlabBlockStart(block)) / slab->fullChunkSize)
 
#define SlabFreeInfo(_cxt, _chunk)
 
#define SlabAllocInfo(_cxt, _chunk)
 

Typedefs

typedef struct SlabContext SlabContext
 
typedef struct SlabBlock SlabBlock
 
typedef struct SlabChunk SlabChunk
 

Functions

static void * SlabAlloc (MemoryContext context, Size size)
 
static void SlabFree (MemoryContext context, void *pointer)
 
static void * SlabRealloc (MemoryContext context, void *pointer, Size size)
 
static void SlabReset (MemoryContext context)
 
static void SlabDelete (MemoryContext context)
 
static Size SlabGetChunkSpace (MemoryContext context, void *pointer)
 
static bool SlabIsEmpty (MemoryContext context)
 
static void SlabStats (MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
 
MemoryContext SlabContextCreate (MemoryContext parent, const char *name, Size blockSize, Size chunkSize)
 

Variables

static const MemoryContextMethods SlabMethods
 

Macro Definition Documentation

◆ SlabAllocInfo

#define SlabAllocInfo (   _cxt,
  _chunk 
)

Definition at line 173 of file slab.c.

Referenced by SlabAlloc().

◆ SlabBlockGetChunk

#define SlabBlockGetChunk (   slab,
  block,
  idx 
)
Value:
((SlabChunk *) ((char *) (block) + sizeof(SlabBlock) \
+ (idx * slab->fullChunkSize)))
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
Definition: slab.c:88

Definition at line 118 of file slab.c.

Referenced by SlabAlloc(), and SlabStats().

◆ SlabBlockStart

#define SlabBlockStart (   block)    ((char *) block + sizeof(SlabBlock))

Definition at line 121 of file slab.c.

◆ SlabChunkGetPointer

#define SlabChunkGetPointer (   chk)    ((void *)(((char *)(chk)) + sizeof(SlabChunk)))

Definition at line 116 of file slab.c.

Referenced by SlabAlloc(), and SlabStats().

◆ SlabChunkIndex

#define SlabChunkIndex (   slab,
  block,
  chunk 
)    (((char *) chunk - SlabBlockStart(block)) / slab->fullChunkSize)

Definition at line 123 of file slab.c.

Referenced by SlabFree().

◆ SlabFreeInfo

#define SlabFreeInfo (   _cxt,
  _chunk 
)

Definition at line 172 of file slab.c.

Referenced by SlabFree().

◆ SlabPointerGetChunk

#define SlabPointerGetChunk (   ptr)    ((SlabChunk *)(((char *)(ptr)) - sizeof(SlabChunk)))

Definition at line 114 of file slab.c.

Referenced by SlabFree().

Typedef Documentation

◆ SlabBlock

typedef struct SlabBlock SlabBlock

◆ SlabChunk

typedef struct SlabChunk SlabChunk

◆ SlabContext

typedef struct SlabContext SlabContext

Function Documentation

◆ SlabAlloc()

static void * SlabAlloc ( MemoryContext  context,
Size  size 
)
static

Definition at line 354 of file slab.c.

References Assert, SlabChunk::block, SlabContext::blockSize, castNode, SlabContext::chunkSize, SlabContext::chunksPerBlock, dlist_delete(), dlist_head_element, dlist_is_empty(), dlist_push_head(), elog, ERROR, SlabBlock::firstFreeChunk, SlabContext::freelist, SlabContext::fullChunkSize, idx(), malloc, MemoryContextData::mem_allocated, SlabContext::minFreeChunks, SlabContext::nblocks, SlabBlock::nfree, SlabBlock::node, SlabChunk::slab, SlabAllocInfo, SlabBlockGetChunk, SlabChunkGetPointer, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MAKE_MEM_UNDEFINED.

355 {
356  SlabContext *slab = castNode(SlabContext, context);
357  SlabBlock *block;
358  SlabChunk *chunk;
359  int idx;
360 
361  Assert(slab);
362 
363  Assert((slab->minFreeChunks >= 0) &&
364  (slab->minFreeChunks < slab->chunksPerBlock));
365 
366  /* make sure we only allow correct request size */
367  if (size != slab->chunkSize)
368  elog(ERROR, "unexpected alloc chunk size %zu (expected %zu)",
369  size, slab->chunkSize);
370 
371  /*
372  * If there are no free chunks in any existing block, create a new block
373  * and put it to the last freelist bucket.
374  *
375  * slab->minFreeChunks == 0 means there are no blocks with free chunks,
376  * thanks to how minFreeChunks is updated at the end of SlabAlloc().
377  */
378  if (slab->minFreeChunks == 0)
379  {
380  block = (SlabBlock *) malloc(slab->blockSize);
381 
382  if (block == NULL)
383  return NULL;
384 
385  block->nfree = slab->chunksPerBlock;
386  block->firstFreeChunk = 0;
387 
388  /*
389  * Put all the chunks on a freelist. Walk the chunks and point each
390  * one to the next one.
391  */
392  for (idx = 0; idx < slab->chunksPerBlock; idx++)
393  {
394  chunk = SlabBlockGetChunk(slab, block, idx);
395  *(int32 *) SlabChunkGetPointer(chunk) = (idx + 1);
396  }
397 
398  /*
399  * And add it to the last freelist with all chunks empty.
400  *
401  * We know there are no blocks in the freelist, otherwise we wouldn't
402  * need a new block.
403  */
405 
406  dlist_push_head(&slab->freelist[slab->chunksPerBlock], &block->node);
407 
408  slab->minFreeChunks = slab->chunksPerBlock;
409  slab->nblocks += 1;
410  context->mem_allocated += slab->blockSize;
411  }
412 
413  /* grab the block from the freelist (even the new block is there) */
414  block = dlist_head_element(SlabBlock, node,
415  &slab->freelist[slab->minFreeChunks]);
416 
417  /* make sure we actually got a valid block, with matching nfree */
418  Assert(block != NULL);
419  Assert(slab->minFreeChunks == block->nfree);
420  Assert(block->nfree > 0);
421 
422  /* we know index of the first free chunk in the block */
423  idx = block->firstFreeChunk;
424 
425  /* make sure the chunk index is valid, and that it's marked as empty */
426  Assert((idx >= 0) && (idx < slab->chunksPerBlock));
427 
428  /* compute the chunk location block start (after the block header) */
429  chunk = SlabBlockGetChunk(slab, block, idx);
430 
431  /*
432  * Update the block nfree count, and also the minFreeChunks as we've
433  * decreased nfree for a block with the minimum number of free chunks
434  * (because that's how we chose the block).
435  */
436  block->nfree--;
437  slab->minFreeChunks = block->nfree;
438 
439  /*
440  * Remove the chunk from the freelist head. The index of the next free
441  * chunk is stored in the chunk itself.
442  */
444  block->firstFreeChunk = *(int32 *) SlabChunkGetPointer(chunk);
445 
446  Assert(block->firstFreeChunk >= 0);
447  Assert(block->firstFreeChunk <= slab->chunksPerBlock);
448 
449  Assert((block->nfree != 0 &&
450  block->firstFreeChunk < slab->chunksPerBlock) ||
451  (block->nfree == 0 &&
452  block->firstFreeChunk == slab->chunksPerBlock));
453 
454  /* move the whole block to the right place in the freelist */
455  dlist_delete(&block->node);
456  dlist_push_head(&slab->freelist[block->nfree], &block->node);
457 
458  /*
459  * And finally update minFreeChunks, i.e. the index to the block with the
460  * lowest number of free chunks. We only need to do that when the block
461  * got full (otherwise we know the current block is the right one). We'll
462  * simply walk the freelist until we find a non-empty entry.
463  */
464  if (slab->minFreeChunks == 0)
465  {
466  for (idx = 1; idx <= slab->chunksPerBlock; idx++)
467  {
468  if (dlist_is_empty(&slab->freelist[idx]))
469  continue;
470 
471  /* found a non-empty freelist */
472  slab->minFreeChunks = idx;
473  break;
474  }
475  }
476 
477  if (slab->minFreeChunks == slab->chunksPerBlock)
478  slab->minFreeChunks = 0;
479 
480  /* Prepare to initialize the chunk header. */
481  VALGRIND_MAKE_MEM_UNDEFINED(chunk, sizeof(SlabChunk));
482 
483  chunk->block = block;
484  chunk->slab = slab;
485 
486 #ifdef MEMORY_CONTEXT_CHECKING
487  /* slab mark to catch clobber of "unused" space */
488  if (slab->chunkSize < (slab->fullChunkSize - sizeof(SlabChunk)))
489  {
490  set_sentinel(SlabChunkGetPointer(chunk), size);
491  VALGRIND_MAKE_MEM_NOACCESS(((char *) chunk) +
492  sizeof(SlabChunk) + slab->chunkSize,
493  slab->fullChunkSize -
494  (slab->chunkSize + sizeof(SlabChunk)));
495  }
496 #endif
497 #ifdef RANDOMIZE_ALLOCATED_MEMORY
498  /* fill the allocated space with junk */
499  randomize_mem((char *) SlabChunkGetPointer(chunk), size);
500 #endif
501 
502  SlabAllocInfo(slab, chunk);
503 
504  Assert(slab->nblocks * slab->blockSize == context->mem_allocated);
505 
506  return SlabChunkGetPointer(chunk);
507 }
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
int nblocks
Definition: slab.c:72
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Size blockSize
Definition: slab.c:68
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
SlabBlock * block
Definition: slab.c:108
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
dlist_head freelist[FLEXIBLE_ARRAY_MEMBER]
Definition: slab.c:77
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
dlist_node node
Definition: slab.c:90
signed int int32
Definition: c.h:345
Size chunkSize
Definition: slab.c:66
#define malloc(a)
Definition: header.h:50
int firstFreeChunk
Definition: slab.c:92
#define ERROR
Definition: elog.h:43
SlabContext * slab
Definition: slab.c:109
int chunksPerBlock
Definition: slab.c:70
Size fullChunkSize
Definition: slab.c:67
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
int nfree
Definition: slab.c:91
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
#define SlabAllocInfo(_cxt, _chunk)
Definition: slab.c:173
#define Assert(condition)
Definition: c.h:728
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
Size mem_allocated
Definition: memnodes.h:82
Definition: slab.c:88
#define SlabBlockGetChunk(slab, block, idx)
Definition: slab.c:118
#define elog(elevel,...)
Definition: elog.h:228
int minFreeChunks
Definition: slab.c:71
#define SlabChunkGetPointer(chk)
Definition: slab.c:116

◆ SlabContextCreate()

MemoryContext SlabContextCreate ( MemoryContext  parent,
const char *  name,
Size  blockSize,
Size  chunkSize 
)

Definition at line 190 of file slab.c.

References SlabContext::blockSize, SlabContext::chunkSize, SlabContext::chunksPerBlock, dlist_init(), elog, ereport, errcode(), errdetail(), errmsg(), ERROR, SlabContext::freelist, SlabContext::fullChunkSize, SlabContext::headerSize, i, malloc, MAXALIGN, MemoryContextCreate(), MemoryContextStats(), offsetof, StaticAssertStmt, T_SlabContext, and TopMemoryContext.

Referenced by ReorderBufferAllocate().

194 {
195  int chunksPerBlock;
196  Size fullChunkSize;
197  Size freelistSize;
198  Size headerSize;
199  SlabContext *slab;
200  int i;
201 
202  /* Assert we padded SlabChunk properly */
203  StaticAssertStmt(sizeof(SlabChunk) == MAXALIGN(sizeof(SlabChunk)),
204  "sizeof(SlabChunk) is not maxaligned");
206  sizeof(SlabChunk),
207  "padding calculation in SlabChunk is wrong");
208 
209  /* Make sure the linked list node fits inside a freed chunk */
210  if (chunkSize < sizeof(int))
211  chunkSize = sizeof(int);
212 
213  /* chunk, including SLAB header (both addresses nicely aligned) */
214  fullChunkSize = sizeof(SlabChunk) + MAXALIGN(chunkSize);
215 
216  /* Make sure the block can store at least one chunk. */
217  if (blockSize < fullChunkSize + sizeof(SlabBlock))
218  elog(ERROR, "block size %zu for slab is too small for %zu chunks",
219  blockSize, chunkSize);
220 
221  /* Compute maximum number of chunks per block */
222  chunksPerBlock = (blockSize - sizeof(SlabBlock)) / fullChunkSize;
223 
224  /* The freelist starts with 0, ends with chunksPerBlock. */
225  freelistSize = sizeof(dlist_head) * (chunksPerBlock + 1);
226 
227  /*
228  * Allocate the context header. Unlike aset.c, we never try to combine
229  * this with the first regular block; not worth the extra complication.
230  */
231 
232  /* Size of the memory context header */
233  headerSize = offsetof(SlabContext, freelist) + freelistSize;
234 
235 #ifdef MEMORY_CONTEXT_CHECKING
236  /*
237  * With memory checking, we need to allocate extra space for the bitmap
238  * of free chunks. The bitmap is an array of bools, so we don't need to
239  * worry about alignment.
240  */
241  headerSize += chunksPerBlock * sizeof(bool);
242 #endif
243 
244  slab = (SlabContext *) malloc(headerSize);
245  if (slab == NULL)
246  {
248  ereport(ERROR,
249  (errcode(ERRCODE_OUT_OF_MEMORY),
250  errmsg("out of memory"),
251  errdetail("Failed while creating memory context \"%s\".",
252  name)));
253  }
254 
255  /*
256  * Avoid writing code that can fail between here and MemoryContextCreate;
257  * we'd leak the header if we ereport in this stretch.
258  */
259 
260  /* Fill in SlabContext-specific header fields */
261  slab->chunkSize = chunkSize;
262  slab->fullChunkSize = fullChunkSize;
263  slab->blockSize = blockSize;
264  slab->headerSize = headerSize;
265  slab->chunksPerBlock = chunksPerBlock;
266  slab->minFreeChunks = 0;
267  slab->nblocks = 0;
268 
269  /* initialize the freelist slots */
270  for (i = 0; i < (slab->chunksPerBlock + 1); i++)
271  dlist_init(&slab->freelist[i]);
272 
273 #ifdef MEMORY_CONTEXT_CHECKING
274  /* set the freechunks pointer right after the freelists array */
275  slab->freechunks
276  = (bool *) slab + offsetof(SlabContext, freelist) + freelistSize;
277 #endif
278 
279  /* Finally, do the type-independent part of context creation */
282  &SlabMethods,
283  parent,
284  name);
285 
286  return (MemoryContext) slab;
287 }
struct dlist_head dlist_head
int errcode(int sqlerrcode)
Definition: elog.c:608
#define malloc(a)
Definition: header.h:50
static const MemoryContextMethods SlabMethods
Definition: slab.c:146
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:842
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:498
int errdetail(const char *fmt,...)
Definition: elog.c:955
void MemoryContextCreate(MemoryContext node, NodeTag tag, const MemoryContextMethods *methods, MemoryContext parent, const char *name)
Definition: mcxt.c:748
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
struct SlabBlock SlabBlock
size_t Size
Definition: c.h:456
#define MAXALIGN(LEN)
Definition: c.h:681
const char * name
Definition: encode.c:521
Definition: slab.c:88
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
struct SlabChunk SlabChunk
#define offsetof(type, field)
Definition: c.h:651
unsigned char bool
Definition: c.h:307

◆ SlabDelete()

static void SlabDelete ( MemoryContext  context)
static

Definition at line 340 of file slab.c.

References free, and SlabReset().

341 {
342  /* Reset to release all the SlabBlocks */
343  SlabReset(context);
344  /* And free the context header */
345  free(context);
346 }
static void SlabReset(MemoryContext context)
Definition: slab.c:297
#define free(a)
Definition: header.h:65

◆ SlabFree()

static void SlabFree ( MemoryContext  context,
void *  pointer 
)
static

Definition at line 514 of file slab.c.

References Assert, SlabChunk::block, SlabContext::blockSize, castNode, SlabContext::chunkSize, SlabContext::chunksPerBlock, dlist_delete(), dlist_is_empty(), dlist_push_head(), elog, SlabBlock::firstFreeChunk, free, SlabContext::freelist, SlabContext::fullChunkSize, SlabContext::header, idx(), MemoryContextData::mem_allocated, SlabContext::minFreeChunks, MemoryContextData::name, SlabContext::nblocks, SlabBlock::nfree, SlabBlock::node, SlabChunkIndex, SlabFreeInfo, SlabPointerGetChunk, and WARNING.

515 {
516  int idx;
517  SlabContext *slab = castNode(SlabContext, context);
518  SlabChunk *chunk = SlabPointerGetChunk(pointer);
519  SlabBlock *block = chunk->block;
520 
521  SlabFreeInfo(slab, chunk);
522 
523 #ifdef MEMORY_CONTEXT_CHECKING
524  /* Test for someone scribbling on unused space in chunk */
525  if (slab->chunkSize < (slab->fullChunkSize - sizeof(SlabChunk)))
526  if (!sentinel_ok(pointer, slab->chunkSize))
527  elog(WARNING, "detected write past chunk end in %s %p",
528  slab->header.name, chunk);
529 #endif
530 
531  /* compute index of the chunk with respect to block start */
532  idx = SlabChunkIndex(slab, block, chunk);
533 
534  /* add chunk to freelist, and update block nfree count */
535  *(int32 *) pointer = block->firstFreeChunk;
536  block->firstFreeChunk = idx;
537  block->nfree++;
538 
539  Assert(block->nfree > 0);
540  Assert(block->nfree <= slab->chunksPerBlock);
541 
542 #ifdef CLOBBER_FREED_MEMORY
543  /* XXX don't wipe the int32 index, used for block-level freelist */
544  wipe_mem((char *) pointer + sizeof(int32),
545  slab->chunkSize - sizeof(int32));
546 #endif
547 
548  /* remove the block from a freelist */
549  dlist_delete(&block->node);
550 
551  /*
552  * See if we need to update the minFreeChunks field for the slab - we only
553  * need to do that if there the block had that number of free chunks
554  * before we freed one. In that case, we check if there still are blocks
555  * in the original freelist and we either keep the current value (if there
556  * still are blocks) or increment it by one (the new block is still the
557  * one with minimum free chunks).
558  *
559  * The one exception is when the block will get completely free - in that
560  * case we will free it, se we can't use it for minFreeChunks. It however
561  * means there are no more blocks with free chunks.
562  */
563  if (slab->minFreeChunks == (block->nfree - 1))
564  {
565  /* Have we removed the last chunk from the freelist? */
566  if (dlist_is_empty(&slab->freelist[slab->minFreeChunks]))
567  {
568  /* but if we made the block entirely free, we'll free it */
569  if (block->nfree == slab->chunksPerBlock)
570  slab->minFreeChunks = 0;
571  else
572  slab->minFreeChunks++;
573  }
574  }
575 
576  /* If the block is now completely empty, free it. */
577  if (block->nfree == slab->chunksPerBlock)
578  {
579  free(block);
580  slab->nblocks--;
581  context->mem_allocated -= slab->blockSize;
582  }
583  else
584  dlist_push_head(&slab->freelist[block->nfree], &block->node);
585 
586  Assert(slab->nblocks >= 0);
587  Assert(slab->nblocks * slab->blockSize == context->mem_allocated);
588 }
MemoryContextData header
Definition: slab.c:64
int nblocks
Definition: slab.c:72
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Size blockSize
Definition: slab.c:68
SlabBlock * block
Definition: slab.c:108
dlist_head freelist[FLEXIBLE_ARRAY_MEMBER]
Definition: slab.c:77
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
dlist_node node
Definition: slab.c:90
#define SlabFreeInfo(_cxt, _chunk)
Definition: slab.c:172
signed int int32
Definition: c.h:345
Size chunkSize
Definition: slab.c:66
int firstFreeChunk
Definition: slab.c:92
int chunksPerBlock
Definition: slab.c:70
Size fullChunkSize
Definition: slab.c:67
#define SlabChunkIndex(slab, block, chunk)
Definition: slab.c:123
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
int nfree
Definition: slab.c:91
#define WARNING
Definition: elog.h:40
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:728
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
Size mem_allocated
Definition: memnodes.h:82
Definition: slab.c:88
#define elog(elevel,...)
Definition: elog.h:228
int minFreeChunks
Definition: slab.c:71
#define SlabPointerGetChunk(ptr)
Definition: slab.c:114
const char * name
Definition: memnodes.h:88

◆ SlabGetChunkSpace()

static Size SlabGetChunkSpace ( MemoryContext  context,
void *  pointer 
)
static

Definition at line 624 of file slab.c.

References Assert, castNode, and SlabContext::fullChunkSize.

625 {
626  SlabContext *slab = castNode(SlabContext, context);
627 
628  Assert(slab);
629 
630  return slab->fullChunkSize;
631 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Size fullChunkSize
Definition: slab.c:67
#define Assert(condition)
Definition: c.h:728

◆ SlabIsEmpty()

static bool SlabIsEmpty ( MemoryContext  context)
static

Definition at line 638 of file slab.c.

References Assert, castNode, and SlabContext::nblocks.

639 {
640  SlabContext *slab = castNode(SlabContext, context);
641 
642  Assert(slab);
643 
644  return (slab->nblocks == 0);
645 }
int nblocks
Definition: slab.c:72
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
#define Assert(condition)
Definition: c.h:728

◆ SlabRealloc()

static void * SlabRealloc ( MemoryContext  context,
void *  pointer,
Size  size 
)
static

Definition at line 604 of file slab.c.

References Assert, castNode, SlabContext::chunkSize, elog, and ERROR.

605 {
606  SlabContext *slab = castNode(SlabContext, context);
607 
608  Assert(slab);
609 
610  /* can't do actual realloc with slab, but let's try to be gentle */
611  if (size == slab->chunkSize)
612  return pointer;
613 
614  elog(ERROR, "slab allocator does not support realloc()");
615  return NULL; /* keep compiler quiet */
616 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Size chunkSize
Definition: slab.c:66
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:728
#define elog(elevel,...)
Definition: elog.h:228

◆ SlabReset()

static void SlabReset ( MemoryContext  context)
static

Definition at line 297 of file slab.c.

References Assert, SlabContext::blockSize, castNode, SlabContext::chunksPerBlock, dlist_mutable_iter::cur, dlist_container, dlist_delete(), dlist_foreach_modify, free, SlabContext::freelist, i, MemoryContextData::mem_allocated, SlabContext::minFreeChunks, and SlabContext::nblocks.

Referenced by SlabDelete().

298 {
299  int i;
300  SlabContext *slab = castNode(SlabContext, context);
301 
302  Assert(slab);
303 
304 #ifdef MEMORY_CONTEXT_CHECKING
305  /* Check for corruption and leaks before freeing */
306  SlabCheck(context);
307 #endif
308 
309  /* walk over freelists and free the blocks */
310  for (i = 0; i <= slab->chunksPerBlock; i++)
311  {
312  dlist_mutable_iter miter;
313 
314  dlist_foreach_modify(miter, &slab->freelist[i])
315  {
316  SlabBlock *block = dlist_container(SlabBlock, node, miter.cur);
317 
318  dlist_delete(miter.cur);
319 
320 #ifdef CLOBBER_FREED_MEMORY
321  wipe_mem(block, slab->blockSize);
322 #endif
323  free(block);
324  slab->nblocks--;
325  context->mem_allocated -= slab->blockSize;
326  }
327  }
328 
329  slab->minFreeChunks = 0;
330 
331  Assert(slab->nblocks == 0);
332  Assert(context->mem_allocated == 0);
333 }
dlist_node * cur
Definition: ilist.h:180
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
int nblocks
Definition: slab.c:72
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Size blockSize
Definition: slab.c:68
dlist_head freelist[FLEXIBLE_ARRAY_MEMBER]
Definition: slab.c:77
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
int chunksPerBlock
Definition: slab.c:70
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:728
Size mem_allocated
Definition: memnodes.h:82
Definition: slab.c:88
int i
int minFreeChunks
Definition: slab.c:71

◆ SlabStats()

static void SlabStats ( MemoryContext  context,
MemoryStatsPrintFunc  printfunc,
void *  passthru,
MemoryContextCounters totals 
)
static

Definition at line 656 of file slab.c.

References Assert, SlabChunk::block, SlabContext::blockSize, castNode, SlabContext::chunkSize, SlabContext::chunksPerBlock, dlist_iter::cur, dlist_container, dlist_foreach, elog, SlabBlock::firstFreeChunk, MemoryContextCounters::freechunks, SlabContext::freelist, MemoryContextCounters::freespace, SlabContext::fullChunkSize, SlabContext::header, SlabContext::headerSize, i, idx(), MemoryContextData::mem_allocated, MemoryContextData::name, name, MemoryContextCounters::nblocks, SlabContext::nblocks, SlabBlock::nfree, SlabChunk::slab, SlabBlockGetChunk, SlabChunkGetPointer, snprintf, MemoryContextCounters::totalspace, VALGRIND_MAKE_MEM_DEFINED, and WARNING.

659 {
660  SlabContext *slab = castNode(SlabContext, context);
661  Size nblocks = 0;
662  Size freechunks = 0;
663  Size totalspace;
664  Size freespace = 0;
665  int i;
666 
667  /* Include context header in totalspace */
668  totalspace = slab->headerSize;
669 
670  for (i = 0; i <= slab->chunksPerBlock; i++)
671  {
672  dlist_iter iter;
673 
674  dlist_foreach(iter, &slab->freelist[i])
675  {
676  SlabBlock *block = dlist_container(SlabBlock, node, iter.cur);
677 
678  nblocks++;
679  totalspace += slab->blockSize;
680  freespace += slab->fullChunkSize * block->nfree;
681  freechunks += block->nfree;
682  }
683  }
684 
685  if (printfunc)
686  {
687  char stats_string[200];
688 
689  snprintf(stats_string, sizeof(stats_string),
690  "%zu total in %zd blocks; %zu free (%zd chunks); %zu used",
691  totalspace, nblocks, freespace, freechunks,
692  totalspace - freespace);
693  printfunc(context, passthru, stats_string);
694  }
695 
696  if (totals)
697  {
698  totals->nblocks += nblocks;
699  totals->freechunks += freechunks;
700  totals->totalspace += totalspace;
701  totals->freespace += freespace;
702  }
703 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Size blockSize
Definition: slab.c:68
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
dlist_head freelist[FLEXIBLE_ARRAY_MEMBER]
Definition: slab.c:77
Size headerSize
Definition: slab.c:69
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
int chunksPerBlock
Definition: slab.c:70
Size fullChunkSize
Definition: slab.c:67
int nfree
Definition: slab.c:91
dlist_node * cur
Definition: ilist.h:161
size_t Size
Definition: c.h:456
Definition: slab.c:88
int i
#define snprintf
Definition: port.h:192

Variable Documentation

◆ SlabMethods

const MemoryContextMethods SlabMethods
static
Initial value:
= {
}
static Size SlabGetChunkSpace(MemoryContext context, void *pointer)
Definition: slab.c:624
static void SlabReset(MemoryContext context)
Definition: slab.c:297
static void * SlabRealloc(MemoryContext context, void *pointer, Size size)
Definition: slab.c:604
static void * SlabAlloc(MemoryContext context, Size size)
Definition: slab.c:354
static bool SlabIsEmpty(MemoryContext context)
Definition: slab.c:638
static void SlabFree(MemoryContext context, void *pointer)
Definition: slab.c:514
static void SlabDelete(MemoryContext context)
Definition: slab.c:340
static void SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
Definition: slab.c:656

Definition at line 146 of file slab.c.