1 /* heap.cc: Cygwin heap manager.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 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. */
38 /* If we're the forkee, we must allocate the heap at exactly the same place
39 as our parent. If not, we don't care where it ends up. */
41 page_const = system_info.dwPageSize;
42 if (!cygheap->user_heap.base)
44 cygheap->user_heap.chunk = cygwin_shared->heap_chunk_size ();
45 while (cygheap->user_heap.chunk >= MINHEAP_SIZE)
47 /* Initialize page mask and default heap size. Preallocate a heap
48 * to assure contiguous memory. */
49 cygheap->user_heap.ptr = cygheap->user_heap.top =
50 cygheap->user_heap.base =
51 VirtualAlloc (NULL, cygheap->user_heap.chunk, MEM_RESERVE, PAGE_NOACCESS);
52 if (cygheap->user_heap.base)
54 cygheap->user_heap.chunk -= 1 * 1024 * 1024;
56 if (cygheap->user_heap.base == NULL)
57 api_fatal ("unable to allocate heap, heap_chunk_size %d, %E",
58 cygheap->user_heap.chunk);
59 cygheap->user_heap.max = (char *) cygheap->user_heap.base + cygheap->user_heap.chunk;
63 DWORD chunk = cygheap->user_heap.chunk; /* allocation chunk */
64 /* total size commited in parent */
65 DWORD allocsize = (char *) cygheap->user_heap.top -
66 (char *) cygheap->user_heap.base;
67 /* round up by chunk size */
68 DWORD reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
70 /* Loop until we've managed to reserve an adequate amount of memory. */
74 p = (char *) VirtualAlloc (cygheap->user_heap.base, reserve_size,
75 MEM_RESERVE, PAGE_READWRITE);
78 if ((reserve_size -= page_const) <= allocsize)
81 if (p != cygheap->user_heap.base)
82 api_fatal ("heap allocated but not at %p", cygheap->user_heap.base);
83 if (!VirtualAlloc (cygheap->user_heap.base, allocsize, MEM_COMMIT, PAGE_READWRITE))
84 api_fatal ("MEM_COMMIT failed, %E");
87 debug_printf ("heap base %p, heap top %p", cygheap->user_heap.base,
88 cygheap->user_heap.top);
93 #define pround(n) (((size_t)(n) + page_const) & ~page_const)
95 /* FIXME: This function no longer handles "split heaps". */
100 char *newtop, *newbrk;
101 unsigned commitbytes, newbrksize;
104 return cygheap->user_heap.ptr; /* Just wanted to find current cygheap->user_heap.ptr address */
106 newbrk = (char *) cygheap->user_heap.ptr + n; /* Where new cygheap->user_heap.ptr will be */
107 newtop = (char *) pround (newbrk); /* Actual top of allocated memory -
110 if (newtop == cygheap->user_heap.top)
114 { /* Freeing memory */
115 assert (newtop < cygheap->user_heap.top);
116 n = (char *) cygheap->user_heap.top - newtop;
117 if (VirtualFree (newtop, n, MEM_DECOMMIT)) /* Give it back to OS */
118 goto good; /* Didn't take */
123 assert (newtop > cygheap->user_heap.top);
125 /* Find the number of bytes to commit, rounded up to the nearest page. */
126 commitbytes = pround (newtop - (char *) cygheap->user_heap.top);
128 /* Need to grab more pages from the OS. If this fails it may be because
129 we have used up previously reserved memory. Or, we're just plumb out
130 of memory. Only attempt to commit memory that we know we've previously
132 if (newtop <= cygheap->user_heap.max)
134 if (VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
138 /* Couldn't allocate memory. Maybe we can reserve some more.
139 Reserve either the maximum of the standard cygwin_shared->heap_chunk_size ()
140 or the requested amount. Then attempt to actually allocate it. */
141 if ((newbrksize = cygheap->user_heap.chunk) < commitbytes)
142 newbrksize = commitbytes;
144 if ((VirtualAlloc (cygheap->user_heap.top, newbrksize, MEM_RESERVE, PAGE_NOACCESS)
145 || VirtualAlloc (cygheap->user_heap.top, newbrksize = commitbytes, MEM_RESERVE, PAGE_NOACCESS))
146 && VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
148 cygheap->user_heap.max = (char *) cygheap->user_heap.max + pround (newbrksize);
157 void *oldbrk = cygheap->user_heap.ptr;
158 cygheap->user_heap.ptr = newbrk;
159 cygheap->user_heap.top = newtop;