summaryrefslogtreecommitdiff
path: root/src/backend/port/win32/shmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/port/win32/shmem.c')
-rw-r--r--src/backend/port/win32/shmem.c62
1 files changed, 61 insertions, 1 deletions
diff --git a/src/backend/port/win32/shmem.c b/src/backend/port/win32/shmem.c
index c7865bda325..2ba144c9a4a 100644
--- a/src/backend/port/win32/shmem.c
+++ b/src/backend/port/win32/shmem.c
@@ -6,14 +6,17 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/port/win32/shmem.c,v 1.13.2.1 2009/05/04 08:36:42 mha Exp $
+ * $PostgreSQL: pgsql/src/backend/port/win32/shmem.c,v 1.13.2.2 2010/07/23 13:53:30 mha Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
+#include "miscadmin.h"
static DWORD s_segsize = 0;
+extern void *UsedShmemSegAddr;
+extern Size UsedShmemSegSize;
/* Detach from a shared mem area based on its address */
int
@@ -29,6 +32,13 @@ shmdt(const void *shmaddr)
void *
shmat(int memId, void *shmaddr, int flag)
{
+ /* Release the memory region reserved in the postmaster */
+ if (IsUnderPostmaster)
+ {
+ if (VirtualFree(shmaddr, 0, MEM_RELEASE) == 0)
+ elog(FATAL, "failed to release reserved memory region (addr=%p): %lu",
+ shmaddr, GetLastError());
+ }
/* TODO -- shmat needs to count # attached to shared mem */
void *lpmem = MapViewOfFileEx((HANDLE) memId,
FILE_MAP_WRITE | FILE_MAP_READ,
@@ -128,3 +138,53 @@ shmget(int memKey, int size, int flag)
return (int) hmap;
}
+
+/*
+ * pgwin32_ReserveSharedMemoryRegion(hChild)
+ *
+ * Reserve the memory region that will be used for shared memory in a child
+ * process. It is called before the child process starts, to make sure the
+ * memory is available.
+ *
+ * Once the child starts, DLLs loading in different order or threads getting
+ * scheduled differently may allocate memory which can conflict with the
+ * address space we need for our shared memory. By reserving the shared
+ * memory region before the child starts, and freeing it only just before we
+ * attempt to get access to the shared memory forces these allocations to
+ * be given different address ranges that don't conflict.
+ *
+ * NOTE! This function executes in the postmaster, and should for this
+ * reason not use elog(FATAL) since that would take down the postmaster.
+ */
+int
+pgwin32_ReserveSharedMemoryRegion(HANDLE hChild)
+{
+ void *address;
+
+ Assert(UsedShmemSegAddr != NULL);
+ Assert(UsedShmemSegSize != 0);
+
+ address = VirtualAllocEx(hChild, UsedShmemSegAddr, UsedShmemSegSize,
+ MEM_RESERVE, PAGE_READWRITE);
+ if (address == NULL) {
+ /* Don't use FATAL since we're running in the postmaster */
+ elog(LOG, "could not reserve shared memory region (addr=%p) for child %lu: %lu",
+ UsedShmemSegAddr, hChild, GetLastError());
+ return false;
+ }
+ if (address != UsedShmemSegAddr)
+ {
+ /*
+ * Should never happen - in theory if allocation granularity causes strange
+ * effects it could, so check just in case.
+ *
+ * Don't use FATAL since we're running in the postmaster.
+ */
+ elog(LOG, "reserved shared memory region got incorrect address %p, expected %p",
+ address, UsedShmemSegAddr);
+ VirtualFreeEx(hChild, address, 0, MEM_RELEASE);
+ return false;
+ }
+
+ return true;
+}