PostgreSQL Source Code  git master
generation.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * generation.c
4  * Generational allocator definitions.
5  *
6  * Generation is a custom MemoryContext implementation designed for cases of
7  * chunks with similar lifespan.
8  *
9  * Portions Copyright (c) 2017-2019, PostgreSQL Global Development Group
10  *
11  * IDENTIFICATION
12  * src/backend/utils/mmgr/generation.c
13  *
14  *
15  * This memory context is based on the assumption that the chunks are freed
16  * roughly in the same order as they were allocated (FIFO), or in groups with
17  * similar lifespan (generations - hence the name of the context). This is
18  * typical for various queue-like use cases, i.e. when tuples are constructed,
19  * processed and then thrown away.
20  *
21  * The memory context uses a very simple approach to free space management.
22  * Instead of a complex global freelist, each block tracks a number
23  * of allocated and freed chunks. Freed chunks are not reused, and once all
24  * chunks in a block are freed, the whole block is thrown away. When the
25  * chunks allocated in the same block have similar lifespan, this works
26  * very well and is very cheap.
27  *
28  * The current implementation only uses a fixed block size - maybe it should
29  * adapt a min/max block size range, and grow the blocks automatically.
30  * It already uses dedicated blocks for oversized chunks.
31  *
32  * XXX It might be possible to improve this by keeping a small freelist for
33  * only a small number of recent blocks, but it's not clear it's worth the
34  * additional complexity.
35  *
36  *-------------------------------------------------------------------------
37  */
38 
39 #include "postgres.h"
40 
41 #include "lib/ilist.h"
42 #include "utils/memdebug.h"
43 #include "utils/memutils.h"
44 
45 
46 #define Generation_BLOCKHDRSZ MAXALIGN(sizeof(GenerationBlock))
47 #define Generation_CHUNKHDRSZ sizeof(GenerationChunk)
48 
49 typedef struct GenerationBlock GenerationBlock; /* forward reference */
51 
52 typedef void *GenerationPointer;
53 
54 /*
55  * GenerationContext is a simple memory context not reusing allocated chunks,
56  * and freeing blocks once all chunks are freed.
57  */
58 typedef struct GenerationContext
59 {
60  MemoryContextData header; /* Standard memory-context fields */
61 
62  /* Generational context parameters */
63  Size blockSize; /* standard block size */
64 
65  GenerationBlock *block; /* current (most recently allocated) block */
66  dlist_head blocks; /* list of blocks */
68 
69 /*
70  * GenerationBlock
71  * GenerationBlock is the unit of memory that is obtained by generation.c
72  * from malloc(). It contains one or more GenerationChunks, which are
73  * the units requested by palloc() and freed by pfree(). GenerationChunks
74  * cannot be returned to malloc() individually, instead pfree()
75  * updates the free counter of the block and when all chunks in a block
76  * are free the whole block is returned to malloc().
77  *
78  * GenerationBlock is the header data for a block --- the usable space
79  * within the block begins at the next alignment boundary.
80  */
82 {
83  dlist_node node; /* doubly-linked list of blocks */
84  Size blksize; /* allocated size of this block */
85  int nchunks; /* number of chunks in the block */
86  int nfree; /* number of free chunks */
87  char *freeptr; /* start of free space in this block */
88  char *endptr; /* end of space in this block */
89 };
90 
91 /*
92  * GenerationChunk
93  * The prefix of each piece of memory in a GenerationBlock
94  *
95  * Note: to meet the memory context APIs, the payload area of the chunk must
96  * be maxaligned, and the "context" link must be immediately adjacent to the
97  * payload area (cf. GetMemoryChunkContext). We simplify matters for this
98  * module by requiring sizeof(GenerationChunk) to be maxaligned, and then
99  * we can ensure things work by adding any required alignment padding before
100  * the pointer fields. There is a static assertion below that the alignment
101  * is done correctly.
102  */
104 {
105  /* size is always the size of the usable space in the chunk */
107 #ifdef MEMORY_CONTEXT_CHECKING
108  /* when debugging memory usage, also store actual requested size */
109  /* this is zero in a free chunk */
110  Size requested_size;
111 
112 #define GENERATIONCHUNK_RAWSIZE (SIZEOF_SIZE_T * 2 + SIZEOF_VOID_P * 2)
113 #else
114 #define GENERATIONCHUNK_RAWSIZE (SIZEOF_SIZE_T + SIZEOF_VOID_P * 2)
115 #endif /* MEMORY_CONTEXT_CHECKING */
116 
117  /* ensure proper alignment by adding padding if needed */
118 #if (GENERATIONCHUNK_RAWSIZE % MAXIMUM_ALIGNOF) != 0
119  char padding[MAXIMUM_ALIGNOF - GENERATIONCHUNK_RAWSIZE % MAXIMUM_ALIGNOF];
120 #endif
121 
122  GenerationBlock *block; /* block owning this chunk */
123  GenerationContext *context; /* owning context, or NULL if freed chunk */
124  /* there must not be any padding to reach a MAXALIGN boundary here! */
125 };
126 
127 /*
128  * Only the "context" field should be accessed outside this module.
129  * We keep the rest of an allocated chunk's header marked NOACCESS when using
130  * valgrind. But note that freed chunk headers are kept accessible, for
131  * simplicity.
132  */
133 #define GENERATIONCHUNK_PRIVATE_LEN offsetof(GenerationChunk, context)
134 
135 /*
136  * GenerationIsValid
137  * True iff set is valid allocation set.
138  */
139 #define GenerationIsValid(set) PointerIsValid(set)
140 
141 #define GenerationPointerGetChunk(ptr) \
142  ((GenerationChunk *)(((char *)(ptr)) - Generation_CHUNKHDRSZ))
143 #define GenerationChunkGetPointer(chk) \
144  ((GenerationPointer *)(((char *)(chk)) + Generation_CHUNKHDRSZ))
145 
146 /*
147  * These functions implement the MemoryContext API for Generation contexts.
148  */
149 static void *GenerationAlloc(MemoryContext context, Size size);
150 static void GenerationFree(MemoryContext context, void *pointer);
151 static void *GenerationRealloc(MemoryContext context, void *pointer, Size size);
152 static void GenerationReset(MemoryContext context);
153 static void GenerationDelete(MemoryContext context);
154 static Size GenerationGetChunkSpace(MemoryContext context, void *pointer);
155 static bool GenerationIsEmpty(MemoryContext context);
156 static void GenerationStats(MemoryContext context,
157  MemoryStatsPrintFunc printfunc, void *passthru,
158  MemoryContextCounters *totals);
159 
160 #ifdef MEMORY_CONTEXT_CHECKING
161 static void GenerationCheck(MemoryContext context);
162 #endif
163 
164 /*
165  * This is the virtual function table for Generation contexts.
166  */
176 #ifdef MEMORY_CONTEXT_CHECKING
177  ,GenerationCheck
178 #endif
179 };
180 
181 /* ----------
182  * Debug macros
183  * ----------
184  */
185 #ifdef HAVE_ALLOCINFO
186 #define GenerationFreeInfo(_cxt, _chunk) \
187  fprintf(stderr, "GenerationFree: %s: %p, %lu\n", \
188  (_cxt)->name, (_chunk), (_chunk)->size)
189 #define GenerationAllocInfo(_cxt, _chunk) \
190  fprintf(stderr, "GenerationAlloc: %s: %p, %lu\n", \
191  (_cxt)->name, (_chunk), (_chunk)->size)
192 #else
193 #define GenerationFreeInfo(_cxt, _chunk)
194 #define GenerationAllocInfo(_cxt, _chunk)
195 #endif
196 
197 
198 /*
199  * Public routines
200  */
201 
202 
203 /*
204  * GenerationContextCreate
205  * Create a new Generation context.
206  *
207  * parent: parent context, or NULL if top-level context
208  * name: name of context (must be statically allocated)
209  * blockSize: generation block size
210  */
213  const char *name,
214  Size blockSize)
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 */
267  &GenerationMethods,
268  parent,
269  name);
270 
271  return (MemoryContext) set;
272 }
273 
274 /*
275  * GenerationReset
276  * Frees all memory which is allocated in the given set.
277  *
278  * The code simply frees all the blocks in the context - we don't keep any
279  * keeper blocks or anything like that.
280  */
281 static void
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  {
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 }
313 
314 /*
315  * GenerationDelete
316  * Free all memory which is allocated in the given context.
317  */
318 static void
320 {
321  /* Reset to release all the GenerationBlocks */
322  GenerationReset(context);
323  /* And free the context header */
324  free(context);
325 }
326 
327 /*
328  * GenerationAlloc
329  * Returns pointer to allocated memory of given size or NULL if
330  * request could not be completed; memory is added to the set.
331  *
332  * No request may exceed:
333  * MAXALIGN_DOWN(SIZE_MAX) - Generation_BLOCKHDRSZ - Generation_CHUNKHDRSZ
334  * All callers use a much-lower limit.
335  *
336  * Note: when using valgrind, it doesn't matter how the returned allocation
337  * is marked, as mcxt.c will set it to UNDEFINED. In some paths we will
338  * return space that is marked NOACCESS - GenerationRealloc has to beware!
339  */
340 static void *
342 {
343  GenerationContext *set = (GenerationContext *) context;
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. */
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 }
474 
475 /*
476  * GenerationFree
477  * Update number of chunks in the block, and if all chunks in the block
478  * are now free then discard the block.
479  */
480 static void
481 GenerationFree(MemoryContext context, void *pointer)
482 {
483  GenerationContext *set = (GenerationContext *) context;
484  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
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 }
534 
535 /*
536  * GenerationRealloc
537  * When handling repalloc, we simply allocate a new chunk, copy the data
538  * and discard the old one. The only exception is when the new size fits
539  * into the old chunk - in that case we just update chunk header.
540  */
541 static void *
542 GenerationRealloc(MemoryContext context, void *pointer, Size size)
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 }
651 
652 /*
653  * GenerationGetChunkSpace
654  * Given a currently-allocated chunk, determine the total space
655  * it occupies (including all memory-allocation overhead).
656  */
657 static Size
658 GenerationGetChunkSpace(MemoryContext context, void *pointer)
659 {
660  GenerationChunk *chunk = GenerationPointerGetChunk(pointer);
661  Size result;
662 
664  result = chunk->size + Generation_CHUNKHDRSZ;
666  return result;
667 }
668 
669 /*
670  * GenerationIsEmpty
671  * Is a GenerationContext empty of any allocated space?
672  */
673 static bool
675 {
676  GenerationContext *set = (GenerationContext *) context;
677 
678  return dlist_is_empty(&set->blocks);
679 }
680 
681 /*
682  * GenerationStats
683  * Compute stats about memory consumption of a Generation context.
684  *
685  * printfunc: if not NULL, pass a human-readable stats string to this.
686  * passthru: pass this pointer through to printfunc.
687  * totals: if not NULL, add stats about this context into *totals.
688  *
689  * XXX freespace only accounts for empty space at the end of the block, not
690  * space of freed chunks (which is unknown).
691  */
692 static void
694  MemoryStatsPrintFunc printfunc, void *passthru,
695  MemoryContextCounters *totals)
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  {
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 }
738 
739 
740 #ifdef MEMORY_CONTEXT_CHECKING
741 
742 /*
743  * GenerationCheck
744  * Walk through chunks and check consistency of memory.
745  *
746  * NOTE: report errors as WARNING, *not* ERROR or FATAL. Otherwise you'll
747  * find yourself in an infinite loop when trouble occurs, because this
748  * routine will be entered again when elog cleanup tries to release memory!
749  */
750 static void
751 GenerationCheck(MemoryContext context)
752 {
753  GenerationContext *gen = (GenerationContext *) context;
754  const char *name = context->name;
755  dlist_iter iter;
756  Size total_allocated = 0;
757 
758  /* walk all blocks in this context */
759  dlist_foreach(iter, &gen->blocks)
760  {
762  int nfree,
763  nchunks;
764  char *ptr;
765 
766  total_allocated += block->blksize;
767 
768  /*
769  * nfree > nchunks is surely wrong, and we don't expect to see
770  * equality either, because such a block should have gotten freed.
771  */
772  if (block->nfree >= block->nchunks)
773  elog(WARNING, "problem in Generation %s: number of free chunks %d in block %p exceeds %d allocated",
774  name, block->nfree, block, block->nchunks);
775 
776  /* Now walk through the chunks and count them. */
777  nfree = 0;
778  nchunks = 0;
779  ptr = ((char *) block) + Generation_BLOCKHDRSZ;
780 
781  while (ptr < block->freeptr)
782  {
783  GenerationChunk *chunk = (GenerationChunk *) ptr;
784 
785  /* Allow access to private part of chunk header. */
787 
788  /* move to the next chunk */
789  ptr += (chunk->size + Generation_CHUNKHDRSZ);
790 
791  nchunks += 1;
792 
793  /* chunks have both block and context pointers, so check both */
794  if (chunk->block != block)
795  elog(WARNING, "problem in Generation %s: bogus block link in block %p, chunk %p",
796  name, block, chunk);
797 
798  /*
799  * Check for valid context pointer. Note this is an incomplete
800  * test, since palloc(0) produces an allocated chunk with
801  * requested_size == 0.
802  */
803  if ((chunk->requested_size > 0 && chunk->context != gen) ||
804  (chunk->context != gen && chunk->context != NULL))
805  elog(WARNING, "problem in Generation %s: bogus context link in block %p, chunk %p",
806  name, block, chunk);
807 
808  /* now make sure the chunk size is correct */
809  if (chunk->size < chunk->requested_size ||
810  chunk->size != MAXALIGN(chunk->size))
811  elog(WARNING, "problem in Generation %s: bogus chunk size in block %p, chunk %p",
812  name, block, chunk);
813 
814  /* is chunk allocated? */
815  if (chunk->context != NULL)
816  {
817  /* check sentinel, but only in allocated blocks */
818  if (chunk->requested_size < chunk->size &&
819  !sentinel_ok(chunk, Generation_CHUNKHDRSZ + chunk->requested_size))
820  elog(WARNING, "problem in Generation %s: detected write past chunk end in block %p, chunk %p",
821  name, block, chunk);
822  }
823  else
824  nfree += 1;
825 
826  /*
827  * If chunk is allocated, disallow external access to private part
828  * of chunk header.
829  */
830  if (chunk->context != NULL)
832  }
833 
834  /*
835  * Make sure we got the expected number of allocated and free chunks
836  * (as tracked in the block header).
837  */
838  if (nchunks != block->nchunks)
839  elog(WARNING, "problem in Generation %s: number of allocated chunks %d in block %p does not match header %d",
840  name, nchunks, block, block->nchunks);
841 
842  if (nfree != block->nfree)
843  elog(WARNING, "problem in Generation %s: number of free chunks %d in block %p does not match header %d",
844  name, nfree, block, block->nfree);
845  }
846 
847  Assert(total_allocated == context->mem_allocated);
848 }
849 
850 #endif /* MEMORY_CONTEXT_CHECKING */
static void GenerationDelete(MemoryContext context)
Definition: generation.c:319
#define GenerationPointerGetChunk(ptr)
Definition: generation.c:141
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
dlist_node * cur
Definition: ilist.h:180
void(* MemoryStatsPrintFunc)(MemoryContext context, void *passthru, const char *stats_string)
Definition: memnodes.h:54
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
static void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
Definition: generation.c:693
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 dlist_foreach(iter, lhead)
Definition: ilist.h:507
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
static void GenerationReset(MemoryContext context)
Definition: generation.c:282
#define GENERATIONCHUNK_RAWSIZE
Definition: generation.c:114
GenerationBlock * block
Definition: generation.c:122
int errcode(int sqlerrcode)
Definition: elog.c:608
void * GenerationPointer
Definition: generation.c:52
MemoryContextData header
Definition: generation.c:60
#define GenerationAllocInfo(_cxt, _chunk)
Definition: generation.c:194
#define GENERATIONCHUNK_PRIVATE_LEN
Definition: generation.c:133
#define malloc(a)
Definition: header.h:50
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:849
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define GenerationIsValid(set)
Definition: generation.c:139
#define ERROR
Definition: elog.h:43
dlist_head blocks
Definition: generation.c:66
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:498
GenerationContext * context
Definition: generation.c:123
int errdetail(const char *fmt,...)
Definition: elog.c:955
dlist_node node
Definition: generation.c:83
static Size GenerationGetChunkSpace(MemoryContext context, void *pointer)
Definition: generation.c:658
static const MemoryContextMethods GenerationMethods
Definition: generation.c:167
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
static void GenerationFree(MemoryContext context, void *pointer)
Definition: generation.c:481
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
#define AssertArg(condition)
Definition: c.h:741
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define GenerationChunkGetPointer(chk)
Definition: generation.c:143
#define WARNING
Definition: elog.h:40
MemoryContext GenerationContextCreate(MemoryContext parent, const char *name, Size blockSize)
Definition: generation.c:212
struct GenerationContext GenerationContext
dlist_node * cur
Definition: ilist.h:161
char * freeptr
Definition: generation.c:87
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:739
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
size_t Size
Definition: c.h:467
#define MAXALIGN(LEN)
Definition: c.h:692
static void * GenerationRealloc(MemoryContext context, void *pointer, Size size)
Definition: generation.c:542
const char * name
Definition: encode.c:521
Size mem_allocated
Definition: memnodes.h:82
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:822
GenerationBlock * block
Definition: generation.c:65
static bool GenerationIsEmpty(MemoryContext context)
Definition: generation.c:674
#define elog(elevel,...)
Definition: elog.h:228
#define Generation_CHUNKHDRSZ
Definition: generation.c:47
static void * GenerationAlloc(MemoryContext context, Size size)
Definition: generation.c:341
#define Generation_BLOCKHDRSZ
Definition: generation.c:46
#define snprintf
Definition: port.h:192
const char * name
Definition: memnodes.h:88
#define offsetof(type, field)
Definition: c.h:662