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

Go to the source code of this file.

Data Structures

struct  GenerationContext
 
struct  GenerationBlock
 
struct  GenerationChunk
 

Macros

#define Generation_BLOCKHDRSZ   MAXALIGN(sizeof(GenerationBlock))
 
#define Generation_CHUNKHDRSZ   sizeof(GenerationChunk)
 
#define GENERATIONCHUNK_RAWSIZE   (SIZEOF_SIZE_T + SIZEOF_VOID_P * 2)
 
#define GENERATIONCHUNK_PRIVATE_LEN   offsetof(GenerationChunk, context)
 
#define GenerationIsValid(set)   PointerIsValid(set)
 
#define GenerationPointerGetChunk(ptr)   ((GenerationChunk *)(((char *)(ptr)) - Generation_CHUNKHDRSZ))
 
#define GenerationChunkGetPointer(chk)   ((GenerationPointer *)(((char *)(chk)) + Generation_CHUNKHDRSZ))
 
#define GenerationFreeInfo(_cxt, _chunk)
 
#define GenerationAllocInfo(_cxt, _chunk)
 

Typedefs

typedef struct GenerationBlock GenerationBlock
 
typedef struct GenerationChunk GenerationChunk
 
typedef void * GenerationPointer
 
typedef struct GenerationContext GenerationContext
 

Functions

static void * GenerationAlloc (MemoryContext context, Size size)
 
static void GenerationFree (MemoryContext context, void *pointer)
 
static void * GenerationRealloc (MemoryContext context, void *pointer, Size size)
 
static void GenerationReset (MemoryContext context)
 
static void GenerationDelete (MemoryContext context)
 
static Size GenerationGetChunkSpace (MemoryContext context, void *pointer)
 
static bool GenerationIsEmpty (MemoryContext context)
 
static void GenerationStats (MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
 
MemoryContext GenerationContextCreate (MemoryContext parent, const char *name, Size blockSize)
 

Variables

static const MemoryContextMethods GenerationMethods
 

Macro Definition Documentation

◆ Generation_BLOCKHDRSZ

#define Generation_BLOCKHDRSZ   MAXALIGN(sizeof(GenerationBlock))

Definition at line 46 of file generation.c.

Referenced by GenerationAlloc(), and GenerationStats().

◆ Generation_CHUNKHDRSZ

#define Generation_CHUNKHDRSZ   sizeof(GenerationChunk)

◆ GenerationAllocInfo

#define GenerationAllocInfo (   _cxt,
  _chunk 
)

Definition at line 194 of file generation.c.

Referenced by GenerationAlloc().

◆ GENERATIONCHUNK_PRIVATE_LEN

#define GENERATIONCHUNK_PRIVATE_LEN   offsetof(GenerationChunk, context)

◆ GENERATIONCHUNK_RAWSIZE

#define GENERATIONCHUNK_RAWSIZE   (SIZEOF_SIZE_T + SIZEOF_VOID_P * 2)

Definition at line 114 of file generation.c.

◆ GenerationChunkGetPointer

#define GenerationChunkGetPointer (   chk)    ((GenerationPointer *)(((char *)(chk)) + Generation_CHUNKHDRSZ))

Definition at line 143 of file generation.c.

Referenced by GenerationAlloc().

◆ GenerationFreeInfo

#define GenerationFreeInfo (   _cxt,
  _chunk 
)

Definition at line 193 of file generation.c.

◆ GenerationIsValid

#define GenerationIsValid (   set)    PointerIsValid(set)

Definition at line 139 of file generation.c.

Referenced by GenerationReset().

◆ GenerationPointerGetChunk

#define GenerationPointerGetChunk (   ptr)    ((GenerationChunk *)(((char *)(ptr)) - Generation_CHUNKHDRSZ))

Definition at line 141 of file generation.c.

Referenced by GenerationFree(), GenerationGetChunkSpace(), and GenerationRealloc().

Typedef Documentation

◆ GenerationBlock

Definition at line 49 of file generation.c.

◆ GenerationChunk

Definition at line 50 of file generation.c.

◆ GenerationContext

◆ GenerationPointer

typedef void* GenerationPointer

Definition at line 52 of file generation.c.

Function Documentation

◆ GenerationAlloc()

static void * GenerationAlloc ( MemoryContext  context,
Size  size 
)
static

Definition at line 341 of file generation.c.

References Assert, GenerationBlock::blksize, GenerationContext::block, GenerationChunk::block, GenerationChunk::context, dlist_push_head(), GenerationBlock::endptr, GenerationBlock::freeptr, Generation_BLOCKHDRSZ, Generation_CHUNKHDRSZ, GenerationAllocInfo, GENERATIONCHUNK_PRIVATE_LEN, GenerationChunkGetPointer, malloc, MAXALIGN, MemoryContextData::mem_allocated, GenerationBlock::nchunks, GenerationBlock::nfree, GenerationBlock::node, GenerationChunk::size, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MAKE_MEM_UNDEFINED.

Referenced by GenerationRealloc().

342 {
343  GenerationContext *set = (GenerationContext *) context;
344  GenerationBlock *block;
345  GenerationChunk *chunk;
346  Size chunk_size = MAXALIGN(size);
347 
348  /* is it an over-sized chunk? if yes, allocate special block */
349  if (chunk_size > set->blockSize / 8)
350  {
351  Size blksize = chunk_size + Generation_BLOCKHDRSZ + Generation_CHUNKHDRSZ;
352 
353  block = (GenerationBlock *) malloc(blksize);
354  if (block == NULL)
355  return NULL;
356 
357  context->mem_allocated += blksize;
358 
359  /* block with a single (used) chunk */
360  block->blksize = blksize;
361  block->nchunks = 1;
362  block->nfree = 0;
363 
364  /* the block is completely full */
365  block->freeptr = block->endptr = ((char *) block) + blksize;
366 
367  chunk = (GenerationChunk *) (((char *) block) + Generation_BLOCKHDRSZ);
368  chunk->block = block;
369  chunk->context = set;
370  chunk->size = chunk_size;
371 
372 #ifdef MEMORY_CONTEXT_CHECKING
373  chunk->requested_size = size;
374  /* set mark to catch clobber of "unused" space */
375  if (size < chunk_size)
376  set_sentinel(GenerationChunkGetPointer(chunk), size);
377 #endif
378 #ifdef RANDOMIZE_ALLOCATED_MEMORY
379  /* fill the allocated space with junk */
380  randomize_mem((char *) GenerationChunkGetPointer(chunk), size);
381 #endif
382 
383  /* add the block to the list of allocated blocks */
384  dlist_push_head(&set->blocks, &block->node);
385 
386  GenerationAllocInfo(set, chunk);
387 
388  /* Ensure any padding bytes are marked NOACCESS. */
390  chunk_size - size);
391 
392  /* Disallow external access to private part of chunk header. */
394 
395  return GenerationChunkGetPointer(chunk);
396  }
397 
398  /*
399  * Not an over-sized chunk. Is there enough space in the current block? If
400  * not, allocate a new "regular" block.
401  */
402  block = set->block;
403 
404  if ((block == NULL) ||
405  (block->endptr - block->freeptr) < Generation_CHUNKHDRSZ + chunk_size)
406  {
407  Size blksize = set->blockSize;
408 
409  block = (GenerationBlock *) malloc(blksize);
410 
411  if (block == NULL)
412  return NULL;
413 
414  context->mem_allocated += blksize;
415 
416  block->blksize = blksize;
417  block->nchunks = 0;
418  block->nfree = 0;
419 
420  block->freeptr = ((char *) block) + Generation_BLOCKHDRSZ;
421  block->endptr = ((char *) block) + blksize;
422 
423  /* Mark unallocated space NOACCESS. */
425  blksize - Generation_BLOCKHDRSZ);
426 
427  /* add it to the doubly-linked list of blocks */
428  dlist_push_head(&set->blocks, &block->node);
429 
430  /* and also use it as the current allocation block */
431  set->block = block;
432  }
433 
434  /* we're supposed to have a block with enough free space now */
435  Assert(block != NULL);
436  Assert((block->endptr - block->freeptr) >= Generation_CHUNKHDRSZ + chunk_size);
437 
438  chunk = (GenerationChunk *) block->freeptr;
439 
440  /* Prepare to initialize the chunk header. */
441  VALGRIND_MAKE_MEM_UNDEFINED(chunk, Generation_CHUNKHDRSZ);
442 
443  block->nchunks += 1;
444  block->freeptr += (Generation_CHUNKHDRSZ + chunk_size);
445 
446  Assert(block->freeptr <= block->endptr);
447 
448  chunk->block = block;
449  chunk->context = set;
450  chunk->size = chunk_size;
451 
452 #ifdef MEMORY_CONTEXT_CHECKING
453  chunk->requested_size = size;
454  /* set mark to catch clobber of "unused" space */
455  if (size < chunk->size)
456  set_sentinel(GenerationChunkGetPointer(chunk), size);
457 #endif
458 #ifdef RANDOMIZE_ALLOCATED_MEMORY
459  /* fill the allocated space with junk */
460  randomize_mem((char *) GenerationChunkGetPointer(chunk), size);
461 #endif
462 
463  GenerationAllocInfo(set, chunk);
464 
465  /* Ensure any padding bytes are marked NOACCESS. */
467  chunk_size - size);
468 
469  /* Disallow external access to private part of chunk header. */
471 
472  return GenerationChunkGetPointer(chunk);
473 }
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
GenerationBlock * block
Definition: generation.c:122
#define GenerationAllocInfo(_cxt, _chunk)
Definition: generation.c:194
#define GENERATIONCHUNK_PRIVATE_LEN
Definition: generation.c:133
#define malloc(a)
Definition: header.h:50
GenerationContext * context
Definition: generation.c:123
dlist_node node
Definition: generation.c:83
#define GenerationChunkGetPointer(chk)
Definition: generation.c:143
char * freeptr
Definition: generation.c:87
#define Assert(condition)
Definition: c.h:732
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:685
Size mem_allocated
Definition: memnodes.h:82
#define Generation_CHUNKHDRSZ
Definition: generation.c:47
#define Generation_BLOCKHDRSZ
Definition: generation.c:46

◆ GenerationContextCreate()

MemoryContext GenerationContextCreate ( MemoryContext  parent,
const char *  name,
Size  blockSize 
)

Definition at line 212 of file generation.c.

References AllocHugeSizeIsValid, GenerationContext::blockSize, dlist_init(), elog, ereport, errcode(), errdetail(), errmsg(), ERROR, Generation_CHUNKHDRSZ, malloc, MAXALIGN, MemoryContextCreate(), MemoryContextStats(), offsetof, StaticAssertStmt, T_GenerationContext, and TopMemoryContext.

Referenced by create_GistBulkDeleteResult(), and ReorderBufferAllocate().

215 {
216  GenerationContext *set;
217 
218  /* Assert we padded GenerationChunk properly */
220  "sizeof(GenerationChunk) is not maxaligned");
223  "padding calculation in GenerationChunk is wrong");
224 
225  /*
226  * First, validate allocation parameters. (If we're going to throw an
227  * error, we should do so before the context is created, not after.) We
228  * somewhat arbitrarily enforce a minimum 1K block size, mostly because
229  * that's what AllocSet does.
230  */
231  if (blockSize != MAXALIGN(blockSize) ||
232  blockSize < 1024 ||
233  !AllocHugeSizeIsValid(blockSize))
234  elog(ERROR, "invalid blockSize for memory context: %zu",
235  blockSize);
236 
237  /*
238  * Allocate the context header. Unlike aset.c, we never try to combine
239  * this with the first regular block, since that would prevent us from
240  * freeing the first generation of allocations.
241  */
242 
244  if (set == NULL)
245  {
247  ereport(ERROR,
248  (errcode(ERRCODE_OUT_OF_MEMORY),
249  errmsg("out of memory"),
250  errdetail("Failed while creating memory context \"%s\".",
251  name)));
252  }
253 
254  /*
255  * Avoid writing code that can fail between here and MemoryContextCreate;
256  * we'd leak the header if we ereport in this stretch.
257  */
258 
259  /* Fill in GenerationContext-specific header fields */
260  set->blockSize = blockSize;
261  set->block = NULL;
262  dlist_init(&set->blocks);
263 
264  /* Finally, do the type-independent part of context creation */
268  parent,
269  name);
270 
271  return (MemoryContext) set;
272 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#define malloc(a)
Definition: header.h:50
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:842
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:498
int errdetail(const char *fmt,...)
Definition: elog.c:860
static const MemoryContextMethods GenerationMethods
Definition: generation.c:167
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
#define MAXALIGN(LEN)
Definition: c.h:685
const char * name
Definition: encode.c:521
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define Generation_CHUNKHDRSZ
Definition: generation.c:47
#define offsetof(type, field)
Definition: c.h:655

◆ GenerationDelete()

static void GenerationDelete ( MemoryContext  context)
static

Definition at line 319 of file generation.c.

References free, and GenerationReset().

320 {
321  /* Reset to release all the GenerationBlocks */
322  GenerationReset(context);
323  /* And free the context header */
324  free(context);
325 }
static void GenerationReset(MemoryContext context)
Definition: generation.c:282
#define free(a)
Definition: header.h:65

◆ GenerationFree()

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

Definition at line 481 of file generation.c.

References Assert, GenerationBlock::blksize, GenerationContext::block, GenerationChunk::block, GenerationChunk::context, dlist_delete(), elog, free, GENERATIONCHUNK_PRIVATE_LEN, GenerationPointerGetChunk, MemoryContextData::mem_allocated, name, GenerationBlock::nchunks, GenerationBlock::nfree, GenerationBlock::node, GenerationChunk::size, VALGRIND_MAKE_MEM_DEFINED, and WARNING.

Referenced by GenerationRealloc().

482 {
483  GenerationContext *set = (GenerationContext *) context;
484  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
485  GenerationBlock *block;
486 
487  /* Allow access to private part of chunk header. */
489 
490  block = chunk->block;
491 
492 #ifdef MEMORY_CONTEXT_CHECKING
493  /* Test for someone scribbling on unused space in chunk */
494  if (chunk->requested_size < chunk->size)
495  if (!sentinel_ok(pointer, chunk->requested_size))
496  elog(WARNING, "detected write past chunk end in %s %p",
497  ((MemoryContext) set)->name, chunk);
498 #endif
499 
500 #ifdef CLOBBER_FREED_MEMORY
501  wipe_mem(pointer, chunk->size);
502 #endif
503 
504  /* Reset context to NULL in freed chunks */
505  chunk->context = NULL;
506 
507 #ifdef MEMORY_CONTEXT_CHECKING
508  /* Reset requested_size to 0 in freed chunks */
509  chunk->requested_size = 0;
510 #endif
511 
512  block->nfree += 1;
513 
514  Assert(block->nchunks > 0);
515  Assert(block->nfree <= block->nchunks);
516 
517  /* If there are still allocated chunks in the block, we're done. */
518  if (block->nfree < block->nchunks)
519  return;
520 
521  /*
522  * The block is empty, so let's get rid of it. First remove it from the
523  * list of blocks, then return it to malloc().
524  */
525  dlist_delete(&block->node);
526 
527  /* Also make sure the block is not marked as the current block. */
528  if (set->block == block)
529  set->block = NULL;
530 
531  context->mem_allocated -= block->blksize;
532  free(block);
533 }
#define GenerationPointerGetChunk(ptr)
Definition: generation.c:141
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
GenerationBlock * block
Definition: generation.c:122
#define GENERATIONCHUNK_PRIVATE_LEN
Definition: generation.c:133
GenerationContext * context
Definition: generation.c:123
dlist_node node
Definition: generation.c:83
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define WARNING
Definition: elog.h:40
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:732
const char * name
Definition: encode.c:521
Size mem_allocated
Definition: memnodes.h:82
#define elog(elevel,...)
Definition: elog.h:226

◆ GenerationGetChunkSpace()

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

Definition at line 658 of file generation.c.

References Generation_CHUNKHDRSZ, GENERATIONCHUNK_PRIVATE_LEN, GenerationPointerGetChunk, GenerationChunk::size, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

659 {
660  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
661  Size result;
662 
664  result = chunk->size + Generation_CHUNKHDRSZ;
666  return result;
667 }
#define GenerationPointerGetChunk(ptr)
Definition: generation.c:141
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
#define GENERATIONCHUNK_PRIVATE_LEN
Definition: generation.c:133
size_t Size
Definition: c.h:466
#define Generation_CHUNKHDRSZ
Definition: generation.c:47

◆ GenerationIsEmpty()

static bool GenerationIsEmpty ( MemoryContext  context)
static

Definition at line 674 of file generation.c.

References dlist_is_empty().

675 {
676  GenerationContext *set = (GenerationContext *) context;
677 
678  return dlist_is_empty(&set->blocks);
679 }
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289

◆ GenerationRealloc()

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

Definition at line 542 of file generation.c.

References elog, GenerationAlloc(), GENERATIONCHUNK_PRIVATE_LEN, GenerationFree(), GenerationPointerGetChunk, name, GenerationChunk::size, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, VALGRIND_MAKE_MEM_UNDEFINED, and WARNING.

543 {
544  GenerationContext *set = (GenerationContext *) context;
545  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
546  GenerationPointer newPointer;
547  Size oldsize;
548 
549  /* Allow access to private part of chunk header. */
551 
552  oldsize = chunk->size;
553 
554 #ifdef MEMORY_CONTEXT_CHECKING
555  /* Test for someone scribbling on unused space in chunk */
556  if (chunk->requested_size < oldsize)
557  if (!sentinel_ok(pointer, chunk->requested_size))
558  elog(WARNING, "detected write past chunk end in %s %p",
559  ((MemoryContext) set)->name, chunk);
560 #endif
561 
562  /*
563  * Maybe the allocated area already is >= the new size. (In particular,
564  * we always fall out here if the requested size is a decrease.)
565  *
566  * This memory context does not use power-of-2 chunk sizing and instead
567  * carves the chunks to be as small as possible, so most repalloc() calls
568  * will end up in the palloc/memcpy/pfree branch.
569  *
570  * XXX Perhaps we should annotate this condition with unlikely()?
571  */
572  if (oldsize >= size)
573  {
574 #ifdef MEMORY_CONTEXT_CHECKING
575  Size oldrequest = chunk->requested_size;
576 
577 #ifdef RANDOMIZE_ALLOCATED_MEMORY
578  /* We can only fill the extra space if we know the prior request */
579  if (size > oldrequest)
580  randomize_mem((char *) pointer + oldrequest,
581  size - oldrequest);
582 #endif
583 
584  chunk->requested_size = size;
585 
586  /*
587  * If this is an increase, mark any newly-available part UNDEFINED.
588  * Otherwise, mark the obsolete part NOACCESS.
589  */
590  if (size > oldrequest)
591  VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + oldrequest,
592  size - oldrequest);
593  else
594  VALGRIND_MAKE_MEM_NOACCESS((char *) pointer + size,
595  oldsize - size);
596 
597  /* set mark to catch clobber of "unused" space */
598  if (size < oldsize)
599  set_sentinel(pointer, size);
600 #else /* !MEMORY_CONTEXT_CHECKING */
601 
602  /*
603  * We don't have the information to determine whether we're growing
604  * the old request or shrinking it, so we conservatively mark the
605  * entire new allocation DEFINED.
606  */
607  VALGRIND_MAKE_MEM_NOACCESS(pointer, oldsize);
608  VALGRIND_MAKE_MEM_DEFINED(pointer, size);
609 #endif
610 
611  /* Disallow external access to private part of chunk header. */
613 
614  return pointer;
615  }
616 
617  /* allocate new chunk */
618  newPointer = GenerationAlloc((MemoryContext) set, size);
619 
620  /* leave immediately if request was not completed */
621  if (newPointer == NULL)
622  {
623  /* Disallow external access to private part of chunk header. */
625  return NULL;
626  }
627 
628  /*
629  * GenerationAlloc() may have returned a region that is still NOACCESS.
630  * Change it to UNDEFINED for the moment; memcpy() will then transfer
631  * definedness from the old allocation to the new. If we know the old
632  * allocation, copy just that much. Otherwise, make the entire old chunk
633  * defined to avoid errors as we copy the currently-NOACCESS trailing
634  * bytes.
635  */
636  VALGRIND_MAKE_MEM_UNDEFINED(newPointer, size);
637 #ifdef MEMORY_CONTEXT_CHECKING
638  oldsize = chunk->requested_size;
639 #else
640  VALGRIND_MAKE_MEM_DEFINED(pointer, oldsize);
641 #endif
642 
643  /* transfer existing data (certain to fit) */
644  memcpy(newPointer, pointer, oldsize);
645 
646  /* free old chunk */
647  GenerationFree((MemoryContext) set, pointer);
648 
649  return newPointer;
650 }
#define GenerationPointerGetChunk(ptr)
Definition: generation.c:141
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
void * GenerationPointer
Definition: generation.c:52
#define GENERATIONCHUNK_PRIVATE_LEN
Definition: generation.c:133
static void GenerationFree(MemoryContext context, void *pointer)
Definition: generation.c:481
#define WARNING
Definition: elog.h:40
size_t Size
Definition: c.h:466
const char * name
Definition: encode.c:521
#define elog(elevel,...)
Definition: elog.h:226
static void * GenerationAlloc(MemoryContext context, Size size)
Definition: generation.c:341

◆ GenerationReset()

static void GenerationReset ( MemoryContext  context)
static

Definition at line 282 of file generation.c.

References Assert, AssertArg, GenerationBlock::blksize, GenerationContext::block, dlist_mutable_iter::cur, dlist_container, dlist_delete(), dlist_foreach_modify, dlist_is_empty(), free, GenerationIsValid, and MemoryContextData::mem_allocated.

Referenced by GenerationDelete().

283 {
284  GenerationContext *set = (GenerationContext *) context;
285  dlist_mutable_iter miter;
286 
288 
289 #ifdef MEMORY_CONTEXT_CHECKING
290  /* Check for corruption and leaks before freeing */
291  GenerationCheck(context);
292 #endif
293 
294  dlist_foreach_modify(miter, &set->blocks)
295  {
296  GenerationBlock *block = dlist_container(GenerationBlock, node, miter.cur);
297 
298  dlist_delete(miter.cur);
299 
300  context->mem_allocated -= block->blksize;
301 
302 #ifdef CLOBBER_FREED_MEMORY
303  wipe_mem(block, block->blksize);
304 #endif
305 
306  free(block);
307  }
308 
309  set->block = NULL;
310 
311  Assert(dlist_is_empty(&set->blocks));
312 }
dlist_node * cur
Definition: ilist.h:180
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define GenerationIsValid(set)
Definition: generation.c:139
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define AssertArg(condition)
Definition: c.h:734
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:732
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
Size mem_allocated
Definition: memnodes.h:82

◆ GenerationStats()

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

Definition at line 693 of file generation.c.

References Assert, GenerationBlock::blksize, GenerationContext::block, GenerationChunk::block, GenerationContext::blocks, GenerationChunk::context, dlist_iter::cur, dlist_container, dlist_foreach, elog, GenerationBlock::endptr, MemoryContextCounters::freechunks, GenerationBlock::freeptr, MemoryContextCounters::freespace, Generation_BLOCKHDRSZ, Generation_CHUNKHDRSZ, GENERATIONCHUNK_PRIVATE_LEN, MAXALIGN, MemoryContextData::mem_allocated, MemoryContextData::name, name, MemoryContextCounters::nblocks, GenerationBlock::nchunks, GenerationBlock::nfree, GenerationChunk::size, snprintf, MemoryContextCounters::totalspace, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, and WARNING.

696 {
697  GenerationContext *set = (GenerationContext *) context;
698  Size nblocks = 0;
699  Size nchunks = 0;
700  Size nfreechunks = 0;
701  Size totalspace;
702  Size freespace = 0;
703  dlist_iter iter;
704 
705  /* Include context header in totalspace */
706  totalspace = MAXALIGN(sizeof(GenerationContext));
707 
708  dlist_foreach(iter, &set->blocks)
709  {
710  GenerationBlock *block = dlist_container(GenerationBlock, node, iter.cur);
711 
712  nblocks++;
713  nchunks += block->nchunks;
714  nfreechunks += block->nfree;
715  totalspace += block->blksize;
716  freespace += (block->endptr - block->freeptr);
717  }
718 
719  if (printfunc)
720  {
721  char stats_string[200];
722 
723  snprintf(stats_string, sizeof(stats_string),
724  "%zu total in %zd blocks (%zd chunks); %zu free (%zd chunks); %zu used",
725  totalspace, nblocks, nchunks, freespace,
726  nfreechunks, totalspace - freespace);
727  printfunc(context, passthru, stats_string);
728  }
729 
730  if (totals)
731  {
732  totals->nblocks += nblocks;
733  totals->freechunks += nfreechunks;
734  totals->totalspace += totalspace;
735  totals->freespace += freespace;
736  }
737 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
dlist_node * cur
Definition: ilist.h:161
char * freeptr
Definition: generation.c:87
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:685
#define snprintf
Definition: port.h:192

Variable Documentation

◆ GenerationMethods

const MemoryContextMethods GenerationMethods
static
Initial value:
= {
}
static void GenerationDelete(MemoryContext context)
Definition: generation.c:319
static void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
Definition: generation.c:693
static void GenerationReset(MemoryContext context)
Definition: generation.c:282
static Size GenerationGetChunkSpace(MemoryContext context, void *pointer)
Definition: generation.c:658
static void GenerationFree(MemoryContext context, void *pointer)
Definition: generation.c:481
static void * GenerationRealloc(MemoryContext context, void *pointer, Size size)
Definition: generation.c:542
static bool GenerationIsEmpty(MemoryContext context)
Definition: generation.c:674
static void * GenerationAlloc(MemoryContext context, Size size)
Definition: generation.c:341

Definition at line 167 of file generation.c.