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_internal.h"
#include "utils/memutils_memorychunk.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)   ((set) && IsA(set, SlabContext))
 
#define SlabBlockIsValid(block)    ((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)
 
static void * SlabAllocSetupNewChunk (MemoryContext context, SlabBlock *block, MemoryChunk *chunk, Size size)
 
static pg_noinline void * SlabAllocFromNewBlock (MemoryContext context, Size size, int flags)
 
pg_noinline static pg_noreturn void SlabAllocInvalidSize (MemoryContext context, Size size)
 
void * SlabAlloc (MemoryContext context, Size size, int flags)
 
void SlabFree (void *pointer)
 
void * SlabRealloc (void *pointer, Size size, int flags)
 
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)     ((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)    ((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,
int  flags 
)

Definition at line 658 of file slab.c.

659{
660 SlabContext *slab = (SlabContext *) context;
661 SlabBlock *block;
662 MemoryChunk *chunk;
663
664 Assert(SlabIsValid(slab));
665
666 /* sanity check that this is pointing to a valid blocklist */
667 Assert(slab->curBlocklistIndex >= 0);
669
670 /*
671 * Make sure we only allow correct request size. This doubles as the
672 * MemoryContextCheckSize check.
673 */
674 if (unlikely(size != slab->chunkSize))
675 SlabAllocInvalidSize(context, size);
676
677 if (unlikely(slab->curBlocklistIndex == 0))
678 {
679 /*
680 * Handle the case when there are no partially filled blocks
681 * available. This happens either when the last allocation took the
682 * last chunk in the block, or when SlabFree() free'd the final block.
683 */
684 return SlabAllocFromNewBlock(context, size, flags);
685 }
686 else
687 {
688 dlist_head *blocklist = &slab->blocklist[slab->curBlocklistIndex];
689 int new_blocklist_idx;
690
691 Assert(!dlist_is_empty(blocklist));
692
693 /* grab the block from the blocklist */
694 block = dlist_head_element(SlabBlock, node, blocklist);
695
696 /* make sure we actually got a valid block, with matching nfree */
697 Assert(block != NULL);
698 Assert(slab->curBlocklistIndex == SlabBlocklistIndex(slab, block->nfree));
699 Assert(block->nfree > 0);
700
701 /* fetch the next chunk from this block */
702 chunk = SlabGetNextFreeChunk(slab, block);
703
704 /* get the new blocklist index based on the new free chunk count */
705 new_blocklist_idx = SlabBlocklistIndex(slab, block->nfree);
706
707 /*
708 * Handle the case where the blocklist index changes. This also deals
709 * with blocks becoming full as only full blocks go at index 0.
710 */
711 if (unlikely(slab->curBlocklistIndex != new_blocklist_idx))
712 {
713 dlist_delete_from(blocklist, &block->node);
714 dlist_push_head(&slab->blocklist[new_blocklist_idx], &block->node);
715
716 if (dlist_is_empty(blocklist))
718 }
719 }
720
721 return SlabAllocSetupNewChunk(context, block, chunk, size);
722}
#define unlikely(x)
Definition: c.h:407
Assert(PointerIsAligned(start, uint64))
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 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
static pg_noinline void * SlabAllocFromNewBlock(MemoryContext context, Size size, int flags)
Definition: slab.c:564
#define SlabIsValid(set)
Definition: slab.c:196
static MemoryChunk * SlabGetNextFreeChunk(SlabContext *slab, SlabBlock *block)
Definition: slab.c:271
static int32 SlabBlocklistIndex(SlabContext *slab, int nfree)
Definition: slab.c:211
static void * SlabAllocSetupNewChunk(MemoryContext context, SlabBlock *block, MemoryChunk *chunk, Size size)
Definition: slab.c:523
pg_noinline static pg_noreturn void SlabAllocInvalidSize(MemoryContext context, Size size)
Definition: slab.c:634
static int32 SlabFindNextBlockListIndex(SlabContext *slab)
Definition: slab.c:251
int32 nfree
Definition: slab.c:149
dlist_node node
Definition: slab.c:153
dlist_head blocklist[SLAB_BLOCKLIST_COUNT]
Definition: slab.c:129
int32 chunksPerBlock
Definition: slab.c:110
int32 curBlocklistIndex
Definition: slab.c:111
uint32 chunkSize
Definition: slab.c:107

References Assert(), SlabContext::blocklist, SlabContext::chunkSize, SlabContext::chunksPerBlock, SlabContext::curBlocklistIndex, dlist_delete_from(), dlist_head_element, dlist_is_empty(), dlist_push_head(), SlabBlock::nfree, SlabBlock::node, SlabAllocFromNewBlock(), SlabAllocInvalidSize(), SlabAllocSetupNewChunk(), SlabBlocklistIndex(), SlabFindNextBlockListIndex(), SlabGetNextFreeChunk(), SlabIsValid, and unlikely.

◆ SlabAllocFromNewBlock()

static pg_noinline void * SlabAllocFromNewBlock ( MemoryContext  context,
Size  size,
int  flags 
)
static

Definition at line 564 of file slab.c.

565{
566 SlabContext *slab = (SlabContext *) context;
567 SlabBlock *block;
568 MemoryChunk *chunk;
569 dlist_head *blocklist;
570 int blocklist_idx;
571
572 /* to save allocating a new one, first check the empty blocks list */
573 if (dclist_count(&slab->emptyblocks) > 0)
574 {
576
577 block = dlist_container(SlabBlock, node, node);
578
579 /*
580 * SlabFree() should have left this block in a valid state with all
581 * chunks free. Ensure that's the case.
582 */
583 Assert(block->nfree == slab->chunksPerBlock);
584
585 /* fetch the next chunk from this block */
586 chunk = SlabGetNextFreeChunk(slab, block);
587 }
588 else
589 {
590 block = (SlabBlock *) malloc(slab->blockSize);
591
592 if (unlikely(block == NULL))
593 return MemoryContextAllocationFailure(context, size, flags);
594
595 /* Make a vchunk covering the new block's header */
597
598 block->slab = slab;
599 context->mem_allocated += slab->blockSize;
600
601 /* use the first chunk in the new block */
602 chunk = SlabBlockGetChunk(slab, block, 0);
603
604 block->nfree = slab->chunksPerBlock - 1;
605 block->unused = SlabBlockGetChunk(slab, block, 1);
606 block->freehead = NULL;
607 block->nunused = slab->chunksPerBlock - 1;
608 }
609
610 /* find the blocklist element for storing blocks with 1 used chunk */
611 blocklist_idx = SlabBlocklistIndex(slab, block->nfree);
612 blocklist = &slab->blocklist[blocklist_idx];
613
614 /* this better be empty. We just added a block thinking it was */
615 Assert(dlist_is_empty(blocklist));
616
617 dlist_push_head(blocklist, &block->node);
618
619 slab->curBlocklistIndex = blocklist_idx;
620
621 return SlabAllocSetupNewChunk(context, block, chunk, size);
622}
#define malloc(a)
Definition: header.h:50
static uint32 dclist_count(const dclist_head *head)
Definition: ilist.h:932
static dlist_node * dclist_pop_head_node(dclist_head *head)
Definition: ilist.h:789
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
void * MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1195
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
#define SlabBlockGetChunk(slab, block, n)
Definition: slab.c:165
MemoryChunk * freehead
Definition: slab.c:151
MemoryChunk * unused
Definition: slab.c:152
SlabContext * slab
Definition: slab.c:148
int32 nunused
Definition: slab.c:150
uint32 blockSize
Definition: slab.c:109
dclist_head emptyblocks
Definition: slab.c:120

References Assert(), SlabContext::blocklist, SlabContext::blockSize, SlabContext::chunksPerBlock, SlabContext::curBlocklistIndex, dclist_count(), dclist_pop_head_node(), dlist_container, dlist_is_empty(), dlist_push_head(), SlabContext::emptyblocks, SlabBlock::freehead, malloc, MemoryContextData::mem_allocated, MemoryContextAllocationFailure(), SlabBlock::nfree, SlabBlock::node, SlabBlock::nunused, SlabBlock::slab, Slab_BLOCKHDRSZ, SlabAllocSetupNewChunk(), SlabBlockGetChunk, SlabBlocklistIndex(), SlabGetNextFreeChunk(), unlikely, SlabBlock::unused, and VALGRIND_MEMPOOL_ALLOC.

Referenced by SlabAlloc().

◆ SlabAllocInvalidSize()

pg_noinline static pg_noreturn void SlabAllocInvalidSize ( MemoryContext  context,
Size  size 
)
static

Definition at line 634 of file slab.c.

635{
636 SlabContext *slab = (SlabContext *) context;
637
638 elog(ERROR, "unexpected alloc chunk size %zu (expected %u)", size,
639 slab->chunkSize);
640}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226

References SlabContext::chunkSize, elog, and ERROR.

Referenced by SlabAlloc().

◆ SlabAllocSetupNewChunk()

static void * SlabAllocSetupNewChunk ( MemoryContext  context,
SlabBlock block,
MemoryChunk chunk,
Size  size 
)
inlinestatic

Definition at line 523 of file slab.c.

525{
526 SlabContext *slab = (SlabContext *) context;
527
528 /*
529 * Check that the chunk pointer is actually somewhere on the block and is
530 * aligned as expected.
531 */
532 Assert(chunk >= SlabBlockGetChunk(slab, block, 0));
533 Assert(chunk <= SlabBlockGetChunk(slab, block, slab->chunksPerBlock - 1));
534 Assert(SlabChunkMod(slab, block, chunk) == 0);
535
536 /* Prepare to initialize the chunk header. */
538
540
541#ifdef MEMORY_CONTEXT_CHECKING
542 /* slab mark to catch clobber of "unused" space */
544 set_sentinel(MemoryChunkGetPointer(chunk), size);
546 slab->chunkSize,
547 slab->fullChunkSize -
548 (slab->chunkSize + Slab_CHUNKHDRSZ));
549#endif
550
551#ifdef RANDOMIZE_ALLOCATED_MEMORY
552 /* fill the allocated space with junk */
553 randomize_mem((char *) MemoryChunkGetPointer(chunk), size);
554#endif
555
556 /* Disallow access to the chunk header. */
558
559 return MemoryChunkGetPointer(chunk);
560}
#define MAXALIGN(LEN)
Definition: c.h:813
#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 Slab_CHUNKHDRSZ
Definition: slab.c:157
uint32 fullChunkSize
Definition: slab.c:108

References Assert(), SlabContext::chunkSize, SlabContext::chunksPerBlock, SlabContext::fullChunkSize, MAXALIGN, MCTX_SLAB_ID, MemoryChunkGetPointer, MemoryChunkSetHdrMask(), Slab_CHUNKHDRSZ, SlabBlockGetChunk, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MAKE_MEM_UNDEFINED.

Referenced by SlabAlloc(), and SlabAllocFromNewBlock().

◆ 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}
int32_t int32
Definition: c.h:537
#define SLAB_BLOCKLIST_COUNT
Definition: slab.c:95
int32 blocklist_shift
Definition: slab.c:118
Definition: type.h:96

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

Referenced by SlabAlloc(), SlabAllocFromNewBlock(), 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");
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 {
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 /* See comments about Valgrind interactions in aset.c */
381 VALGRIND_CREATE_MEMPOOL(slab, 0, false);
382 /* This vchunk covers the SlabContext only */
383 VALGRIND_MEMPOOL_ALLOC(slab, slab, sizeof(SlabContext));
384
385 /* Fill in SlabContext-specific header fields */
386 slab->chunkSize = (uint32) chunkSize;
387 slab->fullChunkSize = (uint32) fullChunkSize;
388 slab->blockSize = (uint32) blockSize;
389 slab->chunksPerBlock = chunksPerBlock;
390 slab->curBlocklistIndex = 0;
391
392 /*
393 * Compute a shift that guarantees that shifting chunksPerBlock with it is
394 * < SLAB_BLOCKLIST_COUNT - 1. The reason that we subtract 1 from
395 * SLAB_BLOCKLIST_COUNT in this calculation is that we reserve the 0th
396 * blocklist element for blocks which have no free chunks.
397 *
398 * We calculate the number of bits to shift by rather than a divisor to
399 * divide by as performing division each time we need to find the
400 * blocklist index would be much slower.
401 */
402 slab->blocklist_shift = 0;
403 while ((slab->chunksPerBlock >> slab->blocklist_shift) >= (SLAB_BLOCKLIST_COUNT - 1))
404 slab->blocklist_shift++;
405
406 /* initialize the list to store empty blocks to be reused */
407 dclist_init(&slab->emptyblocks);
408
409 /* initialize each blocklist slot */
410 for (i = 0; i < SLAB_BLOCKLIST_COUNT; i++)
411 dlist_init(&slab->blocklist[i]);
412
413#ifdef MEMORY_CONTEXT_CHECKING
414 /* set the isChunkFree pointer right after the end of the context */
415 slab->isChunkFree = (bool *) ((char *) slab + sizeof(SlabContext));
416#endif
417
418 /* Finally, do the type-independent part of context creation */
420 T_SlabContext,
422 parent,
423 name);
424
425 return (MemoryContext) slab;
426}
uint32_t uint32
Definition: c.h:541
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:938
size_t Size
Definition: c.h:613
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
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:77
void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)
Definition: mcxt.c:1146
MemoryContext TopMemoryContext
Definition: mcxt.c:166
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:860
#define VALGRIND_CREATE_MEMPOOL(context, redzones, zeroed)
Definition: memdebug.h:24
#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, TopMemoryContext, VALGRIND_CREATE_MEMPOOL, and VALGRIND_MEMPOOL_ALLOC.

Referenced by for(), ReorderBufferAllocate(), and test_random().

◆ SlabDelete()

void SlabDelete ( MemoryContext  context)

Definition at line 506 of file slab.c.

507{
508 /* Reset to release all the SlabBlocks */
509 SlabReset(context);
510
511 /* Destroy the vpool -- see notes in aset.c */
513
514 /* And free the context header */
515 free(context);
516}
#define free(a)
Definition: header.h:65
#define VALGRIND_DESTROY_MEMPOOL(context)
Definition: memdebug.h:25
void SlabReset(MemoryContext context)
Definition: slab.c:436

References free, SlabReset(), and VALGRIND_DESTROY_MEMPOOL.

◆ 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 729 of file slab.c.

730{
731 MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
732 SlabBlock *block;
733 SlabContext *slab;
734 int curBlocklistIdx;
735 int newBlocklistIdx;
736
737 /* Allow access to the chunk header. */
739
740 block = MemoryChunkGetBlock(chunk);
741
742 /*
743 * For speed reasons we just Assert that the referenced block is good.
744 * Future field experience may show that this Assert had better become a
745 * regular runtime test-and-elog check.
746 */
747 Assert(SlabBlockIsValid(block));
748 slab = block->slab;
749
750#ifdef MEMORY_CONTEXT_CHECKING
751 /* Test for someone scribbling on unused space in chunk */
753 if (!sentinel_ok(pointer, slab->chunkSize))
754 elog(WARNING, "detected write past chunk end in %s %p",
755 slab->header.name, chunk);
756#endif
757
758 /* push this chunk onto the head of the block's free list */
759 *(MemoryChunk **) pointer = block->freehead;
760 block->freehead = chunk;
761
762 block->nfree++;
763
764 Assert(block->nfree > 0);
765 Assert(block->nfree <= slab->chunksPerBlock);
766
767#ifdef CLOBBER_FREED_MEMORY
768 /* don't wipe the free list MemoryChunk pointer stored in the chunk */
769 wipe_mem((char *) pointer + sizeof(MemoryChunk *),
770 slab->chunkSize - sizeof(MemoryChunk *));
771#endif
772
773 curBlocklistIdx = SlabBlocklistIndex(slab, block->nfree - 1);
774 newBlocklistIdx = SlabBlocklistIndex(slab, block->nfree);
775
776 /*
777 * Check if the block needs to be moved to another element on the
778 * blocklist based on it now having 1 more free chunk.
779 */
780 if (unlikely(curBlocklistIdx != newBlocklistIdx))
781 {
782 /* do the move */
783 dlist_delete_from(&slab->blocklist[curBlocklistIdx], &block->node);
784 dlist_push_head(&slab->blocklist[newBlocklistIdx], &block->node);
785
786 /*
787 * The blocklist[curBlocklistIdx] may now be empty or we may now be
788 * able to use a lower-element blocklist. We'll need to redetermine
789 * what the slab->curBlocklistIndex is if the current blocklist was
790 * changed or if a lower element one was changed. We must ensure we
791 * use the list with the fullest block(s).
792 */
793 if (slab->curBlocklistIndex >= curBlocklistIdx)
794 {
796
797 /*
798 * We know there must be a block with at least 1 unused chunk as
799 * we just pfree'd one. Ensure curBlocklistIndex reflects this.
800 */
801 Assert(slab->curBlocklistIndex > 0);
802 }
803 }
804
805 /* Handle when a block becomes completely empty */
806 if (unlikely(block->nfree == slab->chunksPerBlock))
807 {
808 /* remove the block */
809 dlist_delete_from(&slab->blocklist[newBlocklistIdx], &block->node);
810
811 /*
812 * To avoid thrashing malloc/free, we keep a list of empty blocks that
813 * we can reuse again instead of having to malloc a new one.
814 */
816 dclist_push_head(&slab->emptyblocks, &block->node);
817 else
818 {
819 /*
820 * When we have enough empty blocks stored already, we actually
821 * free the block.
822 */
823#ifdef CLOBBER_FREED_MEMORY
824 wipe_mem(block, slab->blockSize);
825#endif
826
827 /* As in aset.c, free block-header vchunks explicitly */
828 VALGRIND_MEMPOOL_FREE(slab, block);
829
830 free(block);
831 slab->header.mem_allocated -= slab->blockSize;
832 }
833
834 /*
835 * Check if we need to reset the blocklist index. This is required
836 * when the blocklist this block is on has become completely empty.
837 */
838 if (slab->curBlocklistIndex == newBlocklistIdx &&
839 dlist_is_empty(&slab->blocklist[newBlocklistIdx]))
841 }
842}
#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
#define VALGRIND_MEMPOOL_FREE(context, addr)
Definition: memdebug.h:30
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:131
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, VALGRIND_MEMPOOL_FREE, and WARNING.

◆ SlabGetChunkContext()

MemoryContext SlabGetChunkContext ( void *  pointer)

Definition at line 895 of file slab.c.

896{
897 MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
898 SlabBlock *block;
899
900 /* Allow access to the chunk header. */
902
903 block = MemoryChunkGetBlock(chunk);
904
905 /* Disallow access to the chunk header. */
907
908 Assert(SlabBlockIsValid(block));
909
910 return &block->slab->header;
911}

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 919 of file slab.c.

920{
921 MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
922 SlabBlock *block;
923 SlabContext *slab;
924
925 /* Allow access to the chunk header. */
927
928 block = MemoryChunkGetBlock(chunk);
929
930 /* Disallow access to the chunk header. */
932
933 Assert(SlabBlockIsValid(block));
934 slab = block->slab;
935
936 return slab->fullChunkSize;
937}

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(), and SlabAllocFromNewBlock().

◆ SlabIsEmpty()

bool SlabIsEmpty ( MemoryContext  context)

Definition at line 944 of file slab.c.

945{
946 Assert(SlabIsValid((SlabContext *) context));
947
948 return (context->mem_allocated == 0);
949}

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

◆ SlabRealloc()

void * SlabRealloc ( void *  pointer,
Size  size,
int  flags 
)

Definition at line 858 of file slab.c.

859{
860 MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
861 SlabBlock *block;
862 SlabContext *slab;
863
864 /* Allow access to the chunk header. */
866
867 block = MemoryChunkGetBlock(chunk);
868
869 /* Disallow access to the chunk header. */
871
872 /*
873 * Try to verify that we have a sane block pointer: the block header
874 * should reference a slab context. (We use a test-and-elog, not just
875 * Assert, because it seems highly likely that we're here in error in the
876 * first place.)
877 */
878 if (!SlabBlockIsValid(block))
879 elog(ERROR, "could not find block containing chunk %p", chunk);
880 slab = block->slab;
881
882 /* can't do actual realloc with slab, but let's try to be gentle */
883 if (size == slab->chunkSize)
884 return pointer;
885
886 elog(ERROR, "slab allocator does not support realloc()");
887 return NULL; /* keep compiler quiet */
888}

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 436 of file slab.c.

437{
438 SlabContext *slab = (SlabContext *) context;
439 dlist_mutable_iter miter;
440 int i;
441
442 Assert(SlabIsValid(slab));
443
444#ifdef MEMORY_CONTEXT_CHECKING
445 /* Check for corruption and leaks before freeing */
446 SlabCheck(context);
447#endif
448
449 /* release any retained empty blocks */
451 {
452 SlabBlock *block = dlist_container(SlabBlock, node, miter.cur);
453
454 dclist_delete_from(&slab->emptyblocks, miter.cur);
455
456#ifdef CLOBBER_FREED_MEMORY
457 wipe_mem(block, slab->blockSize);
458#endif
459
460 /* As in aset.c, free block-header vchunks explicitly */
461 VALGRIND_MEMPOOL_FREE(slab, block);
462
463 free(block);
464 context->mem_allocated -= slab->blockSize;
465 }
466
467 /* walk over blocklist and free the blocks */
468 for (i = 0; i < SLAB_BLOCKLIST_COUNT; i++)
469 {
470 dlist_foreach_modify(miter, &slab->blocklist[i])
471 {
472 SlabBlock *block = dlist_container(SlabBlock, node, miter.cur);
473
474 dlist_delete(miter.cur);
475
476#ifdef CLOBBER_FREED_MEMORY
477 wipe_mem(block, slab->blockSize);
478#endif
479
480 /* As in aset.c, free block-header vchunks explicitly */
481 VALGRIND_MEMPOOL_FREE(slab, block);
482
483 free(block);
484 context->mem_allocated -= slab->blockSize;
485 }
486 }
487
488 /*
489 * Instruct Valgrind to throw away all the vchunks associated with this
490 * context, except for the one covering the SlabContext. This gets rid of
491 * the vchunks for whatever user data is getting discarded by the context
492 * reset.
493 */
494 VALGRIND_MEMPOOL_TRIM(slab, slab, sizeof(SlabContext));
495
496 slab->curBlocklistIndex = 0;
497
498 Assert(context->mem_allocated == 0);
499}
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
#define VALGRIND_MEMPOOL_TRIM(context, addr, size)
Definition: memdebug.h:32
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, SlabIsValid, VALGRIND_MEMPOOL_FREE, and VALGRIND_MEMPOOL_TRIM.

Referenced by SlabDelete().

◆ SlabStats()

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

Definition at line 961 of file slab.c.

965{
966 SlabContext *slab = (SlabContext *) context;
967 Size nblocks = 0;
968 Size freechunks = 0;
969 Size totalspace;
970 Size freespace = 0;
971 int i;
972
973 Assert(SlabIsValid(slab));
974
975 /* Include context header in totalspace */
976 totalspace = Slab_CONTEXT_HDRSZ(slab->chunksPerBlock);
977
978 /* Add the space consumed by blocks in the emptyblocks list */
979 totalspace += dclist_count(&slab->emptyblocks) * slab->blockSize;
980
981 for (i = 0; i < SLAB_BLOCKLIST_COUNT; i++)
982 {
983 dlist_iter iter;
984
985 dlist_foreach(iter, &slab->blocklist[i])
986 {
987 SlabBlock *block = dlist_container(SlabBlock, node, iter.cur);
988
989 nblocks++;
990 totalspace += slab->blockSize;
991 freespace += slab->fullChunkSize * block->nfree;
992 freechunks += block->nfree;
993 }
994 }
995
996 if (printfunc)
997 {
998 char stats_string[200];
999
1000 /* XXX should we include free chunks on empty blocks? */
1001 snprintf(stats_string, sizeof(stats_string),
1002 "%zu total in %zu blocks; %u empty blocks; %zu free (%zu chunks); %zu used",
1003 totalspace, nblocks, dclist_count(&slab->emptyblocks),
1004 freespace, freechunks, totalspace - freespace);
1005 printfunc(context, passthru, stats_string, print_to_stderr);
1006 }
1007
1008 if (totals)
1009 {
1010 totals->nblocks += nblocks;
1011 totals->freechunks += freechunks;
1012 totals->totalspace += totalspace;
1013 totals->freespace += freespace;
1014 }
1015}
#define dlist_foreach(iter, lhead)
Definition: ilist.h:623
#define snprintf
Definition: port.h:260
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.