47063f00e4
operators to simplify testing for directory and attributes, throughout. * path.h (path_conv::exists): New method. (path_conv::has_attribute): Ditto. (path_conv::isdir): Ditto. (path_conv::DWORD &): New operator. (path_conv::int &): Ditto. * dir.cc (rmdir): Eliminate a goto. * dtable.cc (dtable::build_fhandler): Accept opt and suffix info for path_conv.check. Return fh == NULL on path_conv error. Pass unit to set_name as appropriate. (dtable::reset_unix_path_name): New method. * dtable.h (dtable): Declare new method. Reflect arg changes to build_fhandler. * fhandler.cc (fhandler_disk_dummy_name): Eliminate. (fhandler_base::set_name): Expect paths to be NULL. Build unix_path_name from win32_path_name when it is a device. (fhandler_base::reset_unix_path_name): New method. (fhandler_base::raw_read): Report EISDIR when ERROR_INVALID_FUNCTION or ERROR_INVALID_PARAMETER and reading a directory. (fhandler_disk_file::fstat): Don't call stat_dev since we should now never be calling fhandler_disk_file methods with devices. (fhandler_base::fhandler_base): Clear {unix,win32}_path_name. (fhandler_base::~fhandler_base): Always free {unix,win32}_path_name. (fhandler_disk_file::fhandler_disk_file): Remove set_no_free_names kludge. (fhandler_disk_file::open): Ditto. * fhandler.h (fhandler_base::no_free_names): Eliminate. (fhandler_base::set_no_free_names): Ditto. * fhandler_tty.cc (fhandler_tty_slave::fhandler_tty_slave): Don't set unix_path_name here. * path.cc (fchdir): Lock fd table throughout. Use new dtable::reset_unix_path_name method to reset path. * syscalls.cc (stat_worker): Reorganize to always call fstat method. Pass path_conv method to fhandler_*::open. (chroot): Elminate a goto.
245 lines
4.9 KiB
C++
245 lines
4.9 KiB
C++
/* malloc.cc
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001 Red Hat, Inc.
|
|
|
|
Originally written by Steve Chamberlain of Cygnus Support
|
|
sac@cygnus.com
|
|
|
|
This file is part of Cygwin.
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
details. */
|
|
|
|
#include "winsup.h"
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include "security.h"
|
|
#include "fhandler.h"
|
|
#include "path.h"
|
|
#include "dtable.h"
|
|
#include "cygheap.h"
|
|
#include "heap.h"
|
|
#include "sync.h"
|
|
#include "perprocess.h"
|
|
|
|
/* we provide these stubs to call into a user's
|
|
provided malloc if there is one - otherwise
|
|
functions we provide - like strdup will cause
|
|
problems if malloced on our heap and free'd on theirs.
|
|
*/
|
|
|
|
static int export_malloc_called;
|
|
static int use_internal_malloc = 1;
|
|
|
|
#undef in
|
|
#undef out
|
|
#define in(x)
|
|
#define out(x)
|
|
|
|
#ifdef MALLOC_DEBUG
|
|
extern "C" void * _sbrk (size_t incr_arg);
|
|
|
|
#if 0
|
|
extern "C" void *
|
|
_sbrk_r (struct _reent *, size_t incr_arg)
|
|
{
|
|
return _sbrk (incr_arg);
|
|
}
|
|
#endif
|
|
|
|
extern "C" void *
|
|
_malloc_r (struct _reent *, size_t size)
|
|
{
|
|
export_malloc_called = 1;
|
|
return malloc (size);
|
|
}
|
|
#undef malloc
|
|
|
|
extern "C" void *
|
|
_calloc_r (struct _reent *, size_t nmemb, size_t size)
|
|
{
|
|
export_malloc_called = 1;
|
|
return calloc (nmemb, size);
|
|
}
|
|
#undef calloc
|
|
|
|
extern "C" void
|
|
_free_r (struct _reent *, void *p)
|
|
{
|
|
export_malloc_called = 1;
|
|
assert (!incygheap (p));
|
|
assert (inheap (p));
|
|
free (p);
|
|
}
|
|
#undef free
|
|
|
|
extern "C" void *
|
|
_realloc_r (struct _reent *, void *p, size_t size)
|
|
{
|
|
export_malloc_called = 1;
|
|
assert (!incygheap (p));
|
|
assert (inheap (p));
|
|
return realloc (p, size);
|
|
}
|
|
#undef realloc
|
|
|
|
extern "C" char *
|
|
strdup_dbg (const char *s, const char *file, int line)
|
|
{
|
|
char *p;
|
|
export_malloc_called = 1;
|
|
if ((p = (char *) malloc_dbg (strlen (s) + 1, file, line)) != NULL)
|
|
strcpy (p, s);
|
|
return p;
|
|
}
|
|
|
|
#undef strdup
|
|
extern "C" char *
|
|
strdup (const char *s)
|
|
{
|
|
return strdup_dbg (s, __FILE__, __LINE__);
|
|
}
|
|
#else
|
|
/* Call though the application pointer,
|
|
which either points to export_malloc, or the application's
|
|
own version. */
|
|
|
|
void *
|
|
malloc (size_t size)
|
|
{
|
|
void *res;
|
|
res = user_data->malloc (size);
|
|
return res;
|
|
}
|
|
|
|
void
|
|
free (void *p)
|
|
{
|
|
user_data->free (p);
|
|
}
|
|
|
|
void *
|
|
realloc (void *p, size_t size)
|
|
{
|
|
void *res;
|
|
res = user_data->realloc (p, size);
|
|
return res;
|
|
}
|
|
|
|
void *
|
|
calloc (size_t nmemb, size_t size)
|
|
{
|
|
void *res;
|
|
res = user_data->calloc (nmemb, size);
|
|
return res;
|
|
}
|
|
|
|
extern "C" char *
|
|
strdup (const char *s)
|
|
{
|
|
char *p;
|
|
size_t len = strlen (s) + 1;
|
|
if ((p = (char *) malloc (len)) != NULL)
|
|
memcpy (p, s, len);
|
|
return p;
|
|
}
|
|
|
|
extern "C" char *
|
|
_strdup_r (struct _reent *, const char *s)
|
|
{
|
|
return strdup (s);
|
|
}
|
|
#endif
|
|
|
|
/* These routines are used by the application if it
|
|
doesn't provide its own malloc. */
|
|
|
|
extern "C" void
|
|
export_free (void *p)
|
|
{
|
|
malloc_printf ("(%p), called by %x", p, ((int *)&p)[-1]);
|
|
if (use_internal_malloc)
|
|
_free_r (_impure_ptr, p);
|
|
else
|
|
user_data->free (p);
|
|
}
|
|
|
|
extern "C" void *
|
|
export_malloc (int size)
|
|
{
|
|
void *res;
|
|
export_malloc_called = 1;
|
|
if (use_internal_malloc)
|
|
res = _malloc_r (_impure_ptr, size);
|
|
else
|
|
res = user_data->malloc (size);
|
|
malloc_printf ("(%d) = %x, called by %x", size, res, ((int *)&size)[-1]);
|
|
return res;
|
|
}
|
|
|
|
extern "C" void *
|
|
export_realloc (void *p, int size)
|
|
{
|
|
void *res;
|
|
if (use_internal_malloc)
|
|
res = _realloc_r (_impure_ptr, p, size);
|
|
else
|
|
res = user_data->realloc (p, size);
|
|
malloc_printf ("(%x, %d) = %x, called by %x", p, size, res, ((int *)&p)[-1]);
|
|
return res;
|
|
}
|
|
|
|
extern "C" void *
|
|
export_calloc (size_t nmemb, size_t size)
|
|
{
|
|
void *res;
|
|
if (use_internal_malloc)
|
|
res = _calloc_r (_impure_ptr, nmemb, size);
|
|
else
|
|
res = user_data->calloc (nmemb, size);
|
|
malloc_printf ("(%d, %d) = %x, called by %x", nmemb, size, res, ((int *)&nmemb)[-1]);
|
|
return res;
|
|
}
|
|
|
|
/* We use a critical section to lock access to the malloc data
|
|
structures. This permits malloc to be called from different
|
|
threads. Note that it does not make malloc reentrant, and it does
|
|
not permit a signal handler to call malloc. The malloc code in
|
|
newlib will call __malloc_lock and __malloc_unlock at appropriate
|
|
times. */
|
|
|
|
static NO_COPY muto *mprotect = NULL;
|
|
|
|
void
|
|
malloc_init ()
|
|
{
|
|
mprotect = new_muto (FALSE, "mprotect");
|
|
/* Check if mallock is provided by application. If so, redirect all
|
|
calls to export_malloc/free/realloc to application provided. This may
|
|
happen if some other dll calls cygwin's malloc, but main code provides
|
|
its own malloc */
|
|
if (!user_data->forkee)
|
|
{
|
|
#ifdef MALLOC_DEBUG
|
|
_free_r (NULL, _malloc_r (NULL, 16));
|
|
#else
|
|
free (malloc (16));
|
|
#endif
|
|
if (!export_malloc_called)
|
|
use_internal_malloc = 0;
|
|
}
|
|
}
|
|
|
|
extern "C" void
|
|
__malloc_lock (struct _reent *)
|
|
{
|
|
mprotect->acquire ();
|
|
}
|
|
|
|
extern "C" void
|
|
__malloc_unlock (struct _reent *)
|
|
{
|
|
mprotect->release ();
|
|
}
|