]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/cygheap.cc
* Makefile.in (DLL_OFILES): Add kernel32.o.
[newlib-cygwin.git] / winsup / cygwin / cygheap.cc
1 /* cygheap.cc: Cygwin heap manager.
2
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 <assert.h>
13 #include <stdlib.h>
14 #include "cygerrno.h"
15 #include "security.h"
16 #include "path.h"
17 #include "fhandler.h"
18 #include "dtable.h"
19 #include "cygheap.h"
20 #include "child_info.h"
21 #include "heap.h"
22 #include "sigproc.h"
23 #include "pinfo.h"
24 #include <unistd.h>
25 #include <wchar.h>
26
27 init_cygheap NO_COPY *cygheap;
28 void NO_COPY *cygheap_max;
29
30 extern "C" char _cygheap_mid[] __attribute__((section(".cygheap")));
31 extern "C" char _cygheap_end[];
32
33 static NO_COPY muto cygheap_protect;
34
35 struct cygheap_entry
36 {
37 int type;
38 struct cygheap_entry *next;
39 char data[0];
40 };
41
42 #define NBUCKETS (sizeof (cygheap->buckets) / sizeof (cygheap->buckets[0]))
43 #define N0 ((_cmalloc_entry *) NULL)
44 #define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (unsigned) (N0->data)))
45
46 #define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
47 #define MVMAP_OPTIONS (FILE_MAP_WRITE)
48
49 extern "C" {
50 static void __stdcall _cfree (void *) __attribute__((regparm(1)));
51 static void *__stdcall _csbrk (int);
52 }
53
54 /* Called by fork or spawn to reallocate cygwin heap */
55 void __stdcall
56 cygheap_fixup_in_child (bool execed)
57 {
58 cygheap_max = child_proc_info->cygheap;
59 cygheap = (init_cygheap *) cygheap_max;
60 _csbrk ((char *) child_proc_info->cygheap_max - (char *) cygheap);
61 child_copy (child_proc_info->parent, false, "cygheap", cygheap, cygheap_max, NULL);
62 cygheap_init ();
63 debug_fixup_after_fork_exec ();
64 if (execed)
65 {
66 cygheap->hooks.next = NULL;
67 cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
68 /* Walk the allocated memory chain looking for orphaned memory from
69 previous execs */
70 for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
71 {
72 cygheap_entry *ce = (cygheap_entry *) rvc->data;
73 if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
74 continue;
75 else if (ce->type < HEAP_1_MAX)
76 ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
77 else
78 _cfree (ce); /* Marked by parent for freeing in child */
79 }
80 }
81 }
82
83 int
84 init_cygheap::manage_console_count (const char *something, int amount, bool avoid_freeing_console)
85 {
86 if (console_count == 0 && amount > 0)
87 init_console_handler (true);
88 console_count += amount;
89 debug_printf ("%s: console_count %d, amount %d, %s, avoid_freeing_console %d",
90 something, console_count, amount, myctty (), avoid_freeing_console);
91 if (!avoid_freeing_console && amount <= 0 && !console_count && myself->ctty == -1)
92 {
93 BOOL res = FreeConsole ();
94 debug_printf ("freed console, res %d", res);
95 init_console_handler (false);
96 }
97 return console_count;
98 }
99
100 void
101 init_cygheap::close_ctty ()
102 {
103 debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
104 cygheap->ctty->close ();
105 cygheap->ctty = NULL;
106 }
107
108 #define nextpage(x) ((char *) (((DWORD) ((char *) x + granmask)) & ~granmask))
109 #define allocsize(x) ((DWORD) nextpage (x))
110 #ifdef DEBUGGING
111 #define somekinda_printf debug_printf
112 #else
113 #define somekinda_printf malloc_printf
114 #endif
115
116 static void *__stdcall
117 _csbrk (int sbs)
118 {
119 void *prebrk = cygheap_max;
120 size_t granmask = getpagesize () - 1;
121 char *newbase = nextpage (prebrk);
122 cygheap_max = (char *) cygheap_max + sbs;
123 if (!sbs || (newbase >= cygheap_max) || (cygheap_max <= _cygheap_end))
124 /* nothing to do */;
125 else
126 {
127 if (prebrk <= _cygheap_end)
128 newbase = _cygheap_end;
129
130 DWORD adjsbs = allocsize ((char *) cygheap_max - newbase);
131 if (adjsbs && !VirtualAlloc (newbase, adjsbs, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))
132 {
133 MEMORY_BASIC_INFORMATION m;
134 if (!VirtualQuery (newbase, &m, sizeof m))
135 system_printf ("couldn't get memory info, %E");
136 somekinda_printf ("Couldn't reserve/commit %d bytes of space for cygwin's heap, %E",
137 adjsbs);
138 somekinda_printf ("AllocationBase %p, BaseAddress %p, RegionSize %p, State %p\n",
139 m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
140 __seterrno ();
141 cygheap_max = (char *) cygheap_max - sbs;
142 return NULL;
143 }
144 }
145
146 return prebrk;
147 }
148
149 extern "C" void __stdcall
150 cygheap_init ()
151 {
152 cygheap_protect.init ("cygheap_protect");
153 if (!cygheap)
154 {
155 cygheap = (init_cygheap *) memset (_cygheap_start, 0,
156 _cygheap_mid - _cygheap_start);
157 cygheap_max = cygheap;
158 _csbrk (sizeof (*cygheap));
159 }
160 if (!cygheap->fdtab)
161 cygheap->fdtab.init ();
162 if (!cygheap->sigs)
163 sigalloc ();
164 }
165
166 /* Copyright (C) 1997, 2000 DJ Delorie */
167
168 static void *_cmalloc (unsigned size) __attribute ((regparm(1)));
169 static void *__stdcall _crealloc (void *ptr, unsigned size) __attribute ((regparm(2)));
170
171 static void *__stdcall
172 _cmalloc (unsigned size)
173 {
174 _cmalloc_entry *rvc;
175 unsigned b, sz;
176
177 /* Calculate "bit bucket" and size as a power of two. */
178 for (b = 3, sz = 8; sz && sz < size; b++, sz <<= 1)
179 continue;
180
181 cygheap_protect.acquire ();
182 if (cygheap->buckets[b])
183 {
184 rvc = (_cmalloc_entry *) cygheap->buckets[b];
185 cygheap->buckets[b] = rvc->ptr;
186 rvc->b = b;
187 }
188 else
189 {
190 rvc = (_cmalloc_entry *) _csbrk (sz + sizeof (_cmalloc_entry));
191 if (!rvc)
192 {
193 cygheap_protect.release ();
194 return NULL;
195 }
196
197 rvc->b = b;
198 rvc->prev = cygheap->chain;
199 cygheap->chain = rvc;
200 }
201 cygheap_protect.release ();
202 return rvc->data;
203 }
204
205 static void __stdcall
206 _cfree (void *ptr)
207 {
208 cygheap_protect.acquire ();
209 _cmalloc_entry *rvc = to_cmalloc (ptr);
210 DWORD b = rvc->b;
211 rvc->ptr = cygheap->buckets[b];
212 cygheap->buckets[b] = (char *) rvc;
213 cygheap_protect.release ();
214 }
215
216 static void *__stdcall
217 _crealloc (void *ptr, unsigned size)
218 {
219 void *newptr;
220 if (ptr == NULL)
221 newptr = _cmalloc (size);
222 else
223 {
224 unsigned oldsize = 1 << to_cmalloc (ptr)->b;
225 if (size <= oldsize)
226 return ptr;
227 newptr = _cmalloc (size);
228 if (newptr)
229 {
230 memcpy (newptr, ptr, oldsize);
231 _cfree (ptr);
232 }
233 }
234 return newptr;
235 }
236
237 /* End Copyright (C) 1997 DJ Delorie */
238
239 #define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
240
241 #define N ((cygheap_entry *) NULL)
242 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
243
244 inline static void *
245 creturn (cygheap_types x, cygheap_entry * c, unsigned len, const char *fn = NULL)
246 {
247 if (!c)
248 if (fn)
249 api_fatal ("%s would have returned NULL", fn);
250 else
251 {
252 set_errno (ENOMEM);
253 return NULL;
254 }
255 c->type = x;
256 char *cend = ((char *) c + sizeof (*c) + len);
257 if (cygheap_max < cend)
258 cygheap_max = cend;
259 MALLOC_CHECK;
260 return (void *) c->data;
261 }
262
263 inline static void *
264 cmalloc (cygheap_types x, DWORD n, const char *fn)
265 {
266 cygheap_entry *c;
267 MALLOC_CHECK;
268 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
269 return creturn (x, c, n, fn);
270 }
271
272 extern "C" void *
273 cmalloc (cygheap_types x, DWORD n)
274 {
275 return cmalloc (x, n, NULL);
276 }
277
278 extern "C" void *
279 cmalloc_abort (cygheap_types x, DWORD n)
280 {
281 return cmalloc (x, n, "cmalloc");
282 }
283
284 inline static void *
285 crealloc (void *s, DWORD n, const char *fn)
286 {
287 MALLOC_CHECK;
288 if (s == NULL)
289 return cmalloc (HEAP_STR, n); // kludge
290
291 assert (!inheap (s));
292 cygheap_entry *c = tocygheap (s);
293 cygheap_types t = (cygheap_types) c->type;
294 c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
295 return creturn (t, c, n, fn);
296 }
297
298 extern "C" void *__stdcall
299 crealloc (void *s, DWORD n)
300 {
301 return crealloc (s, n, NULL);
302 }
303
304 extern "C" void *__stdcall
305 crealloc_abort (void *s, DWORD n)
306 {
307 return crealloc (s, n, "crealloc");
308 }
309
310 extern "C" void __stdcall
311 cfree (void *s)
312 {
313 assert (!inheap (s));
314 _cfree (tocygheap (s));
315 MALLOC_CHECK;
316 }
317
318 extern "C" void __stdcall
319 cfree_and_set (char *&s, char *what)
320 {
321 if (s && s != almost_null)
322 cfree (s);
323 s = what;
324 }
325
326 inline static void *
327 ccalloc (cygheap_types x, DWORD n, DWORD size, const char *fn)
328 {
329 cygheap_entry *c;
330 MALLOC_CHECK;
331 n *= size;
332 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
333 if (c)
334 memset (c->data, 0, n);
335 return creturn (x, c, n, fn);
336 }
337
338 extern "C" void *__stdcall
339 ccalloc (cygheap_types x, DWORD n, DWORD size)
340 {
341 return ccalloc (x, n, size, NULL);
342 }
343
344 extern "C" void *__stdcall
345 ccalloc_abort (cygheap_types x, DWORD n, DWORD size)
346 {
347 return ccalloc (x, n, size, "ccalloc");
348 }
349
350 extern "C" PWCHAR __stdcall
351 cwcsdup (const PWCHAR s)
352 {
353 MALLOC_CHECK;
354 PWCHAR p = (PWCHAR) cmalloc (HEAP_STR, wcslen (s) + 1);
355 if (!p)
356 return NULL;
357 wcpcpy (p, s);
358 MALLOC_CHECK;
359 return p;
360 }
361
362 extern "C" PWCHAR __stdcall
363 cwcsdup1 (const PWCHAR s)
364 {
365 MALLOC_CHECK;
366 PWCHAR p = (PWCHAR) cmalloc (HEAP_1_STR, wcslen (s) + 1);
367 if (!p)
368 return NULL;
369 wcpcpy (p, s);
370 MALLOC_CHECK;
371 return p;
372 }
373
374 extern "C" char *__stdcall
375 cstrdup (const char *s)
376 {
377 MALLOC_CHECK;
378 char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
379 if (!p)
380 return NULL;
381 strcpy (p, s);
382 MALLOC_CHECK;
383 return p;
384 }
385
386 extern "C" char *__stdcall
387 cstrdup1 (const char *s)
388 {
389 MALLOC_CHECK;
390 char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
391 if (!p)
392 return NULL;
393 strcpy (p, s);
394 MALLOC_CHECK;
395 return p;
396 }
397
398 void
399 cygheap_root::set (const char *posix, const char *native)
400 {
401 if (*posix == '/' && posix[1] == '\0')
402 {
403 if (m)
404 {
405 cfree (m);
406 m = NULL;
407 }
408 return;
409 }
410 if (!m)
411 m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
412 strcpy (m->posix_path, posix);
413 m->posix_pathlen = strlen (posix);
414 if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
415 m->posix_path[--m->posix_pathlen] = '\0';
416
417 strcpy (m->native_path, native);
418 m->native_pathlen = strlen (native);
419 if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
420 m->native_path[--m->native_pathlen] = '\0';
421 }
422
423 cygheap_user::~cygheap_user ()
424 {
425 #if 0
426 if (pname)
427 cfree (pname);
428 if (plogsrv)
429 cfree (plogsrv - 2);
430 if (pdomain)
431 cfree (pdomain);
432 if (psid)
433 cfree (psid);
434 #endif
435 }
436
437 void
438 cygheap_user::set_name (const char *new_name)
439 {
440 bool allocated = !!pname;
441
442 if (allocated)
443 {
444 if (strcasematch (new_name, pname))
445 return;
446 cfree (pname);
447 }
448
449 pname = cstrdup (new_name ? new_name : "");
450 if (!allocated)
451 return; /* Initializing. Don't bother with other stuff. */
452
453 cfree_and_set (homedrive);
454 cfree_and_set (homepath);
455 cfree_and_set (plogsrv);
456 cfree_and_set (pdomain);
457 cfree_and_set (pwinname);
458 }
This page took 0.055327 seconds and 5 git commands to generate.