1 /* heap.cc: Cygwin heap manager.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
16 #include "shared_info.h"
23 #include "cygwin_version.h"
27 static unsigned page_const;
29 extern "C" size_t getpagesize ();
31 #define MINHEAP_SIZE (4 * 1024 * 1024)
33 /* Initialize the heap at process start up. */
37 /* If we're the forkee, we must allocate the heap at exactly the same place
38 as our parent. If not, we don't care where it ends up. */
40 page_const = system_info.dwPageSize;
41 if (!cygheap->user_heap.base)
43 cygheap->user_heap.chunk = cygwin_shared->heap_chunk_size ();
44 while (cygheap->user_heap.chunk >= MINHEAP_SIZE)
46 /* Initialize page mask and default heap size. Preallocate a heap
47 * to assure contiguous memory. */
48 cygheap->user_heap.ptr = cygheap->user_heap.top =
49 cygheap->user_heap.base =
50 VirtualAlloc (NULL, cygheap->user_heap.chunk, MEM_RESERVE, PAGE_NOACCESS);
51 if (cygheap->user_heap.base)
53 cygheap->user_heap.chunk -= 1 * 1024 * 1024;
55 if (cygheap->user_heap.base == NULL)
56 api_fatal ("unable to allocate heap, heap_chunk_size %d, %E",
57 cygheap->user_heap.chunk);
58 cygheap->user_heap.max = (char *) cygheap->user_heap.base + cygheap->user_heap.chunk;
62 DWORD chunk = cygheap->user_heap.chunk; /* allocation chunk */
63 /* total size commited in parent */
64 DWORD allocsize = (char *) cygheap->user_heap.top -
65 (char *) cygheap->user_heap.base;
67 /* Loop until we've managed to reserve an adequate amount of memory. */
69 DWORD reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
72 p = (char *) VirtualAlloc (cygheap->user_heap.base, reserve_size,
73 MEM_RESERVE, PAGE_READWRITE);
76 if ((reserve_size -= page_const) <= allocsize)
80 api_fatal ("couldn't allocate cygwin heap, %E, base %p, top %p, "
81 "reserve_size %d, allocsize %d, page_const %d",
82 cygheap->user_heap.base, cygheap->user_heap.top,
83 reserve_size, allocsize, page_const);
84 if (p != cygheap->user_heap.base)
85 api_fatal ("heap allocated at wrong address %p (mapped) != %p (expected)", p, cygheap->user_heap.base);
86 if (!VirtualAlloc (cygheap->user_heap.base, allocsize, MEM_COMMIT, PAGE_READWRITE))
87 api_fatal ("MEM_COMMIT failed, %E");
90 debug_printf ("heap base %p, heap top %p", cygheap->user_heap.base,
91 cygheap->user_heap.top);
96 #define pround(n) (((size_t)(n) + page_const) & ~page_const)
98 /* FIXME: This function no longer handles "split heaps". */
103 char *newtop, *newbrk;
104 unsigned commitbytes, newbrksize;
107 return cygheap->user_heap.ptr; /* Just wanted to find current cygheap->user_heap.ptr address */
109 newbrk = (char *) cygheap->user_heap.ptr + n; /* Where new cygheap->user_heap.ptr will be */
110 newtop = (char *) pround (newbrk); /* Actual top of allocated memory -
113 if (newtop == cygheap->user_heap.top)
117 { /* Freeing memory */
118 assert (newtop < cygheap->user_heap.top);
119 n = (char *) cygheap->user_heap.top - newtop;
120 if (VirtualFree (newtop, n, MEM_DECOMMIT)) /* Give it back to OS */
121 goto good; /* Didn't take */
126 assert (newtop > cygheap->user_heap.top);
128 /* Find the number of bytes to commit, rounded up to the nearest page. */
129 commitbytes = pround (newtop - (char *) cygheap->user_heap.top);
131 /* Need to grab more pages from the OS. If this fails it may be because
132 we have used up previously reserved memory. Or, we're just plumb out
133 of memory. Only attempt to commit memory that we know we've previously
135 if (newtop <= cygheap->user_heap.max)
137 if (VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
141 /* Couldn't allocate memory. Maybe we can reserve some more.
142 Reserve either the maximum of the standard cygwin_shared->heap_chunk_size ()
143 or the requested amount. Then attempt to actually allocate it. */
144 if ((newbrksize = cygheap->user_heap.chunk) < commitbytes)
145 newbrksize = commitbytes;
147 if ((VirtualAlloc (cygheap->user_heap.top, newbrksize, MEM_RESERVE, PAGE_NOACCESS)
148 || VirtualAlloc (cygheap->user_heap.top, newbrksize = commitbytes, MEM_RESERVE, PAGE_NOACCESS))
149 && VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
151 cygheap->user_heap.max = (char *) cygheap->user_heap.max + pround (newbrksize);
160 void *oldbrk = cygheap->user_heap.ptr;
161 cygheap->user_heap.ptr = newbrk;
162 cygheap->user_heap.top = newtop;