diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index 8e33550e0..80a3d26c0 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -637,6 +637,9 @@ dll_crt0_1 () (void) SetErrorMode (SEM_FAILCRITICALERRORS); + /* Initialize the heap. */ + heap_init (); + /* Initialize events. */ events_init (); @@ -670,9 +673,6 @@ dll_crt0_1 () longjmp (ciresrv->jmp, ciresrv->cygpid); } - /* Initialize the heap. */ - heap_init (); - /* Initialize our process table entry. Don't use the parent info for dynamically loaded case. */ pinfo_init ((dynamically_loaded) ? NULL : info); diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc index 40158385d..54c668137 100644 --- a/winsup/cygwin/fork.cc +++ b/winsup/cygwin/fork.cc @@ -445,13 +445,13 @@ fork () MALLOC_CHECK; - rc = fork_copy (pi, "dll data", dll_data_start, dll_data_end, - dll_bss_start, dll_bss_end, NULL); rc = fork_copy (pi, "user/cygwin data", user_data->data_start, user_data->data_end, user_data->bss_start, user_data->bss_end, + ch.heapbase, ch.heapptr, stack_here, ch.stackbottom, - NULL); + dll_data_start, dll_data_end, + dll_bss_start, dll_bss_end, NULL); MALLOC_CHECK; if (!rc) @@ -526,22 +526,14 @@ fork () } sync_with_parent ("after longjmp.", TRUE); + ProtectHandle (hParent); #ifdef DEBUGGING char c; if (GetEnvironmentVariable ("FORKDEBUG", &c, 1)) try_to_debug (); - char buf[80]; - if (GetEnvironmentVariable ("CYGWIN_FORK_SLEEP", buf, sizeof (buf))) - { - small_printf ("Sleeping %d after fork, pid %u\n", atoi (buf), GetCurrentProcessId ()); - Sleep (atoi(buf)); - } #endif - heap_init (); - ProtectHandle (hParent); - /* If we've played with the stack, stacksize != 0. That means that fork() was invoked from other than the main thread. Make sure that when the "main" thread exits it calls do_exit, like a normal process. diff --git a/winsup/cygwin/heap.cc b/winsup/cygwin/heap.cc index 5989aefca..ce1cab5c4 100644 --- a/winsup/cygwin/heap.cc +++ b/winsup/cygwin/heap.cc @@ -20,8 +20,6 @@ details. */ static unsigned page_const = 0; -HANDLE cygwin_heap; - static __inline__ int getpagesize(void) { @@ -38,9 +36,9 @@ heap_init () /* If we're the forkee, we must allocate the heap at exactly the same place as our parent. If not, we don't care where it ends up. */ + page_const = getpagesize(); if (brkbase) { - DWORD chunk = brkchunk; /* allocation chunk */ /* total size commited in parent */ DWORD allocsize = (char *) brktop - (char *) brkbase; @@ -48,13 +46,14 @@ heap_init () DWORD reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk); /* Loop until we've managed to reserve an adequate amount of memory. */ - void *p; + char *p; for (;;) { - p = MapViewOfFileEx (cygwin_heap, FILE_MAP_COPY, 0L, 0L, 0L, brkbase); + p = (char *) VirtualAlloc (brkbase, reserve_size, + MEM_RESERVE, PAGE_READWRITE); if (p) break; - if ((reserve_size -= (page_const + 1)) <= allocsize) + if ((reserve_size -= page_const) <= allocsize) break; } if (p == NULL) @@ -62,25 +61,20 @@ heap_init () brkchunk, myself->pid); if (p != brkbase) api_fatal ("heap allocated but not at %p", brkbase); - if (!VirtualAlloc (brkbase, allocsize, MEM_COMMIT, PAGE_READWRITE)) + if (! VirtualAlloc (brkbase, allocsize, MEM_COMMIT, PAGE_READWRITE)) api_fatal ("MEM_COMMIT failed, %E"); } else { - page_const = getpagesize() - 1; /* Initialize page mask and default heap size. Preallocate a heap * to assure contiguous memory. */ - cygwin_heap = CreateFileMapping ((HANDLE) 0xffffffff, &sec_all, PAGE_WRITECOPY | SEC_RESERVE, 0L, brkchunk, NULL); - if (cygwin_heap == NULL) - api_fatal ("2. unable to allocate shared memory for heap, heap_chunk_size %d, %E", brkchunk); - - brk = brktop = brkbase = MapViewOfFile (cygwin_heap, 0, 0L, 0L, 0); -// brk = brktop = brkbase = VirtualAlloc(NULL, brkchunk, MEM_RESERVE, PAGE_NOACCESS); + brk = brktop = brkbase = VirtualAlloc(NULL, brkchunk, MEM_RESERVE, PAGE_NOACCESS); if (brkbase == NULL) api_fatal ("2. unable to allocate heap, heap_chunk_size %d, %E", brkchunk); } + page_const--; malloc_init (); } diff --git a/winsup/cygwin/shared.h b/winsup/cygwin/shared.h index e51b9ec8f..5c5ea8348 100644 --- a/winsup/cygwin/shared.h +++ b/winsup/cygwin/shared.h @@ -172,7 +172,7 @@ pinfo *__stdcall procinfo (int n); enum { - PROC_MAGIC = 0xaf09f000, + PROC_MAGIC = 0xaf08f000, PROC_FORK = PROC_MAGIC + 1, PROC_EXEC = PROC_MAGIC + 2, PROC_SPAWN = PROC_MAGIC + 3,