Index: shared.cc =================================================================== RCS file: /cvs/src/src/winsup/cygwin/shared.cc,v retrieving revision 1.84 diff -u -p -r1.84 shared.cc --- shared.cc 3 Dec 2004 02:00:37 -0000 1.84 +++ shared.cc 31 Dec 2004 12:39:00 -0000 @@ -103,6 +103,53 @@ open_shared (const char *name, int n, HA api_fatal ("CreateFileMapping %s, %E. Terminating.", mapname); } + if (m == SH_CYGWIN_SHARED && wincap.needs_memory_protection ()) + { + // ariel_e: we need to make sure we have enough space to reserve + // (with VirtualAlloc(MEM_RESERVE) after 'shared', so we + // ask for a memory location large enough to hold all + // shared data. After we get a valid location, + // we unmap it and try to map only the actual amount currently + // required + + DWORD size_needed = offsets[SH_TOTAL_SIZE] - offsets[0]; + void* old_addr = addr; + + HANDLE tmp_map = CreateFileMapping(INVALID_HANDLE_VALUE, + NULL, // attributes + PAGE_READWRITE | SEC_RESERVE, + 0, // max size high, + size_needed, + NULL); + + if(tmp_map == NULL) + api_fatal("Failed to reserve memory: %E\n"); + + addr = MapViewOfFileEx (tmp_map, FILE_MAP_READ | FILE_MAP_WRITE, + 0, 0, 0, addr); + + if(addr == NULL) + { + // try again without a predetermined address + addr = MapViewOfFileEx (tmp_map, + FILE_MAP_READ | FILE_MAP_WRITE, + 0, 0, 0, NULL); + + if(addr == NULL) + { + // failed to reserve memory, to maintain old + // compatibility we use original addr + addr = old_addr; + } + } + + UnmapViewOfFile(addr); + CloseHandle(tmp_map); + + // now we are ready to map the actual required amount of memory + // at the address we know has enough free space for everything + } + shared = (shared_info *) MapViewOfFileEx (shared_h, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0, addr);