Stub out sb_alloc/sb_region.
authorRobert Haas <rhaas@postgresql.org>
Tue, 11 Mar 2014 21:33:48 +0000 (17:33 -0400)
committerRobert Haas <rhaas@postgresql.org>
Tue, 11 Mar 2014 21:33:48 +0000 (17:33 -0400)
src/backend/utils/mmgr/Makefile
src/backend/utils/mmgr/sb_alloc.c [new file with mode: 0644]
src/backend/utils/mmgr/sb_region.c [new file with mode: 0644]
src/include/utils/sb_alloc.h [new file with mode: 0644]
src/include/utils/sb_region.h [new file with mode: 0644]

index e4086cc3f29ed34e0a05c77425f2e71b7601798b..c318a73781f16cd9a99e0e871a606599235c464d 100644 (file)
@@ -12,6 +12,6 @@ subdir = src/backend/utils/mmgr
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = aset.o freepage.o mcxt.o portalmem.o sb_map.o
+OBJS = aset.o freepage.o mcxt.o portalmem.o sb_alloc.o sb_map.o sb_region.o
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/utils/mmgr/sb_alloc.c b/src/backend/utils/mmgr/sb_alloc.c
new file mode 100644 (file)
index 0000000..8967c09
--- /dev/null
@@ -0,0 +1,16 @@
+/*-------------------------------------------------------------------------
+ *
+ * sb_alloc.c
+ *       Superblock-based memory allocator.
+ *
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/backend/utils/mmgr/sb_alloc.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "utils/sb_alloc.h"
diff --git a/src/backend/utils/mmgr/sb_region.c b/src/backend/utils/mmgr/sb_region.c
new file mode 100644 (file)
index 0000000..8b73cf6
--- /dev/null
@@ -0,0 +1,16 @@
+/*-------------------------------------------------------------------------
+ *
+ * sb_region.c
+ *       Superblock allocator memory region manager.
+ *
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/backend/utils/mmgr/sb_region.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "utils/sb_region.h"
diff --git a/src/include/utils/sb_alloc.h b/src/include/utils/sb_alloc.h
new file mode 100644 (file)
index 0000000..a46b5d3
--- /dev/null
@@ -0,0 +1,70 @@
+/*-------------------------------------------------------------------------
+ *
+ * sb_alloc.h
+ *       Superblock-based memory allocator.
+ *
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/utils/sb_alloc.h
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef SB_ALLOC_H
+#define SB_ALLOC_H
+
+#include "storage/lwlock.h"
+#include "utils/relptr.h"
+
+typedef struct sb_span sb_span;
+
+/*
+ * Superblocks are binned by how full they are.  Generally, each fullness
+ * class corresponds to one quartile, but the superblock being used for
+ * allocations is always at the head of the list for fullness class 1,
+ * regardless of how full it really is.
+ *
+ * For large objects, we just stick all of the allocations in fullness class
+ * 0. Since we can just return the space directly to the free page manager,
+ * we don't really need them on a list at all, except that if someone wants
+ * to bulk release everything allocated using this sb_allocator, we have no
+ * other way of finding them.
+ */
+#define SB_FULLNESS_CLASSES            4
+
+/*
+ * An sb_heap represents a set of allocations of a given size class.
+ * There can be multiple heaps for the same size class for contention
+ * avoidance.
+ */
+typedef struct sb_heap
+{
+       relptr(LWLock)  lock;
+       relptr(sb_span) spans[SB_FULLNESS_CLASSES];
+} sb_heap;
+
+/*
+ * An sb_allocator is basically just a group of heaps, N per size class, where
+ * N can be chosen by the user.  If locking is required, then we've also got
+ * an array of LWLocks, one per heap.
+ */
+typedef struct sb_allocator
+{
+       uint16  heaps_per_size_class;
+       uint16  num_size_classes;
+       relptr(LWLock) locks;
+       sb_heap heaps[FLEXIBLE_ARRAY_MEMBER];
+} sb_allocator;
+
+/* Allocation options. */
+#define SB_ALLOC_HUGE                          0x0001          /* allow >=1GB */
+#define SB_ALLOC_SOFT_FAIL                     0x0002          /* return NULL if no mem */
+
+/* Exported functions. */
+extern sb_allocator *sb_create_private_allocator(void);
+extern void *sb_alloc(sb_allocator *, Size, int flags);
+extern void sb_free(void *ptr);
+extern void sb_destroy_private_allocator(sb_allocator *);
+
+#endif         /* SB_ALLOC_H */
diff --git a/src/include/utils/sb_region.h b/src/include/utils/sb_region.h
new file mode 100644 (file)
index 0000000..394bc2e
--- /dev/null
@@ -0,0 +1,62 @@
+/*-------------------------------------------------------------------------
+ *
+ * sb_region.h
+ *       Superblock allocator memory region manager.
+ *
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/utils/sb_region.h
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef SB_REGION_H
+#define SB_REGION_H
+
+#include "storage/dsm.h"
+#include "storage/shm_toc.h"
+#include "utils/freepage.h"
+#include "utils/sb_alloc.h"
+#include "utils/sb_map.h"
+
+/*
+ * An sb_region is a backend-private object used to track allocatable regions
+ * of memory, either backend-private or shared.
+ */
+typedef struct sb_region
+{
+       char *region_start;
+       Size region_size;
+       dsm_segment *seg;
+       FreePageManager *fpm;
+       sb_map *pagemap;
+       sb_allocator *allocator;
+} sb_region;
+
+/*
+ * An sb_shared_region is a shared-memory object containing the information
+ * necessary to set up an sb_region object for an individual backend.
+ */
+typedef struct sb_shared_region
+{
+       relptr(FreePageManager) fpm;
+       relptr(sb_map) pagemap;
+       relptr(sb_allocator) allocator;
+       int     lwlock_tranche_id;
+       char lwlock_tranche_name[FLEXIBLE_ARRAY_MEMBER];
+} sb_shared_region;
+
+/* Public API. */
+extern sb_shared_region *sb_create_shared_region(dsm_segment *seg,
+                                               shm_toc *toc, Size size,
+                                               int lwlock_tranche_id,
+                                               char *lwlock_tranche_name);
+extern sb_allocator *sb_attach_shared_region(dsm_segment *,
+                                               sb_shared_region *);
+
+/* For use by sb_alloc/sb_free. */
+extern sb_region *sb_private_region_for_allocator(Size npages);
+extern sb_region *sb_lookup_region(void *);
+
+#endif         /* SB_REGION_H */