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 170 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:263
Definition: slab.c:85

Definition at line 115 of file slab.c.

Referenced by SlabAlloc(), and SlabStats().

◆ SlabBlockStart

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

Definition at line 118 of file slab.c.

◆ SlabChunkGetPointer

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

Definition at line 113 of file slab.c.

Referenced by SlabAlloc(), and SlabStats().

◆ SlabChunkIndex

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

Definition at line 120 of file slab.c.

Referenced by SlabFree().

◆ SlabFreeInfo

#define SlabFreeInfo (   _cxt,
  _chunk 
)

Definition at line 169 of file slab.c.

Referenced by SlabFree().

◆ SlabPointerGetChunk

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

Definition at line 111 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 336 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.

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

◆ SlabContextCreate()

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

Definition at line 187 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(), SlabContext::minFreeChunks, SlabContext::nblocks, offsetof, StaticAssertStmt, T_SlabContext, and TopMemoryContext.

Referenced by ReorderBufferAllocate().

191 {
192  int chunksPerBlock;
193  Size fullChunkSize;
194  Size freelistSize;
195  Size headerSize;
196  SlabContext *slab;
197  int i;
198 
199  /* Assert we padded SlabChunk properly */
200  StaticAssertStmt(sizeof(SlabChunk) == MAXALIGN(sizeof(SlabChunk)),
201  "sizeof(SlabChunk) is not maxaligned");
203  sizeof(SlabChunk),
204  "padding calculation in SlabChunk is wrong");
205 
206  /* Make sure the linked list node fits inside a freed chunk */
207  if (chunkSize < sizeof(int))
208  chunkSize = sizeof(int);
209 
210  /* chunk, including SLAB header (both addresses nicely aligned) */
211  fullChunkSize = sizeof(SlabChunk) + MAXALIGN(chunkSize);
212 
213  /* Make sure the block can store at least one chunk. */
214  if (blockSize < fullChunkSize + sizeof(SlabBlock))
215  elog(ERROR, "block size %zu for slab is too small for %zu chunks",
216  blockSize, chunkSize);
217 
218  /* Compute maximum number of chunks per block */
219  chunksPerBlock = (blockSize - sizeof(SlabBlock)) / fullChunkSize;
220 
221  /* The freelist starts with 0, ends with chunksPerBlock. */
222  freelistSize = sizeof(dlist_head) * (chunksPerBlock + 1);
223 
224  /*
225  * Allocate the context header. Unlike aset.c, we never try to combine
226  * this with the first regular block; not worth the extra complication.
227  */
228 
229  /* Size of the memory context header */
230  headerSize = offsetof(SlabContext, freelist) + freelistSize;
231 
232  slab = (SlabContext *) malloc(headerSize);
233  if (slab == NULL)
234  {
236  ereport(ERROR,
237  (errcode(ERRCODE_OUT_OF_MEMORY),
238  errmsg("out of memory"),
239  errdetail("Failed while creating memory context \"%s\".",
240  name)));
241  }
242 
243  /*
244  * Avoid writing code that can fail between here and MemoryContextCreate;
245  * we'd leak the header if we ereport in this stretch.
246  */
247 
248  /* Fill in SlabContext-specific header fields */
249  slab->chunkSize = chunkSize;
250  slab->fullChunkSize = fullChunkSize;
251  slab->blockSize = blockSize;
252  slab->headerSize = headerSize;
253  slab->chunksPerBlock = chunksPerBlock;
254  slab->minFreeChunks = 0;
255  slab->nblocks = 0;
256 
257  /* initialize the freelist slots */
258  for (i = 0; i < (slab->chunksPerBlock + 1); i++)
259  dlist_init(&slab->freelist[i]);
260 
261  /* Finally, do the type-independent part of context creation */
264  &SlabMethods,
265  parent,
266  name);
267 
268  return (MemoryContext) slab;
269 }
int nblocks
Definition: slab.c:72
Size blockSize
Definition: slab.c:68
struct dlist_head dlist_head
int errcode(int sqlerrcode)
Definition: elog.c:608
dlist_head freelist[FLEXIBLE_ARRAY_MEMBER]
Definition: slab.c:74
Size chunkSize
Definition: slab.c:66
#define malloc(a)
Definition: header.h:50
static const MemoryContextMethods SlabMethods
Definition: slab.c:143
Size headerSize
Definition: slab.c:69
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:843
#define ERROR
Definition: elog.h:43
int chunksPerBlock
Definition: slab.c:70
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:498
Size fullChunkSize
Definition: slab.c:67
int errdetail(const char *fmt,...)
Definition: elog.c:957
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:467
#define MAXALIGN(LEN)
Definition: c.h:686
const char * name
Definition: encode.c:521
Definition: slab.c:85
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
int minFreeChunks
Definition: slab.c:71
struct SlabChunk SlabChunk
#define offsetof(type, field)
Definition: c.h:656

◆ SlabDelete()

static void SlabDelete ( MemoryContext  context)
static

Definition at line 322 of file slab.c.

References free, and SlabReset().

323 {
324  /* Reset to release all the SlabBlocks */
325  SlabReset(context);
326  /* And free the context header */
327  free(context);
328 }
static void SlabReset(MemoryContext context)
Definition: slab.c:279
#define free(a)
Definition: header.h:65

◆ SlabFree()

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

Definition at line 496 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.

497 {
498  int idx;
499  SlabContext *slab = castNode(SlabContext, context);
500  SlabChunk *chunk = SlabPointerGetChunk(pointer);
501  SlabBlock *block = chunk->block;
502 
503  SlabFreeInfo(slab, chunk);
504 
505 #ifdef MEMORY_CONTEXT_CHECKING
506  /* Test for someone scribbling on unused space in chunk */
507  if (slab->chunkSize < (slab->fullChunkSize - sizeof(SlabChunk)))
508  if (!sentinel_ok(pointer, slab->chunkSize))
509  elog(WARNING, "detected write past chunk end in %s %p",
510  slab->header.name, chunk);
511 #endif
512 
513  /* compute index of the chunk with respect to block start */
514  idx = SlabChunkIndex(slab, block, chunk);
515 
516  /* add chunk to freelist, and update block nfree count */
517  *(int32 *) pointer = block->firstFreeChunk;
518  block->firstFreeChunk = idx;
519  block->nfree++;
520 
521  Assert(block->nfree > 0);
522  Assert(block->nfree <= slab->chunksPerBlock);
523 
524 #ifdef CLOBBER_FREED_MEMORY
525  /* XXX don't wipe the int32 index, used for block-level freelist */
526  wipe_mem((char *) pointer + sizeof(int32),
527  slab->chunkSize - sizeof(int32));
528 #endif
529 
530  /* remove the block from a freelist */
531  dlist_delete(&block->node);
532 
533  /*
534  * See if we need to update the minFreeChunks field for the slab - we only
535  * need to do that if there the block had that number of free chunks
536  * before we freed one. In that case, we check if there still are blocks
537  * in the original freelist and we either keep the current value (if there
538  * still are blocks) or increment it by one (the new block is still the
539  * one with minimum free chunks).
540  *
541  * The one exception is when the block will get completely free - in that
542  * case we will free it, se we can't use it for minFreeChunks. It however
543  * means there are no more blocks with free chunks.
544  */
545  if (slab->minFreeChunks == (block->nfree - 1))
546  {
547  /* Have we removed the last chunk from the freelist? */
548  if (dlist_is_empty(&slab->freelist[slab->minFreeChunks]))
549  {
550  /* but if we made the block entirely free, we'll free it */
551  if (block->nfree == slab->chunksPerBlock)
552  slab->minFreeChunks = 0;
553  else
554  slab->minFreeChunks++;
555  }
556  }
557 
558  /* If the block is now completely empty, free it. */
559  if (block->nfree == slab->chunksPerBlock)
560  {
561  free(block);
562  slab->nblocks--;
563  context->mem_allocated -= slab->blockSize;
564  }
565  else
566  dlist_push_head(&slab->freelist[block->nfree], &block->node);
567 
568  Assert(slab->nblocks >= 0);
569  Assert(slab->nblocks * slab->blockSize == context->mem_allocated);
570 }
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:105
dlist_head freelist[FLEXIBLE_ARRAY_MEMBER]
Definition: slab.c:74
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:263
dlist_node node
Definition: slab.c:87
#define SlabFreeInfo(_cxt, _chunk)
Definition: slab.c:169
signed int int32
Definition: c.h:347
Size chunkSize
Definition: slab.c:66
int firstFreeChunk
Definition: slab.c:89
int chunksPerBlock
Definition: slab.c:70
Size fullChunkSize
Definition: slab.c:67
#define SlabChunkIndex(slab, block, chunk)
Definition: slab.c:120
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
int nfree
Definition: slab.c:88
#define WARNING
Definition: elog.h:40
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:733
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
Size mem_allocated
Definition: memnodes.h:82
Definition: slab.c:85
#define elog(elevel,...)
Definition: elog.h:228
int minFreeChunks
Definition: slab.c:71
#define SlabPointerGetChunk(ptr)
Definition: slab.c:111
const char * name
Definition: memnodes.h:88

◆ SlabGetChunkSpace()

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

Definition at line 606 of file slab.c.

References Assert, castNode, and SlabContext::fullChunkSize.

607 {
608  SlabContext *slab = castNode(SlabContext, context);
609 
610  Assert(slab);
611 
612  return slab->fullChunkSize;
613 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Size fullChunkSize
Definition: slab.c:67
#define Assert(condition)
Definition: c.h:733

◆ SlabIsEmpty()

static bool SlabIsEmpty ( MemoryContext  context)
static

Definition at line 620 of file slab.c.

References Assert, castNode, and SlabContext::nblocks.

621 {
622  SlabContext *slab = castNode(SlabContext, context);
623 
624  Assert(slab);
625 
626  return (slab->nblocks == 0);
627 }
int nblocks
Definition: slab.c:72
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
#define Assert(condition)
Definition: c.h:733

◆ SlabRealloc()

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

Definition at line 586 of file slab.c.

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

587 {
588  SlabContext *slab = castNode(SlabContext, context);
589 
590  Assert(slab);
591 
592  /* can't do actual realloc with slab, but let's try to be gentle */
593  if (size == slab->chunkSize)
594  return pointer;
595 
596  elog(ERROR, "slab allocator does not support realloc()");
597  return NULL; /* keep compiler quiet */
598 }
#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:733
#define elog(elevel,...)
Definition: elog.h:228

◆ SlabReset()

static void SlabReset ( MemoryContext  context)
static

Definition at line 279 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().

280 {
281  int i;
282  SlabContext *slab = castNode(SlabContext, context);
283 
284  Assert(slab);
285 
286 #ifdef MEMORY_CONTEXT_CHECKING
287  /* Check for corruption and leaks before freeing */
288  SlabCheck(context);
289 #endif
290 
291  /* walk over freelists and free the blocks */
292  for (i = 0; i <= slab->chunksPerBlock; i++)
293  {
294  dlist_mutable_iter miter;
295 
296  dlist_foreach_modify(miter, &slab->freelist[i])
297  {
298  SlabBlock *block = dlist_container(SlabBlock, node, miter.cur);
299 
300  dlist_delete(miter.cur);
301 
302 #ifdef CLOBBER_FREED_MEMORY
303  wipe_mem(block, slab->blockSize);
304 #endif
305  free(block);
306  slab->nblocks--;
307  context->mem_allocated -= slab->blockSize;
308  }
309  }
310 
311  slab->minFreeChunks = 0;
312 
313  Assert(slab->nblocks == 0);
314  Assert(context->mem_allocated == 0);
315 }
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:74
#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:733
Size mem_allocated
Definition: memnodes.h:82
Definition: slab.c:85
int i
int minFreeChunks
Definition: slab.c:71

◆ SlabStats()

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

Definition at line 638 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, palloc(), SlabChunk::slab, SlabBlockGetChunk, SlabChunkGetPointer, snprintf, MemoryContextCounters::totalspace, VALGRIND_MAKE_MEM_DEFINED, and WARNING.

641 {
642  SlabContext *slab = castNode(SlabContext, context);
643  Size nblocks = 0;
644  Size freechunks = 0;
645  Size totalspace;
646  Size freespace = 0;
647  int i;
648 
649  /* Include context header in totalspace */
650  totalspace = slab->headerSize;
651 
652  for (i = 0; i <= slab->chunksPerBlock; i++)
653  {
654  dlist_iter iter;
655 
656  dlist_foreach(iter, &slab->freelist[i])
657  {
658  SlabBlock *block = dlist_container(SlabBlock, node, iter.cur);
659 
660  nblocks++;
661  totalspace += slab->blockSize;
662  freespace += slab->fullChunkSize * block->nfree;
663  freechunks += block->nfree;
664  }
665  }
666 
667  if (printfunc)
668  {
669  char stats_string[200];
670 
671  snprintf(stats_string, sizeof(stats_string),
672  "%zu total in %zd blocks; %zu free (%zd chunks); %zu used",
673  totalspace, nblocks, freespace, freechunks,
674  totalspace - freespace);
675  printfunc(context, passthru, stats_string);
676  }
677 
678  if (totals)
679  {
680  totals->nblocks += nblocks;
681  totals->freechunks += freechunks;
682  totals->totalspace += totalspace;
683  totals->freespace += freespace;
684  }
685 }
#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:74
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:88
dlist_node * cur
Definition: ilist.h:161
size_t Size
Definition: c.h:467
Definition: slab.c:85
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:606
static void SlabReset(MemoryContext context)
Definition: slab.c:279
static void * SlabRealloc(MemoryContext context, void *pointer, Size size)
Definition: slab.c:586
static void * SlabAlloc(MemoryContext context, Size size)
Definition: slab.c:336
static bool SlabIsEmpty(MemoryContext context)
Definition: slab.c:620
static void SlabFree(MemoryContext context, void *pointer)
Definition: slab.c:496
static void SlabDelete(MemoryContext context)
Definition: slab.c:322
static void SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
Definition: slab.c:638

Definition at line 143 of file slab.c.