1 /* cygheap.cc: Cygwin heap manager.
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
20 #include "child_info.h"
27 static mini_cygheap NO_COPY cygheap_at_start =
29 {__utf8_mbtowc, __utf8_wctomb}
32 init_cygheap NO_COPY *cygheap = (init_cygheap *) &cygheap_at_start;
33 void NO_COPY *cygheap_max;
35 extern "C" char _cygheap_mid[] __attribute__((section(".cygheap")));
36 extern "C" char _cygheap_end[];
38 static NO_COPY muto cygheap_protect;
43 struct cygheap_entry *next;
47 #define NBUCKETS (sizeof (cygheap->buckets) / sizeof (cygheap->buckets[0]))
48 #define N0 ((_cmalloc_entry *) NULL)
49 #define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (unsigned) (N0->data)))
51 #define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
52 #define MVMAP_OPTIONS (FILE_MAP_WRITE)
55 static void __stdcall _cfree (void *) __attribute__((regparm(1)));
56 static void *__stdcall _csbrk (int);
59 /* Called by fork or spawn to reallocate cygwin heap */
61 cygheap_fixup_in_child (bool execed)
63 cygheap_max = child_proc_info->cygheap;
64 cygheap = (init_cygheap *) cygheap_max;
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 */
73 /* Walk the allocated memory chain looking for orphaned memory from
75 for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
77 cygheap_entry *ce = (cygheap_entry *) rvc->data;
78 if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
80 else if (ce->type < HEAP_1_MAX)
81 ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
83 _cfree (ce); /* Marked by parent for freeing in child */
89 init_cygheap::manage_console_count (const char *something, int amount, bool avoid_freeing_console)
91 if (console_count == 0 && amount > 0)
92 init_console_handler (true);
93 console_count += amount;
94 debug_printf ("%s: console_count %d, amount %d, %s, avoid_freeing_console %d",
95 something, console_count, amount, myctty (), avoid_freeing_console);
96 if (!avoid_freeing_console && amount <= 0 && !console_count && myself->ctty == -1)
98 BOOL res = FreeConsole ();
99 debug_printf ("freed console, res %d", res);
100 init_console_handler (false);
102 return console_count;
106 init_cygheap::close_ctty ()
108 debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
109 cygheap->ctty->close ();
110 cygheap->ctty = NULL;
113 #define nextpage(x) ((char *) (((DWORD) ((char *) x + granmask)) & ~granmask))
114 #define allocsize(x) ((DWORD) nextpage (x))
116 #define somekinda_printf debug_printf
118 #define somekinda_printf malloc_printf
121 static void *__stdcall
124 void *prebrk = cygheap_max;
125 size_t granmask = getpagesize () - 1;
126 char *newbase = nextpage (prebrk);
127 cygheap_max = (char *) cygheap_max + sbs;
128 if (!sbs || (newbase >= cygheap_max) || (cygheap_max <= _cygheap_end))
132 if (prebrk <= _cygheap_end)
133 newbase = _cygheap_end;
135 DWORD adjsbs = allocsize ((char *) cygheap_max - newbase);
136 if (adjsbs && !VirtualAlloc (newbase, adjsbs, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))
138 MEMORY_BASIC_INFORMATION m;
139 if (!VirtualQuery (newbase, &m, sizeof m))
140 system_printf ("couldn't get memory info, %E");
141 somekinda_printf ("Couldn't reserve/commit %d bytes of space for cygwin's heap, %E",
143 somekinda_printf ("AllocationBase %p, BaseAddress %p, RegionSize %p, State %p\n",
144 m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
146 cygheap_max = (char *) cygheap_max - sbs;
157 cygheap_protect.init ("cygheap_protect");
158 if (cygheap == &cygheap_at_start)
160 cygheap = (init_cygheap *) memset (_cygheap_start, 0,
161 _cygheap_mid - _cygheap_start);
162 cygheap_max = cygheap;
163 _csbrk (sizeof (*cygheap));
164 /* Default locale settings. */
165 cygheap->locale.mbtowc = __utf8_mbtowc;
166 cygheap->locale.wctomb = __utf8_wctomb;
167 strcpy (cygheap->locale.charset, "ASCII");
168 /* Set umask to a sane default. */
169 cygheap->umask = 022;
172 cygheap->fdtab.init ();
177 /* Copyright (C) 1997, 2000 DJ Delorie */
179 static void *_cmalloc (unsigned size) __attribute ((regparm(1)));
180 static void *__stdcall _crealloc (void *ptr, unsigned size) __attribute ((regparm(2)));
182 static void *__stdcall
183 _cmalloc (unsigned size)
188 /* Calculate "bit bucket" and size as a power of two. */
189 for (b = 3, sz = 8; sz && sz < size; b++, sz <<= 1)
192 cygheap_protect.acquire ();
193 if (cygheap->buckets[b])
195 rvc = (_cmalloc_entry *) cygheap->buckets[b];
196 cygheap->buckets[b] = rvc->ptr;
201 rvc = (_cmalloc_entry *) _csbrk (sz + sizeof (_cmalloc_entry));
204 cygheap_protect.release ();
209 rvc->prev = cygheap->chain;
210 cygheap->chain = rvc;
212 cygheap_protect.release ();
216 static void __stdcall
219 cygheap_protect.acquire ();
220 _cmalloc_entry *rvc = to_cmalloc (ptr);
222 rvc->ptr = cygheap->buckets[b];
223 cygheap->buckets[b] = (char *) rvc;
224 cygheap_protect.release ();
227 static void *__stdcall
228 _crealloc (void *ptr, unsigned size)
232 newptr = _cmalloc (size);
235 unsigned oldsize = 1 << to_cmalloc (ptr)->b;
238 newptr = _cmalloc (size);
241 memcpy (newptr, ptr, oldsize);
248 /* End Copyright (C) 1997 DJ Delorie */
250 #define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
252 #define N ((cygheap_entry *) NULL)
253 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
256 creturn (cygheap_types x, cygheap_entry * c, unsigned len, const char *fn = NULL)
261 api_fatal ("%s would have returned NULL", fn);
268 char *cend = ((char *) c + sizeof (*c) + len);
269 if (cygheap_max < cend)
272 return (void *) c->data;
276 cmalloc (cygheap_types x, DWORD n, const char *fn)
280 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
281 return creturn (x, c, n, fn);
285 cmalloc (cygheap_types x, DWORD n)
287 return cmalloc (x, n, NULL);
291 cmalloc_abort (cygheap_types x, DWORD n)
293 return cmalloc (x, n, "cmalloc");
297 crealloc (void *s, DWORD n, const char *fn)
301 return cmalloc (HEAP_STR, n); // kludge
303 assert (!inheap (s));
304 cygheap_entry *c = tocygheap (s);
305 cygheap_types t = (cygheap_types) c->type;
306 c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
307 return creturn (t, c, n, fn);
310 extern "C" void *__stdcall
311 crealloc (void *s, DWORD n)
313 return crealloc (s, n, NULL);
316 extern "C" void *__stdcall
317 crealloc_abort (void *s, DWORD n)
319 return crealloc (s, n, "crealloc");
322 extern "C" void __stdcall
325 assert (!inheap (s));
326 _cfree (tocygheap (s));
330 extern "C" void __stdcall
331 cfree_and_set (char *&s, char *what)
333 if (s && s != almost_null)
339 ccalloc (cygheap_types x, DWORD n, DWORD size, const char *fn)
344 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
346 memset (c->data, 0, n);
347 return creturn (x, c, n, fn);
350 extern "C" void *__stdcall
351 ccalloc (cygheap_types x, DWORD n, DWORD size)
353 return ccalloc (x, n, size, NULL);
356 extern "C" void *__stdcall
357 ccalloc_abort (cygheap_types x, DWORD n, DWORD size)
359 return ccalloc (x, n, size, "ccalloc");
362 extern "C" PWCHAR __stdcall
363 cwcsdup (const PWCHAR s)
366 PWCHAR p = (PWCHAR) cmalloc (HEAP_STR, wcslen (s) + 1);
374 extern "C" PWCHAR __stdcall
375 cwcsdup1 (const PWCHAR s)
378 PWCHAR p = (PWCHAR) cmalloc (HEAP_1_STR, wcslen (s) + 1);
386 extern "C" char *__stdcall
387 cstrdup (const char *s)
390 char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
398 extern "C" char *__stdcall
399 cstrdup1 (const char *s)
402 char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
411 cygheap_root::set (const char *posix, const char *native, bool caseinsensitive)
413 if (*posix == '/' && posix[1] == '\0')
423 m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
424 strcpy (m->posix_path, posix);
425 m->posix_pathlen = strlen (posix);
426 if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
427 m->posix_path[--m->posix_pathlen] = '\0';
429 strcpy (m->native_path, native);
430 m->native_pathlen = strlen (native);
431 if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
432 m->native_path[--m->native_pathlen] = '\0';
433 m->caseinsensitive = caseinsensitive;
436 cygheap_user::~cygheap_user ()
441 cygheap_user::set_name (const char *new_name)
443 bool allocated = !!pname;
447 if (strcasematch (new_name, pname))
452 pname = cstrdup (new_name ? new_name : "");
454 return; /* Initializing. Don't bother with other stuff. */
456 cfree_and_set (homedrive);
457 cfree_and_set (homepath);
458 cfree_and_set (plogsrv);
459 cfree_and_set (pdomain);
460 cfree_and_set (pwinname);