OSDN Git Service

* debug.h (console_printf): Define for non-debugging condition.
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / heap.cc
1 /* heap.cc: Cygwin heap manager.
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include "cygerrno.h"
13 #include "sigproc.h"
14 #include "pinfo.h"
15 #include "heap.h"
16 #include "shared_info.h"
17 #include "security.h"
18 #include "path.h"
19 #include "fhandler.h"
20 #include "dtable.h"
21 #include "cygheap.h"
22 #include "registry.h"
23 #include "cygwin_version.h"
24
25 #define assert(x)
26
27 static unsigned page_const;
28
29 extern "C" size_t getpagesize ();
30
31 #define MINHEAP_SIZE (4 * 1024 * 1024)
32
33 /* Initialize the heap at process start up.  */
34 void
35 heap_init ()
36 {
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.  */
39
40   page_const = system_info.dwPageSize;
41   if (!cygheap->user_heap.base)
42     {
43       cygheap->user_heap.chunk = cygwin_shared->heap_chunk_size ();
44       while (cygheap->user_heap.chunk >= MINHEAP_SIZE)
45         {
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)
52             break;
53           cygheap->user_heap.chunk -= 1 * 1024 * 1024;
54         }
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;
59     }
60   else
61     {
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;
66
67       /* Loop until we've managed to reserve an adequate amount of memory. */
68       char *p;
69       DWORD reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
70       while (1)
71         {
72           p = (char *) VirtualAlloc (cygheap->user_heap.base, reserve_size,
73                                      MEM_RESERVE, PAGE_READWRITE);
74           if (p)
75             break;
76           if ((reserve_size -= page_const) <= allocsize)
77             break;
78         }
79       if (!p)
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");
88     }
89
90   debug_printf ("heap base %p, heap top %p", cygheap->user_heap.base,
91                 cygheap->user_heap.top);
92   page_const--;
93   // malloc_init ();
94 }
95
96 #define pround(n) (((size_t)(n) + page_const) & ~page_const)
97
98 /* FIXME: This function no longer handles "split heaps". */
99
100 extern "C" void *
101 sbrk (int n)
102 {
103   char *newtop, *newbrk;
104   unsigned commitbytes, newbrksize;
105
106   if (n == 0)
107     return cygheap->user_heap.ptr;              /* Just wanted to find current cygheap->user_heap.ptr address */
108
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 -
111                                                    on page boundary */
112
113   if (newtop == cygheap->user_heap.top)
114     goto good;
115
116   if (n < 0)
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 */
122       else
123         goto err;
124     }
125
126   assert (newtop > cygheap->user_heap.top);
127
128   /* Find the number of bytes to commit, rounded up to the nearest page. */
129   commitbytes = pround (newtop - (char *) cygheap->user_heap.top);
130
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
134      reserved.  */
135   if (newtop <= cygheap->user_heap.max)
136     {
137       if (VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
138         goto good;
139     }
140
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;
146
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)
150      {
151         cygheap->user_heap.max = (char *) cygheap->user_heap.max + pround (newbrksize);
152         goto good;
153      }
154
155 err:
156   set_errno (ENOMEM);
157   return (void *) -1;
158
159 good:
160   void *oldbrk = cygheap->user_heap.ptr;
161   cygheap->user_heap.ptr = newbrk;
162   cygheap->user_heap.top = newtop;
163   return oldbrk;
164 }