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

Go to the source code of this file.

Data Structures

struct  AllocSetContext
 
struct  AllocBlockData
 
struct  AllocChunkData
 
struct  AllocSetFreeList
 

Macros

#define ALLOC_MINBITS   3 /* smallest chunk size is 8 bytes */
 
#define ALLOCSET_NUM_FREELISTS   11
 
#define ALLOC_CHUNK_LIMIT   (1 << (ALLOCSET_NUM_FREELISTS-1+ALLOC_MINBITS))
 
#define ALLOC_CHUNK_FRACTION   4
 
#define ALLOC_BLOCKHDRSZ   MAXALIGN(sizeof(AllocBlockData))
 
#define ALLOC_CHUNKHDRSZ   sizeof(struct AllocChunkData)
 
#define ALLOCCHUNK_RAWSIZE   (SIZEOF_SIZE_T + SIZEOF_VOID_P)
 
#define ALLOCCHUNK_PRIVATE_LEN   offsetof(AllocChunkData, aset)
 
#define AllocPointerIsValid(pointer)   PointerIsValid(pointer)
 
#define AllocSetIsValid(set)   PointerIsValid(set)
 
#define AllocPointerGetChunk(ptr)   ((AllocChunk)(((char *)(ptr)) - ALLOC_CHUNKHDRSZ))
 
#define AllocChunkGetPointer(chk)   ((AllocPointer)(((char *)(chk)) + ALLOC_CHUNKHDRSZ))
 
#define MAX_FREE_CONTEXTS   100 /* arbitrary limit on freelist length */
 
#define AllocFreeInfo(_cxt, _chunk)
 
#define AllocAllocInfo(_cxt, _chunk)
 

Typedefs

typedef struct AllocBlockDataAllocBlock
 
typedef struct AllocChunkDataAllocChunk
 
typedef void * AllocPointer
 
typedef struct AllocSetContext AllocSetContext
 
typedef AllocSetContextAllocSet
 
typedef struct AllocBlockData AllocBlockData
 
typedef struct AllocChunkData AllocChunkData
 
typedef struct AllocSetFreeList AllocSetFreeList
 

Functions

static void * AllocSetAlloc (MemoryContext context, Size size)
 
static void AllocSetFree (MemoryContext context, void *pointer)
 
static void * AllocSetRealloc (MemoryContext context, void *pointer, Size size)
 
static void AllocSetReset (MemoryContext context)
 
static void AllocSetDelete (MemoryContext context)
 
static Size AllocSetGetChunkSpace (MemoryContext context, void *pointer)
 
static bool AllocSetIsEmpty (MemoryContext context)
 
static void AllocSetStats (MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
 
static int AllocSetFreeIndex (Size size)
 
MemoryContext AllocSetContextCreateInternal (MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
 

Variables

static AllocSetFreeList context_freelists [2]
 
static const MemoryContextMethods AllocSetMethods
 

Macro Definition Documentation

◆ ALLOC_BLOCKHDRSZ

#define ALLOC_BLOCKHDRSZ   MAXALIGN(sizeof(AllocBlockData))

◆ ALLOC_CHUNK_FRACTION

#define ALLOC_CHUNK_FRACTION   4

Definition at line 86 of file aset.c.

Referenced by AllocSetContextCreateInternal().

◆ ALLOC_CHUNK_LIMIT

#define ALLOC_CHUNK_LIMIT   (1 << (ALLOCSET_NUM_FREELISTS-1+ALLOC_MINBITS))

Definition at line 84 of file aset.c.

Referenced by AllocSetContextCreateInternal(), and AllocSetFreeIndex().

◆ ALLOC_CHUNKHDRSZ

#define ALLOC_CHUNKHDRSZ   sizeof(struct AllocChunkData)

◆ ALLOC_MINBITS

#define ALLOC_MINBITS   3 /* smallest chunk size is 8 bytes */

Definition at line 82 of file aset.c.

Referenced by AllocSetAlloc(), AllocSetFreeIndex(), and AllocSetStats().

◆ AllocAllocInfo

#define AllocAllocInfo (   _cxt,
  _chunk 
)

Definition at line 314 of file aset.c.

Referenced by AllocSetAlloc().

◆ ALLOCCHUNK_PRIVATE_LEN

#define ALLOCCHUNK_PRIVATE_LEN   offsetof(AllocChunkData, aset)

◆ ALLOCCHUNK_RAWSIZE

#define ALLOCCHUNK_RAWSIZE   (SIZEOF_SIZE_T + SIZEOF_VOID_P)

Definition at line 186 of file aset.c.

◆ AllocChunkGetPointer

#define AllocChunkGetPointer (   chk)    ((AllocPointer)(((char *)(chk)) + ALLOC_CHUNKHDRSZ))

Definition at line 221 of file aset.c.

Referenced by AllocSetAlloc(), and AllocSetRealloc().

◆ AllocFreeInfo

#define AllocFreeInfo (   _cxt,
  _chunk 
)

Definition at line 313 of file aset.c.

Referenced by AllocSetFree().

◆ AllocPointerGetChunk

#define AllocPointerGetChunk (   ptr)    ((AllocChunk)(((char *)(ptr)) - ALLOC_CHUNKHDRSZ))

Definition at line 219 of file aset.c.

Referenced by AllocSetFree(), AllocSetGetChunkSpace(), and AllocSetRealloc().

◆ AllocPointerIsValid

#define AllocPointerIsValid (   pointer)    PointerIsValid(pointer)

Definition at line 211 of file aset.c.

◆ ALLOCSET_NUM_FREELISTS

#define ALLOCSET_NUM_FREELISTS   11

Definition at line 83 of file aset.c.

Referenced by AllocSetFreeIndex(), and AllocSetStats().

◆ AllocSetIsValid

#define AllocSetIsValid (   set)    PointerIsValid(set)

Definition at line 217 of file aset.c.

Referenced by AllocSetAlloc(), AllocSetDelete(), and AllocSetReset().

◆ MAX_FREE_CONTEXTS

#define MAX_FREE_CONTEXTS   100 /* arbitrary limit on freelist length */

Definition at line 247 of file aset.c.

Referenced by AllocSetDelete().

Typedef Documentation

◆ AllocBlock

typedef struct AllocBlockData* AllocBlock

Definition at line 106 of file aset.c.

◆ AllocBlockData

◆ AllocChunk

typedef struct AllocChunkData* AllocChunk

Definition at line 107 of file aset.c.

◆ AllocChunkData

◆ AllocPointer

typedef void* AllocPointer

Definition at line 113 of file aset.c.

◆ AllocSet

Definition at line 140 of file aset.c.

◆ AllocSetContext

◆ AllocSetFreeList

Function Documentation

◆ AllocSetAlloc()

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

Definition at line 738 of file aset.c.

References ALLOC_BLOCKHDRSZ, ALLOC_CHUNKHDRSZ, ALLOC_MINBITS, AllocAllocInfo, ALLOCCHUNK_PRIVATE_LEN, AllocChunkGetPointer, AllocSetFreeIndex(), AllocSetIsValid, AllocBlockData::aset, AllocChunkData::aset, Assert, AssertArg, AllocBlockData::endptr, AllocBlockData::freeptr, malloc, MAXALIGN, MemoryContextData::mem_allocated, AllocBlockData::next, AllocBlockData::prev, AllocChunkData::size, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MAKE_MEM_UNDEFINED.

Referenced by AllocSetRealloc().

739 {
740  AllocSet set = (AllocSet) context;
741  AllocBlock block;
742  AllocChunk chunk;
743  int fidx;
744  Size chunk_size;
745  Size blksize;
746 
748 
749  /*
750  * If requested size exceeds maximum for chunks, allocate an entire block
751  * for this request.
752  */
753  if (size > set->allocChunkLimit)
754  {
755  chunk_size = MAXALIGN(size);
756  blksize = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
757  block = (AllocBlock) malloc(blksize);
758  if (block == NULL)
759  return NULL;
760 
761  context->mem_allocated += blksize;
762 
763  block->aset = set;
764  block->freeptr = block->endptr = ((char *) block) + blksize;
765 
766  chunk = (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ);
767  chunk->aset = set;
768  chunk->size = chunk_size;
769 #ifdef MEMORY_CONTEXT_CHECKING
770  chunk->requested_size = size;
771  /* set mark to catch clobber of "unused" space */
772  if (size < chunk_size)
773  set_sentinel(AllocChunkGetPointer(chunk), size);
774 #endif
775 #ifdef RANDOMIZE_ALLOCATED_MEMORY
776  /* fill the allocated space with junk */
777  randomize_mem((char *) AllocChunkGetPointer(chunk), size);
778 #endif
779 
780  /*
781  * Stick the new block underneath the active allocation block, if any,
782  * so that we don't lose the use of the space remaining therein.
783  */
784  if (set->blocks != NULL)
785  {
786  block->prev = set->blocks;
787  block->next = set->blocks->next;
788  if (block->next)
789  block->next->prev = block;
790  set->blocks->next = block;
791  }
792  else
793  {
794  block->prev = NULL;
795  block->next = NULL;
796  set->blocks = block;
797  }
798 
799  AllocAllocInfo(set, chunk);
800 
801  /* Ensure any padding bytes are marked NOACCESS. */
802  VALGRIND_MAKE_MEM_NOACCESS((char *) AllocChunkGetPointer(chunk) + size,
803  chunk_size - size);
804 
805  /* Disallow external access to private part of chunk header. */
807 
808  return AllocChunkGetPointer(chunk);
809  }
810 
811  /*
812  * Request is small enough to be treated as a chunk. Look in the
813  * corresponding free list to see if there is a free chunk we could reuse.
814  * If one is found, remove it from the free list, make it again a member
815  * of the alloc set and return its data address.
816  */
817  fidx = AllocSetFreeIndex(size);
818  chunk = set->freelist[fidx];
819  if (chunk != NULL)
820  {
821  Assert(chunk->size >= size);
822 
823  set->freelist[fidx] = (AllocChunk) chunk->aset;
824 
825  chunk->aset = (void *) set;
826 
827 #ifdef MEMORY_CONTEXT_CHECKING
828  chunk->requested_size = size;
829  /* set mark to catch clobber of "unused" space */
830  if (size < chunk->size)
831  set_sentinel(AllocChunkGetPointer(chunk), size);
832 #endif
833 #ifdef RANDOMIZE_ALLOCATED_MEMORY
834  /* fill the allocated space with junk */
835  randomize_mem((char *) AllocChunkGetPointer(chunk), size);
836 #endif
837 
838  AllocAllocInfo(set, chunk);
839 
840  /* Ensure any padding bytes are marked NOACCESS. */
841  VALGRIND_MAKE_MEM_NOACCESS((char *) AllocChunkGetPointer(chunk) + size,
842  chunk->size - size);
843 
844  /* Disallow external access to private part of chunk header. */
846 
847  return AllocChunkGetPointer(chunk);
848  }
849 
850  /*
851  * Choose the actual chunk size to allocate.
852  */
853  chunk_size = (1 << ALLOC_MINBITS) << fidx;
854  Assert(chunk_size >= size);
855 
856  /*
857  * If there is enough room in the active allocation block, we will put the
858  * chunk into that block. Else must start a new one.
859  */
860  if ((block = set->blocks) != NULL)
861  {
862  Size availspace = block->endptr - block->freeptr;
863 
864  if (availspace < (chunk_size + ALLOC_CHUNKHDRSZ))
865  {
866  /*
867  * The existing active (top) block does not have enough room for
868  * the requested allocation, but it might still have a useful
869  * amount of space in it. Once we push it down in the block list,
870  * we'll never try to allocate more space from it. So, before we
871  * do that, carve up its free space into chunks that we can put on
872  * the set's freelists.
873  *
874  * Because we can only get here when there's less than
875  * ALLOC_CHUNK_LIMIT left in the block, this loop cannot iterate
876  * more than ALLOCSET_NUM_FREELISTS-1 times.
877  */
878  while (availspace >= ((1 << ALLOC_MINBITS) + ALLOC_CHUNKHDRSZ))
879  {
880  Size availchunk = availspace - ALLOC_CHUNKHDRSZ;
881  int a_fidx = AllocSetFreeIndex(availchunk);
882 
883  /*
884  * In most cases, we'll get back the index of the next larger
885  * freelist than the one we need to put this chunk on. The
886  * exception is when availchunk is exactly a power of 2.
887  */
888  if (availchunk != ((Size) 1 << (a_fidx + ALLOC_MINBITS)))
889  {
890  a_fidx--;
891  Assert(a_fidx >= 0);
892  availchunk = ((Size) 1 << (a_fidx + ALLOC_MINBITS));
893  }
894 
895  chunk = (AllocChunk) (block->freeptr);
896 
897  /* Prepare to initialize the chunk header. */
898  VALGRIND_MAKE_MEM_UNDEFINED(chunk, ALLOC_CHUNKHDRSZ);
899 
900  block->freeptr += (availchunk + ALLOC_CHUNKHDRSZ);
901  availspace -= (availchunk + ALLOC_CHUNKHDRSZ);
902 
903  chunk->size = availchunk;
904 #ifdef MEMORY_CONTEXT_CHECKING
905  chunk->requested_size = 0; /* mark it free */
906 #endif
907  chunk->aset = (void *) set->freelist[a_fidx];
908  set->freelist[a_fidx] = chunk;
909  }
910 
911  /* Mark that we need to create a new block */
912  block = NULL;
913  }
914  }
915 
916  /*
917  * Time to create a new regular (multi-chunk) block?
918  */
919  if (block == NULL)
920  {
921  Size required_size;
922 
923  /*
924  * The first such block has size initBlockSize, and we double the
925  * space in each succeeding block, but not more than maxBlockSize.
926  */
927  blksize = set->nextBlockSize;
928  set->nextBlockSize <<= 1;
929  if (set->nextBlockSize > set->maxBlockSize)
930  set->nextBlockSize = set->maxBlockSize;
931 
932  /*
933  * If initBlockSize is less than ALLOC_CHUNK_LIMIT, we could need more
934  * space... but try to keep it a power of 2.
935  */
936  required_size = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
937  while (blksize < required_size)
938  blksize <<= 1;
939 
940  /* Try to allocate it */
941  block = (AllocBlock) malloc(blksize);
942 
943  /*
944  * We could be asking for pretty big blocks here, so cope if malloc
945  * fails. But give up if there's less than 1 MB or so available...
946  */
947  while (block == NULL && blksize > 1024 * 1024)
948  {
949  blksize >>= 1;
950  if (blksize < required_size)
951  break;
952  block = (AllocBlock) malloc(blksize);
953  }
954 
955  if (block == NULL)
956  return NULL;
957 
958  context->mem_allocated += blksize;
959 
960  block->aset = set;
961  block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
962  block->endptr = ((char *) block) + blksize;
963 
964  /* Mark unallocated space NOACCESS. */
966  blksize - ALLOC_BLOCKHDRSZ);
967 
968  block->prev = NULL;
969  block->next = set->blocks;
970  if (block->next)
971  block->next->prev = block;
972  set->blocks = block;
973  }
974 
975  /*
976  * OK, do the allocation
977  */
978  chunk = (AllocChunk) (block->freeptr);
979 
980  /* Prepare to initialize the chunk header. */
981  VALGRIND_MAKE_MEM_UNDEFINED(chunk, ALLOC_CHUNKHDRSZ);
982 
983  block->freeptr += (chunk_size + ALLOC_CHUNKHDRSZ);
984  Assert(block->freeptr <= block->endptr);
985 
986  chunk->aset = (void *) set;
987  chunk->size = chunk_size;
988 #ifdef MEMORY_CONTEXT_CHECKING
989  chunk->requested_size = size;
990  /* set mark to catch clobber of "unused" space */
991  if (size < chunk->size)
992  set_sentinel(AllocChunkGetPointer(chunk), size);
993 #endif
994 #ifdef RANDOMIZE_ALLOCATED_MEMORY
995  /* fill the allocated space with junk */
996  randomize_mem((char *) AllocChunkGetPointer(chunk), size);
997 #endif
998 
999  AllocAllocInfo(set, chunk);
1000 
1001  /* Ensure any padding bytes are marked NOACCESS. */
1002  VALGRIND_MAKE_MEM_NOACCESS((char *) AllocChunkGetPointer(chunk) + size,
1003  chunk_size - size);
1004 
1005  /* Disallow external access to private part of chunk header. */
1007 
1008  return AllocChunkGetPointer(chunk);
1009 }
#define AllocSetIsValid(set)
Definition: aset.c:217
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
static int AllocSetFreeIndex(Size size)
Definition: aset.c:326
#define AllocChunkGetPointer(chk)
Definition: aset.c:221
#define ALLOCCHUNK_PRIVATE_LEN
Definition: aset.c:205
#define ALLOC_BLOCKHDRSZ
Definition: aset.c:103
AllocSet aset
Definition: aset.c:156
char * freeptr
Definition: aset.c:159
#define malloc(a)
Definition: header.h:50
char * endptr
Definition: aset.c:160
#define ALLOC_CHUNKHDRSZ
Definition: aset.c:104
AllocBlock next
Definition: aset.c:158
#define AssertArg(condition)
Definition: c.h:740
struct AllocBlockData * AllocBlock
Definition: aset.c:106
AllocBlock prev
Definition: aset.c:157
struct AllocChunkData * AllocChunk
Definition: aset.c:107
#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
void * aset
Definition: aset.c:195
AllocSetContext * AllocSet
Definition: aset.c:140
#define ALLOC_MINBITS
Definition: aset.c:82
#define AllocAllocInfo(_cxt, _chunk)
Definition: aset.c:314
Size size
Definition: aset.c:178

◆ AllocSetContextCreateInternal()

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

Definition at line 396 of file aset.c.

References ALLOC_BLOCKHDRSZ, ALLOC_CHUNK_FRACTION, ALLOC_CHUNK_LIMIT, ALLOC_CHUNKHDRSZ, AllocHugeSizeIsValid, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_SEPARATE_THRESHOLD, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MINSIZE, AllocSetMethods, AllocBlockData::aset, Assert, AllocBlockData::endptr, ereport, errcode(), errdetail(), errmsg(), ERROR, AllocSetFreeList::first_free, AllocSetContext::freelist, AllocSetContext::freeListIndex, AllocBlockData::freeptr, AllocSetContext::header, AllocSetContext::initBlockSize, malloc, Max, MAXALIGN, AllocSetContext::maxBlockSize, MemoryContextCreate(), MemoryContextStats(), MemSetAligned, name, AllocBlockData::next, MemoryContextData::nextchild, AllocSetFreeList::num_free, offsetof, AllocBlockData::prev, StaticAssertStmt, T_AllocSetContext, TopMemoryContext, and VALGRIND_MAKE_MEM_NOACCESS.

Referenced by GetMemoryChunkContext().

401 {
402  int freeListIndex;
403  Size firstBlockSize;
404  AllocSet set;
405  AllocBlock block;
406 
407  /* Assert we padded AllocChunkData properly */
409  "sizeof(AllocChunkData) is not maxaligned");
412  "padding calculation in AllocChunkData is wrong");
413 
414  /*
415  * First, validate allocation parameters. Once these were regular runtime
416  * test and elog's, but in practice Asserts seem sufficient because nobody
417  * varies their parameters at runtime. We somewhat arbitrarily enforce a
418  * minimum 1K block size.
419  */
420  Assert(initBlockSize == MAXALIGN(initBlockSize) &&
421  initBlockSize >= 1024);
422  Assert(maxBlockSize == MAXALIGN(maxBlockSize) &&
423  maxBlockSize >= initBlockSize &&
424  AllocHugeSizeIsValid(maxBlockSize)); /* must be safe to double */
425  Assert(minContextSize == 0 ||
426  (minContextSize == MAXALIGN(minContextSize) &&
427  minContextSize >= 1024 &&
428  minContextSize <= maxBlockSize));
429 
430  /*
431  * Check whether the parameters match either available freelist. We do
432  * not need to demand a match of maxBlockSize.
433  */
434  if (minContextSize == ALLOCSET_DEFAULT_MINSIZE &&
435  initBlockSize == ALLOCSET_DEFAULT_INITSIZE)
436  freeListIndex = 0;
437  else if (minContextSize == ALLOCSET_SMALL_MINSIZE &&
438  initBlockSize == ALLOCSET_SMALL_INITSIZE)
439  freeListIndex = 1;
440  else
441  freeListIndex = -1;
442 
443  /*
444  * If a suitable freelist entry exists, just recycle that context.
445  */
446  if (freeListIndex >= 0)
447  {
448  AllocSetFreeList *freelist = &context_freelists[freeListIndex];
449 
450  if (freelist->first_free != NULL)
451  {
452  /* Remove entry from freelist */
453  set = freelist->first_free;
454  freelist->first_free = (AllocSet) set->header.nextchild;
455  freelist->num_free--;
456 
457  /* Update its maxBlockSize; everything else should be OK */
458  set->maxBlockSize = maxBlockSize;
459 
460  /* Reinitialize its header, installing correct name and parent */
464  parent,
465  name);
466 
467  ((MemoryContext) set)->mem_allocated =
468  set->keeper->endptr - ((char *) set);
469 
470  return (MemoryContext) set;
471  }
472  }
473 
474  /* Determine size of initial block */
475  firstBlockSize = MAXALIGN(sizeof(AllocSetContext)) +
477  if (minContextSize != 0)
478  firstBlockSize = Max(firstBlockSize, minContextSize);
479  else
480  firstBlockSize = Max(firstBlockSize, initBlockSize);
481 
482  /*
483  * Allocate the initial block. Unlike other aset.c blocks, it starts with
484  * the context header and its block header follows that.
485  */
486  set = (AllocSet) malloc(firstBlockSize);
487  if (set == NULL)
488  {
489  if (TopMemoryContext)
491  ereport(ERROR,
492  (errcode(ERRCODE_OUT_OF_MEMORY),
493  errmsg("out of memory"),
494  errdetail("Failed while creating memory context \"%s\".",
495  name)));
496  }
497 
498  /*
499  * Avoid writing code that can fail between here and MemoryContextCreate;
500  * we'd leak the header/initial block if we ereport in this stretch.
501  */
502 
503  /* Fill in the initial block's block header */
504  block = (AllocBlock) (((char *) set) + MAXALIGN(sizeof(AllocSetContext)));
505  block->aset = set;
506  block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
507  block->endptr = ((char *) set) + firstBlockSize;
508  block->prev = NULL;
509  block->next = NULL;
510 
511  /* Mark unallocated space NOACCESS; leave the block header alone. */
512  VALGRIND_MAKE_MEM_NOACCESS(block->freeptr, block->endptr - block->freeptr);
513 
514  /* Remember block as part of block list */
515  set->blocks = block;
516  /* Mark block as not to be released at reset time */
517  set->keeper = block;
518 
519  /* Finish filling in aset-specific parts of the context header */
520  MemSetAligned(set->freelist, 0, sizeof(set->freelist));
521 
522  set->initBlockSize = initBlockSize;
523  set->maxBlockSize = maxBlockSize;
524  set->nextBlockSize = initBlockSize;
525  set->freeListIndex = freeListIndex;
526 
527  /*
528  * Compute the allocation chunk size limit for this context. It can't be
529  * more than ALLOC_CHUNK_LIMIT because of the fixed number of freelists.
530  * If maxBlockSize is small then requests exceeding the maxBlockSize, or
531  * even a significant fraction of it, should be treated as large chunks
532  * too. For the typical case of maxBlockSize a power of 2, the chunk size
533  * limit will be at most 1/8th maxBlockSize, so that given a stream of
534  * requests that are all the maximum chunk size we will waste at most
535  * 1/8th of the allocated space.
536  *
537  * We have to have allocChunkLimit a power of two, because the requested
538  * and actually-allocated sizes of any chunk must be on the same side of
539  * the limit, else we get confused about whether the chunk is "big".
540  *
541  * Also, allocChunkLimit must not exceed ALLOCSET_SEPARATE_THRESHOLD.
542  */
544  "ALLOC_CHUNK_LIMIT != ALLOCSET_SEPARATE_THRESHOLD");
545 
546  set->allocChunkLimit = ALLOC_CHUNK_LIMIT;
547  while ((Size) (set->allocChunkLimit + ALLOC_CHUNKHDRSZ) >
548  (Size) ((maxBlockSize - ALLOC_BLOCKHDRSZ) / ALLOC_CHUNK_FRACTION))
549  set->allocChunkLimit >>= 1;
550 
551  /* Finally, do the type-independent part of context creation */
555  parent,
556  name);
557 
558  ((MemoryContext) set)->mem_allocated = firstBlockSize;
559 
560  return (MemoryContext) set;
561 }
#define MemSetAligned(start, val, len)
Definition: c.h:1004
MemoryContextData header
Definition: aset.c:126
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
int num_free
Definition: aset.c:251
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ALLOC_BLOCKHDRSZ
Definition: aset.c:103
AllocSet aset
Definition: aset.c:156
char * freeptr
Definition: aset.c:159
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:189
#define ALLOCSET_SMALL_MINSIZE
Definition: memutils.h:199
AllocSetContext * first_free
Definition: aset.c:252
#define malloc(a)
Definition: header.h:50
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:852
static AllocSetFreeList context_freelists[2]
Definition: aset.c:256
#define ERROR
Definition: elog.h:43
#define ALLOC_CHUNK_LIMIT
Definition: aset.c:84
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:498
char * endptr
Definition: aset.c:160
#define ALLOCSET_SEPARATE_THRESHOLD
Definition: memutils.h:219
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define ALLOC_CHUNKHDRSZ
Definition: aset.c:104
AllocBlock next
Definition: aset.c:158
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
struct MemoryContextData * MemoryContext
Definition: palloc.h:36
#define ALLOCSET_SMALL_INITSIZE
Definition: memutils.h:200
#define ALLOC_CHUNK_FRACTION
Definition: aset.c:86
struct AllocBlockData * AllocBlock
Definition: aset.c:106
AllocBlock prev
Definition: aset.c:157
#define Max(x, y)
Definition: c.h:914
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
const char * name
Definition: encode.c:521
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:190
static const MemoryContextMethods AllocSetMethods
Definition: aset.c:287
AllocSetContext * AllocSet
Definition: aset.c:140
#define offsetof(type, field)
Definition: c.h:661
MemoryContext nextchild
Definition: memnodes.h:87

◆ AllocSetDelete()

static void AllocSetDelete ( MemoryContext  context)
static

Definition at line 644 of file aset.c.

References AllocSetIsValid, Assert, AssertArg, AllocBlockData::endptr, AllocSetFreeList::first_free, free, AllocSetContext::freelist, AllocBlockData::freeptr, AllocSetContext::header, MemoryContextData::isReset, MAX_FREE_CONTEXTS, MemoryContextData::mem_allocated, MemoryContextResetOnly(), AllocBlockData::next, next, MemoryContextData::nextchild, AllocSetFreeList::num_free, and PG_USED_FOR_ASSERTS_ONLY.

645 {
646  AllocSet set = (AllocSet) context;
647  AllocBlock block = set->blocks;
648  Size keepersize PG_USED_FOR_ASSERTS_ONLY
649  = set->keeper->endptr - ((char *) set);
650 
652 
653 #ifdef MEMORY_CONTEXT_CHECKING
654  /* Check for corruption and leaks before freeing */
655  AllocSetCheck(context);
656 #endif
657 
658  /*
659  * If the context is a candidate for a freelist, put it into that freelist
660  * instead of destroying it.
661  */
662  if (set->freeListIndex >= 0)
663  {
664  AllocSetFreeList *freelist = &context_freelists[set->freeListIndex];
665 
666  /*
667  * Reset the context, if it needs it, so that we aren't hanging on to
668  * more than the initial malloc chunk.
669  */
670  if (!context->isReset)
671  MemoryContextResetOnly(context);
672 
673  /*
674  * If the freelist is full, just discard what's already in it. See
675  * comments with context_freelists[].
676  */
677  if (freelist->num_free >= MAX_FREE_CONTEXTS)
678  {
679  while (freelist->first_free != NULL)
680  {
681  AllocSetContext *oldset = freelist->first_free;
682 
683  freelist->first_free = (AllocSetContext *) oldset->header.nextchild;
684  freelist->num_free--;
685 
686  /* All that remains is to free the header/initial block */
687  free(oldset);
688  }
689  Assert(freelist->num_free == 0);
690  }
691 
692  /* Now add the just-deleted context to the freelist. */
693  set->header.nextchild = (MemoryContext) freelist->first_free;
694  freelist->first_free = set;
695  freelist->num_free++;
696 
697  return;
698  }
699 
700  /* Free all blocks, except the keeper which is part of context header */
701  while (block != NULL)
702  {
703  AllocBlock next = block->next;
704 
705  if (block != set->keeper)
706  context->mem_allocated -= block->endptr - ((char *) block);
707 
708 #ifdef CLOBBER_FREED_MEMORY
709  wipe_mem(block, block->freeptr - ((char *) block));
710 #endif
711 
712  if (block != set->keeper)
713  free(block);
714 
715  block = next;
716  }
717 
718  Assert(context->mem_allocated == keepersize);
719 
720  /* Finally, free the context header, including the keeper block */
721  free(set);
722 }
static int32 next
Definition: blutils.c:217
MemoryContextData header
Definition: aset.c:126
#define AllocSetIsValid(set)
Definition: aset.c:217
int num_free
Definition: aset.c:251
char * freeptr
Definition: aset.c:159
void MemoryContextResetOnly(MemoryContext context)
Definition: mcxt.c:155
AllocSetContext * first_free
Definition: aset.c:252
static AllocSetFreeList context_freelists[2]
Definition: aset.c:256
char * endptr
Definition: aset.c:160
AllocBlock next
Definition: aset.c:158
#define AssertArg(condition)
Definition: c.h:740
struct MemoryContextData * MemoryContext
Definition: palloc.h:36
#define MAX_FREE_CONTEXTS
Definition: aset.c:247
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
Size mem_allocated
Definition: memnodes.h:82
AllocSetContext * AllocSet
Definition: aset.c:140
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:121
MemoryContext nextchild
Definition: memnodes.h:87

◆ AllocSetFree()

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

Definition at line 1016 of file aset.c.

References ALLOC_BLOCKHDRSZ, ALLOC_CHUNKHDRSZ, ALLOCCHUNK_PRIVATE_LEN, AllocFreeInfo, AllocPointerGetChunk, AllocSetFreeIndex(), AllocBlockData::aset, AllocChunkData::aset, elog, AllocBlockData::endptr, ERROR, free, AllocBlockData::freeptr, MemoryContextData::mem_allocated, AllocChunkData::size, VALGRIND_MAKE_MEM_DEFINED, and WARNING.

Referenced by AllocSetRealloc().

1017 {
1018  AllocSet set = (AllocSet) context;
1019  AllocChunk chunk = AllocPointerGetChunk(pointer);
1020 
1021  /* Allow access to private part of chunk header. */
1023 
1024  AllocFreeInfo(set, chunk);
1025 
1026 #ifdef MEMORY_CONTEXT_CHECKING
1027  /* Test for someone scribbling on unused space in chunk */
1028  if (chunk->requested_size < chunk->size)
1029  if (!sentinel_ok(pointer, chunk->requested_size))
1030  elog(WARNING, "detected write past chunk end in %s %p",
1031  set->header.name, chunk);
1032 #endif
1033 
1034  if (chunk->size > set->allocChunkLimit)
1035  {
1036  /*
1037  * Big chunks are certain to have been allocated as single-chunk
1038  * blocks. Just unlink that block and return it to malloc().
1039  */
1040  AllocBlock block = (AllocBlock) (((char *) chunk) - ALLOC_BLOCKHDRSZ);
1041 
1042  /*
1043  * Try to verify that we have a sane block pointer: it should
1044  * reference the correct aset, and freeptr and endptr should point
1045  * just past the chunk.
1046  */
1047  if (block->aset != set ||
1048  block->freeptr != block->endptr ||
1049  block->freeptr != ((char *) block) +
1050  (chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
1051  elog(ERROR, "could not find block containing chunk %p", chunk);
1052 
1053  /* OK, remove block from aset's list and free it */
1054  if (block->prev)
1055  block->prev->next = block->next;
1056  else
1057  set->blocks = block->next;
1058  if (block->next)
1059  block->next->prev = block->prev;
1060 
1061  context->mem_allocated -= block->endptr - ((char*) block);
1062 
1063 #ifdef CLOBBER_FREED_MEMORY
1064  wipe_mem(block, block->freeptr - ((char *) block));
1065 #endif
1066  free(block);
1067  }
1068  else
1069  {
1070  /* Normal case, put the chunk into appropriate freelist */
1071  int fidx = AllocSetFreeIndex(chunk->size);
1072 
1073  chunk->aset = (void *) set->freelist[fidx];
1074 
1075 #ifdef CLOBBER_FREED_MEMORY
1076  wipe_mem(pointer, chunk->size);
1077 #endif
1078 
1079 #ifdef MEMORY_CONTEXT_CHECKING
1080  /* Reset requested_size to 0 in chunks that are on freelist */
1081  chunk->requested_size = 0;
1082 #endif
1083  set->freelist[fidx] = chunk;
1084  }
1085 }
#define AllocFreeInfo(_cxt, _chunk)
Definition: aset.c:313
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
static int AllocSetFreeIndex(Size size)
Definition: aset.c:326
#define ALLOCCHUNK_PRIVATE_LEN
Definition: aset.c:205
#define ALLOC_BLOCKHDRSZ
Definition: aset.c:103
AllocSet aset
Definition: aset.c:156
char * freeptr
Definition: aset.c:159
#define ERROR
Definition: elog.h:43
char * endptr
Definition: aset.c:160
#define ALLOC_CHUNKHDRSZ
Definition: aset.c:104
#define WARNING
Definition: elog.h:40
struct AllocBlockData * AllocBlock
Definition: aset.c:106
#define free(a)
Definition: header.h:65
Size mem_allocated
Definition: memnodes.h:82
#define elog(elevel,...)
Definition: elog.h:228
AllocSetContext * AllocSet
Definition: aset.c:140
Size size
Definition: aset.c:178
#define AllocPointerGetChunk(ptr)
Definition: aset.c:219

◆ AllocSetFreeIndex()

static int AllocSetFreeIndex ( Size  size)
inlinestatic

Definition at line 326 of file aset.c.

References ALLOC_CHUNK_LIMIT, ALLOC_MINBITS, ALLOCSET_NUM_FREELISTS, Assert, idx(), pg_leftmost_one_pos, and StaticAssertStmt.

Referenced by AllocSetAlloc(), and AllocSetFree().

327 {
328  int idx;
329 
330  if (size > (1 << ALLOC_MINBITS))
331  {
332  /*----------
333  * At this point we must compute ceil(log2(size >> ALLOC_MINBITS)).
334  * This is the same as
335  * pg_leftmost_one_pos32((size - 1) >> ALLOC_MINBITS) + 1
336  * or equivalently
337  * pg_leftmost_one_pos32(size - 1) - ALLOC_MINBITS + 1
338  *
339  * However, rather than just calling that function, we duplicate the
340  * logic here, allowing an additional optimization. It's reasonable
341  * to assume that ALLOC_CHUNK_LIMIT fits in 16 bits, so we can unroll
342  * the byte-at-a-time loop in pg_leftmost_one_pos32 and just handle
343  * the last two bytes.
344  *
345  * Yes, this function is enough of a hot-spot to make it worth this
346  * much trouble.
347  *----------
348  */
349 #ifdef HAVE__BUILTIN_CLZ
350  idx = 31 - __builtin_clz((uint32) size - 1) - ALLOC_MINBITS + 1;
351 #else
352  uint32 t,
353  tsize;
354 
355  /* Statically assert that we only have a 16-bit input value. */
357  "ALLOC_CHUNK_LIMIT must be less than 64kB");
358 
359  tsize = size - 1;
360  t = tsize >> 8;
361  idx = t ? pg_leftmost_one_pos[t] + 8 : pg_leftmost_one_pos[tsize];
362  idx -= ALLOC_MINBITS - 1;
363 #endif
364 
366  }
367  else
368  idx = 0;
369 
370  return idx;
371 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:852
#define ALLOC_CHUNK_LIMIT
Definition: aset.c:84
PGDLLIMPORT const uint8 pg_leftmost_one_pos[256]
Definition: pg_bitutils.c:34
#define ALLOCSET_NUM_FREELISTS
Definition: aset.c:83
unsigned int uint32
Definition: c.h:367
#define Assert(condition)
Definition: c.h:738
#define ALLOC_MINBITS
Definition: aset.c:82

◆ AllocSetGetChunkSpace()

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

Definition at line 1329 of file aset.c.

References ALLOC_CHUNKHDRSZ, ALLOCCHUNK_PRIVATE_LEN, AllocPointerGetChunk, AllocChunkData::size, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

1330 {
1331  AllocChunk chunk = AllocPointerGetChunk(pointer);
1332  Size result;
1333 
1335  result = chunk->size + ALLOC_CHUNKHDRSZ;
1337  return result;
1338 }
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
#define ALLOCCHUNK_PRIVATE_LEN
Definition: aset.c:205
#define ALLOC_CHUNKHDRSZ
Definition: aset.c:104
size_t Size
Definition: c.h:466
Size size
Definition: aset.c:178
#define AllocPointerGetChunk(ptr)
Definition: aset.c:219

◆ AllocSetIsEmpty()

static bool AllocSetIsEmpty ( MemoryContext  context)
static

Definition at line 1345 of file aset.c.

References MemoryContextData::isReset.

1346 {
1347  /*
1348  * For now, we say "empty" only if the context is new or just reset. We
1349  * could examine the freelists to determine if all space has been freed,
1350  * but it's not really worth the trouble for present uses of this
1351  * functionality.
1352  */
1353  if (context->isReset)
1354  return true;
1355  return false;
1356 }

◆ AllocSetRealloc()

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

Definition at line 1100 of file aset.c.

References ALLOC_BLOCKHDRSZ, ALLOC_CHUNKHDRSZ, ALLOCCHUNK_PRIVATE_LEN, AllocChunkGetPointer, AllocPointerGetChunk, AllocSetAlloc(), AllocSetFree(), AllocBlockData::aset, elog, AllocBlockData::endptr, ERROR, AllocBlockData::freeptr, Max, MAXALIGN, MemoryContextData::mem_allocated, realloc, AllocChunkData::size, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, VALGRIND_MAKE_MEM_UNDEFINED, and WARNING.

1101 {
1102  AllocSet set = (AllocSet) context;
1103  AllocChunk chunk = AllocPointerGetChunk(pointer);
1104  Size oldsize;
1105 
1106  /* Allow access to private part of chunk header. */
1108 
1109  oldsize = chunk->size;
1110 
1111 #ifdef MEMORY_CONTEXT_CHECKING
1112  /* Test for someone scribbling on unused space in chunk */
1113  if (chunk->requested_size < oldsize)
1114  if (!sentinel_ok(pointer, chunk->requested_size))
1115  elog(WARNING, "detected write past chunk end in %s %p",
1116  set->header.name, chunk);
1117 #endif
1118 
1119  if (oldsize > set->allocChunkLimit)
1120  {
1121  /*
1122  * The chunk must have been allocated as a single-chunk block. Use
1123  * realloc() to make the containing block bigger, or smaller, with
1124  * minimum space wastage.
1125  */
1126  AllocBlock block = (AllocBlock) (((char *) chunk) - ALLOC_BLOCKHDRSZ);
1127  Size chksize;
1128  Size blksize;
1129  Size oldblksize;
1130 
1131  /*
1132  * Try to verify that we have a sane block pointer: it should
1133  * reference the correct aset, and freeptr and endptr should point
1134  * just past the chunk.
1135  */
1136  if (block->aset != set ||
1137  block->freeptr != block->endptr ||
1138  block->freeptr != ((char *) block) +
1139  (oldsize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
1140  elog(ERROR, "could not find block containing chunk %p", chunk);
1141 
1142  /*
1143  * Even if the new request is less than set->allocChunkLimit, we stick
1144  * with the single-chunk block approach. Therefore we need
1145  * chunk->size to be bigger than set->allocChunkLimit, so we don't get
1146  * confused about the chunk's status in future calls.
1147  */
1148  chksize = Max(size, set->allocChunkLimit + 1);
1149  chksize = MAXALIGN(chksize);
1150 
1151  /* Do the realloc */
1152  blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
1153  oldblksize = block->endptr - ((char *)block);
1154 
1155  block = (AllocBlock) realloc(block, blksize);
1156  if (block == NULL)
1157  {
1158  /* Disallow external access to private part of chunk header. */
1160  return NULL;
1161  }
1162 
1163  /* updated separately, not to underflow when (oldblksize > blksize) */
1164  context->mem_allocated -= oldblksize;
1165  context->mem_allocated += blksize;
1166 
1167  block->freeptr = block->endptr = ((char *) block) + blksize;
1168 
1169  /* Update pointers since block has likely been moved */
1170  chunk = (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ);
1171  pointer = AllocChunkGetPointer(chunk);
1172  if (block->prev)
1173  block->prev->next = block;
1174  else
1175  set->blocks = block;
1176  if (block->next)
1177  block->next->prev = block;
1178  chunk->size = chksize;
1179 
1180 #ifdef MEMORY_CONTEXT_CHECKING
1181 #ifdef RANDOMIZE_ALLOCATED_MEMORY
1182  /* We can only fill the extra space if we know the prior request */
1183  if (size > chunk->requested_size)
1184  randomize_mem((char *) pointer + chunk->requested_size,
1185  size - chunk->requested_size);
1186 #endif
1187 
1188  /*
1189  * realloc() (or randomize_mem()) will have left any newly-allocated
1190  * part UNDEFINED, but we may need to adjust trailing bytes from the
1191  * old allocation.
1192  */
1193 #ifdef USE_VALGRIND
1194  if (oldsize > chunk->requested_size)
1195  VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + chunk->requested_size,
1196  oldsize - chunk->requested_size);
1197 #endif
1198 
1199  chunk->requested_size = size;
1200 
1201  /* set mark to catch clobber of "unused" space */
1202  if (size < chunk->size)
1203  set_sentinel(pointer, size);
1204 #else /* !MEMORY_CONTEXT_CHECKING */
1205 
1206  /*
1207  * We don't know how much of the old chunk size was the actual
1208  * allocation; it could have been as small as one byte. We have to be
1209  * conservative and just mark the entire old portion DEFINED.
1210  */
1211  VALGRIND_MAKE_MEM_DEFINED(pointer, oldsize);
1212 #endif
1213 
1214  /* Ensure any padding bytes are marked NOACCESS. */
1215  VALGRIND_MAKE_MEM_NOACCESS((char *) pointer + size, chksize - size);
1216 
1217  /* Disallow external access to private part of chunk header. */
1219 
1220  return pointer;
1221  }
1222 
1223  /*
1224  * Chunk sizes are aligned to power of 2 in AllocSetAlloc(). Maybe the
1225  * allocated area already is >= the new size. (In particular, we will
1226  * fall out here if the requested size is a decrease.)
1227  */
1228  else if (oldsize >= size)
1229  {
1230 #ifdef MEMORY_CONTEXT_CHECKING
1231  Size oldrequest = chunk->requested_size;
1232 
1233 #ifdef RANDOMIZE_ALLOCATED_MEMORY
1234  /* We can only fill the extra space if we know the prior request */
1235  if (size > oldrequest)
1236  randomize_mem((char *) pointer + oldrequest,
1237  size - oldrequest);
1238 #endif
1239 
1240  chunk->requested_size = size;
1241 
1242  /*
1243  * If this is an increase, mark any newly-available part UNDEFINED.
1244  * Otherwise, mark the obsolete part NOACCESS.
1245  */
1246  if (size > oldrequest)
1247  VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + oldrequest,
1248  size - oldrequest);
1249  else
1250  VALGRIND_MAKE_MEM_NOACCESS((char *) pointer + size,
1251  oldsize - size);
1252 
1253  /* set mark to catch clobber of "unused" space */
1254  if (size < oldsize)
1255  set_sentinel(pointer, size);
1256 #else /* !MEMORY_CONTEXT_CHECKING */
1257 
1258  /*
1259  * We don't have the information to determine whether we're growing
1260  * the old request or shrinking it, so we conservatively mark the
1261  * entire new allocation DEFINED.
1262  */
1263  VALGRIND_MAKE_MEM_NOACCESS(pointer, oldsize);
1264  VALGRIND_MAKE_MEM_DEFINED(pointer, size);
1265 #endif
1266 
1267  /* Disallow external access to private part of chunk header. */
1269 
1270  return pointer;
1271  }
1272  else
1273  {
1274  /*
1275  * Enlarge-a-small-chunk case. We just do this by brute force, ie,
1276  * allocate a new chunk and copy the data. Since we know the existing
1277  * data isn't huge, this won't involve any great memcpy expense, so
1278  * it's not worth being smarter. (At one time we tried to avoid
1279  * memcpy when it was possible to enlarge the chunk in-place, but that
1280  * turns out to misbehave unpleasantly for repeated cycles of
1281  * palloc/repalloc/pfree: the eventually freed chunks go into the
1282  * wrong freelist for the next initial palloc request, and so we leak
1283  * memory indefinitely. See pgsql-hackers archives for 2007-08-11.)
1284  */
1285  AllocPointer newPointer;
1286 
1287  /* allocate new chunk */
1288  newPointer = AllocSetAlloc((MemoryContext) set, size);
1289 
1290  /* leave immediately if request was not completed */
1291  if (newPointer == NULL)
1292  {
1293  /* Disallow external access to private part of chunk header. */
1295  return NULL;
1296  }
1297 
1298  /*
1299  * AllocSetAlloc() may have returned a region that is still NOACCESS.
1300  * Change it to UNDEFINED for the moment; memcpy() will then transfer
1301  * definedness from the old allocation to the new. If we know the old
1302  * allocation, copy just that much. Otherwise, make the entire old
1303  * chunk defined to avoid errors as we copy the currently-NOACCESS
1304  * trailing bytes.
1305  */
1306  VALGRIND_MAKE_MEM_UNDEFINED(newPointer, size);
1307 #ifdef MEMORY_CONTEXT_CHECKING
1308  oldsize = chunk->requested_size;
1309 #else
1310  VALGRIND_MAKE_MEM_DEFINED(pointer, oldsize);
1311 #endif
1312 
1313  /* transfer existing data (certain to fit) */
1314  memcpy(newPointer, pointer, oldsize);
1315 
1316  /* free old chunk */
1317  AllocSetFree((MemoryContext) set, pointer);
1318 
1319  return newPointer;
1320  }
1321 }
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
void * AllocPointer
Definition: aset.c:113
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
#define AllocChunkGetPointer(chk)
Definition: aset.c:221
#define ALLOCCHUNK_PRIVATE_LEN
Definition: aset.c:205
#define ALLOC_BLOCKHDRSZ
Definition: aset.c:103
AllocSet aset
Definition: aset.c:156
char * freeptr
Definition: aset.c:159
#define ERROR
Definition: elog.h:43
char * endptr
Definition: aset.c:160
#define ALLOC_CHUNKHDRSZ
Definition: aset.c:104
#define WARNING
Definition: elog.h:40
struct AllocBlockData * AllocBlock
Definition: aset.c:106
#define Max(x, y)
Definition: c.h:914
struct AllocChunkData * AllocChunk
Definition: aset.c:107
static void AllocSetFree(MemoryContext context, void *pointer)
Definition: aset.c:1016
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
#define realloc(a, b)
Definition: header.h:60
Size mem_allocated
Definition: memnodes.h:82
#define elog(elevel,...)
Definition: elog.h:228
static void * AllocSetAlloc(MemoryContext context, Size size)
Definition: aset.c:738
AllocSetContext * AllocSet
Definition: aset.c:140
Size size
Definition: aset.c:178
#define AllocPointerGetChunk(ptr)
Definition: aset.c:219

◆ AllocSetReset()

static void AllocSetReset ( MemoryContext  context)
static

Definition at line 576 of file aset.c.

References ALLOC_BLOCKHDRSZ, AllocSetIsValid, Assert, AssertArg, AllocBlockData::endptr, free, AllocBlockData::freeptr, MemoryContextData::mem_allocated, MemSetAligned, AllocBlockData::next, next, PG_USED_FOR_ASSERTS_ONLY, AllocBlockData::prev, and VALGRIND_MAKE_MEM_NOACCESS.

577 {
578  AllocSet set = (AllocSet) context;
579  AllocBlock block;
580  Size keepersize PG_USED_FOR_ASSERTS_ONLY
581  = set->keeper->endptr - ((char *) set);
582 
584 
585 #ifdef MEMORY_CONTEXT_CHECKING
586  /* Check for corruption and leaks before freeing */
587  AllocSetCheck(context);
588 #endif
589 
590  /* Clear chunk freelists */
591  MemSetAligned(set->freelist, 0, sizeof(set->freelist));
592 
593  block = set->blocks;
594 
595  /* New blocks list will be just the keeper block */
596  set->blocks = set->keeper;
597 
598  while (block != NULL)
599  {
600  AllocBlock next = block->next;
601 
602  if (block == set->keeper)
603  {
604  /* Reset the block, but don't return it to malloc */
605  char *datastart = ((char *) block) + ALLOC_BLOCKHDRSZ;
606 
607 #ifdef CLOBBER_FREED_MEMORY
608  wipe_mem(datastart, block->freeptr - datastart);
609 #else
610  /* wipe_mem() would have done this */
611  VALGRIND_MAKE_MEM_NOACCESS(datastart, block->freeptr - datastart);
612 #endif
613  block->freeptr = datastart;
614  block->prev = NULL;
615  block->next = NULL;
616  }
617  else
618  {
619  /* Normal case, release the block */
620  context->mem_allocated -= block->endptr - ((char*) block);
621 
622 #ifdef CLOBBER_FREED_MEMORY
623  wipe_mem(block, block->freeptr - ((char *) block));
624 #endif
625  free(block);
626  }
627  block = next;
628  }
629 
630  Assert(context->mem_allocated == keepersize);
631 
632  /* Reset block size allocation sequence, too */
633  set->nextBlockSize = set->initBlockSize;
634 }
#define MemSetAligned(start, val, len)
Definition: c.h:1004
static int32 next
Definition: blutils.c:217
#define AllocSetIsValid(set)
Definition: aset.c:217
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
#define ALLOC_BLOCKHDRSZ
Definition: aset.c:103
char * freeptr
Definition: aset.c:159
char * endptr
Definition: aset.c:160
AllocBlock next
Definition: aset.c:158
#define AssertArg(condition)
Definition: c.h:740
AllocBlock prev
Definition: aset.c:157
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
Size mem_allocated
Definition: memnodes.h:82
AllocSetContext * AllocSet
Definition: aset.c:140
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:121

◆ AllocSetStats()

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

Definition at line 1367 of file aset.c.

References ALLOC_BLOCKHDRSZ, ALLOC_CHUNKHDRSZ, ALLOC_MINBITS, ALLOCCHUNK_PRIVATE_LEN, ALLOCSET_NUM_FREELISTS, AllocBlockData::aset, AllocChunkData::aset, Assert, elog, AllocBlockData::endptr, MemoryContextCounters::freechunks, AllocBlockData::freeptr, MemoryContextCounters::freespace, MAXALIGN, MemoryContextData::mem_allocated, name, MemoryContextCounters::nblocks, AllocBlockData::next, AllocBlockData::prev, AllocChunkData::size, snprintf, MemoryContextCounters::totalspace, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, and WARNING.

1370 {
1371  AllocSet set = (AllocSet) context;
1372  Size nblocks = 0;
1373  Size freechunks = 0;
1374  Size totalspace;
1375  Size freespace = 0;
1376  AllocBlock block;
1377  int fidx;
1378 
1379  /* Include context header in totalspace */
1380  totalspace = MAXALIGN(sizeof(AllocSetContext));
1381 
1382  for (block = set->blocks; block != NULL; block = block->next)
1383  {
1384  nblocks++;
1385  totalspace += block->endptr - ((char *) block);
1386  freespace += block->endptr - block->freeptr;
1387  }
1388  for (fidx = 0; fidx < ALLOCSET_NUM_FREELISTS; fidx++)
1389  {
1390  AllocChunk chunk;
1391 
1392  for (chunk = set->freelist[fidx]; chunk != NULL;
1393  chunk = (AllocChunk) chunk->aset)
1394  {
1395  freechunks++;
1396  freespace += chunk->size + ALLOC_CHUNKHDRSZ;
1397  }
1398  }
1399 
1400  if (printfunc)
1401  {
1402  char stats_string[200];
1403 
1404  snprintf(stats_string, sizeof(stats_string),
1405  "%zu total in %zd blocks; %zu free (%zd chunks); %zu used",
1406  totalspace, nblocks, freespace, freechunks,
1407  totalspace - freespace);
1408  printfunc(context, passthru, stats_string);
1409  }
1410 
1411  if (totals)
1412  {
1413  totals->nblocks += nblocks;
1414  totals->freechunks += freechunks;
1415  totals->totalspace += totalspace;
1416  totals->freespace += freespace;
1417  }
1418 }
char * freeptr
Definition: aset.c:159
char * endptr
Definition: aset.c:160
#define ALLOCSET_NUM_FREELISTS
Definition: aset.c:83
#define ALLOC_CHUNKHDRSZ
Definition: aset.c:104
AllocBlock next
Definition: aset.c:158
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
void * aset
Definition: aset.c:195
AllocSetContext * AllocSet
Definition: aset.c:140
#define snprintf
Definition: port.h:192
Size size
Definition: aset.c:178

Variable Documentation

◆ AllocSetMethods

const MemoryContextMethods AllocSetMethods
static
Initial value:
= {
}
static Size AllocSetGetChunkSpace(MemoryContext context, void *pointer)
Definition: aset.c:1329
static void AllocSetReset(MemoryContext context)
Definition: aset.c:576
static void * AllocSetRealloc(MemoryContext context, void *pointer, Size size)
Definition: aset.c:1100
static void AllocSetStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
Definition: aset.c:1367
static bool AllocSetIsEmpty(MemoryContext context)
Definition: aset.c:1345
static void AllocSetFree(MemoryContext context, void *pointer)
Definition: aset.c:1016
static void AllocSetDelete(MemoryContext context)
Definition: aset.c:644
static void * AllocSetAlloc(MemoryContext context, Size size)
Definition: aset.c:738

Definition at line 287 of file aset.c.

Referenced by AllocSetContextCreateInternal().

◆ context_freelists

AllocSetFreeList context_freelists[2]
static
Initial value:
=
{
{
0, NULL
},
{
0, NULL
}
}

Definition at line 256 of file aset.c.