OSDN Git Service

Eliminate use of sigframe and sigthread throughout.
[pf3gnuchains/sourceware.git] / winsup / cygwin / heap.cc
1 /* heap.cc: Cygwin heap manager.
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 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
35 void
36 heap_init ()
37 {
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.  */
40
41   page_const = system_info.dwPageSize;
42   if (!cygheap->user_heap.base)
43     {
44       cygheap->user_heap.chunk = cygwin_shared->heap_chunk_size ();
45       while (cygheap->user_heap.chunk >= MINHEAP_SIZE)
46         {
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)
53             break;
54           cygheap->user_heap.chunk -= 1 * 1024 * 1024;
55         }
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;
60     }
61   else
62     {
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);
69
70       /* Loop until we've managed to reserve an adequate amount of memory. */
71       char *p;
72       for (;;)
73         {
74           p = (char *) VirtualAlloc (cygheap->user_heap.base, reserve_size,
75                                      MEM_RESERVE, PAGE_READWRITE);
76           if (p)
77             break;
78           if ((reserve_size -= page_const) <= allocsize)
79             break;
80         }
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");
85     }
86
87   debug_printf ("heap base %p, heap top %p", cygheap->user_heap.base,
88                 cygheap->user_heap.top);
89   page_const--;
90   malloc_init ();
91 }
92
93 #define pround(n) (((size_t)(n) + page_const) & ~page_const)
94
95 /* FIXME: This function no longer handles "split heaps". */
96
97 extern "C" void *
98 sbrk (int n)
99 {
100   char *newtop, *newbrk;
101   unsigned commitbytes, newbrksize;
102
103   if (n == 0)
104     return cygheap->user_heap.ptr;              /* Just wanted to find current cygheap->user_heap.ptr address */
105
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 -
108                                                    on page boundary */
109
110   if (newtop == cygheap->user_heap.top)
111     goto good;
112
113   if (n < 0)
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 */
119       else
120         goto err;
121     }
122
123   assert (newtop > cygheap->user_heap.top);
124
125   /* Find the number of bytes to commit, rounded up to the nearest page. */
126   commitbytes = pround (newtop - (char *) cygheap->user_heap.top);
127
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
131      reserved.  */
132   if (newtop <= cygheap->user_heap.max)
133     {
134       if (VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
135         goto good;
136     }
137
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;
143
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)
147      {
148         cygheap->user_heap.max = (char *) cygheap->user_heap.max + pround (newbrksize);
149         goto good;
150      }
151
152 err:
153   set_errno (ENOMEM);
154   return (void *) -1;
155
156 good:
157   void *oldbrk = cygheap->user_heap.ptr;
158   cygheap->user_heap.ptr = newbrk;
159   cygheap->user_heap.top = newtop;
160   return oldbrk;
161 }