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))
 

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)

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

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

326 {
327  GenerationContext *set = (GenerationContext *) context;
328  GenerationBlock *block;
329  GenerationChunk *chunk;
330  Size chunk_size = MAXALIGN(size);
331 
332  /* is it an over-sized chunk? if yes, allocate special block */
333  if (chunk_size > set->blockSize / 8)
334  {
335  Size blksize = chunk_size + Generation_BLOCKHDRSZ + Generation_CHUNKHDRSZ;
336 
337  block = (GenerationBlock *) malloc(blksize);
338  if (block == NULL)
339  return NULL;
340 
341  context->mem_allocated += blksize;
342 
343  /* block with a single (used) chunk */
344  block->blksize = blksize;
345  block->nchunks = 1;
346  block->nfree = 0;
347 
348  /* the block is completely full */
349  block->freeptr = block->endptr = ((char *) block) + blksize;
350 
351  chunk = (GenerationChunk *) (((char *) block) + Generation_BLOCKHDRSZ);
352  chunk->block = block;
353  chunk->context = set;
354  chunk->size = chunk_size;
355 
356 #ifdef MEMORY_CONTEXT_CHECKING
357  chunk->requested_size = size;
358  /* set mark to catch clobber of "unused" space */
359  if (size < chunk_size)
360  set_sentinel(GenerationChunkGetPointer(chunk), size);
361 #endif
362 #ifdef RANDOMIZE_ALLOCATED_MEMORY
363  /* fill the allocated space with junk */
364  randomize_mem((char *) GenerationChunkGetPointer(chunk), size);
365 #endif
366 
367  /* add the block to the list of allocated blocks */
368  dlist_push_head(&set->blocks, &block->node);
369 
370  /* Ensure any padding bytes are marked NOACCESS. */
372  chunk_size - size);
373 
374  /* Disallow external access to private part of chunk header. */
376 
377  return GenerationChunkGetPointer(chunk);
378  }
379 
380  /*
381  * Not an over-sized chunk. Is there enough space in the current block? If
382  * not, allocate a new "regular" block.
383  */
384  block = set->block;
385 
386  if ((block == NULL) ||
387  (block->endptr - block->freeptr) < Generation_CHUNKHDRSZ + chunk_size)
388  {
389  Size blksize = set->blockSize;
390 
391  block = (GenerationBlock *) malloc(blksize);
392 
393  if (block == NULL)
394  return NULL;
395 
396  context->mem_allocated += blksize;
397 
398  block->blksize = blksize;
399  block->nchunks = 0;
400  block->nfree = 0;
401 
402  block->freeptr = ((char *) block) + Generation_BLOCKHDRSZ;
403  block->endptr = ((char *) block) + blksize;
404 
405  /* Mark unallocated space NOACCESS. */
407  blksize - Generation_BLOCKHDRSZ);
408 
409  /* add it to the doubly-linked list of blocks */
410  dlist_push_head(&set->blocks, &block->node);
411 
412  /* and also use it as the current allocation block */
413  set->block = block;
414  }
415 
416  /* we're supposed to have a block with enough free space now */
417  Assert(block != NULL);
418  Assert((block->endptr - block->freeptr) >= Generation_CHUNKHDRSZ + chunk_size);
419 
420  chunk = (GenerationChunk *) block->freeptr;
421 
422  /* Prepare to initialize the chunk header. */
423  VALGRIND_MAKE_MEM_UNDEFINED(chunk, Generation_CHUNKHDRSZ);
424 
425  block->nchunks += 1;
426  block->freeptr += (Generation_CHUNKHDRSZ + chunk_size);
427 
428  Assert(block->freeptr <= block->endptr);
429 
430  chunk->block = block;
431  chunk->context = set;
432  chunk->size = chunk_size;
433 
434 #ifdef MEMORY_CONTEXT_CHECKING
435  chunk->requested_size = size;
436  /* set mark to catch clobber of "unused" space */
437  if (size < chunk->size)
438  set_sentinel(GenerationChunkGetPointer(chunk), size);
439 #endif
440 #ifdef RANDOMIZE_ALLOCATED_MEMORY
441  /* fill the allocated space with junk */
442  randomize_mem((char *) GenerationChunkGetPointer(chunk), size);
443 #endif
444 
445  /* Ensure any padding bytes are marked NOACCESS. */
447  chunk_size - size);
448 
449  /* Disallow external access to private part of chunk header. */
451 
452  return GenerationChunkGetPointer(chunk);
453 }
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 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:738
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
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 196 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 gistvacuumscan(), and ReorderBufferAllocate().

199 {
200  GenerationContext *set;
201 
202  /* Assert we padded GenerationChunk properly */
204  "sizeof(GenerationChunk) is not maxaligned");
207  "padding calculation in GenerationChunk is wrong");
208 
209  /*
210  * First, validate allocation parameters. (If we're going to throw an
211  * error, we should do so before the context is created, not after.) We
212  * somewhat arbitrarily enforce a minimum 1K block size, mostly because
213  * that's what AllocSet does.
214  */
215  if (blockSize != MAXALIGN(blockSize) ||
216  blockSize < 1024 ||
217  !AllocHugeSizeIsValid(blockSize))
218  elog(ERROR, "invalid blockSize for memory context: %zu",
219  blockSize);
220 
221  /*
222  * Allocate the context header. Unlike aset.c, we never try to combine
223  * this with the first regular block, since that would prevent us from
224  * freeing the first generation of allocations.
225  */
226 
228  if (set == NULL)
229  {
231  ereport(ERROR,
232  (errcode(ERRCODE_OUT_OF_MEMORY),
233  errmsg("out of memory"),
234  errdetail("Failed while creating memory context \"%s\".",
235  name)));
236  }
237 
238  /*
239  * Avoid writing code that can fail between here and MemoryContextCreate;
240  * we'd leak the header if we ereport in this stretch.
241  */
242 
243  /* Fill in GenerationContext-specific header fields */
244  set->blockSize = blockSize;
245  set->block = NULL;
246  dlist_init(&set->blocks);
247 
248  /* Finally, do the type-independent part of context creation */
252  parent,
253  name);
254 
255  return (MemoryContext) set;
256 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define malloc(a)
Definition: header.h:50
#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
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
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
#define ereport(elevel,...)
Definition: elog.h:144
#define MAXALIGN(LEN)
Definition: c.h:691
const char * name
Definition: encode.c:561
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
#define Generation_CHUNKHDRSZ
Definition: generation.c:47
#define offsetof(type, field)
Definition: c.h:661

◆ GenerationDelete()

static void GenerationDelete ( MemoryContext  context)
static

Definition at line 303 of file generation.c.

References free, and GenerationReset().

304 {
305  /* Reset to release all the GenerationBlocks */
306  GenerationReset(context);
307  /* And free the context header */
308  free(context);
309 }
static void GenerationReset(MemoryContext context)
Definition: generation.c:266
#define free(a)
Definition: header.h:65

◆ GenerationFree()

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

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

462 {
463  GenerationContext *set = (GenerationContext *) context;
464  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
465  GenerationBlock *block;
466 
467  /* Allow access to private part of chunk header. */
469 
470  block = chunk->block;
471 
472 #ifdef MEMORY_CONTEXT_CHECKING
473  /* Test for someone scribbling on unused space in chunk */
474  if (chunk->requested_size < chunk->size)
475  if (!sentinel_ok(pointer, chunk->requested_size))
476  elog(WARNING, "detected write past chunk end in %s %p",
477  ((MemoryContext) set)->name, chunk);
478 #endif
479 
480 #ifdef CLOBBER_FREED_MEMORY
481  wipe_mem(pointer, chunk->size);
482 #endif
483 
484  /* Reset context to NULL in freed chunks */
485  chunk->context = NULL;
486 
487 #ifdef MEMORY_CONTEXT_CHECKING
488  /* Reset requested_size to 0 in freed chunks */
489  chunk->requested_size = 0;
490 #endif
491 
492  block->nfree += 1;
493 
494  Assert(block->nchunks > 0);
495  Assert(block->nfree <= block->nchunks);
496 
497  /* If there are still allocated chunks in the block, we're done. */
498  if (block->nfree < block->nchunks)
499  return;
500 
501  /*
502  * The block is empty, so let's get rid of it. First remove it from the
503  * list of blocks, then return it to malloc().
504  */
505  dlist_delete(&block->node);
506 
507  /* Also make sure the block is not marked as the current block. */
508  if (set->block == block)
509  set->block = NULL;
510 
511  context->mem_allocated -= block->blksize;
512  free(block);
513 }
#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:738
const char * name
Definition: encode.c:561
Size mem_allocated
Definition: memnodes.h:82
#define elog(elevel,...)
Definition: elog.h:214

◆ GenerationGetChunkSpace()

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

Definition at line 638 of file generation.c.

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

639 {
640  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
641  Size result;
642 
644  result = chunk->size + Generation_CHUNKHDRSZ;
646  return result;
647 }
#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 654 of file generation.c.

References dlist_is_empty().

655 {
656  GenerationContext *set = (GenerationContext *) context;
657 
658  return dlist_is_empty(&set->blocks);
659 }
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 522 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.

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

◆ GenerationReset()

static void GenerationReset ( MemoryContext  context)
static

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

267 {
268  GenerationContext *set = (GenerationContext *) context;
269  dlist_mutable_iter miter;
270 
272 
273 #ifdef MEMORY_CONTEXT_CHECKING
274  /* Check for corruption and leaks before freeing */
275  GenerationCheck(context);
276 #endif
277 
278  dlist_foreach_modify(miter, &set->blocks)
279  {
280  GenerationBlock *block = dlist_container(GenerationBlock, node, miter.cur);
281 
282  dlist_delete(miter.cur);
283 
284  context->mem_allocated -= block->blksize;
285 
286 #ifdef CLOBBER_FREED_MEMORY
287  wipe_mem(block, block->blksize);
288 #endif
289 
290  free(block);
291  }
292 
293  set->block = NULL;
294 
295  Assert(dlist_is_empty(&set->blocks));
296 }
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:740
#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

◆ GenerationStats()

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

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

676 {
677  GenerationContext *set = (GenerationContext *) context;
678  Size nblocks = 0;
679  Size nchunks = 0;
680  Size nfreechunks = 0;
681  Size totalspace;
682  Size freespace = 0;
683  dlist_iter iter;
684 
685  /* Include context header in totalspace */
686  totalspace = MAXALIGN(sizeof(GenerationContext));
687 
688  dlist_foreach(iter, &set->blocks)
689  {
690  GenerationBlock *block = dlist_container(GenerationBlock, node, iter.cur);
691 
692  nblocks++;
693  nchunks += block->nchunks;
694  nfreechunks += block->nfree;
695  totalspace += block->blksize;
696  freespace += (block->endptr - block->freeptr);
697  }
698 
699  if (printfunc)
700  {
701  char stats_string[200];
702 
703  snprintf(stats_string, sizeof(stats_string),
704  "%zu total in %zd blocks (%zd chunks); %zu free (%zd chunks); %zu used",
705  totalspace, nblocks, nchunks, freespace,
706  nfreechunks, totalspace - freespace);
707  printfunc(context, passthru, stats_string);
708  }
709 
710  if (totals)
711  {
712  totals->nblocks += nblocks;
713  totals->freechunks += nfreechunks;
714  totals->totalspace += totalspace;
715  totals->freespace += freespace;
716  }
717 }
#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:691
#define snprintf
Definition: port.h:193

Variable Documentation

◆ GenerationMethods

const MemoryContextMethods GenerationMethods
static
Initial value:
= {
}
static void GenerationDelete(MemoryContext context)
Definition: generation.c:303
static void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
Definition: generation.c:673
static void GenerationReset(MemoryContext context)
Definition: generation.c:266
static Size GenerationGetChunkSpace(MemoryContext context, void *pointer)
Definition: generation.c:638
static void GenerationFree(MemoryContext context, void *pointer)
Definition: generation.c:461
static void * GenerationRealloc(MemoryContext context, void *pointer, Size size)
Definition: generation.c:522
static bool GenerationIsEmpty(MemoryContext context)
Definition: generation.c:654
static void * GenerationAlloc(MemoryContext context, Size size)
Definition: generation.c:325

Definition at line 167 of file generation.c.