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 "utils/memutils_memorychunk.h"
#include "utils/memutils_internal.h"
Include dependency graph for slab.c:

Go to the source code of this file.

Data Structures

struct  SlabContext
 
struct  SlabBlock
 

Macros

#define Slab_BLOCKHDRSZ   MAXALIGN(sizeof(SlabBlock))
 
#define Slab_CONTEXT_HDRSZ(chunksPerBlock)   sizeof(SlabContext)
 
#define SLAB_BLOCKLIST_COUNT   3
 
#define SLAB_MAXIMUM_EMPTY_BLOCKS   10
 
#define Slab_CHUNKHDRSZ   sizeof(MemoryChunk)
 
#define SlabChunkGetPointer(chk)    ((void *) (((char *) (chk)) + sizeof(MemoryChunk)))
 
#define SlabBlockGetChunk(slab, block, n)
 
#define SlabIsValid(set)   (PointerIsValid(set) && IsA(set, SlabContext))
 
#define SlabBlockIsValid(block)    (PointerIsValid(block) && SlabIsValid((block)->slab))
 

Typedefs

typedef struct SlabContext SlabContext
 
typedef struct SlabBlock SlabBlock
 

Functions

static int32 SlabBlocklistIndex (SlabContext *slab, int nfree)
 
static int32 SlabFindNextBlockListIndex (SlabContext *slab)
 
static MemoryChunkSlabGetNextFreeChunk (SlabContext *slab, SlabBlock *block)
 
MemoryContext SlabContextCreate (MemoryContext parent, const char *name, Size blockSize, Size chunkSize)
 
void SlabReset (MemoryContext context)
 
void SlabDelete (MemoryContext context)
 
void * SlabAlloc (MemoryContext context, Size size)
 
void SlabFree (void *pointer)
 
void * SlabRealloc (void *pointer, Size size)
 
MemoryContext SlabGetChunkContext (void *pointer)
 
Size SlabGetChunkSpace (void *pointer)
 
bool SlabIsEmpty (MemoryContext context)
 
void SlabStats (MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
 

Macro Definition Documentation

◆ Slab_BLOCKHDRSZ

#define Slab_BLOCKHDRSZ   MAXALIGN(sizeof(SlabBlock))

Definition at line 77 of file slab.c.

◆ SLAB_BLOCKLIST_COUNT

#define SLAB_BLOCKLIST_COUNT   3

Definition at line 95 of file slab.c.

◆ Slab_CHUNKHDRSZ

#define Slab_CHUNKHDRSZ   sizeof(MemoryChunk)

Definition at line 157 of file slab.c.

◆ Slab_CONTEXT_HDRSZ

#define Slab_CONTEXT_HDRSZ (   chunksPerBlock)    sizeof(SlabContext)

Definition at line 88 of file slab.c.

◆ SLAB_MAXIMUM_EMPTY_BLOCKS

#define SLAB_MAXIMUM_EMPTY_BLOCKS   10

Definition at line 98 of file slab.c.

◆ SlabBlockGetChunk

#define SlabBlockGetChunk (   slab,
  block,
 
)
Value:
((MemoryChunk *) ((char *) (block) + Slab_BLOCKHDRSZ \
+ ((n) * (slab)->fullChunkSize)))
#define Slab_BLOCKHDRSZ
Definition: slab.c:77

Definition at line 165 of file slab.c.

◆ SlabBlockIsValid

#define SlabBlockIsValid (   block)     (PointerIsValid(block) && SlabIsValid((block)->slab))

Definition at line 202 of file slab.c.

◆ SlabChunkGetPointer

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

Definition at line 158 of file slab.c.

◆ SlabIsValid

#define SlabIsValid (   set)    (PointerIsValid(set) && IsA(set, SlabContext))

Definition at line 196 of file slab.c.

Typedef Documentation

◆ SlabBlock

typedef struct SlabBlock SlabBlock

◆ SlabContext

typedef struct SlabContext SlabContext

Function Documentation

◆ SlabAlloc()

void* SlabAlloc ( MemoryContext  context,
Size  size 
)

Definition at line 499 of file slab.c.

500 {
501  SlabContext *slab = (SlabContext *) context;
502  SlabBlock *block;
503  MemoryChunk *chunk;
504 
505  Assert(SlabIsValid(slab));
506 
507  /* sanity check that this is pointing to a valid blocklist */
508  Assert(slab->curBlocklistIndex >= 0);
510 
511  /* make sure we only allow correct request size */
512  if (unlikely(size != slab->chunkSize))
513  elog(ERROR, "unexpected alloc chunk size %zu (expected %u)",
514  size, slab->chunkSize);
515 
516  /*
517  * Handle the case when there are no partially filled blocks available.
518  * SlabFree() will have updated the curBlocklistIndex setting it to zero
519  * to indicate that it has freed the final block. Also later in
520  * SlabAlloc() we will set the curBlocklistIndex to zero if we end up
521  * filling the final block.
522  */
523  if (unlikely(slab->curBlocklistIndex == 0))
524  {
525  dlist_head *blocklist;
526  int blocklist_idx;
527 
528  /* to save allocating a new one, first check the empty blocks list */
529  if (dclist_count(&slab->emptyblocks) > 0)
530  {
532 
533  block = dlist_container(SlabBlock, node, node);
534 
535  /*
536  * SlabFree() should have left this block in a valid state with
537  * all chunks free. Ensure that's the case.
538  */
539  Assert(block->nfree == slab->chunksPerBlock);
540 
541  /* fetch the next chunk from this block */
542  chunk = SlabGetNextFreeChunk(slab, block);
543  }
544  else
545  {
546  block = (SlabBlock *) malloc(slab->blockSize);
547 
548  if (unlikely(block == NULL))
549  return NULL;
550 
551  block->slab = slab;
552  context->mem_allocated += slab->blockSize;
553 
554  /* use the first chunk in the new block */
555  chunk = SlabBlockGetChunk(slab, block, 0);
556 
557  block->nfree = slab->chunksPerBlock - 1;
558  block->unused = SlabBlockGetChunk(slab, block, 1);
559  block->freehead = NULL;
560  block->nunused = slab->chunksPerBlock - 1;
561  }
562 
563  /* find the blocklist element for storing blocks with 1 used chunk */
564  blocklist_idx = SlabBlocklistIndex(slab, block->nfree);
565  blocklist = &slab->blocklist[blocklist_idx];
566 
567  /* this better be empty. We just added a block thinking it was */
568  Assert(dlist_is_empty(blocklist));
569 
570  dlist_push_head(blocklist, &block->node);
571 
572  slab->curBlocklistIndex = blocklist_idx;
573  }
574  else
575  {
576  dlist_head *blocklist = &slab->blocklist[slab->curBlocklistIndex];
577  int new_blocklist_idx;
578 
579  Assert(!dlist_is_empty(blocklist));
580 
581  /* grab the block from the blocklist */
582  block = dlist_head_element(SlabBlock, node, blocklist);
583 
584  /* make sure we actually got a valid block, with matching nfree */
585  Assert(block != NULL);
586  Assert(slab->curBlocklistIndex == SlabBlocklistIndex(slab, block->nfree));
587  Assert(block->nfree > 0);
588 
589  /* fetch the next chunk from this block */
590  chunk = SlabGetNextFreeChunk(slab, block);
591 
592  /* get the new blocklist index based on the new free chunk count */
593  new_blocklist_idx = SlabBlocklistIndex(slab, block->nfree);
594 
595  /*
596  * Handle the case where the blocklist index changes. This also deals
597  * with blocks becoming full as only full blocks go at index 0.
598  */
599  if (unlikely(slab->curBlocklistIndex != new_blocklist_idx))
600  {
601  dlist_delete_from(blocklist, &block->node);
602  dlist_push_head(&slab->blocklist[new_blocklist_idx], &block->node);
603 
604  if (dlist_is_empty(blocklist))
606  }
607  }
608 
609  /*
610  * Check that the chunk pointer is actually somewhere on the block and is
611  * aligned as expected.
612  */
613  Assert(chunk >= SlabBlockGetChunk(slab, block, 0));
614  Assert(chunk <= SlabBlockGetChunk(slab, block, slab->chunksPerBlock - 1));
615  Assert(SlabChunkMod(slab, block, chunk) == 0);
616 
617  /* Prepare to initialize the chunk header. */
619 
620  MemoryChunkSetHdrMask(chunk, block, MAXALIGN(slab->chunkSize),
621  MCTX_SLAB_ID);
622 #ifdef MEMORY_CONTEXT_CHECKING
623  /* slab mark to catch clobber of "unused" space */
624  Assert(slab->chunkSize < (slab->fullChunkSize - Slab_CHUNKHDRSZ));
625  set_sentinel(MemoryChunkGetPointer(chunk), size);
626  VALGRIND_MAKE_MEM_NOACCESS(((char *) chunk) +
627  Slab_CHUNKHDRSZ + slab->chunkSize,
628  slab->fullChunkSize -
629  (slab->chunkSize + Slab_CHUNKHDRSZ));
630 #endif
631 
632 #ifdef RANDOMIZE_ALLOCATED_MEMORY
633  /* fill the allocated space with junk */
634  randomize_mem((char *) MemoryChunkGetPointer(chunk), size);
635 #endif
636 
637  /* Disallow access to the chunk header. */
639 
640  return MemoryChunkGetPointer(chunk);
641 }
#define MAXALIGN(LEN)
Definition: c.h:800
#define unlikely(x)
Definition: c.h:300
#define ERROR
Definition: elog.h:39
#define malloc(a)
Definition: header.h:50
static void dlist_delete_from(dlist_head *head, dlist_node *node)
Definition: ilist.h:429
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:603
static uint32 dclist_count(const dclist_head *head)
Definition: ilist.h:932
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:347
static bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
static dlist_node * dclist_pop_head_node(dclist_head *head)
Definition: ilist.h:789
Assert(fmt[strlen(fmt) - 1] !='\n')
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
@ MCTX_SLAB_ID
#define MemoryChunkGetPointer(c)
static void MemoryChunkSetHdrMask(MemoryChunk *chunk, void *block, Size value, MemoryContextMethodID methodid)
#define SlabIsValid(set)
Definition: slab.c:196
#define Slab_CHUNKHDRSZ
Definition: slab.c:157
static int32 SlabBlocklistIndex(SlabContext *slab, int nfree)
Definition: slab.c:211
static int32 SlabFindNextBlockListIndex(SlabContext *slab)
Definition: slab.c:251
static MemoryChunk * SlabGetNextFreeChunk(SlabContext *slab, SlabBlock *block)
Definition: slab.c:271
#define SlabBlockGetChunk(slab, block, n)
Definition: slab.c:165
Size mem_allocated
Definition: memnodes.h:87
int32 nfree
Definition: slab.c:149
MemoryChunk * freehead
Definition: slab.c:151
MemoryChunk * unused
Definition: slab.c:152
SlabContext * slab
Definition: slab.c:148
dlist_node node
Definition: slab.c:153
int32 nunused
Definition: slab.c:150
dlist_head blocklist[SLAB_BLOCKLIST_COUNT]
Definition: slab.c:129
int32 chunksPerBlock
Definition: slab.c:110
uint32 fullChunkSize
Definition: slab.c:108
uint32 blockSize
Definition: slab.c:109
int32 curBlocklistIndex
Definition: slab.c:111
uint32 chunkSize
Definition: slab.c:107
dclist_head emptyblocks
Definition: slab.c:120

References Assert(), SlabContext::blocklist, SlabContext::blockSize, SlabContext::chunkSize, SlabContext::chunksPerBlock, SlabContext::curBlocklistIndex, dclist_count(), dclist_pop_head_node(), dlist_container, dlist_delete_from(), dlist_head_element, dlist_is_empty(), dlist_push_head(), elog(), SlabContext::emptyblocks, ERROR, SlabBlock::freehead, SlabContext::fullChunkSize, malloc, MAXALIGN, MCTX_SLAB_ID, MemoryContextData::mem_allocated, MemoryChunkGetPointer, MemoryChunkSetHdrMask(), SlabBlock::nfree, SlabBlock::node, SlabBlock::nunused, SlabBlock::slab, Slab_CHUNKHDRSZ, SlabBlockGetChunk, SlabBlocklistIndex(), SlabFindNextBlockListIndex(), SlabGetNextFreeChunk(), SlabIsValid, unlikely, SlabBlock::unused, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MAKE_MEM_UNDEFINED.

◆ SlabBlocklistIndex()

static int32 SlabBlocklistIndex ( SlabContext slab,
int  nfree 
)
inlinestatic

Definition at line 211 of file slab.c.

212 {
213  int32 index;
214  int32 blocklist_shift = slab->blocklist_shift;
215 
216  Assert(nfree >= 0 && nfree <= slab->chunksPerBlock);
217 
218  /*
219  * Determine the blocklist index based on the number of free chunks. We
220  * must ensure that 0 free chunks is dedicated to index 0. Everything
221  * else must be >= 1 and < SLAB_BLOCKLIST_COUNT.
222  *
223  * To make this as efficient as possible, we exploit some two's complement
224  * arithmetic where we reverse the sign before bit shifting. This results
225  * in an nfree of 0 using index 0 and anything non-zero staying non-zero.
226  * This is exploiting 0 and -0 being the same in two's complement. When
227  * we're done, we just need to flip the sign back over again for a
228  * positive index.
229  */
230  index = -((-nfree) >> blocklist_shift);
231 
232  if (nfree == 0)
233  Assert(index == 0);
234  else
236 
237  return index;
238 }
signed int int32
Definition: c.h:483
#define SLAB_BLOCKLIST_COUNT
Definition: slab.c:95
int32 blocklist_shift
Definition: slab.c:118
Definition: type.h:95

References Assert(), SlabContext::blocklist_shift, and SLAB_BLOCKLIST_COUNT.

Referenced by SlabAlloc(), and SlabFree().

◆ SlabContextCreate()

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

Definition at line 322 of file slab.c.

326 {
327  int chunksPerBlock;
328  Size fullChunkSize;
329  SlabContext *slab;
330  int i;
331 
332  /* ensure MemoryChunk's size is properly maxaligned */
334  "sizeof(MemoryChunk) is not maxaligned");
335  Assert(blockSize <= MEMORYCHUNK_MAX_BLOCKOFFSET);
336 
337  /*
338  * Ensure there's enough space to store the pointer to the next free chunk
339  * in the memory of the (otherwise) unused allocation.
340  */
341  if (chunkSize < sizeof(MemoryChunk *))
342  chunkSize = sizeof(MemoryChunk *);
343 
344  /* length of the maxaligned chunk including the chunk header */
345 #ifdef MEMORY_CONTEXT_CHECKING
346  /* ensure there's always space for the sentinel byte */
347  fullChunkSize = Slab_CHUNKHDRSZ + MAXALIGN(chunkSize + 1);
348 #else
349  fullChunkSize = Slab_CHUNKHDRSZ + MAXALIGN(chunkSize);
350 #endif
351 
352  Assert(fullChunkSize <= MEMORYCHUNK_MAX_VALUE);
353 
354  /* compute the number of chunks that will fit on each block */
355  chunksPerBlock = (blockSize - Slab_BLOCKHDRSZ) / fullChunkSize;
356 
357  /* Make sure the block can store at least one chunk. */
358  if (chunksPerBlock == 0)
359  elog(ERROR, "block size %zu for slab is too small for %zu-byte chunks",
360  blockSize, chunkSize);
361 
362 
363 
364  slab = (SlabContext *) malloc(Slab_CONTEXT_HDRSZ(chunksPerBlock));
365  if (slab == NULL)
366  {
368  ereport(ERROR,
369  (errcode(ERRCODE_OUT_OF_MEMORY),
370  errmsg("out of memory"),
371  errdetail("Failed while creating memory context \"%s\".",
372  name)));
373  }
374 
375  /*
376  * Avoid writing code that can fail between here and MemoryContextCreate;
377  * we'd leak the header if we ereport in this stretch.
378  */
379 
380  /* Fill in SlabContext-specific header fields */
381  slab->chunkSize = (uint32) chunkSize;
382  slab->fullChunkSize = (uint32) fullChunkSize;
383  slab->blockSize = (uint32) blockSize;
384  slab->chunksPerBlock = chunksPerBlock;
385  slab->curBlocklistIndex = 0;
386 
387  /*
388  * Compute a shift that guarantees that shifting chunksPerBlock with it is
389  * < SLAB_BLOCKLIST_COUNT - 1. The reason that we subtract 1 from
390  * SLAB_BLOCKLIST_COUNT in this calculation is that we reserve the 0th
391  * blocklist element for blocks which have no free chunks.
392  *
393  * We calculate the number of bits to shift by rather than a divisor to
394  * divide by as performing division each time we need to find the
395  * blocklist index would be much slower.
396  */
397  slab->blocklist_shift = 0;
398  while ((slab->chunksPerBlock >> slab->blocklist_shift) >= (SLAB_BLOCKLIST_COUNT - 1))
399  slab->blocklist_shift++;
400 
401  /* initialize the list to store empty blocks to be reused */
402  dclist_init(&slab->emptyblocks);
403 
404  /* initialize each blocklist slot */
405  for (i = 0; i < SLAB_BLOCKLIST_COUNT; i++)
406  dlist_init(&slab->blocklist[i]);
407 
408 #ifdef MEMORY_CONTEXT_CHECKING
409  /* set the isChunkFree pointer right after the end of the context */
410  slab->isChunkFree = (bool *) ((char *) slab + sizeof(SlabContext));
411 #endif
412 
413  /* Finally, do the type-independent part of context creation */
415  T_SlabContext,
416  MCTX_SLAB_ID,
417  parent,
418  name);
419 
420  return (MemoryContext) slab;
421 }
unsigned int uint32
Definition: c.h:495
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:925
size_t Size
Definition: c.h:594
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
static void dclist_init(dclist_head *head)
Definition: ilist.h:671
int i
Definition: isn.c:73
void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)
Definition: mcxt.c:973
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:699
#define MEMORYCHUNK_MAX_BLOCKOFFSET
#define MEMORYCHUNK_MAX_VALUE
struct SlabContext SlabContext
#define Slab_CONTEXT_HDRSZ(chunksPerBlock)
Definition: slab.c:88
const char * name

References Assert(), SlabContext::blocklist, SlabContext::blocklist_shift, SlabContext::blockSize, SlabContext::chunkSize, SlabContext::chunksPerBlock, SlabContext::curBlocklistIndex, dclist_init(), dlist_init(), elog(), SlabContext::emptyblocks, ereport, errcode(), errdetail(), errmsg(), ERROR, SlabContext::fullChunkSize, i, malloc, MAXALIGN, MCTX_SLAB_ID, MEMORYCHUNK_MAX_BLOCKOFFSET, MEMORYCHUNK_MAX_VALUE, MemoryContextCreate(), MemoryContextStats(), name, Slab_BLOCKHDRSZ, SLAB_BLOCKLIST_COUNT, Slab_CHUNKHDRSZ, Slab_CONTEXT_HDRSZ, StaticAssertDecl, and TopMemoryContext.

Referenced by ReorderBufferAllocate().

◆ SlabDelete()

void SlabDelete ( MemoryContext  context)

Definition at line 485 of file slab.c.

486 {
487  /* Reset to release all the SlabBlocks */
488  SlabReset(context);
489  /* And free the context header */
490  free(context);
491 }
#define free(a)
Definition: header.h:65
void SlabReset(MemoryContext context)
Definition: slab.c:431

References free, and SlabReset().

◆ SlabFindNextBlockListIndex()

static int32 SlabFindNextBlockListIndex ( SlabContext slab)
static

Definition at line 251 of file slab.c.

252 {
253  /* start at 1 as blocklist[0] is for full blocks. */
254  for (int i = 1; i < SLAB_BLOCKLIST_COUNT; i++)
255  {
256  /* return the first found non-empty index */
257  if (!dlist_is_empty(&slab->blocklist[i]))
258  return i;
259  }
260 
261  /* no blocks with free space */
262  return 0;
263 }

References SlabContext::blocklist, dlist_is_empty(), i, and SLAB_BLOCKLIST_COUNT.

Referenced by SlabAlloc(), and SlabFree().

◆ SlabFree()

void SlabFree ( void *  pointer)

Definition at line 648 of file slab.c.

649 {
650  MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
651  SlabBlock *block;
652  SlabContext *slab;
653  int curBlocklistIdx;
654  int newBlocklistIdx;
655 
656  /* Allow access to the chunk header. */
658 
659  block = MemoryChunkGetBlock(chunk);
660 
661  /*
662  * For speed reasons we just Assert that the referenced block is good.
663  * Future field experience may show that this Assert had better become a
664  * regular runtime test-and-elog check.
665  */
666  Assert(SlabBlockIsValid(block));
667  slab = block->slab;
668 
669 #ifdef MEMORY_CONTEXT_CHECKING
670  /* Test for someone scribbling on unused space in chunk */
671  Assert(slab->chunkSize < (slab->fullChunkSize - Slab_CHUNKHDRSZ));
672  if (!sentinel_ok(pointer, slab->chunkSize))
673  elog(WARNING, "detected write past chunk end in %s %p",
674  slab->header.name, chunk);
675 #endif
676 
677  /* push this chunk onto the head of the block's free list */
678  *(MemoryChunk **) pointer = block->freehead;
679  block->freehead = chunk;
680 
681  block->nfree++;
682 
683  Assert(block->nfree > 0);
684  Assert(block->nfree <= slab->chunksPerBlock);
685 
686 #ifdef CLOBBER_FREED_MEMORY
687  /* don't wipe the free list MemoryChunk pointer stored in the chunk */
688  wipe_mem((char *) pointer + sizeof(MemoryChunk *),
689  slab->chunkSize - sizeof(MemoryChunk *));
690 #endif
691 
692  curBlocklistIdx = SlabBlocklistIndex(slab, block->nfree - 1);
693  newBlocklistIdx = SlabBlocklistIndex(slab, block->nfree);
694 
695  /*
696  * Check if the block needs to be moved to another element on the
697  * blocklist based on it now having 1 more free chunk.
698  */
699  if (unlikely(curBlocklistIdx != newBlocklistIdx))
700  {
701  /* do the move */
702  dlist_delete_from(&slab->blocklist[curBlocklistIdx], &block->node);
703  dlist_push_head(&slab->blocklist[newBlocklistIdx], &block->node);
704 
705  /*
706  * The blocklist[curBlocklistIdx] may now be empty or we may now be
707  * able to use a lower-element blocklist. We'll need to redetermine
708  * what the slab->curBlocklistIndex is if the current blocklist was
709  * changed or if a lower element one was changed. We must ensure we
710  * use the list with the fullest block(s).
711  */
712  if (slab->curBlocklistIndex >= curBlocklistIdx)
713  {
715 
716  /*
717  * We know there must be a block with at least 1 unused chunk as
718  * we just pfree'd one. Ensure curBlocklistIndex reflects this.
719  */
720  Assert(slab->curBlocklistIndex > 0);
721  }
722  }
723 
724  /* Handle when a block becomes completely empty */
725  if (unlikely(block->nfree == slab->chunksPerBlock))
726  {
727  /* remove the block */
728  dlist_delete_from(&slab->blocklist[newBlocklistIdx], &block->node);
729 
730  /*
731  * To avoid thrashing malloc/free, we keep a list of empty blocks that
732  * we can reuse again instead of having to malloc a new one.
733  */
735  dclist_push_head(&slab->emptyblocks, &block->node);
736  else
737  {
738  /*
739  * When we have enough empty blocks stored already, we actually
740  * free the block.
741  */
742 #ifdef CLOBBER_FREED_MEMORY
743  wipe_mem(block, slab->blockSize);
744 #endif
745  free(block);
746  slab->header.mem_allocated -= slab->blockSize;
747  }
748 
749  /*
750  * Check if we need to reset the blocklist index. This is required
751  * when the blocklist this block is on has become completely empty.
752  */
753  if (slab->curBlocklistIndex == newBlocklistIdx &&
754  dlist_is_empty(&slab->blocklist[newBlocklistIdx]))
756  }
757 }
#define WARNING
Definition: elog.h:36
static void dclist_push_head(dclist_head *head, dlist_node *node)
Definition: ilist.h:693
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
static void * MemoryChunkGetBlock(MemoryChunk *chunk)
#define PointerGetMemoryChunk(p)
#define SlabBlockIsValid(block)
Definition: slab.c:202
#define SLAB_MAXIMUM_EMPTY_BLOCKS
Definition: slab.c:98
const char * name
Definition: memnodes.h:93
MemoryContextData header
Definition: slab.c:105

References Assert(), SlabContext::blocklist, SlabContext::blockSize, SlabContext::chunkSize, SlabContext::chunksPerBlock, SlabContext::curBlocklistIndex, dclist_count(), dclist_push_head(), dlist_delete_from(), dlist_is_empty(), dlist_push_head(), elog(), SlabContext::emptyblocks, free, SlabBlock::freehead, SlabContext::fullChunkSize, SlabContext::header, MemoryContextData::mem_allocated, MemoryChunkGetBlock(), MemoryContextData::name, SlabBlock::nfree, SlabBlock::node, PointerGetMemoryChunk, SlabBlock::slab, Slab_CHUNKHDRSZ, SLAB_MAXIMUM_EMPTY_BLOCKS, SlabBlockIsValid, SlabBlocklistIndex(), SlabFindNextBlockListIndex(), unlikely, VALGRIND_MAKE_MEM_DEFINED, and WARNING.

◆ SlabGetChunkContext()

MemoryContext SlabGetChunkContext ( void *  pointer)

Definition at line 810 of file slab.c.

811 {
812  MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
813  SlabBlock *block;
814 
815  /* Allow access to the chunk header. */
817 
818  block = MemoryChunkGetBlock(chunk);
819 
820  /* Disallow access to the chunk header. */
822 
823  Assert(SlabBlockIsValid(block));
824 
825  return &block->slab->header;
826 }

References Assert(), SlabContext::header, MemoryChunkGetBlock(), PointerGetMemoryChunk, SlabBlock::slab, Slab_CHUNKHDRSZ, SlabBlockIsValid, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

◆ SlabGetChunkSpace()

Size SlabGetChunkSpace ( void *  pointer)

Definition at line 834 of file slab.c.

835 {
836  MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
837  SlabBlock *block;
838  SlabContext *slab;
839 
840  /* Allow access to the chunk header. */
842 
843  block = MemoryChunkGetBlock(chunk);
844 
845  /* Disallow access to the chunk header. */
847 
848  Assert(SlabBlockIsValid(block));
849  slab = block->slab;
850 
851  return slab->fullChunkSize;
852 }

References Assert(), SlabContext::fullChunkSize, MemoryChunkGetBlock(), PointerGetMemoryChunk, SlabBlock::slab, Slab_CHUNKHDRSZ, SlabBlockIsValid, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

◆ SlabGetNextFreeChunk()

static MemoryChunk* SlabGetNextFreeChunk ( SlabContext slab,
SlabBlock block 
)
inlinestatic

Definition at line 271 of file slab.c.

272 {
273  MemoryChunk *chunk;
274 
275  Assert(block->nfree > 0);
276 
277  if (block->freehead != NULL)
278  {
279  chunk = block->freehead;
280 
281  /*
282  * Pop the chunk from the linked list of free chunks. The pointer to
283  * the next free chunk is stored in the chunk itself.
284  */
286  block->freehead = *(MemoryChunk **) SlabChunkGetPointer(chunk);
287 
288  /* check nothing stomped on the free chunk's memory */
289  Assert(block->freehead == NULL ||
290  (block->freehead >= SlabBlockGetChunk(slab, block, 0) &&
291  block->freehead <= SlabBlockGetChunk(slab, block, slab->chunksPerBlock - 1) &&
292  SlabChunkMod(slab, block, block->freehead) == 0));
293  }
294  else
295  {
296  Assert(block->nunused > 0);
297 
298  chunk = block->unused;
299  block->unused = (MemoryChunk *) (((char *) block->unused) + slab->fullChunkSize);
300  block->nunused--;
301  }
302 
303  block->nfree--;
304 
305  return chunk;
306 }
#define SlabChunkGetPointer(chk)
Definition: slab.c:158

References Assert(), SlabContext::chunksPerBlock, SlabBlock::freehead, SlabContext::fullChunkSize, SlabBlock::nfree, SlabBlock::nunused, SlabBlockGetChunk, SlabChunkGetPointer, SlabBlock::unused, and VALGRIND_MAKE_MEM_DEFINED.

Referenced by SlabAlloc().

◆ SlabIsEmpty()

bool SlabIsEmpty ( MemoryContext  context)

Definition at line 859 of file slab.c.

860 {
861  Assert(SlabIsValid((SlabContext *) context));
862 
863  return (context->mem_allocated == 0);
864 }

References Assert(), MemoryContextData::mem_allocated, and SlabIsValid.

◆ SlabRealloc()

void* SlabRealloc ( void *  pointer,
Size  size 
)

Definition at line 773 of file slab.c.

774 {
775  MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
776  SlabBlock *block;
777  SlabContext *slab;
778 
779  /* Allow access to the chunk header. */
781 
782  block = MemoryChunkGetBlock(chunk);
783 
784  /* Disallow access to the chunk header. */
786 
787  /*
788  * Try to verify that we have a sane block pointer: the block header
789  * should reference a slab context. (We use a test-and-elog, not just
790  * Assert, because it seems highly likely that we're here in error in the
791  * first place.)
792  */
793  if (!SlabBlockIsValid(block))
794  elog(ERROR, "could not find block containing chunk %p", chunk);
795  slab = block->slab;
796 
797  /* can't do actual realloc with slab, but let's try to be gentle */
798  if (size == slab->chunkSize)
799  return pointer;
800 
801  elog(ERROR, "slab allocator does not support realloc()");
802  return NULL; /* keep compiler quiet */
803 }

References SlabContext::chunkSize, elog(), ERROR, MemoryChunkGetBlock(), PointerGetMemoryChunk, SlabBlock::slab, Slab_CHUNKHDRSZ, SlabBlockIsValid, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

◆ SlabReset()

void SlabReset ( MemoryContext  context)

Definition at line 431 of file slab.c.

432 {
433  SlabContext *slab = (SlabContext *) context;
434  dlist_mutable_iter miter;
435  int i;
436 
437  Assert(SlabIsValid(slab));
438 
439 #ifdef MEMORY_CONTEXT_CHECKING
440  /* Check for corruption and leaks before freeing */
441  SlabCheck(context);
442 #endif
443 
444  /* release any retained empty blocks */
445  dclist_foreach_modify(miter, &slab->emptyblocks)
446  {
447  SlabBlock *block = dlist_container(SlabBlock, node, miter.cur);
448 
449  dclist_delete_from(&slab->emptyblocks, miter.cur);
450 
451 #ifdef CLOBBER_FREED_MEMORY
452  wipe_mem(block, slab->blockSize);
453 #endif
454  free(block);
455  context->mem_allocated -= slab->blockSize;
456  }
457 
458  /* walk over blocklist and free the blocks */
459  for (i = 0; i < SLAB_BLOCKLIST_COUNT; i++)
460  {
461  dlist_foreach_modify(miter, &slab->blocklist[i])
462  {
463  SlabBlock *block = dlist_container(SlabBlock, node, miter.cur);
464 
465  dlist_delete(miter.cur);
466 
467 #ifdef CLOBBER_FREED_MEMORY
468  wipe_mem(block, slab->blockSize);
469 #endif
470  free(block);
471  context->mem_allocated -= slab->blockSize;
472  }
473  }
474 
475  slab->curBlocklistIndex = 0;
476 
477  Assert(context->mem_allocated == 0);
478 }
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:640
static void dclist_delete_from(dclist_head *head, dlist_node *node)
Definition: ilist.h:763
#define dclist_foreach_modify(iter, lhead)
Definition: ilist.h:973
dlist_node * cur
Definition: ilist.h:200

References Assert(), SlabContext::blocklist, SlabContext::blockSize, dlist_mutable_iter::cur, SlabContext::curBlocklistIndex, dclist_delete_from(), dclist_foreach_modify, dlist_container, dlist_delete(), dlist_foreach_modify, SlabContext::emptyblocks, free, i, MemoryContextData::mem_allocated, SLAB_BLOCKLIST_COUNT, and SlabIsValid.

Referenced by SlabDelete().

◆ SlabStats()

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

Definition at line 876 of file slab.c.

880 {
881  SlabContext *slab = (SlabContext *) context;
882  Size nblocks = 0;
883  Size freechunks = 0;
884  Size totalspace;
885  Size freespace = 0;
886  int i;
887 
888  Assert(SlabIsValid(slab));
889 
890  /* Include context header in totalspace */
891  totalspace = Slab_CONTEXT_HDRSZ(slab->chunksPerBlock);
892 
893  /* Add the space consumed by blocks in the emptyblocks list */
894  totalspace += dclist_count(&slab->emptyblocks) * slab->blockSize;
895 
896  for (i = 0; i < SLAB_BLOCKLIST_COUNT; i++)
897  {
898  dlist_iter iter;
899 
900  dlist_foreach(iter, &slab->blocklist[i])
901  {
902  SlabBlock *block = dlist_container(SlabBlock, node, iter.cur);
903 
904  nblocks++;
905  totalspace += slab->blockSize;
906  freespace += slab->fullChunkSize * block->nfree;
907  freechunks += block->nfree;
908  }
909  }
910 
911  if (printfunc)
912  {
913  char stats_string[200];
914 
915  /* XXX should we include free chunks on empty blocks? */
916  snprintf(stats_string, sizeof(stats_string),
917  "%zu total in %zu blocks; %u empty blocks; %zu free (%zu chunks); %zu used",
918  totalspace, nblocks, dclist_count(&slab->emptyblocks),
919  freespace, freechunks, totalspace - freespace);
920  printfunc(context, passthru, stats_string, print_to_stderr);
921  }
922 
923  if (totals)
924  {
925  totals->nblocks += nblocks;
926  totals->freechunks += freechunks;
927  totals->totalspace += totalspace;
928  totals->freespace += freespace;
929  }
930 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:623
#define snprintf
Definition: port.h:238
dlist_node * cur
Definition: ilist.h:179

References Assert(), SlabContext::blocklist, SlabContext::blockSize, SlabContext::chunksPerBlock, dlist_iter::cur, dclist_count(), dlist_container, dlist_foreach, SlabContext::emptyblocks, MemoryContextCounters::freechunks, MemoryContextCounters::freespace, SlabContext::fullChunkSize, i, MemoryContextCounters::nblocks, SlabBlock::nfree, SLAB_BLOCKLIST_COUNT, Slab_CONTEXT_HDRSZ, SlabIsValid, snprintf, and MemoryContextCounters::totalspace.