From 33d97d1329769b273c592a8474021c669bf72e6f Mon Sep 17 00:00:00 2001 From: cgf Date: Mon, 2 Oct 2000 02:26:04 +0000 Subject: [PATCH] * cygheap.cc (cygheap_init): Born again function. (_cmalloc): Reorganize to accomodate muto locking. (_cfree): Use muto lock to avoid multi-thread problems. * cygheap.h (incygheap): Just use cygheap_max to find upper cygwin heap bounds. * dcrt0.cc (dll_crt0_1): Reinstitute cygheap_init call. * path.cc (getcwd): Just return cwdstuff::get result, allowing correct handling of negative length. (cwdstuff::get): Malloc a buffer if one is not available. --- winsup/cygwin/ChangeLog | 15 ++++++++++++++- winsup/cygwin/cygheap.cc | 39 ++++++++++++++++++++++++++++++++------- winsup/cygwin/cygheap.h | 3 ++- winsup/cygwin/dcrt0.cc | 1 + winsup/cygwin/debug.cc | 2 +- winsup/cygwin/path.cc | 42 +++++++++--------------------------------- winsup/cygwin/sync.cc | 2 +- 7 files changed, 60 insertions(+), 44 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 8d9569945c..0c6bd674cc 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,15 @@ +Sun Oct 1 22:20:39 2000 Christopher Faylor + + * cygheap.cc (cygheap_init): Born again function. + (_cmalloc): Reorganize to accomodate muto locking. + (_cfree): Use muto lock to avoid multi-thread problems. + * cygheap.h (incygheap): Just use cygheap_max to find upper cygwin heap + bounds. + * dcrt0.cc (dll_crt0_1): Reinstitute cygheap_init call. + * path.cc (getcwd): Just return cwdstuff::get result, allowing correct + handling of negative length. + (cwdstuff::get): Malloc a buffer if one is not available. + Sun Oct 1 2:56:00 2000 Corinna Vinschen * Makefile.in: Add fhandler_mem.o to the dependencies. @@ -115,7 +127,8 @@ Sun Sep 17 22:18:39 2000 Christopher Faylor Fri Sep 15 22:30:40 2000 Christopher Faylor - * exceptions.cc (handle_exceptions): Just "core dump" if SIGSEGV in signal thread. + * exceptions.cc (handle_exceptions): Just "core dump" if SIGSEGV in + signal thread. * external.cc (fillout_pinfo): Fix compiler warning. * sigproc.h: Eliminate special asm naming for sig_dispatch_pending. * sigproc.cc (sig_send): Remove debugging statements. diff --git a/winsup/cygwin/cygheap.cc b/winsup/cygwin/cygheap.cc index 5509ebae06..e71c94d0e4 100644 --- a/winsup/cygwin/cygheap.cc +++ b/winsup/cygwin/cygheap.cc @@ -12,13 +12,23 @@ #include #include #include +#include #include "cygheap.h" #include "heap.h" #include "cygerrno.h" +#include "sync.h" void *cygheap = NULL; void *cygheap_max = NULL; +static NO_COPY muto *cygheap_protect = NULL; + +extern "C" void __stdcall +cygheap_init () +{ + cygheap_protect = new_muto (FALSE, "cygheap_protect"); +} + inline static void init_cheap () { @@ -116,30 +126,35 @@ _cmalloc (int size) init_buckets (); b = size2bucket (size); + cygheap_protect->acquire (); if (buckets[b]) { rvc = (_cmalloc_entry *) buckets[b]; buckets[b] = rvc->ptr; rvc->b = b; - return rvc->data; } + else + { + size = bucket2size[b] + sizeof (_cmalloc_entry); + rvc = (_cmalloc_entry *) _csbrk (size); - size = bucket2size[b] + sizeof (_cmalloc_entry); - rvc = (_cmalloc_entry *) _csbrk (size); - - rvc->b = b; - rvc->prev = *cygheap_chain; - *cygheap_chain = rvc; + rvc->b = b; + rvc->prev = *cygheap_chain; + *cygheap_chain = rvc; + } + cygheap_protect->release (); return rvc->data; } static void __stdcall _cfree (void *ptr) { + cygheap_protect->acquire (); _cmalloc_entry *rvc = to_cmalloc (ptr); DWORD b = rvc->b; rvc->ptr = buckets[b]; buckets[b] = (char *) rvc; + cygheap_protect->release (); } static void *__stdcall @@ -226,6 +241,7 @@ creturn (cygheap_types x, cygheap_entry * c, int len) c->type = x; if (cygheap_max < ((char *) c + len)) cygheap_max = (char *) c + len; + MALLOC_CHECK; return (void *) c->data; } @@ -233,6 +249,7 @@ extern "C" void *__stdcall cmalloc (cygheap_types x, DWORD n) { cygheap_entry *c; + MALLOC_CHECK; c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n)); if (!c) system_printf ("cmalloc returned NULL"); @@ -242,6 +259,7 @@ cmalloc (cygheap_types x, DWORD n) extern "C" void *__stdcall crealloc (void *s, DWORD n) { + MALLOC_CHECK; if (s == NULL) return cmalloc (HEAP_STR, n); // kludge @@ -257,14 +275,17 @@ crealloc (void *s, DWORD n) extern "C" void __stdcall cfree (void *s) { + MALLOC_CHECK; assert (!inheap (s)); (void) _cfree (tocygheap (s)); + MALLOC_CHECK; } extern "C" void *__stdcall ccalloc (cygheap_types x, DWORD n, DWORD size) { cygheap_entry *c; + MALLOC_CHECK; c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n * size)); if (c) memset (c->data, 0, size); @@ -276,19 +297,23 @@ ccalloc (cygheap_types x, DWORD n, DWORD size) extern "C" char *__stdcall cstrdup (const char *s) { + MALLOC_CHECK; char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1); if (!p) return NULL; strcpy (p, s); + MALLOC_CHECK; return p; } extern "C" char *__stdcall cstrdup1 (const char *s) { + MALLOC_CHECK; char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1); if (!p) return NULL; strcpy (p, s); + MALLOC_CHECK; return p; } diff --git a/winsup/cygwin/cygheap.h b/winsup/cygwin/cygheap.h index 18a4a6b787..fc335c231d 100644 --- a/winsup/cygwin/cygheap.h +++ b/winsup/cygwin/cygheap.h @@ -29,7 +29,7 @@ enum cygheap_types extern HANDLE cygheap; extern HANDLE cygheap_max; -#define incygheap(s) (cygheap && ((char *) (s) >= (char *) cygheap) && ((char *) (s) <= ((char *) cygheap) + CYGHEAPSIZE)) +#define incygheap(s) (cygheap && ((char *) (s) >= (char *) cygheap) && ((char *) (s) <= ((char *) cygheap_max))) extern "C" { void __stdcall cfree (void *); @@ -39,4 +39,5 @@ void *__stdcall crealloc (void *, DWORD); void *__stdcall ccalloc (cygheap_types, DWORD, DWORD); char *__stdcall cstrdup (const char *); char *__stdcall cstrdup1 (const char *); +void __stdcall cygheap_init (); } diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index 4fb5f8d404..cb456b3cc4 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -654,6 +654,7 @@ dll_crt0_1 () threadname_init (); debug_init (); + cygheap_init (); /* Initialize cygheap muto */ regthread ("main", GetCurrentThreadId ()); diff --git a/winsup/cygwin/debug.cc b/winsup/cygwin/debug.cc index f89b21a5fe..f96660c51a 100644 --- a/winsup/cygwin/debug.cc +++ b/winsup/cygwin/debug.cc @@ -87,7 +87,7 @@ thread_stub (VOID *arg) exception_list except_entry; /* Give up our slot in the start_buf array */ - InterlockedExchange (&((thread_start *) arg)->notavail, 0); + (void) InterlockedExchange (&((thread_start *) arg)->notavail, 0); /* Initialize this thread's ability to respond to things like SIGSEGV or SIGFPE. */ diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc index 856bdeff50..ad60e07a16 100644 --- a/winsup/cygwin/path.cc +++ b/winsup/cygwin/path.cc @@ -2436,36 +2436,7 @@ hashit: char * getcwd (char *buf, size_t ulen) { - char *res; - char *usebuf, uselen; - - if (buf != NULL) - { - usebuf = buf; - uselen = TRUE; - } - else - { - if (ulen >= 0) - uselen = TRUE; - else - { - uselen = FALSE; - ulen = MAX_PATH + 1; - } - - usebuf = (char *) malloc (ulen); - usebuf [ulen - 1] = '\0'; - } - - res = cygcwd.get (usebuf, 1, 1, ulen); - - if (res && !uselen) - usebuf = (char *) realloc (usebuf, strlen (usebuf) + 1); - else if (!res && buf == NULL) - free (usebuf); - - return res; + return cygcwd.get (buf, 1, 1, ulen); } /* getwd: standards? */ @@ -2481,6 +2452,7 @@ extern "C" int chdir (const char *dir) { + MALLOC_CHECK; syscall_printf ("dir %s", dir); path_conv path (dir, PC_FULL | PC_SYM_FOLLOW); @@ -2529,6 +2501,7 @@ chdir (const char *dir) it was worth locking just for strace. */ syscall_printf ("%d = chdir() cygcwd.posix '%s' native '%s'", res, cygcwd.posix, native_dir); + MALLOC_CHECK; return res; } @@ -2962,8 +2935,7 @@ cwdstuff::set (const char *win32_cwd, const char *posix_cwd) char * cwdstuff::get (char *buf, int need_posix, int with_chroot, unsigned ulen) { - size_t len = ulen; - + MALLOC_CHECK; if (!get_initial ()) /* Get initial cwd and set cwd lock */ return NULL; @@ -2982,6 +2954,8 @@ cwdstuff::get (char *buf, int need_posix, int with_chroot, unsigned ulen) } else { + if (need_posix && !buf) + buf = (char *) malloc (strlen (tocopy) + 1); strcpy (buf, tocopy); if (!buf[0]) /* Should only happen when chroot */ strcpy (buf, "/"); @@ -2989,7 +2963,8 @@ cwdstuff::get (char *buf, int need_posix, int with_chroot, unsigned ulen) lock->release (); syscall_printf ("(%s) = cwdstuff::get (%p, %d, %d, %d)", - buf, buf, len, need_posix, with_chroot); + buf, buf, ulen, need_posix, with_chroot); + MALLOC_CHECK; return buf; } @@ -3002,5 +2977,6 @@ cwdstuff::copy (char * &posix_cwd, char * &win32_cwd, DWORD hash_cwd) posix_cwd = cstrdup (posix); win32_cwd = cstrdup (win32); hash_cwd = hash; + MALLOC_CHECK; lock->release (); } diff --git a/winsup/cygwin/sync.cc b/winsup/cygwin/sync.cc index dededa5ed1..86bcc0440e 100644 --- a/winsup/cygwin/sync.cc +++ b/winsup/cygwin/sync.cc @@ -116,7 +116,7 @@ muto::release () if (!--visits) { tid = 0; /* We were the last unlocker. */ - InterlockedExchange (&sync, 0); /* Reset trigger. */ + (void) InterlockedExchange (&sync, 0); /* Reset trigger. */ /* This thread had incremented waiters but had never decremented it. Decrement it now. If it is >= 0 then there are possibly other threads waiting for the lock, so trigger bruteforce. */ -- 2.11.0