1 /* cygheap.cc: Cygwin heap manager.
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010, 2011 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
22 #include "child_info.h"
29 static mini_cygheap NO_COPY cygheap_dummy =
31 {__utf8_mbtowc, __utf8_wctomb}
34 init_cygheap NO_COPY *cygheap = (init_cygheap *) &cygheap_dummy;
35 void NO_COPY *cygheap_max;
37 extern "C" char _cygheap_end[];
39 static NO_COPY muto cygheap_protect;
44 struct cygheap_entry *next;
48 #define NBUCKETS (sizeof (cygheap->buckets) / sizeof (cygheap->buckets[0]))
49 #define N0 ((_cmalloc_entry *) NULL)
50 #define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (unsigned) (N0->data)))
52 #define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
53 #define MVMAP_OPTIONS (FILE_MAP_WRITE)
56 static void __stdcall _cfree (void *) __attribute__((regparm(1)));
57 static void *__stdcall _csbrk (int);
60 /* Called by fork or spawn to reallocate cygwin heap */
62 cygheap_fixup_in_child (bool execed)
64 cygheap_max = cygheap = (init_cygheap *) _cygheap_start;
65 _csbrk ((char *) child_proc_info->cygheap_max - (char *) cygheap);
66 child_copy (child_proc_info->parent, false, "cygheap", cygheap, cygheap_max, NULL);
68 debug_fixup_after_fork_exec ();
71 cygheap->hooks.next = NULL;
72 cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
74 /* Walk the allocated memory chain looking for orphaned memory from
75 previous execs or forks */
76 for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
78 cygheap_entry *ce = (cygheap_entry *) rvc->data;
79 if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
81 else if (ce->type > HEAP_2_MAX)
82 _cfree (ce); /* Marked for freeing in any child */
85 else if (ce->type > HEAP_1_MAX)
86 _cfree (ce); /* Marked for freeing in execed child */
88 ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
93 init_cygheap::manage_console_count (const char *something, int amount, bool avoid_freeing_console)
95 if (console_count == 0 && amount > 0)
96 init_console_handler (true);
97 console_count += amount;
98 debug_printf ("%s: console_count %d, amount %d, %s, avoid_freeing_console %d",
99 something, console_count, amount, myctty (), avoid_freeing_console);
100 if (!avoid_freeing_console && amount <= 0 && !console_count && myself->ctty == -1)
102 BOOL res = FreeConsole ();
103 debug_printf ("freed console, res %d", res);
104 init_console_handler (false);
106 return console_count;
110 init_cygheap::close_ctty ()
112 debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
113 cygheap->ctty->close_with_arch ();
114 cygheap->ctty = NULL;
117 #define nextpage(x) ((char *) (((DWORD) ((char *) x + granmask)) & ~granmask))
118 #define allocsize(x) ((DWORD) nextpage (x))
120 #define somekinda_printf debug_printf
122 #define somekinda_printf malloc_printf
125 static void *__stdcall
128 void *prebrk = cygheap_max;
129 size_t granmask = getpagesize () - 1;
130 char *newbase = nextpage (prebrk);
131 cygheap_max = (char *) cygheap_max + sbs;
132 if (!sbs || (newbase >= cygheap_max) || (cygheap_max <= _cygheap_end))
136 if (prebrk <= _cygheap_end)
137 newbase = _cygheap_end;
139 DWORD adjsbs = allocsize ((char *) cygheap_max - newbase);
140 if (adjsbs && !VirtualAlloc (newbase, adjsbs, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))
142 MEMORY_BASIC_INFORMATION m;
143 if (!VirtualQuery (newbase, &m, sizeof m))
144 system_printf ("couldn't get memory info, %E");
145 somekinda_printf ("Couldn't reserve/commit %d bytes of space for cygwin's heap, %E",
147 somekinda_printf ("AllocationBase %p, BaseAddress %p, RegionSize %p, State %p\n",
148 m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
150 cygheap_max = (char *) cygheap_max - sbs;
161 cygheap_protect.init ("cygheap_protect");
162 if (cygheap == &cygheap_dummy)
164 cygheap = (init_cygheap *) memset (_cygheap_start, 0,
166 cygheap_max = cygheap;
167 _csbrk (sizeof (*cygheap));
168 /* Default locale settings. */
169 cygheap->locale.mbtowc = __utf8_mbtowc;
170 cygheap->locale.wctomb = __utf8_wctomb;
171 strcpy (cygheap->locale.charset, "UTF-8");
172 /* Set umask to a sane default. */
173 cygheap->umask = 022;
174 cygheap->rlim_core = RLIM_INFINITY;
177 cygheap->fdtab.init ();
182 /* Copyright (C) 1997, 2000 DJ Delorie */
184 static void *__stdcall _cmalloc (unsigned size) __attribute__ ((regparm(1)));
185 static void *__stdcall _crealloc (void *ptr, unsigned size) __attribute__ ((regparm(2)));
187 static void *__stdcall __attribute__ ((regparm(1)))
188 _cmalloc (unsigned size)
193 /* Calculate "bit bucket" and size as a power of two. */
194 for (b = 3, sz = 8; sz && sz < size; b++, sz <<= 1)
197 cygheap_protect.acquire ();
198 if (cygheap->buckets[b])
200 rvc = (_cmalloc_entry *) cygheap->buckets[b];
201 cygheap->buckets[b] = rvc->ptr;
206 rvc = (_cmalloc_entry *) _csbrk (sz + sizeof (_cmalloc_entry));
209 cygheap_protect.release ();
214 rvc->prev = cygheap->chain;
215 cygheap->chain = rvc;
217 cygheap_protect.release ();
221 static void __stdcall __attribute__ ((regparm(1)))
224 cygheap_protect.acquire ();
225 _cmalloc_entry *rvc = to_cmalloc (ptr);
227 rvc->ptr = cygheap->buckets[b];
228 cygheap->buckets[b] = (char *) rvc;
229 cygheap_protect.release ();
232 static void *__stdcall __attribute__ ((regparm(2)))
233 _crealloc (void *ptr, unsigned size)
237 newptr = _cmalloc (size);
240 unsigned oldsize = 1 << to_cmalloc (ptr)->b;
243 newptr = _cmalloc (size);
246 memcpy (newptr, ptr, oldsize);
253 /* End Copyright (C) 1997 DJ Delorie */
255 #define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
257 #define N ((cygheap_entry *) NULL)
258 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
261 creturn (cygheap_types x, cygheap_entry * c, unsigned len, const char *fn = NULL)
266 api_fatal ("%s would have returned NULL", fn);
273 char *cend = ((char *) c + sizeof (*c) + len);
274 if (cygheap_max < cend)
277 return (void *) c->data;
281 cmalloc (cygheap_types x, DWORD n, const char *fn)
285 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
286 return creturn (x, c, n, fn);
290 cmalloc (cygheap_types x, DWORD n)
292 return cmalloc (x, n, NULL);
296 cmalloc_abort (cygheap_types x, DWORD n)
298 return cmalloc (x, n, "cmalloc");
302 crealloc (void *s, DWORD n, const char *fn)
306 return cmalloc (HEAP_STR, n); // kludge
308 assert (!inheap (s));
309 cygheap_entry *c = tocygheap (s);
310 cygheap_types t = (cygheap_types) c->type;
311 c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
312 return creturn (t, c, n, fn);
315 extern "C" void *__stdcall __attribute__ ((regparm(2)))
316 crealloc (void *s, DWORD n)
318 return crealloc (s, n, NULL);
321 extern "C" void *__stdcall __attribute__ ((regparm(2)))
322 crealloc_abort (void *s, DWORD n)
324 return crealloc (s, n, "crealloc");
327 extern "C" void __stdcall __attribute__ ((regparm(1)))
330 assert (!inheap (s));
331 _cfree (tocygheap (s));
335 extern "C" void __stdcall __attribute__ ((regparm(2)))
336 cfree_and_set (char *&s, char *what)
338 if (s && s != almost_null)
344 ccalloc (cygheap_types x, DWORD n, DWORD size, const char *fn)
349 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
351 memset (c->data, 0, n);
352 return creturn (x, c, n, fn);
355 extern "C" void *__stdcall __attribute__ ((regparm(3)))
356 ccalloc (cygheap_types x, DWORD n, DWORD size)
358 return ccalloc (x, n, size, NULL);
361 extern "C" void *__stdcall __attribute__ ((regparm(3)))
362 ccalloc_abort (cygheap_types x, DWORD n, DWORD size)
364 return ccalloc (x, n, size, "ccalloc");
367 extern "C" PWCHAR __stdcall __attribute__ ((regparm(1)))
368 cwcsdup (const PWCHAR s)
371 PWCHAR p = (PWCHAR) cmalloc (HEAP_STR, (wcslen (s) + 1) * sizeof (WCHAR));
379 extern "C" PWCHAR __stdcall __attribute__ ((regparm(1)))
380 cwcsdup1 (const PWCHAR s)
383 PWCHAR p = (PWCHAR) cmalloc (HEAP_1_STR, (wcslen (s) + 1) * sizeof (WCHAR));
391 extern "C" char *__stdcall __attribute__ ((regparm(1)))
392 cstrdup (const char *s)
395 char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
403 extern "C" char *__stdcall __attribute__ ((regparm(1)))
404 cstrdup1 (const char *s)
407 char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
416 cygheap_root::set (const char *posix, const char *native, bool caseinsensitive)
418 if (*posix == '/' && posix[1] == '\0')
428 m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
429 strcpy (m->posix_path, posix);
430 m->posix_pathlen = strlen (posix);
431 if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
432 m->posix_path[--m->posix_pathlen] = '\0';
434 strcpy (m->native_path, native);
435 m->native_pathlen = strlen (native);
436 if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
437 m->native_path[--m->native_pathlen] = '\0';
438 m->caseinsensitive = caseinsensitive;
441 cygheap_user::~cygheap_user ()
446 cygheap_user::set_name (const char *new_name)
448 bool allocated = !!pname;
452 /* Windows user names are case-insensitive. Here we want the correct
453 username, though, even if it only differs by case. */
454 if (!strcmp (new_name, pname))
459 pname = cstrdup (new_name ? new_name : "");
461 return; /* Initializing. Don't bother with other stuff. */
463 cfree_and_set (homedrive);
464 cfree_and_set (homepath);
465 cfree_and_set (plogsrv);
466 cfree_and_set (pdomain);
467 cfree_and_set (pwinname);