Remove balloc_map stuff and sub in an inline implementation.
authorRobert Haas <rhaas@postgresql.org>
Fri, 13 Jun 2014 17:50:06 +0000 (17:50 +0000)
committerRobert Haas <rhaas@postgresql.org>
Fri, 13 Jun 2014 17:50:06 +0000 (17:50 +0000)
src/backend/utils/mmgr/aregion.c
src/backend/utils/mmgr/balloc.c
src/backend/utils/mmgr/balloc_map.c [deleted file]
src/include/utils/aregion.h
src/include/utils/balloc_map.h [deleted file]

index a9483a71576fdf905e8dae9fc0171a12992833ae..38b501a921810ab48cf89e281ab5b766daf2e4f5 100644 (file)
@@ -383,8 +383,7 @@ GetRegionForPrivateAllocation(Size npages)
                 */
                metadata_bytes = MAXALIGN(sizeof(AllocatorRegion));
                metadata_bytes += MAXALIGN(sizeof(FreePageManager));
-               metadata_bytes +=
-                       MAXALIGN(BlockAllocatorMapSize(NULL, new_region_net_pages));
+               metadata_bytes += MAXALIGN(new_region_net_pages * sizeof(Size));
                if (metadata_bytes % FPM_PAGE_SIZE != 0)
                        metadata_bytes += FPM_PAGE_SIZE - (metadata_bytes % FPM_PAGE_SIZE);
                region_size = new_region_net_pages * FPM_PAGE_SIZE + metadata_bytes;
@@ -418,7 +417,7 @@ GetRegionForPrivateAllocation(Size npages)
        region->context = NULL;
        region->fpm = (FreePageManager *)
                (region_start + MAXALIGN(sizeof(AllocatorRegion)));
-       region->pagemap = (BlockAllocatorMap *)
+       region->pagemap = (Size *)
                (((char *) region->fpm) + MAXALIGN(sizeof(FreePageManager)));
        region->contiguous_pages = new_region_net_pages + 1;
 
@@ -426,9 +425,6 @@ GetRegionForPrivateAllocation(Size npages)
        FreePageManagerInitialize(region->fpm, region->region_start, NULL, false);
        FreePageManagerPut(region->fpm, metadata_bytes / FPM_PAGE_SIZE,
                                           new_region_net_pages);
-       BlockAllocatorMapInitialize(region->pagemap, NULL,
-                                                               metadata_bytes / FPM_PAGE_SIZE,
-                                                               new_region_net_pages);
        region->contiguous_pages = new_region_net_pages; /* Now fix the value. */
        freelist = contiguous_pages_to_freelist(new_region_net_pages);
        dlist_push_head(&private_freelist[freelist], &region->fl_node);
index 4aa8f236c01dbe59154b9a4456fa70f12b7eb164..31674f387727b259d51e1c7b4a0179978b6b75f3 100644 (file)
@@ -310,7 +310,7 @@ BlockAllocatorAlloc(MemoryContext context, Size size)
                                                           BA_SCLASS_SPAN_LARGE);
                if (lock != NULL)
                        LWLockRelease(lock);
-               BlockAllocatorMapSet(region->pagemap, first_page, span);
+               region->pagemap[first_page] = ((char *) span) - base;
 
                return ptr;
        }
@@ -373,7 +373,6 @@ BlockAllocatorFree(AllocatorRegion *aregion, void *ptr)
        /* Locate the containing span. */
        fpm_base = fpm_segment_base(aregion->fpm);
        pageno = fpm_pointer_to_page(fpm_base, ptr);
-       span = BlockAllocatorMapGet(aregion->pagemap, pageno);
 
        /*
         * If this is a shared-memory region, we might need locking.  If so,
@@ -389,6 +388,7 @@ BlockAllocatorFree(AllocatorRegion *aregion, void *ptr)
        }
 
        /* Compute the object size. */
+       span = (BlockAllocatorSpan *) (base + aregion->pagemap[pageno]);
        size_class = span->size_class;
        obsize = balloc_size_classes[size_class];
 
@@ -518,7 +518,8 @@ BlockAllocatorRealloc(AllocatorRegion *aregion, void *ptr, Size size)
        /* Locate the containing span. */
        fpm_base = fpm_segment_base(aregion->fpm);
        pageno = fpm_pointer_to_page(fpm_base, ptr);
-       span = BlockAllocatorMapGet(aregion->pagemap, pageno);
+       base = aregion->seg != NULL ? fpm_base : NULL;
+       span = (BlockAllocatorSpan *) (base + aregion->pagemap[pageno]);
 
        /*
         * Extract relevant details from span.  We can read this information
@@ -584,7 +585,6 @@ BlockAllocatorRealloc(AllocatorRegion *aregion, void *ptr, Size size)
        }
 
        /* Ugly code to find context; see BlockAllocatorGetChunkContext. */
-       base = aregion->seg != NULL ? fpm_base : NULL;
        heap = relptr_access(base, span->parent);
        heap0 = heap - span->size_class;
        context = (BlockAllocatorContext *)
@@ -671,6 +671,7 @@ Size
 BlockAllocatorGetChunkSpace(AllocatorRegion *aregion, void *ptr)
 {
        char   *fpm_base;
+       char   *base;
        BlockAllocatorSpan *span;
        Size    pageno;
        uint16  size_class;
@@ -678,7 +679,8 @@ BlockAllocatorGetChunkSpace(AllocatorRegion *aregion, void *ptr)
        /* Locate the containing block. */
        fpm_base = fpm_segment_base(aregion->fpm);
        pageno = fpm_pointer_to_page(fpm_base, ptr);
-       span = BlockAllocatorMapGet(aregion->pagemap, pageno);
+       base = aregion->seg != NULL ? fpm_base : NULL;
+       span = (BlockAllocatorSpan *) (base + aregion->pagemap[pageno]);
 
        /* Work out the size of the allocation. */      
        size_class = span->size_class;
@@ -706,7 +708,8 @@ BlockAllocatorGetChunkContext(AllocatorRegion *aregion, void *ptr)
        /* Locate the containing block. */
        fpm_base = fpm_segment_base(aregion->fpm);
        pageno = fpm_pointer_to_page(fpm_base, ptr);
-       span = BlockAllocatorMapGet(aregion->pagemap, pageno);
+       base = aregion->seg != NULL ? fpm_base : NULL;
+       span = (BlockAllocatorSpan *) (base + aregion->pagemap[pageno]);
 
        /* Follow the parent poiner to find the containing heap. */
        base = aregion->seg != NULL ? fpm_base : NULL;
@@ -1103,7 +1106,7 @@ BlockAllocatorEnsureActiveBlock(char *base, AllocatorRegion *region,
        /* Initialize span and pagemap. */
        BlockAllocatorInitSpan(base, span, heap, ptr, npages, size_class);
        for (i = 0; i < npages; ++i)
-               BlockAllocatorMapSet(region->pagemap, first_page + i, span);
+               region->pagemap[first_page + i] = ((char *) span) - base;
 
        return true;
 }
diff --git a/src/backend/utils/mmgr/balloc_map.c b/src/backend/utils/mmgr/balloc_map.c
deleted file mode 100644 (file)
index f35c951..0000000
+++ /dev/null
@@ -1,130 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * balloc_map.c
- *       Block allocator page-mapping infrastructure.
- *
- * The block allocator does not store metadata with each chunk, and
- * therefore needs a way to find the metadata given only the pointer
- * address.  The first step is to translate the pointer address to a
- * an offset relative to some base address, from which a page number
- * can be calculated.  Then, this module is reponsible for mapping the
- * page number to an offset with the chunk where the associated span
- * object is stored.  We do this in the simplest possible way: one big
- * array.
- *
- * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * src/backend/utils/mmgr/balloc_map.c
- *
- *-------------------------------------------------------------------------
- */
-
-#include "postgres.h"
-
-#include "storage/shmem.h"
-#include "utils/balloc_map.h"
-#include "utils/freepage.h"
-
-const uint64 maxpages_4b = UINT64CONST(0x100000000) / FPM_PAGE_SIZE;
-
-struct BlockAllocatorMap
-{
-       relptr(BlockAllocatorMap) self;
-       Size    offset;
-       Size    npages;
-       bool    use64;
-};
-
-/* Map layout for systems with 32-bit pointers, or shared segments < 4GB. */
-typedef struct BlockAllocatorMap32
-{
-       BlockAllocatorMap       hdr;
-       uint32  map[FLEXIBLE_ARRAY_MEMBER];
-} BlockAllocatorMap32;
-
-/* Map layout for systems with 64-bit pointers, except shared segments < 4GB. */
-typedef struct BlockAllocatorMap64
-{
-       BlockAllocatorMap       hdr;
-       uint64  map[FLEXIBLE_ARRAY_MEMBER];
-} BlockAllocatorMap64;
-
-#define balloc_map_base(m) \
-       (((char *) m) - m->self.relptr_off)
-
-/*
- * Compute the amount of space required for an BlockAllocatorMap covering a
- * given number of pages.  Note that for shared memory (i.e. when base != NULL),
- * we assume that the pointers will always point to addresses within that
- * same segment, but for backend-private memory that might not be the case.
- */
-Size
-BlockAllocatorMapSize(char *base, Size npages)
-{
-       Size    map_bytes;
-
-       if (sizeof(Size) <= 4 || (base != NULL && npages < maxpages_4b))
-               map_bytes = add_size(offsetof(BlockAllocatorMap32, map),
-                                                        mul_size(npages, sizeof(uint32)));
-       else
-               map_bytes = add_size(offsetof(BlockAllocatorMap64, map),
-                                                        mul_size(npages, sizeof(uint64)));
-
-       return map_bytes;
-}
-
-/*
- * Initialize a BlockAllocatorMap.  Storage is provided by the caller.  Note
- * that we don't zero the array; the caller shouldn't try to get a value that
- * hasn't been set.
- */
-void
-BlockAllocatorMapInitialize(BlockAllocatorMap *m, char *base, Size offset,
-                                                       Size npages)
-{
-       relptr_store(base, m->self, m);
-       m->offset = offset;
-       m->npages = npages;
-       if (sizeof(Size) <= 4 || (base != NULL && npages < maxpages_4b))
-               m->use64 = false;
-       else
-               m->use64 = true;
-}
-
-/*
- * Store a value into a BlockAllocatorMap.
- */
-void
-BlockAllocatorMapSet(BlockAllocatorMap *m, Size pageno, void *ptr)
-{
-       char   *base = balloc_map_base(m);
-       Assert(pageno >= m->offset);
-       pageno -= m->offset;
-       Assert(pageno < m->npages);
-
-       if (m->use64)
-               ((BlockAllocatorMap64 *) m)->map[pageno] =
-                       (uint64) (((char *) ptr) - base);
-       else
-               ((BlockAllocatorMap32 *) m)->map[pageno] =
-                       (uint32) (((char *) ptr) - base);
-}
-
-/*
- * Get a value from a BlockAllocatorMap.  Getting a value not previously stored
- * will produce an undefined result, so don't do that.
- */
-void *
-BlockAllocatorMapGet(BlockAllocatorMap *m, Size pageno)
-{
-       char   *base = balloc_map_base(m);
-       Assert(pageno >= m->offset);
-       pageno -= m->offset;
-       Assert(pageno < m->npages);
-
-       if (m->use64)
-               return base + ((BlockAllocatorMap64 *) m)->map[pageno];
-       else
-               return base + ((BlockAllocatorMap32 *) m)->map[pageno];
-}
index a9d9d4cc13ab0876e9632562ddf8a0e92d43a3fb..c622dabaf70100016893c5bb69106cf7de8ec949 100644 (file)
@@ -19,7 +19,6 @@
 #include "storage/shm_toc.h"
 #include "utils/freepage.h"
 #include "utils/balloc.h"
-#include "utils/balloc_map.h"
 
 /*
  * An AllocatorRegion is a backend-private object used to track allocatable
@@ -33,7 +32,7 @@ typedef struct AllocatorRegion
        dsm_segment *seg;                       /* DSM handle (if not private). */
        BlockAllocatorContext *context; /* Shared allocator (if not private). */
        FreePageManager *fpm;           /* Free page manager for region (if any). */
-       BlockAllocatorMap *pagemap;     /* Page map for region (if any). */
+       Size *pagemap;                          /* Page map for region (if any). */
        Size contiguous_pages;          /* Last reported contiguous free pages. */
        dlist_node fl_node;                     /* Freelist links. */
 } AllocatorRegion;
@@ -47,7 +46,7 @@ typedef struct AllocatorRegion
 typedef struct AllocatorSharedRegion
 {
        relptr(FreePageManager) fpm;
-       relptr(BlockAllocatorMap) pagemap;
+       relptr(Size) pagemap;
        relptr(BlockAllocatorContext) allocator;
        int     lwlock_tranche_id;
        char lwlock_tranche_name[FLEXIBLE_ARRAY_MEMBER];
diff --git a/src/include/utils/balloc_map.h b/src/include/utils/balloc_map.h
deleted file mode 100644 (file)
index e32ae76..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * balloc_map.h
- *       Block allocator page-mapping infrastructure.
- *
- * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * src/include/utils/ba_map.h
- *
- *-------------------------------------------------------------------------
- */
-
-#ifndef BALLOC_MAP_H
-#define BALLOC_MAP_H
-
-typedef struct BlockAllocatorMap BlockAllocatorMap;
-
-extern Size BlockAllocatorMapSize(char *base, Size npages);
-extern void BlockAllocatorMapInitialize(BlockAllocatorMap *, char *base,
-                                                       Size offset, Size npages);
-extern void BlockAllocatorMapSet(BlockAllocatorMap *, Size pageno, void *ptr);
-extern void *BlockAllocatorMapGet(BlockAllocatorMap *, Size pageno);
-
-#endif /* BALLOC_MAP_H */