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)
 

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

◆ 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().

◆ 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 339 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, SlabBlockGetChunk, SlabChunkGetPointer, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MAKE_MEM_UNDEFINED.

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

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

◆ SlabDelete()

static void SlabDelete ( MemoryContext  context)
static

Definition at line 325 of file slab.c.

References free, and SlabReset().

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

◆ SlabFree()

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

Definition at line 497 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, SlabPointerGetChunk, and WARNING.

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

References Assert, castNode, and SlabContext::fullChunkSize.

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

◆ SlabIsEmpty()

static bool SlabIsEmpty ( MemoryContext  context)
static

Definition at line 619 of file slab.c.

References Assert, castNode, and SlabContext::nblocks.

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

◆ SlabRealloc()

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

Definition at line 585 of file slab.c.

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

586 {
587  SlabContext *slab = castNode(SlabContext, context);
588 
589  Assert(slab);
590 
591  /* can't do actual realloc with slab, but let's try to be gentle */
592  if (size == slab->chunkSize)
593  return pointer;
594 
595  elog(ERROR, "slab allocator does not support realloc()");
596  return NULL; /* keep compiler quiet */
597 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
Size chunkSize
Definition: slab.c:66
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:738
#define elog(elevel,...)
Definition: elog.h:214

◆ SlabReset()

static void SlabReset ( MemoryContext  context)
static

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

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

640 {
641  SlabContext *slab = castNode(SlabContext, context);
642  Size nblocks = 0;
643  Size freechunks = 0;
644  Size totalspace;
645  Size freespace = 0;
646  int i;
647 
648  /* Include context header in totalspace */
649  totalspace = slab->headerSize;
650 
651  for (i = 0; i <= slab->chunksPerBlock; i++)
652  {
653  dlist_iter iter;
654 
655  dlist_foreach(iter, &slab->freelist[i])
656  {
657  SlabBlock *block = dlist_container(SlabBlock, node, iter.cur);
658 
659  nblocks++;
660  totalspace += slab->blockSize;
661  freespace += slab->fullChunkSize * block->nfree;
662  freechunks += block->nfree;
663  }
664  }
665 
666  if (printfunc)
667  {
668  char stats_string[200];
669 
670  snprintf(stats_string, sizeof(stats_string),
671  "%zu total in %zd blocks; %zu free (%zd chunks); %zu used",
672  totalspace, nblocks, freespace, freechunks,
673  totalspace - freespace);
674  printfunc(context, passthru, stats_string);
675  }
676 
677  if (totals)
678  {
679  totals->nblocks += nblocks;
680  totals->freechunks += freechunks;
681  totals->totalspace += totalspace;
682  totals->freespace += freespace;
683  }
684 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
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:466
Definition: slab.c:88
int i
#define snprintf
Definition: port.h:193

Variable Documentation

◆ SlabMethods

const MemoryContextMethods SlabMethods
static
Initial value:
= {
}
static Size SlabGetChunkSpace(MemoryContext context, void *pointer)
Definition: slab.c:605
static void SlabReset(MemoryContext context)
Definition: slab.c:282
static void * SlabRealloc(MemoryContext context, void *pointer, Size size)
Definition: slab.c:585
static void * SlabAlloc(MemoryContext context, Size size)
Definition: slab.c:339
static bool SlabIsEmpty(MemoryContext context)
Definition: slab.c:619
static void SlabFree(MemoryContext context, void *pointer)
Definition: slab.c:497
static void SlabDelete(MemoryContext context)
Definition: slab.c:325
static void SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
Definition: slab.c:637

Definition at line 146 of file slab.c.