]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/cygheap.cc
* spawn.cc (spawn_guts): Attempt to accomodate archaic windows quoting
[newlib-cygwin.git] / winsup / cygwin / cygheap.cc
1 /* cygheap.cc: Cygwin heap manager.
2
3 Copyright 2000 Cygnus Solutions.
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 <errno.h>
13 #include <fhandler.h>
14 #include <assert.h>
15 #include "cygheap.h"
16 #include "heap.h"
17 #include "cygerrno.h"
18
19 inline static void
20 init_cheap ()
21 {
22 cygheap = VirtualAlloc (NULL, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS);
23 if (!cygheap)
24 api_fatal ("Couldn't reserve space for cygwin's heap, %E");
25 cygheap_max = cygheap;
26 }
27
28 #define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
29 static void *__stdcall
30 _csbrk (int sbs)
31 {
32 void *lastheap;
33 if (!cygheap)
34 init_cheap ();
35 lastheap = cygheap_max;
36 (char *) cygheap_max += sbs;
37 void *heapalign = (void *) pagetrunc (lastheap);
38 int needalloc = sbs && ((heapalign == lastheap) || heapalign != pagetrunc (cygheap_max));
39 if (needalloc && !VirtualAlloc (lastheap, (DWORD) sbs, MEM_COMMIT, PAGE_READWRITE))
40 api_fatal ("couldn't commit memory for cygwin heap, %E");
41
42 return lastheap;
43 }
44
45 /* Copyright (C) 1997, 2000 DJ Delorie */
46
47 char *buckets[32] = {0};
48 int bucket2size[32] = {0};
49
50 static inline int
51 size2bucket (int size)
52 {
53 int rv = 0x1f;
54 int bit = ~0x10;
55 int i;
56
57 if (size < 4)
58 size = 4;
59 size = (size + 3) & ~3;
60
61 for (i = 0; i < 5; i++)
62 {
63 if (bucket2size[rv & bit] >= size)
64 rv &= bit;
65 bit >>= 1;
66 }
67 return rv;
68 }
69
70 static inline void
71 init_buckets ()
72 {
73 unsigned b;
74 for (b = 0; b < 32; b++)
75 bucket2size[b] = (1 << b);
76 }
77
78 static void *__stdcall
79 _cmalloc (int size)
80 {
81 char *rv;
82 int b;
83
84 if (bucket2size[0] == 0)
85 init_buckets ();
86
87 b = size2bucket (size);
88 if (buckets[b])
89 {
90 rv = buckets[b];
91 buckets[b] = *(char **) rv;
92 return rv;
93 }
94
95 size = bucket2size[b] + 4;
96 rv = (char *) _csbrk (size);
97
98 *(int *) rv = b;
99 rv += 4;
100 return rv;
101 }
102
103 static void __stdcall
104 _cfree (void *ptr)
105 {
106 int b = *(int *) ((char *) ptr - 4);
107 *(char **) ptr = buckets[b];
108 buckets[b] = (char *) ptr;
109 }
110
111 static void *__stdcall
112 _crealloc (void *ptr, int size)
113 {
114 void *newptr;
115 if (ptr == NULL)
116 newptr = _cmalloc (size);
117 else
118 {
119 int oldsize = bucket2size[*(int *) ((char *) ptr - 4)];
120 if (size <= oldsize)
121 return ptr;
122 newptr = _cmalloc (size);
123 memcpy (newptr, ptr, oldsize);
124 _cfree (ptr);
125 }
126 return newptr;
127 }
128
129 /* End Copyright (C) 1997 DJ Delorie */
130
131 void *cygheap = NULL;
132 void *cygheap_max = NULL;
133
134 #define sizeof_cygheap(n) ((n) + sizeof(cygheap_entry))
135
136 struct cygheap_entry
137 {
138 cygheap_types type;
139 char data[0];
140 };
141
142 #define N ((cygheap_entry *) NULL)
143 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
144
145 void
146 cygheap_init ()
147 {
148 if (!cygheap)
149 init_cheap ();
150 }
151
152 extern "C" void __stdcall
153 cygheap_fixup_in_child (HANDLE parent)
154 {
155 DWORD m, n;
156 n = (DWORD) cygheap_max - (DWORD) cygheap;
157 if (!VirtualAlloc (cygheap, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS))
158 api_fatal ("Couldn't reserve space for cygwin's heap in child, %E");
159
160 if (!VirtualAlloc (cygheap, n, MEM_COMMIT, PAGE_READWRITE))
161 api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
162 cygheap, n);
163 m = 0;
164 n = (DWORD) pagetrunc (n + 4095);
165 if (!ReadProcessMemory (parent, cygheap, cygheap, n, &m) ||
166 m != n)
167 api_fatal ("Couldn't read parent's cygwin heap %d bytes != %d, %E",
168 n, m);
169 }
170
171 static void *__stdcall
172 creturn (cygheap_types x, cygheap_entry * c, int len)
173 {
174 if (!c)
175 {
176 __seterrno ();
177 return NULL;
178 }
179 c->type = x;
180 if (cygheap_max < ((char *) c + len))
181 cygheap_max = (char *) c + len;
182 return (void *) c->data;
183 }
184
185 extern "C" void *__stdcall
186 cmalloc (cygheap_types x, DWORD n)
187 {
188 cygheap_entry *c;
189 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
190 if (!c)
191 system_printf ("cmalloc returned NULL");
192 return creturn (x, c, n);
193 }
194
195 extern "C" void *__stdcall
196 crealloc (void *s, DWORD n)
197 {
198 if (s == NULL)
199 return cmalloc (HEAP_STR, n); // kludge
200
201 assert (!inheap (s));
202 cygheap_entry *c = tocygheap (s);
203 cygheap_types t = c->type;
204 c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
205 if (!c)
206 system_printf ("crealloc returned NULL");
207 return creturn (t, c, n);
208 }
209
210 extern "C" void __stdcall
211 cfree (void *s)
212 {
213 assert (!inheap (s));
214 (void) _cfree (tocygheap (s));
215 }
216
217 extern "C" void *__stdcall
218 ccalloc (cygheap_types x, DWORD n, DWORD size)
219 {
220 cygheap_entry *c;
221 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n * size));
222 if (c)
223 memset (c->data, 0, size);
224 if (!c)
225 system_printf ("ccalloc returned NULL");
226 return creturn (x, c, n);
227 }
228
229 extern "C" char *__stdcall
230 cstrdup (const char *s)
231 {
232 char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
233 if (!p)
234 return NULL;
235 strcpy (p, s);
236 return p;
237 }
238
239 extern "C" char *__stdcall
240 cstrdup1 (const char *s)
241 {
242 char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
243 if (!p)
244 return NULL;
245 strcpy (p, s);
246 return p;
247 }
This page took 0.103239 seconds and 6 git commands to generate.