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, bool print_to_stderr)
 
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 326 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().

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

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

◆ GenerationDelete()

static void GenerationDelete ( MemoryContext  context)
static

Definition at line 304 of file generation.c.

References free, and GenerationReset().

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

◆ GenerationFree()

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

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

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

◆ GenerationGetChunkSpace()

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

Definition at line 639 of file generation.c.

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

640 {
641  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
642  Size result;
643 
645  result = chunk->size + Generation_CHUNKHDRSZ;
647  return result;
648 }
#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:540
#define Generation_CHUNKHDRSZ
Definition: generation.c:47

◆ GenerationIsEmpty()

static bool GenerationIsEmpty ( MemoryContext  context)
static

Definition at line 655 of file generation.c.

References dlist_is_empty().

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

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

◆ GenerationReset()

static void GenerationReset ( MemoryContext  context)
static

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

268 {
269  GenerationContext *set = (GenerationContext *) context;
270  dlist_mutable_iter miter;
271 
273 
274 #ifdef MEMORY_CONTEXT_CHECKING
275  /* Check for corruption and leaks before freeing */
276  GenerationCheck(context);
277 #endif
278 
279  dlist_foreach_modify(miter, &set->blocks)
280  {
281  GenerationBlock *block = dlist_container(GenerationBlock, node, miter.cur);
282 
283  dlist_delete(miter.cur);
284 
285  context->mem_allocated -= block->blksize;
286 
287 #ifdef CLOBBER_FREED_MEMORY
288  wipe_mem(block, block->blksize);
289 #endif
290 
291  free(block);
292  }
293 
294  set->block = NULL;
295 
296  Assert(dlist_is_empty(&set->blocks));
297 }
dlist_node * cur
Definition: ilist.h:180
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:543
#define dlist_container(type, membername, ptr)
Definition: ilist.h:496
#define GenerationIsValid(set)
Definition: generation.c:139
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define AssertArg(condition)
Definition: c.h:806
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:804
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
Size mem_allocated
Definition: memnodes.h:84

◆ GenerationStats()

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

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

678 {
679  GenerationContext *set = (GenerationContext *) context;
680  Size nblocks = 0;
681  Size nchunks = 0;
682  Size nfreechunks = 0;
683  Size totalspace;
684  Size freespace = 0;
685  dlist_iter iter;
686 
687  /* Include context header in totalspace */
688  totalspace = MAXALIGN(sizeof(GenerationContext));
689 
690  dlist_foreach(iter, &set->blocks)
691  {
692  GenerationBlock *block = dlist_container(GenerationBlock, node, iter.cur);
693 
694  nblocks++;
695  nchunks += block->nchunks;
696  nfreechunks += block->nfree;
697  totalspace += block->blksize;
698  freespace += (block->endptr - block->freeptr);
699  }
700 
701  if (printfunc)
702  {
703  char stats_string[200];
704 
705  snprintf(stats_string, sizeof(stats_string),
706  "%zu total in %zd blocks (%zd chunks); %zu free (%zd chunks); %zu used",
707  totalspace, nblocks, nchunks, freespace,
708  nfreechunks, totalspace - freespace);
709  printfunc(context, passthru, stats_string, print_to_stderr);
710  }
711 
712  if (totals)
713  {
714  totals->nblocks += nblocks;
715  totals->freechunks += nfreechunks;
716  totals->totalspace += totalspace;
717  totals->freespace += freespace;
718  }
719 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:526
#define dlist_container(type, membername, ptr)
Definition: ilist.h:496
dlist_node * cur
Definition: ilist.h:161
char * freeptr
Definition: generation.c:87
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
#define snprintf
Definition: port.h:216

Variable Documentation

◆ GenerationMethods

const MemoryContextMethods GenerationMethods
static
Initial value:
= {
}
static void GenerationDelete(MemoryContext context)
Definition: generation.c:304
static void GenerationReset(MemoryContext context)
Definition: generation.c:267
static void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: generation.c:675
static Size GenerationGetChunkSpace(MemoryContext context, void *pointer)
Definition: generation.c:639
static void GenerationFree(MemoryContext context, void *pointer)
Definition: generation.c:462
static void * GenerationRealloc(MemoryContext context, void *pointer, Size size)
Definition: generation.c:523
static bool GenerationIsEmpty(MemoryContext context)
Definition: generation.c:655
static void * GenerationAlloc(MemoryContext context, Size size)
Definition: generation.c:326

Definition at line 168 of file generation.c.