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

Go to the source code of this file.

Data Structures

struct  BumpContext
 
struct  BumpBlock
 

Macros

#define Bump_BLOCKHDRSZ   MAXALIGN(sizeof(BumpBlock))
 
#define Bump_CHUNKHDRSZ   0
 
#define Bump_CHUNK_FRACTION   8
 
#define KeeperBlock(set)
 
#define IsKeeperBlock(set, blk)   (KeeperBlock(set) == (blk))
 
#define BumpIsValid(set)    (PointerIsValid(set) && IsA(set, BumpContext))
 
#define ExternalChunkGetBlock(chunk)    (BumpBlock *) ((char *) chunk - Bump_BLOCKHDRSZ)
 

Typedefs

typedef struct BumpBlock BumpBlock
 
typedef struct BumpContext BumpContext
 

Functions

static void BumpBlockInit (BumpContext *context, BumpBlock *block, Size blksize)
 
static bool BumpBlockIsEmpty (BumpBlock *block)
 
static void BumpBlockMarkEmpty (BumpBlock *block)
 
static Size BumpBlockFreeBytes (BumpBlock *block)
 
static void BumpBlockFree (BumpContext *set, BumpBlock *block)
 
MemoryContext BumpContextCreate (MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
 
void BumpReset (MemoryContext context)
 
void BumpDelete (MemoryContext context)
 
static pg_noinline void * BumpAllocLarge (MemoryContext context, Size size, int flags)
 
static void * BumpAllocChunkFromBlock (MemoryContext context, BumpBlock *block, Size size, Size chunk_size)
 
static pg_noinline void * BumpAllocFromNewBlock (MemoryContext context, Size size, int flags, Size chunk_size)
 
void * BumpAlloc (MemoryContext context, Size size, int flags)
 
void BumpFree (void *pointer)
 
void * BumpRealloc (void *pointer, Size size, int flags)
 
MemoryContext BumpGetChunkContext (void *pointer)
 
Size BumpGetChunkSpace (void *pointer)
 
bool BumpIsEmpty (MemoryContext context)
 
void BumpStats (MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
 

Macro Definition Documentation

◆ Bump_BLOCKHDRSZ

#define Bump_BLOCKHDRSZ   MAXALIGN(sizeof(BumpBlock))

Definition at line 48 of file bump.c.

◆ Bump_CHUNK_FRACTION

#define Bump_CHUNK_FRACTION   8

Definition at line 57 of file bump.c.

◆ Bump_CHUNKHDRSZ

#define Bump_CHUNKHDRSZ   0

Definition at line 54 of file bump.c.

◆ BumpIsValid

#define BumpIsValid (   set)     (PointerIsValid(set) && IsA(set, BumpContext))

Definition at line 100 of file bump.c.

◆ ExternalChunkGetBlock

#define ExternalChunkGetBlock (   chunk)     (BumpBlock *) ((char *) chunk - Bump_BLOCKHDRSZ)

Definition at line 108 of file bump.c.

◆ IsKeeperBlock

#define IsKeeperBlock (   set,
  blk 
)    (KeeperBlock(set) == (blk))

Definition at line 62 of file bump.c.

◆ KeeperBlock

#define KeeperBlock (   set)
Value:
((BumpBlock *) ((char *) (set) + \
MAXALIGN(sizeof(BumpContext))))
#define MAXALIGN(LEN)
Definition: c.h:811
Definition: bump.c:87

Definition at line 60 of file bump.c.

Typedef Documentation

◆ BumpBlock

typedef struct BumpBlock BumpBlock

Definition at line 1 of file bump.c.

◆ BumpContext

typedef struct BumpContext BumpContext

Function Documentation

◆ BumpAlloc()

void* BumpAlloc ( MemoryContext  context,
Size  size,
int  flags 
)

Definition at line 491 of file bump.c.

492 {
493  BumpContext *set = (BumpContext *) context;
494  BumpBlock *block;
495  Size chunk_size;
496  Size required_size;
497 
498  Assert(BumpIsValid(set));
499 
500 #ifdef MEMORY_CONTEXT_CHECKING
501  /* ensure there's always space for the sentinel byte */
502  chunk_size = MAXALIGN(size + 1);
503 #else
504  chunk_size = MAXALIGN(size);
505 #endif
506 
507  /*
508  * If requested size exceeds maximum for chunks we hand the request off to
509  * BumpAllocLarge().
510  */
511  if (chunk_size > set->allocChunkLimit)
512  return BumpAllocLarge(context, size, flags);
513 
514  required_size = chunk_size + Bump_CHUNKHDRSZ;
515 
516  /*
517  * Not an oversized chunk. We try to first make use of the latest block,
518  * but if there's not enough space in it we must allocate a new block.
519  */
520  block = dlist_container(BumpBlock, node, dlist_head_node(&set->blocks));
521 
522  if (BumpBlockFreeBytes(block) < required_size)
523  return BumpAllocFromNewBlock(context, size, flags, chunk_size);
524 
525  /* The current block has space, so just allocate chunk there. */
526  return BumpAllocChunkFromBlock(context, block, size, chunk_size);
527 }
static pg_noinline void * BumpAllocLarge(MemoryContext context, Size size, int flags)
Definition: bump.c:293
#define Bump_CHUNKHDRSZ
Definition: bump.c:54
#define BumpIsValid(set)
Definition: bump.c:100
static void * BumpAllocChunkFromBlock(MemoryContext context, BumpBlock *block, Size size, Size chunk_size)
Definition: bump.c:371
static pg_noinline void * BumpAllocFromNewBlock(MemoryContext context, Size size, int flags, Size chunk_size)
Definition: bump.c:430
static Size BumpBlockFreeBytes(BumpBlock *block)
Definition: bump.c:585
#define Assert(condition)
Definition: c.h:858
size_t Size
Definition: c.h:605
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:565
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
tree context
Definition: radixtree.h:1833
static pg_noinline void Size size
Definition: slab.c:607
dlist_head blocks
Definition: bump.c:76
uint32 allocChunkLimit
Definition: bump.c:74

References BumpContext::allocChunkLimit, Assert, BumpContext::blocks, Bump_CHUNKHDRSZ, BumpAllocChunkFromBlock(), BumpAllocFromNewBlock(), BumpAllocLarge(), BumpBlockFreeBytes(), BumpIsValid, context, dlist_container, dlist_head_node(), MAXALIGN, and size.

◆ BumpAllocChunkFromBlock()

static void* BumpAllocChunkFromBlock ( MemoryContext  context,
BumpBlock block,
Size  size,
Size  chunk_size 
)
inlinestatic

Definition at line 371 of file bump.c.

373 {
374 #ifdef MEMORY_CONTEXT_CHECKING
376 #else
377  void *ptr;
378 #endif
379 
380  /* validate we've been given a block with enough free space */
381  Assert(block != NULL);
382  Assert((block->endptr - block->freeptr) >= Bump_CHUNKHDRSZ + chunk_size);
383 
384 #ifdef MEMORY_CONTEXT_CHECKING
385  chunk = (MemoryChunk *) block->freeptr;
386 #else
387  ptr = (void *) block->freeptr;
388 #endif
389 
390  /* point the freeptr beyond this chunk */
391  block->freeptr += (Bump_CHUNKHDRSZ + chunk_size);
392  Assert(block->freeptr <= block->endptr);
393 
394 #ifdef MEMORY_CONTEXT_CHECKING
395  /* Prepare to initialize the chunk header. */
397 
398  MemoryChunkSetHdrMask(chunk, block, chunk_size, MCTX_BUMP_ID);
399  chunk->requested_size = size;
400  /* set mark to catch clobber of "unused" space */
401  Assert(size < chunk_size);
402  set_sentinel(MemoryChunkGetPointer(chunk), size);
403 
404 #ifdef RANDOMIZE_ALLOCATED_MEMORY
405  /* fill the allocated space with junk */
406  randomize_mem((char *) MemoryChunkGetPointer(chunk), size);
407 #endif
408 
409  /* Ensure any padding bytes are marked NOACCESS. */
411  chunk_size - size);
412 
413  /* Disallow access to the chunk header. */
415 
417 #else
418  return ptr;
419 #endif /* MEMORY_CONTEXT_CHECKING */
420 }
uint64 chunk
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
@ MCTX_BUMP_ID
#define MemoryChunkGetPointer(c)
static void MemoryChunkSetHdrMask(MemoryChunk *chunk, void *block, Size value, MemoryContextMethodID methodid)
char * endptr
Definition: bump.c:93
char * freeptr
Definition: bump.c:92

References Assert, Bump_CHUNKHDRSZ, chunk, BumpBlock::endptr, BumpBlock::freeptr, MCTX_BUMP_ID, MemoryChunkGetPointer, MemoryChunkSetHdrMask(), size, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MAKE_MEM_UNDEFINED.

Referenced by BumpAlloc(), and BumpAllocFromNewBlock().

◆ BumpAllocFromNewBlock()

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

Definition at line 430 of file bump.c.

432 {
433  BumpContext *set = (BumpContext *) context;
434  BumpBlock *block;
435  Size blksize;
436  Size required_size;
437 
438  /*
439  * The first such block has size initBlockSize, and we double the space in
440  * each succeeding block, but not more than maxBlockSize.
441  */
442  blksize = set->nextBlockSize;
443  set->nextBlockSize <<= 1;
444  if (set->nextBlockSize > set->maxBlockSize)
445  set->nextBlockSize = set->maxBlockSize;
446 
447  /* we'll need space for the chunk, chunk hdr and block hdr */
448  required_size = chunk_size + Bump_CHUNKHDRSZ + Bump_BLOCKHDRSZ;
449  /* round the size up to the next power of 2 */
450  if (blksize < required_size)
451  blksize = pg_nextpower2_size_t(required_size);
452 
453  block = (BumpBlock *) malloc(blksize);
454 
455  if (block == NULL)
457 
458  context->mem_allocated += blksize;
459 
460  /* initialize the new block */
461  BumpBlockInit(set, block, blksize);
462 
463  /* add it to the doubly-linked list of blocks */
464  dlist_push_head(&set->blocks, &block->node);
465 
466  return BumpAllocChunkFromBlock(context, block, size, chunk_size);
467 }
static void BumpBlockInit(BumpContext *context, BumpBlock *block, Size blksize)
Definition: bump.c:535
#define Bump_BLOCKHDRSZ
Definition: bump.c:48
#define malloc(a)
Definition: header.h:50
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:347
void * MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1147
#define pg_nextpower2_size_t
Definition: pg_bitutils.h:417
dlist_node node
Definition: bump.c:88
uint32 maxBlockSize
Definition: bump.c:72
uint32 nextBlockSize
Definition: bump.c:73

References BumpContext::blocks, Bump_BLOCKHDRSZ, Bump_CHUNKHDRSZ, BumpAllocChunkFromBlock(), BumpBlockInit(), context, dlist_push_head(), malloc, BumpContext::maxBlockSize, MemoryContextAllocationFailure(), BumpContext::nextBlockSize, BumpBlock::node, pg_nextpower2_size_t, and size.

Referenced by BumpAlloc().

◆ BumpAllocLarge()

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

Definition at line 293 of file bump.c.

294 {
295  BumpContext *set = (BumpContext *) context;
296  BumpBlock *block;
297 #ifdef MEMORY_CONTEXT_CHECKING
299 #endif
300  Size chunk_size;
301  Size required_size;
302  Size blksize;
303 
304  /* validate 'size' is within the limits for the given 'flags' */
306 
307 #ifdef MEMORY_CONTEXT_CHECKING
308  /* ensure there's always space for the sentinel byte */
309  chunk_size = MAXALIGN(size + 1);
310 #else
311  chunk_size = MAXALIGN(size);
312 #endif
313 
314  required_size = chunk_size + Bump_CHUNKHDRSZ;
315  blksize = required_size + Bump_BLOCKHDRSZ;
316 
317  block = (BumpBlock *) malloc(blksize);
318  if (block == NULL)
319  return NULL;
320 
321  context->mem_allocated += blksize;
322 
323  /* the block is completely full */
324  block->freeptr = block->endptr = ((char *) block) + blksize;
325 
326 #ifdef MEMORY_CONTEXT_CHECKING
327  /* block with a single (used) chunk */
328  block->context = set;
329 
330  chunk = (MemoryChunk *) (((char *) block) + Bump_BLOCKHDRSZ);
331 
332  /* mark the MemoryChunk as externally managed */
334 
335  chunk->requested_size = size;
336  /* set mark to catch clobber of "unused" space */
337  Assert(size < chunk_size);
338  set_sentinel(MemoryChunkGetPointer(chunk), size);
339 #endif
340 #ifdef RANDOMIZE_ALLOCATED_MEMORY
341  /* fill the allocated space with junk */
342  randomize_mem((char *) MemoryChunkGetPointer(chunk), size);
343 #endif
344 
345  /*
346  * Add the block to the tail of allocated blocks list. The current block
347  * is left at the head of the list as it may still have space for
348  * non-large allocations.
349  */
350  dlist_push_tail(&set->blocks, &block->node);
351 
352 #ifdef MEMORY_CONTEXT_CHECKING
353  /* Ensure any padding bytes are marked NOACCESS. */
355  chunk_size - size);
356 
357  /* Disallow access to the chunk header. */
359 
361 #else
362  return (void *) (((char *) block) + Bump_BLOCKHDRSZ);
363 #endif
364 }
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
static void MemoryContextCheckSize(MemoryContext context, Size size, int flags)
static void MemoryChunkSetHdrMaskExternal(MemoryChunk *chunk, MemoryContextMethodID methodid)

References Assert, BumpContext::blocks, Bump_BLOCKHDRSZ, Bump_CHUNKHDRSZ, chunk, context, dlist_push_tail(), BumpBlock::endptr, BumpBlock::freeptr, malloc, MAXALIGN, MCTX_BUMP_ID, MemoryChunkGetPointer, MemoryChunkSetHdrMaskExternal(), MemoryContextCheckSize(), BumpBlock::node, size, and VALGRIND_MAKE_MEM_NOACCESS.

Referenced by BumpAlloc().

◆ BumpBlockFree()

static void BumpBlockFree ( BumpContext set,
BumpBlock block 
)
inlinestatic

Definition at line 595 of file bump.c.

596 {
597  /* Make sure nobody tries to free the keeper block */
598  Assert(!IsKeeperBlock(set, block));
599 
600  /* release the block from the list of blocks */
601  dlist_delete(&block->node);
602 
603  ((MemoryContext) set)->mem_allocated -= ((char *) block->endptr - (char *) block);
604 
605 #ifdef CLOBBER_FREED_MEMORY
606  wipe_mem(block, ((char *) block->endptr - (char *) block));
607 #endif
608 
609  free(block);
610 }
#define IsKeeperBlock(set, blk)
Definition: bump.c:62
#define free(a)
Definition: header.h:65
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
struct MemoryContextData * MemoryContext
Definition: palloc.h:36

References Assert, dlist_delete(), BumpBlock::endptr, free, IsKeeperBlock, and BumpBlock::node.

Referenced by BumpReset().

◆ BumpBlockFreeBytes()

static Size BumpBlockFreeBytes ( BumpBlock block)
inlinestatic

Definition at line 585 of file bump.c.

586 {
587  return (block->endptr - block->freeptr);
588 }

References BumpBlock::endptr, and BumpBlock::freeptr.

Referenced by BumpAlloc().

◆ BumpBlockInit()

static void BumpBlockInit ( BumpContext context,
BumpBlock block,
Size  blksize 
)
inlinestatic

Definition at line 535 of file bump.c.

536 {
537 #ifdef MEMORY_CONTEXT_CHECKING
538  block->context = context;
539 #endif
540  block->freeptr = ((char *) block) + Bump_BLOCKHDRSZ;
541  block->endptr = ((char *) block) + blksize;
542 
543  /* Mark unallocated space NOACCESS. */
545 }

References Bump_BLOCKHDRSZ, context, BumpBlock::endptr, BumpBlock::freeptr, and VALGRIND_MAKE_MEM_NOACCESS.

Referenced by BumpAllocFromNewBlock(), and BumpContextCreate().

◆ BumpBlockIsEmpty()

static bool BumpBlockIsEmpty ( BumpBlock block)
inlinestatic

Definition at line 552 of file bump.c.

553 {
554  /* it's empty if the freeptr has not moved */
555  return (block->freeptr == ((char *) block + Bump_BLOCKHDRSZ));
556 }

References Bump_BLOCKHDRSZ, and BumpBlock::freeptr.

Referenced by BumpIsEmpty().

◆ BumpBlockMarkEmpty()

static void BumpBlockMarkEmpty ( BumpBlock block)
inlinestatic

Definition at line 563 of file bump.c.

564 {
565 #if defined(USE_VALGRIND) || defined(CLOBBER_FREED_MEMORY)
566  char *datastart = ((char *) block) + Bump_BLOCKHDRSZ;
567 #endif
568 
569 #ifdef CLOBBER_FREED_MEMORY
570  wipe_mem(datastart, block->freeptr - datastart);
571 #else
572  /* wipe_mem() would have done this */
573  VALGRIND_MAKE_MEM_NOACCESS(datastart, block->freeptr - datastart);
574 #endif
575 
576  /* Reset the block, but don't return it to malloc */
577  block->freeptr = ((char *) block) + Bump_BLOCKHDRSZ;
578 }

References Bump_BLOCKHDRSZ, BumpBlock::freeptr, and VALGRIND_MAKE_MEM_NOACCESS.

Referenced by BumpReset().

◆ BumpContextCreate()

MemoryContext BumpContextCreate ( MemoryContext  parent,
const char *  name,
Size  minContextSize,
Size  initBlockSize,
Size  maxBlockSize 
)

Definition at line 131 of file bump.c.

133 {
134  Size firstBlockSize;
135  Size allocSize;
136  BumpContext *set;
137  BumpBlock *block;
138 
139  /* ensure MemoryChunk's size is properly maxaligned */
141  "sizeof(MemoryChunk) is not maxaligned");
142 
143  /*
144  * First, validate allocation parameters. Asserts seem sufficient because
145  * nobody varies their parameters at runtime. We somewhat arbitrarily
146  * enforce a minimum 1K block size. We restrict the maximum block size to
147  * MEMORYCHUNK_MAX_BLOCKOFFSET as MemoryChunks are limited to this in
148  * regards to addressing the offset between the chunk and the block that
149  * the chunk is stored on. We would be unable to store the offset between
150  * the chunk and block for any chunks that were beyond
151  * MEMORYCHUNK_MAX_BLOCKOFFSET bytes into the block if the block was to be
152  * larger than this.
153  */
154  Assert(initBlockSize == MAXALIGN(initBlockSize) &&
155  initBlockSize >= 1024);
156  Assert(maxBlockSize == MAXALIGN(maxBlockSize) &&
157  maxBlockSize >= initBlockSize &&
158  AllocHugeSizeIsValid(maxBlockSize)); /* must be safe to double */
159  Assert(minContextSize == 0 ||
160  (minContextSize == MAXALIGN(minContextSize) &&
161  minContextSize >= 1024 &&
162  minContextSize <= maxBlockSize));
163  Assert(maxBlockSize <= MEMORYCHUNK_MAX_BLOCKOFFSET);
164 
165  /* Determine size of initial block */
166  allocSize = MAXALIGN(sizeof(BumpContext)) + Bump_BLOCKHDRSZ +
168  if (minContextSize != 0)
169  allocSize = Max(allocSize, minContextSize);
170  else
171  allocSize = Max(allocSize, initBlockSize);
172 
173  /*
174  * Allocate the initial block. Unlike other bump.c blocks, it starts with
175  * the context header and its block header follows that.
176  */
177  set = (BumpContext *) malloc(allocSize);
178  if (set == NULL)
179  {
181  ereport(ERROR,
182  (errcode(ERRCODE_OUT_OF_MEMORY),
183  errmsg("out of memory"),
184  errdetail("Failed while creating memory context \"%s\".",
185  name)));
186  }
187 
188  /*
189  * Avoid writing code that can fail between here and MemoryContextCreate;
190  * we'd leak the header and initial block if we ereport in this stretch.
191  */
192  dlist_init(&set->blocks);
193 
194  /* Fill in the initial block's block header */
195  block = KeeperBlock(set);
196  /* determine the block size and initialize it */
197  firstBlockSize = allocSize - MAXALIGN(sizeof(BumpContext));
198  BumpBlockInit(set, block, firstBlockSize);
199 
200  /* add it to the doubly-linked list of blocks */
201  dlist_push_head(&set->blocks, &block->node);
202 
203  /*
204  * Fill in BumpContext-specific header fields. The Asserts above should
205  * ensure that these all fit inside a uint32.
206  */
207  set->initBlockSize = (uint32) initBlockSize;
208  set->maxBlockSize = (uint32) maxBlockSize;
209  set->nextBlockSize = (uint32) initBlockSize;
210 
211  /*
212  * Compute the allocation chunk size limit for this context.
213  *
214  * Limit the maximum size a non-dedicated chunk can be so that we can fit
215  * at least Bump_CHUNK_FRACTION of chunks this big onto the maximum sized
216  * block. We must further limit this value so that it's no more than
217  * MEMORYCHUNK_MAX_VALUE. We're unable to have non-external chunks larger
218  * than that value as we store the chunk size in the MemoryChunk 'value'
219  * field in the call to MemoryChunkSetHdrMask().
220  */
221  set->allocChunkLimit = Min(maxBlockSize, MEMORYCHUNK_MAX_VALUE);
222  while ((Size) (set->allocChunkLimit + Bump_CHUNKHDRSZ) >
223  (Size) ((Size) (maxBlockSize - Bump_BLOCKHDRSZ) / Bump_CHUNK_FRACTION))
224  set->allocChunkLimit >>= 1;
225 
226  /* Finally, do the type-independent part of context creation */
227  MemoryContextCreate((MemoryContext) set, T_BumpContext, MCTX_BUMP_ID,
228  parent, name);
229 
230  ((MemoryContext) set)->mem_allocated = allocSize;
231 
232  return (MemoryContext) set;
233 }
#define Bump_CHUNK_FRACTION
Definition: bump.c:57
#define KeeperBlock(set)
Definition: bump.c:60
unsigned int uint32
Definition: c.h:506
#define Min(x, y)
Definition: c.h:1004
#define Max(x, y)
Definition: c.h:998
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:936
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)
Definition: mcxt.c:1100
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:814
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:49
#define MEMORYCHUNK_MAX_BLOCKOFFSET
#define MEMORYCHUNK_MAX_VALUE
uint32 initBlockSize
Definition: bump.c:71
const char * name

References BumpContext::allocChunkLimit, AllocHugeSizeIsValid, Assert, BumpContext::blocks, Bump_BLOCKHDRSZ, Bump_CHUNK_FRACTION, Bump_CHUNKHDRSZ, BumpBlockInit(), dlist_init(), dlist_push_head(), ereport, errcode(), errdetail(), errmsg(), ERROR, BumpContext::initBlockSize, KeeperBlock, malloc, Max, MAXALIGN, BumpContext::maxBlockSize, MCTX_BUMP_ID, MEMORYCHUNK_MAX_BLOCKOFFSET, MEMORYCHUNK_MAX_VALUE, MemoryContextCreate(), MemoryContextStats(), Min, name, BumpContext::nextBlockSize, BumpBlock::node, StaticAssertDecl, and TopMemoryContext.

Referenced by TidStoreCreateLocal(), and tuplesort_begin_batch().

◆ BumpDelete()

void BumpDelete ( MemoryContext  context)

Definition at line 278 of file bump.c.

279 {
280  /* Reset to release all releasable BumpBlocks */
282  /* And free the context header and keeper block */
283  free(context);
284 }
void BumpReset(MemoryContext context)
Definition: bump.c:243

References BumpReset(), context, and free.

◆ BumpFree()

void BumpFree ( void *  pointer)

Definition at line 617 of file bump.c.

618 {
619  elog(ERROR, "%s is not supported by the bump memory allocator", "pfree");
620 }
#define elog(elevel,...)
Definition: elog.h:224

References elog, and ERROR.

◆ BumpGetChunkContext()

MemoryContext BumpGetChunkContext ( void *  pointer)

Definition at line 638 of file bump.c.

639 {
640  elog(ERROR, "%s is not supported by the bump memory allocator", "GetMemoryChunkContext");
641  return NULL; /* keep compiler quiet */
642 }

References elog, and ERROR.

◆ BumpGetChunkSpace()

Size BumpGetChunkSpace ( void *  pointer)

Definition at line 649 of file bump.c.

650 {
651  elog(ERROR, "%s is not supported by the bump memory allocator", "GetMemoryChunkSpace");
652  return 0; /* keep compiler quiet */
653 }

References elog, and ERROR.

◆ BumpIsEmpty()

bool BumpIsEmpty ( MemoryContext  context)

Definition at line 660 of file bump.c.

661 {
662  BumpContext *set = (BumpContext *) context;
663  dlist_iter iter;
664 
665  Assert(BumpIsValid(set));
666 
667  dlist_foreach(iter, &set->blocks)
668  {
669  BumpBlock *block = dlist_container(BumpBlock, node, iter.cur);
670 
671  if (!BumpBlockIsEmpty(block))
672  return false;
673  }
674 
675  return true;
676 }
static bool BumpBlockIsEmpty(BumpBlock *block)
Definition: bump.c:552
#define dlist_foreach(iter, lhead)
Definition: ilist.h:623
dlist_node * cur
Definition: ilist.h:179

References Assert, BumpContext::blocks, BumpBlockIsEmpty(), BumpIsValid, context, dlist_iter::cur, dlist_container, and dlist_foreach.

◆ BumpRealloc()

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

Definition at line 627 of file bump.c.

628 {
629  elog(ERROR, "%s is not supported by the bump memory allocator", "realloc");
630  return NULL; /* keep compiler quiet */
631 }

References elog, and ERROR.

◆ BumpReset()

void BumpReset ( MemoryContext  context)

Definition at line 243 of file bump.c.

244 {
245  BumpContext *set = (BumpContext *) context;
246  dlist_mutable_iter miter;
247 
248  Assert(BumpIsValid(set));
249 
250 #ifdef MEMORY_CONTEXT_CHECKING
251  /* Check for corruption and leaks before freeing */
252  BumpCheck(context);
253 #endif
254 
255  dlist_foreach_modify(miter, &set->blocks)
256  {
257  BumpBlock *block = dlist_container(BumpBlock, node, miter.cur);
258 
259  if (IsKeeperBlock(set, block))
260  BumpBlockMarkEmpty(block);
261  else
262  BumpBlockFree(set, block);
263  }
264 
265  /* Reset block size allocation sequence, too */
266  set->nextBlockSize = set->initBlockSize;
267 
268  /* Ensure there is only 1 item in the dlist */
269  Assert(!dlist_is_empty(&set->blocks));
271 }
static void BumpBlockFree(BumpContext *set, BumpBlock *block)
Definition: bump.c:595
static void BumpBlockMarkEmpty(BumpBlock *block)
Definition: bump.c:563
static bool dlist_has_next(const dlist_head *head, const dlist_node *node)
Definition: ilist.h:503
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:640
static bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
dlist_node * cur
Definition: ilist.h:200

References Assert, BumpContext::blocks, BumpBlockFree(), BumpBlockMarkEmpty(), BumpIsValid, context, dlist_mutable_iter::cur, dlist_container, dlist_foreach_modify, dlist_has_next(), dlist_head_node(), dlist_is_empty(), BumpContext::initBlockSize, IsKeeperBlock, and BumpContext::nextBlockSize.

Referenced by BumpDelete().

◆ BumpStats()

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

Definition at line 688 of file bump.c.

690 {
691  BumpContext *set = (BumpContext *) context;
692  Size nblocks = 0;
693  Size totalspace = 0;
694  Size freespace = 0;
695  dlist_iter iter;
696 
697  Assert(BumpIsValid(set));
698 
699  dlist_foreach(iter, &set->blocks)
700  {
701  BumpBlock *block = dlist_container(BumpBlock, node, iter.cur);
702 
703  nblocks++;
704  totalspace += (block->endptr - (char *) block);
705  freespace += (block->endptr - block->freeptr);
706  }
707 
708  if (printfunc)
709  {
710  char stats_string[200];
711 
712  snprintf(stats_string, sizeof(stats_string),
713  "%zu total in %zu blocks; %zu free; %zu used",
714  totalspace, nblocks, freespace, totalspace - freespace);
715  printfunc(context, passthru, stats_string, print_to_stderr);
716  }
717 
718  if (totals)
719  {
720  totals->nblocks += nblocks;
721  totals->totalspace += totalspace;
722  totals->freespace += freespace;
723  }
724 }
#define snprintf
Definition: port.h:238

References Assert, BumpContext::blocks, BumpIsValid, context, dlist_iter::cur, dlist_container, dlist_foreach, BumpBlock::endptr, BumpBlock::freeptr, MemoryContextCounters::freespace, MemoryContextCounters::nblocks, snprintf, and MemoryContextCounters::totalspace.