Better integration for BlockAllocatorGetChunkSpace.
authorRobert Haas <rhaas@postgresql.org>
Wed, 4 Jun 2014 17:42:50 +0000 (13:42 -0400)
committerRobert Haas <rhaas@postgresql.org>
Wed, 4 Jun 2014 17:42:50 +0000 (13:42 -0400)
src/backend/utils/mmgr/aregion.c
src/backend/utils/mmgr/balloc.c
src/backend/utils/mmgr/mcxt.c
src/backend/utils/sort/tuplesort.c
src/include/utils/aregion.h
src/include/utils/balloc.h

index f40c46782d9b5e81012dfd822fddbcd8e52f0c67..029ffd023e6db1f7cc7d5bfc3da83d461e34f1ca 100644 (file)
@@ -105,7 +105,7 @@ static dlist_head private_freelist[NUM_PRIVATE_FREELISTS];
 #define AREGION_INITSIZE               (16 * BLOCK_ALLOCATOR_PAGES_PER_CHUNK)
 #define AREGION_MAXSIZE                        ((64 * 1024 * 1024) / FPM_PAGE_SIZE)
 
-static Size aregion_private_pages_allocated = 0;
+Size aregion_private_pages_allocated = 0;
 static Size aregion_private_bytes_allocated = 0;
 static Size aregion_peak_private_bytes_allocated = 0;
 
index 16e5763210495a22e24a72a012b571de7c6a51c2..2c54cb9b30d2e2903a8caff2835cc3c3222fdd56 100644 (file)
@@ -539,19 +539,17 @@ BlockAllocatorGetAllocSpace(Size size)
  * overhead of its own.
  */
 Size
-BlockAllocatorGetChunkSpace(void *ptr)
+BlockAllocatorGetChunkSpace(AllocatorRegion *aregion, void *ptr)
 {
-       AllocatorRegion *region;
        char   *fpm_base;
        BlockAllocatorSpan *span;
        Size    pageno;
        uint16  size_class;
 
        /* Locate the containing block. */
-       region = LookupAllocatorRegion(ptr);
-       fpm_base = fpm_segment_base(region->fpm);
+       fpm_base = fpm_segment_base(aregion->fpm);
        pageno = fpm_pointer_to_page(fpm_base, ptr);
-       span = BlockAllocatorMapGet(region->pagemap, pageno);
+       span = BlockAllocatorMapGet(aregion->pagemap, pageno);
 
        /* Work out the size of the allocation. */      
        size_class = span->size_class;
index 4185a03e9ff1969704c854c4faefc2906414ad00..7b1cc74a46e20aa0c60de339637a1f5482e7e646 100644 (file)
@@ -25,6 +25,7 @@
 #include "postgres.h"
 
 #include "miscadmin.h"
+#include "utils/aregion.h"
 #include "utils/memdebug.h"
 #include "utils/memutils.h"
 
@@ -344,6 +345,20 @@ GetMemoryChunkSpace(void *pointer)
        Assert(pointer != NULL);
        Assert(pointer == (void *) MAXALIGN(pointer));
 
+       /*
+        * If allocator regions are in use, then this pointer might be within
+        * such a region, in which case it won't have a chunk header.  So, we have
+        * to test for and handle that case first.
+        */
+       if (aregion_private_pages_allocated > 0)
+       {
+               AllocatorRegion *aregion;
+
+               aregion = LookupAllocatorRegion(pointer);
+               if (aregion != NULL)
+                       return BlockAllocatorGetChunkSpace(aregion, pointer);
+       }
+
        /*
         * OK, it's probably safe to look at the chunk header.
         */
index f3200fb33d186e7c27f126c10015d30793fb1b82..ad88d1a8e1aaa5af9c2ecd72fcfc5a1372334d61 100644 (file)
@@ -3377,7 +3377,7 @@ writetup_index(Tuplesortstate *state, int tapenum, SortTuple *stup)
                LogicalTapeWrite(state->tapeset, tapenum,
                                                 (void *) &tuplen, sizeof(tuplen));
 
-       FREEMEM(state, BlockAllocatorGetChunkSpace(tuple));
+       FREEMEM(state, GetMemoryChunkSpace(tuple));
        BlockAllocatorFree(tuple);
 }
 
@@ -3389,7 +3389,7 @@ readtup_index(Tuplesortstate *state, SortTuple *stup,
        IndexTuple      tuple;
 
        tuple = (IndexTuple) BlockAllocatorAlloc(state->sortallocator, tuplen);
-       USEMEM(state, BlockAllocatorGetChunkSpace(tuple));
+       USEMEM(state, GetMemoryChunkSpace(tuple));
        LogicalTapeReadExact(state->tapeset, tapenum,
                                                 tuple, tuplen);
        if (state->randomAccess)        /* need trailing length word? */
index 2309a38a2722b60bb54a6aa6e87585ce4aaedc83..a9d9d4cc13ab0876e9632562ddf8a0e92d43a3fb 100644 (file)
@@ -67,4 +67,6 @@ extern AllocatorRegion *LookupAllocatorRegion(void *);
 extern AllocatorRegion *GetRegionForPrivateAllocation(Size npages);
 extern void ReportRegionContiguousFreespace(AllocatorRegion *, Size npages);
 
+extern Size aregion_private_pages_allocated;
+
 #endif         /* AREGION_H */
index e970349a2037c4fdd9795795c22231c2f28f5909..7758b778ea647fa968173abca42161f998e69c99 100644 (file)
@@ -17,6 +17,7 @@
 #include "storage/lwlock.h"
 #include "utils/relptr.h"
 
+struct AllocatorRegion;
 typedef struct BlockAllocatorContext BlockAllocatorContext;
 
 /* Number of pages (see FPM_PAGE_SIZE) per block-allocator chunk. */
@@ -33,7 +34,7 @@ extern void BlockAllocatorFree(void *ptr);                    /* WRONG SIGNATURE */
 extern void BlockAllocatorInit(MemoryContext);
 extern void BlockAllocatorReset(MemoryContext);
 extern void BlockAllocatorDelete(MemoryContext);
-extern Size BlockAllocatorGetChunkSpace(void *ptr);    /* WRONG SIGNATURE */
+extern Size BlockAllocatorGetChunkSpace(struct AllocatorRegion *, void *ptr);
 extern Size BlockAllocatorGetAllocSpace(Size size);    /* EXTRA FUNCTION */
 /* IS_EMPTY IS MISSING! */
 /* STATS IS MISSING! */