]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/cygheap.cc
* cygheap.cc (init_cheap): Allocate cygheap in shared memory for Windows NT.
[newlib-cygwin.git] / winsup / cygwin / cygheap.cc
1 /* cygheap.cc: Cygwin heap manager.
2
3 Copyright 2000, 2001 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 <string.h>
13 #include <errno.h>
14 #include <assert.h>
15 #include <stdlib.h>
16 #include "security.h"
17 #include "fhandler.h"
18 #include "dtable.h"
19 #include "cygheap.h"
20 #include "child_info.h"
21 #include "heap.h"
22 #include "cygerrno.h"
23 #include "sync.h"
24 #include "shared_info.h"
25
26 init_cygheap NO_COPY *cygheap;
27 void NO_COPY *cygheap_max = NULL;
28
29 static NO_COPY muto *cygheap_protect = NULL;
30
31 struct cygheap_entry
32 {
33 int type;
34 struct cygheap_entry *next;
35 char data[0];
36 };
37
38 #define NBUCKETS 32
39 char *buckets[NBUCKETS] = {0};
40
41 #define N0 ((_cmalloc_entry *) NULL)
42 #define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (int) (N0->data)))
43
44 #define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
45 #define MVMAP_OPTIONS (FILE_MAP_WRITE)
46
47 extern "C" {
48 static void __stdcall _cfree (void *ptr) __attribute__((regparm(1)));
49 }
50
51 inline static void
52 init_cheap ()
53 {
54 if (!iswinnt)
55 {
56 cygheap = (init_cygheap *) VirtualAlloc (NULL, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS);
57 if (!cygheap)
58 api_fatal ("Couldn't reserve space for cygwin's heap, %E");
59 }
60 else
61 {
62 HANDLE h;
63 h = CreateFileMapping (INVALID_HANDLE_VALUE, &sec_none, PAGE_READWRITE,
64 0, CYGHEAPSIZE, NULL);
65 if (!h)
66 api_fatal ("CreateFileMapping failed, %E");
67 cygheap = (init_cygheap *) MapViewOfFile (h, FILE_MAP_WRITE, 0, 0, 0);
68 if (!cygheap)
69 api_fatal ("Couldn't allocate shared memory for cygwin heap, %E");
70 CloseHandle (h);
71 }
72 cygheap_max = cygheap + 1;
73 }
74
75 void __stdcall
76 cygheap_setup_for_child (child_info *ci)
77 {
78 void *newcygheap;
79 cygheap_protect->acquire ();
80 unsigned n = (char *) cygheap_max - (char *) cygheap;
81 ci->cygheap_h = CreateFileMapping (INVALID_HANDLE_VALUE, &sec_none,
82 CFMAP_OPTIONS, 0, CYGHEAPSIZE, NULL);
83 newcygheap = MapViewOfFileEx (ci->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, NULL);
84 if (!VirtualAlloc (newcygheap, n, MEM_COMMIT, PAGE_READWRITE))
85 api_fatal ("couldn't allocate new cygwin heap for child, %E");
86 memcpy (newcygheap, cygheap, n);
87 UnmapViewOfFile (newcygheap);
88 ci->cygheap = cygheap;
89 ci->cygheap_max = cygheap_max;
90 ProtectHandle1 (ci->cygheap_h, passed_cygheap_h);
91 cygheap_protect->release ();
92 return;
93 }
94
95 void __stdcall
96 cygheap_setup_for_child_cleanup (child_info *ci)
97 {
98 ForceCloseHandle1 (ci->cygheap_h, passed_cygheap_h);
99 }
100
101 /* Called by fork or spawn to reallocate cygwin heap */
102 void __stdcall
103 cygheap_fixup_in_child (child_info *ci, bool execed)
104 {
105 cygheap = ci->cygheap;
106 cygheap_max = ci->cygheap_max;
107 void *addr = iswinnt ? cygheap : NULL;
108 void *newaddr;
109 newaddr = MapViewOfFileEx (ci->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, addr);
110 if (!iswinnt || newaddr != addr)
111 {
112 DWORD n = (DWORD) cygheap_max - (DWORD) cygheap;
113 /* Reserve cygwin heap in same spot as parent */
114 if (!VirtualAlloc (cygheap, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS))
115 api_fatal ("Couldn't reserve space for cygwin's heap (%p) in child, cygheap, %E", cygheap);
116
117 /* Allocate same amount of memory as parent */
118 if (!VirtualAlloc (cygheap, n, MEM_COMMIT, PAGE_READWRITE))
119 api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
120 cygheap, n);
121 memcpy (cygheap, newaddr, n);
122 UnmapViewOfFile (newaddr);
123 }
124
125 ForceCloseHandle1 (ci->cygheap_h, passed_cygheap_h);
126
127 cygheap_init ();
128
129 if (execed)
130 {
131 /* Walk the allocated memory chain looking for orphaned memory from
132 previous execs */
133 for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
134 {
135 cygheap_entry *ce = (cygheap_entry *) rvc->data;
136 if (rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
137 continue;
138 else if (ce->type < HEAP_1_MAX)
139 ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
140 else
141 _cfree (ce); /* Marked by parent for freeing in child */
142 }
143 }
144 }
145
146 #define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
147
148 static void *__stdcall
149 _csbrk (int sbs)
150 {
151 void *lastheap;
152 bool needalloc;
153
154 if (cygheap)
155 needalloc = 0;
156 else
157 {
158 init_cheap ();
159 needalloc = 1;
160 }
161
162 lastheap = cygheap_max;
163 (char *) cygheap_max += sbs;
164 void *heapalign = (void *) pagetrunc (lastheap);
165
166 if (!needalloc)
167 needalloc = sbs && ((heapalign == lastheap) || heapalign != pagetrunc (cygheap_max));
168 if (needalloc && !VirtualAlloc (lastheap, (DWORD) sbs ?: 1, MEM_COMMIT, PAGE_READWRITE))
169 api_fatal ("couldn't commit memory for cygwin heap, %E");
170
171 return lastheap;
172 }
173
174 extern "C" void __stdcall
175 cygheap_init ()
176 {
177 cygheap_protect = new_muto (FALSE, "cygheap_protect");
178 _csbrk (0);
179 if (!cygheap->fdtab)
180 cygheap->fdtab.init ();
181 }
182
183 /* Copyright (C) 1997, 2000 DJ Delorie */
184
185 static void *_cmalloc (int size) __attribute ((regparm(1)));
186 static void *__stdcall _crealloc (void *ptr, int size) __attribute ((regparm(2)));
187
188 static void *__stdcall
189 _cmalloc (int size)
190 {
191 _cmalloc_entry *rvc;
192 unsigned b, sz;
193
194 /* Calculate "bit bucket" and size as a power of two. */
195 for (b = 3, sz = 8; sz && sz < (size + sizeof (_cmalloc_entry));
196 b++, sz <<= 1)
197 continue;
198
199 cygheap_protect->acquire ();
200 if (buckets[b])
201 {
202 rvc = (_cmalloc_entry *) buckets[b];
203 buckets[b] = rvc->ptr;
204 rvc->b = b;
205 }
206 else
207 {
208 size = sz + sizeof (_cmalloc_entry);
209 rvc = (_cmalloc_entry *) _csbrk (size);
210
211 rvc->b = b;
212 rvc->prev = cygheap->chain;
213 cygheap->chain = rvc;
214 }
215 cygheap_protect->release ();
216 return rvc->data;
217 }
218
219 static void __stdcall
220 _cfree (void *ptr)
221 {
222 cygheap_protect->acquire ();
223 _cmalloc_entry *rvc = to_cmalloc (ptr);
224 DWORD b = rvc->b;
225 rvc->ptr = buckets[b];
226 buckets[b] = (char *) rvc;
227 cygheap_protect->release ();
228 }
229
230 static void *__stdcall _crealloc (void *ptr, int size) __attribute__((regparm(2)));
231 static void *__stdcall
232 _crealloc (void *ptr, int size)
233 {
234 void *newptr;
235 if (ptr == NULL)
236 newptr = _cmalloc (size);
237 else
238 {
239 int oldsize = 1 << to_cmalloc (ptr)->b;
240 if (size <= oldsize)
241 return ptr;
242 newptr = _cmalloc (size);
243 memcpy (newptr, ptr, oldsize);
244 _cfree (ptr);
245 }
246 return newptr;
247 }
248
249 /* End Copyright (C) 1997 DJ Delorie */
250
251 #define sizeof_cygheap(n) ((n) + sizeof(cygheap_entry))
252
253 #define N ((cygheap_entry *) NULL)
254 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
255
256 inline static void *
257 creturn (cygheap_types x, cygheap_entry * c, int len)
258 {
259 if (!c)
260 {
261 __seterrno ();
262 return NULL;
263 }
264 c->type = x;
265 char *cend = ((char *) c + sizeof (*c) + len);
266 if (cygheap_max < cend)
267 cygheap_max = cend;
268 MALLOC_CHECK;
269 return (void *) c->data;
270 }
271
272 extern "C" void *__stdcall
273 cmalloc (cygheap_types x, DWORD n)
274 {
275 cygheap_entry *c;
276 MALLOC_CHECK;
277 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
278 if (!c)
279 system_printf ("cmalloc returned NULL");
280 return creturn (x, c, n);
281 }
282
283 extern "C" void *__stdcall
284 crealloc (void *s, DWORD n)
285 {
286 MALLOC_CHECK;
287 if (s == NULL)
288 return cmalloc (HEAP_STR, n); // kludge
289
290 assert (!inheap (s));
291 cygheap_entry *c = tocygheap (s);
292 cygheap_types t = (cygheap_types) c->type;
293 c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
294 if (!c)
295 system_printf ("crealloc returned NULL");
296 return creturn (t, c, n);
297 }
298
299 extern "C" void __stdcall
300 cfree (void *s)
301 {
302 MALLOC_CHECK;
303 assert (!inheap (s));
304 (void) _cfree (tocygheap (s));
305 MALLOC_CHECK;
306 }
307
308 extern "C" void *__stdcall
309 ccalloc (cygheap_types x, DWORD n, DWORD size)
310 {
311 cygheap_entry *c;
312 MALLOC_CHECK;
313 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n * size));
314 if (c)
315 memset (c->data, 0, n * size);
316 if (!c)
317 system_printf ("ccalloc returned NULL");
318 return creturn (x, c, n);
319 }
320
321 extern "C" char *__stdcall
322 cstrdup (const char *s)
323 {
324 MALLOC_CHECK;
325 char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
326 if (!p)
327 return NULL;
328 strcpy (p, s);
329 MALLOC_CHECK;
330 return p;
331 }
332
333 extern "C" char *__stdcall
334 cstrdup1 (const char *s)
335 {
336 MALLOC_CHECK;
337 char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
338 if (!p)
339 return NULL;
340 strcpy (p, s);
341 MALLOC_CHECK;
342 return p;
343 }
344
345 void
346 cygheap_root::set (const char *posix, const char *native)
347 {
348 if (*posix == '/' && posix[1] == '\0')
349 {
350 if (m)
351 {
352 cfree (m);
353 m = NULL;
354 }
355 return;
356 }
357 if (!m)
358 m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
359 strcpy (m->posix_path, posix);
360 m->posix_pathlen = strlen (posix);
361 if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
362 m->posix_path[--m->posix_pathlen] = '\0';
363
364 strcpy (m->native_path, native);
365 m->native_pathlen = strlen (native);
366 if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
367 m->native_path[--m->native_pathlen] = '\0';
368 }
369
370 cygheap_user::~cygheap_user ()
371 {
372 #if 0
373 if (pname)
374 cfree (pname);
375 if (plogsrv)
376 cfree (plogsrv);
377 if (pdomain)
378 cfree (pdomain);
379 if (psid)
380 cfree (psid);
381 #endif
382 }
383
384 void
385 cygheap_user::set_name (const char *new_name)
386 {
387 if (pname)
388 cfree (pname);
389 pname = cstrdup (new_name ? new_name : "");
390 }
391
392 void
393 cygheap_user::set_logsrv (const char *new_logsrv)
394 {
395 if (plogsrv)
396 cfree (plogsrv);
397 plogsrv = (new_logsrv && *new_logsrv) ? cstrdup (new_logsrv) : NULL;
398 }
399
400 void
401 cygheap_user::set_domain (const char *new_domain)
402 {
403 if (pdomain)
404 cfree (pdomain);
405 pdomain = (new_domain && *new_domain) ? cstrdup (new_domain) : NULL;
406 }
407
408 BOOL
409 cygheap_user::set_sid (PSID new_sid)
410 {
411 if (!new_sid)
412 {
413 if (psid)
414 cfree (psid);
415 psid = NULL;
416 return TRUE;
417 }
418 else
419 {
420 if (!psid)
421 psid = cmalloc (HEAP_STR, MAX_SID_LEN);
422 return CopySid (MAX_SID_LEN, psid, new_sid);
423 }
424 }
This page took 0.058709 seconds and 6 git commands to generate.