2002-08-16 21:50:22 +02:00
|
|
|
/* malloc_wrapper.cc
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-06-05 06:01:43 +02:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-09-11 22:01:02 +02:00
|
|
|
Originally written by Steve Chamberlain of Cygnus Support
|
2000-02-17 20:38:33 +01:00
|
|
|
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>
|
2000-09-03 06:16:35 +02:00
|
|
|
#include <assert.h>
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "fhandler.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "dtable.h"
|
2001-10-16 01:39:33 +02:00
|
|
|
#include "cygerrno.h"
|
2000-09-03 06:16:35 +02:00
|
|
|
#include "cygheap.h"
|
|
|
|
#include "heap.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sync.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "perprocess.h"
|
2002-08-16 21:50:22 +02:00
|
|
|
#include "cygmalloc.h"
|
2003-02-23 00:02:15 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
extern "C" struct mallinfo dlmallinfo ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2001-09-06 06:41:59 +02:00
|
|
|
static int export_malloc_called;
|
2000-02-17 20:38:33 +01:00
|
|
|
static int use_internal_malloc = 1;
|
|
|
|
|
|
|
|
#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;
|
2000-09-03 06:16:35 +02:00
|
|
|
assert (!incygheap (p));
|
|
|
|
assert (inheap (p));
|
2000-02-17 20:38:33 +01:00
|
|
|
free (p);
|
|
|
|
}
|
|
|
|
#undef free
|
|
|
|
|
|
|
|
extern "C" void *
|
|
|
|
_realloc_r (struct _reent *, void *p, size_t size)
|
|
|
|
{
|
|
|
|
export_malloc_called = 1;
|
2000-09-03 06:16:35 +02:00
|
|
|
assert (!incygheap (p));
|
|
|
|
assert (inheap (p));
|
2000-02-17 20:38:33 +01:00
|
|
|
return realloc (p, size);
|
|
|
|
}
|
|
|
|
#undef realloc
|
|
|
|
|
|
|
|
extern "C" char *
|
|
|
|
strdup_dbg (const char *s, const char *file, int line)
|
|
|
|
{
|
2000-02-21 04:13:24 +01:00
|
|
|
char *p;
|
2000-02-17 20:38:33 +01:00
|
|
|
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
|
2002-08-16 21:50:22 +02:00
|
|
|
#endif
|
|
|
|
/* These routines are used by the application if it
|
|
|
|
doesn't provide its own malloc. */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" void
|
|
|
|
free (void *p)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-08-18 05:28:52 +02:00
|
|
|
malloc_printf ("(%p), called by %p", p, __builtin_return_address (0));
|
2002-08-16 21:50:22 +02:00
|
|
|
if (!use_internal_malloc)
|
|
|
|
user_data->free (p);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
dlfree (p);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" void *
|
|
|
|
malloc (size_t size)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
void *res;
|
|
|
|
export_malloc_called = 1;
|
|
|
|
if (!use_internal_malloc)
|
|
|
|
res = user_data->malloc (size);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlmalloc (size);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
2002-08-18 05:28:52 +02:00
|
|
|
malloc_printf ("(%d) = %x, called by %p", size, res, __builtin_return_address (0));
|
2002-08-16 21:50:22 +02:00
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" void *
|
2000-02-17 20:38:33 +01:00
|
|
|
realloc (void *p, size_t size)
|
|
|
|
{
|
|
|
|
void *res;
|
2002-08-16 21:50:22 +02:00
|
|
|
if (!use_internal_malloc)
|
|
|
|
res = user_data->realloc (p, size);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlrealloc (p, size);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
2002-08-18 05:28:52 +02:00
|
|
|
malloc_printf ("(%x, %d) = %x, called by %x", p, size, res, __builtin_return_address (0));
|
2000-02-17 20:38:33 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" void *
|
2000-02-17 20:38:33 +01:00
|
|
|
calloc (size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
void *res;
|
2002-08-16 21:50:22 +02:00
|
|
|
if (!use_internal_malloc)
|
|
|
|
res = user_data->calloc (nmemb, size);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlcalloc (nmemb, size);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
2002-08-18 05:28:52 +02:00
|
|
|
malloc_printf ("(%d, %d) = %x, called by %x", nmemb, size, res, __builtin_return_address (0));
|
2000-02-17 20:38:33 +01:00
|
|
|
return res;
|
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" void *
|
|
|
|
memalign (size_t alignment, size_t bytes)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
void *res;
|
|
|
|
if (!use_internal_malloc)
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlmemalign (alignment, bytes);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" void *
|
|
|
|
valloc (size_t bytes)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
void *res;
|
|
|
|
if (!use_internal_malloc)
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlvalloc (bytes);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
return res;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" size_t
|
|
|
|
malloc_usable_size (void *p)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
size_t res;
|
|
|
|
if (!use_internal_malloc)
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
res = 0;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2002-08-16 21:50:22 +02:00
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlmalloc_usable_size (p);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" int
|
|
|
|
malloc_trim (size_t pad)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
size_t res;
|
|
|
|
if (!use_internal_malloc)
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
res = 0;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2002-08-16 21:50:22 +02:00
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlmalloc_trim (pad);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" int
|
|
|
|
mallopt (int p, int v)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
int res;
|
|
|
|
if (!use_internal_malloc)
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
res = 0;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2002-08-16 21:50:22 +02:00
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
res = dlmallopt (p, v);
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
extern "C" void
|
|
|
|
malloc_stats ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
if (!use_internal_malloc)
|
|
|
|
set_errno (ENOSYS);
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2002-08-16 21:50:22 +02:00
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
dlmalloc_stats ();
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-02-23 00:02:15 +01:00
|
|
|
extern "C" struct mallinfo
|
|
|
|
mallinfo ()
|
|
|
|
{
|
|
|
|
struct mallinfo m;
|
|
|
|
if (!use_internal_malloc)
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__malloc_lock ();
|
|
|
|
m = dlmallinfo ();
|
|
|
|
__malloc_unlock ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
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;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
2002-08-16 21:50:22 +02:00
|
|
|
NO_COPY muto *mallock = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
malloc_init ()
|
|
|
|
{
|
2002-08-16 21:50:22 +02:00
|
|
|
new_muto (mallock);
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Check if mallock is provided by application. If so, redirect all
|
2002-08-16 21:50:22 +02:00
|
|
|
calls to malloc/free/realloc to application provided. This may
|
2000-02-17 20:38:33 +01:00
|
|
|
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
|
2002-08-18 05:28:52 +02:00
|
|
|
user_data->free (user_data->malloc (16));
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
|
|
|
if (!export_malloc_called)
|
|
|
|
use_internal_malloc = 0;
|
|
|
|
}
|
|
|
|
}
|