* Remove ptmalloc3 once and for all.

This commit is contained in:
Corinna Vinschen 2013-04-23 09:06:12 +00:00
parent 31e8f0f8c1
commit 440ee329b8
9 changed files with 869 additions and 3296 deletions

View File

@ -1,3 +1,7 @@
2013-04-23 Corinna Vinschen <corinna@vinschen.de>
* Remove ptmalloc3 once and for all.
2013-04-21 Yaakov Selkowitz <yselkowitz@users.sourceforge.net>
* include/stdint.h (INTPTR_MAX): Fix missing parenthesis.

View File

@ -150,7 +150,7 @@ EXTRA_DLL_OFILES:=${addsuffix .o,${basename ${notdir ${wildcard $(CONFIG_DIR)/*.
EXTRA_OFILES:=
MALLOC_OFILES:=malloc.o ptmalloc3.o
MALLOC_OFILES:=malloc.o
DLL_IMPORTS:=${shell $(CC) -print-file-name=w32api/libkernel32.a} ${shell $(CC) -print-file-name=w32api/libntdll.a}

View File

@ -13,16 +13,16 @@ extern "C" {
#endif
#include "regparm.h"
void __reg1 ptfree (void *p);
void __reg1 *ptmalloc (size_t size);
void __reg2 *ptrealloc (void *p, size_t size);
void __reg2 *ptcalloc (size_t nmemb, size_t size);
void __reg2 *ptmemalign (size_t alignment, size_t bytes);
void __reg1 *ptvalloc (size_t bytes);
size_t __reg1 ptmalloc_usable_size (void *p);
int __reg1 ptmalloc_trim (size_t);
int __reg2 ptmallopt (int p, int v);
void ptmalloc_stats ();
void __reg1 dlfree (void *p);
void __reg1 *dlmalloc (size_t size);
void __reg2 *dlrealloc (void *p, size_t size);
void __reg2 *dlcalloc (size_t nmemb, size_t size);
void __reg2 *dlmemalign (size_t alignment, size_t bytes);
void __reg1 *dlvalloc (size_t bytes);
size_t __reg1 dlmalloc_usable_size (void *p);
int __reg1 dlmalloc_trim (size_t);
int __reg2 dlmallopt (int p, int v);
void dlmalloc_stats ();
#ifdef __x86_64__
#define MALLOC_ALIGNMENT ((size_t)16U)
@ -34,9 +34,10 @@ void *mmap64 (void *, size_t, int, int, int, off_t);
#define mmap mmap64
# define MALLOC_FAILURE_ACTION __set_ENOMEM ()
# define USE_DL_PREFIX 1
# define MSPACES 1
# define ONLY_MSPACES 1
# define HAVE_MORECORE 1
#else
# define __malloc_lock() mallock.acquire ()
# define __malloc_unlock() mallock.release ()
extern muto mallock;
#endif
#ifdef __cplusplus
}

View File

@ -342,6 +342,7 @@ frok::parent (volatile char * volatile stack_here)
syscall_printf ("CreateProcessW (%W, %W, 0, 0, 1, %y, 0, 0, %p, %p)",
myself->progname, myself->progname, c_flags, &si, &pi);
bool locked = __malloc_lock ();
time_t start_time = time (NULL);
/* Remove impersonation */
@ -472,6 +473,8 @@ frok::parent (volatile char * volatile stack_here)
impure, impure_beg, impure_end,
NULL);
__malloc_unlock ();
locked = false;
MALLOC_CHECK;
if (!rc)
{
@ -539,6 +542,8 @@ frok::parent (volatile char * volatile stack_here)
cleanup:
if (fix_impersonation)
cygheap->user.reimpersonate ();
if (locked)
__malloc_unlock ();
/* Remember to de-allocate the fd table. */
if (hchild && !child.hProcess)

View File

@ -1,858 +0,0 @@
/*
$Id$
Private header file for ptmalloc3, created by Wolfram Gloger
and released to the public domain, as explained at
http://creativecommons.org/licenses/publicdomain.
*/
/* ======================== START malloc-machine.h ========================== */
/* Basic platform-independent macro definitions for mutexes,
thread-specific data and parameters for malloc.
Posix threads (pthreads) version.
Copyright (C) 2004 Wolfram Gloger <wg@malloc.de>.
Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that (i) the above copyright notices and this permission
notice appear in all copies of the software and related documentation,
and (ii) the name of Wolfram Gloger may not be used in any advertising
or publicity relating to the software.
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL WOLFRAM GLOGER BE LIABLE FOR ANY SPECIAL,
INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY
OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _PTHREAD_MALLOC_MACHINE_H
#define _PTHREAD_MALLOC_MACHINE_H
#include <pthread.h>
#undef thread_atfork_static
/* Use fast inline spinlocks with gcc. */
#if (defined __i386__ || defined __x86_64__) && defined __GNUC__ && \
!defined USE_NO_SPINLOCKS
#include <time.h>
#include <sched.h>
typedef struct {
volatile unsigned int lock;
int pad0_;
} mutex_t;
#define MUTEX_INITIALIZER { 0 }
#define mutex_init(m) ((m)->lock = 0)
static inline int mutex_lock(mutex_t *m) {
int cnt = 0, r;
struct timespec tm;
for(;;) {
__asm__ __volatile__
("xchgl %0, %1"
: "=r"(r), "=m"(m->lock)
: "0"(1), "m"(m->lock)
: "memory");
if(!r)
return 0;
if(cnt < 50) {
sched_yield();
cnt++;
} else {
tm.tv_sec = 0;
tm.tv_nsec = 2000001;
nanosleep(&tm, NULL);
cnt = 0;
}
}
}
static inline int mutex_trylock(mutex_t *m) {
int r;
__asm__ __volatile__
("xchgl %0, %1"
: "=r"(r), "=m"(m->lock)
: "0"(1), "m"(m->lock)
: "memory");
return r;
}
static inline int mutex_unlock(mutex_t *m) {
__asm__ __volatile__ ("movl %1, %0" : "=m" (m->lock) : "g"(0) : "memory");
return 0;
}
#else
/* Normal pthread mutex. */
typedef pthread_mutex_t mutex_t;
#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#define mutex_init(m) pthread_mutex_init(m, NULL)
#define mutex_lock(m) pthread_mutex_lock(m)
#define mutex_trylock(m) pthread_mutex_trylock(m)
#define mutex_unlock(m) pthread_mutex_unlock(m)
#endif /* (__i386__ || __x86_64__) && __GNUC__ && !USE_NO_SPINLOCKS */
/* thread specific data */
#if defined(__sgi) || defined(USE_TSD_DATA_HACK)
/* Hack for thread-specific data, e.g. on Irix 6.x. We can't use
pthread_setspecific because that function calls malloc() itself.
The hack only works when pthread_t can be converted to an integral
type. */
typedef void *tsd_key_t[256];
#define tsd_key_create(key, destr) do { \
int i; \
for(i=0; i<256; i++) (*key)[i] = 0; \
} while(0)
#define tsd_setspecific(key, data) \
(key[(unsigned)pthread_self() % 256] = (data))
#define tsd_getspecific(key, vptr) \
(vptr = key[(unsigned)pthread_self() % 256])
#else
typedef pthread_key_t tsd_key_t;
#define tsd_key_create(key, destr) pthread_key_create(key, destr)
#define tsd_setspecific(key, data) pthread_setspecific(key, data)
#define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
#endif
/* at fork */
#define thread_atfork(prepare, parent, child) \
pthread_atfork(prepare, parent, child)
#ifndef atomic_full_barrier
# define atomic_full_barrier() __asm ("" ::: "memory")
#endif
#ifndef atomic_read_barrier
# define atomic_read_barrier() atomic_full_barrier ()
#endif
#ifndef atomic_write_barrier
# define atomic_write_barrier() atomic_full_barrier ()
#endif
#ifndef DEFAULT_TOP_PAD
# define DEFAULT_TOP_PAD 131072
#endif
#endif /* !defined(_MALLOC_MACHINE_H) */
/* ======================== END malloc-machine.h ========================== */
/* ======================== START malloc-2.8.3.h ========================== */
/*
Default header file for malloc-2.8.x, written by Doug Lea
and released to the public domain, as explained at
http://creativecommons.org/licenses/publicdomain.
last update: Mon Aug 15 08:55:52 2005 Doug Lea (dl at gee)
This header is for ANSI C/C++ only. You can set any of
the following #defines before including:
* If USE_DL_PREFIX is defined, it is assumed that malloc.c
was also compiled with this option, so all routines
have names starting with "dl".
* If HAVE_USR_INCLUDE_MALLOC_H is defined, it is assumed that this
file will be #included AFTER <malloc.h>. This is needed only if
your system defines a struct mallinfo that is incompatible with the
standard one declared here. Otherwise, you can include this file
INSTEAD of your system system <malloc.h>. At least on ANSI, all
declarations should be compatible with system versions
* If MSPACES is defined, declarations for mspace versions are included.
*/
#ifndef MALLOC_280_H
#define MALLOC_280_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h> /* for size_t */
#if !NO_MALLINFO
/*
mallinfo()
Returns (by copy) a struct containing various summary statistics:
arena: current total non-mmapped bytes allocated from system
ordblks: the number of free chunks
smblks: always zero.
hblks: current number of mmapped regions
hblkhd: total bytes held in mmapped regions
usmblks: the maximum total allocated space. This will be greater
than current total if trimming has occurred.
fsmblks: always zero
uordblks: current total allocated space (normal or mmapped)
fordblks: total free space
keepcost: the maximum number of bytes that could ideally be released
back to system via malloc_trim. ("ideally" means that
it ignores page restrictions etc.)
Because these fields are ints, but internal bookkeeping may
be kept as longs, the reported values may wrap around zero and
thus be inaccurate.
*/
#ifndef HAVE_USR_INCLUDE_MALLOC_H
#ifndef _MALLOC_H
#ifndef MALLINFO_FIELD_TYPE
#define MALLINFO_FIELD_TYPE size_t
#endif /* MALLINFO_FIELD_TYPE */
struct mallinfo {
MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */
MALLINFO_FIELD_TYPE smblks; /* always 0 */
MALLINFO_FIELD_TYPE hblks; /* always 0 */
MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */
MALLINFO_FIELD_TYPE fsmblks; /* always 0 */
MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
MALLINFO_FIELD_TYPE fordblks; /* total free space */
MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
};
#define M_TRIM_THRESHOLD (-1)
#define M_GRANULARITY (-2)
#define M_MMAP_THRESHOLD (-3)
#endif /* _MALLOC_H */
#endif /* HAVE_USR_INCLUDE_MALLOC_H */
#endif /* NO_MALLINFO */
#if !ONLY_MSPACES
#ifndef USE_DL_PREFIX
#define dlcalloc calloc
#define dlfree free
#define dlmalloc malloc
#define dlmemalign memalign
#define dlrealloc realloc
#define dlvalloc valloc
#define dlpvalloc pvalloc
#define dlmallinfo mallinfo
#define dlmallopt mallopt
#define dlmalloc_trim malloc_trim
#define dlmalloc_stats malloc_stats
#define dlmalloc_usable_size malloc_usable_size
#define dlmalloc_footprint malloc_footprint
#define dlindependent_calloc independent_calloc
#define dlindependent_comalloc independent_comalloc
#endif /* USE_DL_PREFIX */
/*
malloc(size_t n)
Returns a pointer to a newly allocated chunk of at least n bytes, or
null if no space is available, in which case errno is set to ENOMEM
on ANSI C systems.
If n is zero, malloc returns a minimum-sized chunk. (The minimum
size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
systems.) Note that size_t is an unsigned type, so calls with
arguments that would be negative if signed are interpreted as
requests for huge amounts of space, which will often fail. The
maximum supported value of n differs across systems, but is in all
cases less than the maximum representable value of a size_t.
*/
void* dlmalloc(size_t);
/*
free(void* p)
Releases the chunk of memory pointed to by p, that had been previously
allocated using malloc or a related routine such as realloc.
It has no effect if p is null. If p was not malloced or already
freed, free(p) will by default cuase the current program to abort.
*/
void dlfree(void*);
/*
calloc(size_t n_elements, size_t element_size);
Returns a pointer to n_elements * element_size bytes, with all locations
set to zero.
*/
void* dlcalloc(size_t, size_t);
/*
realloc(void* p, size_t n)
Returns a pointer to a chunk of size n that contains the same data
as does chunk p up to the minimum of (n, p's size) bytes, or null
if no space is available.
The returned pointer may or may not be the same as p. The algorithm
prefers extending p in most cases when possible, otherwise it
employs the equivalent of a malloc-copy-free sequence.
If p is null, realloc is equivalent to malloc.
If space is not available, realloc returns null, errno is set (if on
ANSI) and p is NOT freed.
if n is for fewer bytes than already held by p, the newly unused
space is lopped off and freed if possible. realloc with a size
argument of zero (re)allocates a minimum-sized chunk.
The old unix realloc convention of allowing the last-free'd chunk
to be used as an argument to realloc is not supported.
*/
void* dlrealloc(void*, size_t);
/*
memalign(size_t alignment, size_t n);
Returns a pointer to a newly allocated chunk of n bytes, aligned
in accord with the alignment argument.
The alignment argument should be a power of two. If the argument is
not a power of two, the nearest greater power is used.
8-byte alignment is guaranteed by normal malloc calls, so don't
bother calling memalign with an argument of 8 or less.
Overreliance on memalign is a sure way to fragment space.
*/
void* dlmemalign(size_t, size_t);
/*
valloc(size_t n);
Equivalent to memalign(pagesize, n), where pagesize is the page
size of the system. If the pagesize is unknown, 4096 is used.
*/
void* dlvalloc(size_t);
/*
mallopt(int parameter_number, int parameter_value)
Sets tunable parameters The format is to provide a
(parameter-number, parameter-value) pair. mallopt then sets the
corresponding parameter to the argument value if it can (i.e., so
long as the value is meaningful), and returns 1 if successful else
0. SVID/XPG/ANSI defines four standard param numbers for mallopt,
normally defined in malloc.h. None of these are use in this malloc,
so setting them has no effect. But this malloc also supports other
options in mallopt:
Symbol param # default allowed param values
M_TRIM_THRESHOLD -1 2*1024*1024 any (-1U disables trimming)
M_GRANULARITY -2 page size any power of 2 >= page size
M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support)
*/
int dlmallopt(int, int);
/*
malloc_footprint();
Returns the number of bytes obtained from the system. The total
number of bytes allocated by malloc, realloc etc., is less than this
value. Unlike mallinfo, this function returns only a precomputed
result, so can be called frequently to monitor memory consumption.
Even if locks are otherwise defined, this function does not use them,
so results might not be up to date.
*/
size_t dlmalloc_footprint(void);
#if !NO_MALLINFO
struct mallinfo dlmallinfo(void);
#endif /* NO_MALLINFO */
/*
independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);
independent_calloc is similar to calloc, but instead of returning a
single cleared space, it returns an array of pointers to n_elements
independent elements that can hold contents of size elem_size, each
of which starts out cleared, and can be independently freed,
realloc'ed etc. The elements are guaranteed to be adjacently
allocated (this is not guaranteed to occur with multiple callocs or
mallocs), which may also improve cache locality in some
applications.
The "chunks" argument is optional (i.e., may be null, which is
probably the most typical usage). If it is null, the returned array
is itself dynamically allocated and should also be freed when it is
no longer needed. Otherwise, the chunks array must be of at least
n_elements in length. It is filled in with the pointers to the
chunks.
In either case, independent_calloc returns this pointer array, or
null if the allocation failed. If n_elements is zero and "chunks"
is null, it returns a chunk representing an array with zero elements
(which should be freed if not wanted).
Each element must be individually freed when it is no longer
needed. If you'd like to instead be able to free all at once, you
should instead use regular calloc and assign pointers into this
space to represent elements. (In this case though, you cannot
independently free elements.)
independent_calloc simplifies and speeds up implementations of many
kinds of pools. It may also be useful when constructing large data
structures that initially have a fixed number of fixed-sized nodes,
but the number is not known at compile time, and some of the nodes
may later need to be freed. For example:
struct Node { int item; struct Node* next; };
struct Node* build_list() {
struct Node** pool;
int n = read_number_of_nodes_needed();
if (n <= 0) return 0;
pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
if (pool == 0) die();
// organize into a linked list...
struct Node* first = pool[0];
for (i = 0; i < n-1; ++i)
pool[i]->next = pool[i+1];
free(pool); // Can now free the array (or not, if it is needed later)
return first;
}
*/
void** dlindependent_calloc(size_t, size_t, void**);
/*
independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
independent_comalloc allocates, all at once, a set of n_elements
chunks with sizes indicated in the "sizes" array. It returns
an array of pointers to these elements, each of which can be
independently freed, realloc'ed etc. The elements are guaranteed to
be adjacently allocated (this is not guaranteed to occur with
multiple callocs or mallocs), which may also improve cache locality
in some applications.
The "chunks" argument is optional (i.e., may be null). If it is null
the returned array is itself dynamically allocated and should also
be freed when it is no longer needed. Otherwise, the chunks array
must be of at least n_elements in length. It is filled in with the
pointers to the chunks.
In either case, independent_comalloc returns this pointer array, or
null if the allocation failed. If n_elements is zero and chunks is
null, it returns a chunk representing an array with zero elements
(which should be freed if not wanted).
Each element must be individually freed when it is no longer
needed. If you'd like to instead be able to free all at once, you
should instead use a single regular malloc, and assign pointers at
particular offsets in the aggregate space. (In this case though, you
cannot independently free elements.)
independent_comallac differs from independent_calloc in that each
element may have a different size, and also that it does not
automatically clear elements.
independent_comalloc can be used to speed up allocation in cases
where several structs or objects must always be allocated at the
same time. For example:
struct Head { ... }
struct Foot { ... }
void send_message(char* msg) {
int msglen = strlen(msg);
size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
void* chunks[3];
if (independent_comalloc(3, sizes, chunks) == 0)
die();
struct Head* head = (struct Head*)(chunks[0]);
char* body = (char*)(chunks[1]);
struct Foot* foot = (struct Foot*)(chunks[2]);
// ...
}
In general though, independent_comalloc is worth using only for
larger values of n_elements. For small values, you probably won't
detect enough difference from series of malloc calls to bother.
Overuse of independent_comalloc can increase overall memory usage,
since it cannot reuse existing noncontiguous small chunks that
might be available for some of the elements.
*/
void** dlindependent_comalloc(size_t, size_t*, void**);
/*
pvalloc(size_t n);
Equivalent to valloc(minimum-page-that-holds(n)), that is,
round up n to nearest pagesize.
*/
void* dlpvalloc(size_t);
/*
malloc_trim(size_t pad);
If possible, gives memory back to the system (via negative arguments
to sbrk) if there is unused memory at the `high' end of the malloc
pool or in unused MMAP segments. You can call this after freeing
large blocks of memory to potentially reduce the system-level memory
requirements of a program. However, it cannot guarantee to reduce
memory. Under some allocation patterns, some large free blocks of
memory will be locked between two used chunks, so they cannot be
given back to the system.
The `pad' argument to malloc_trim represents the amount of free
trailing space to leave untrimmed. If this argument is zero, only
the minimum amount of memory to maintain internal data structures
will be left. Non-zero arguments can be supplied to maintain enough
trailing space to service future expected allocations without having
to re-obtain memory from the system.
Malloc_trim returns 1 if it actually released any memory, else 0.
*/
int dlmalloc_trim(size_t);
/*
malloc_usable_size(void* p);
Returns the number of bytes you can actually use in
an allocated chunk, which may be more than you requested (although
often not) due to alignment and minimum size constraints.
You can use this many bytes without worrying about
overwriting other allocated objects. This is not a particularly great
programming practice. malloc_usable_size can be more useful in
debugging and assertions, for example:
p = malloc(n);
assert(malloc_usable_size(p) >= 256);
*/
size_t dlmalloc_usable_size(void*);
/*
malloc_stats();
Prints on stderr the amount of space obtained from the system (both
via sbrk and mmap), the maximum amount (which may be more than
current if malloc_trim and/or munmap got called), and the current
number of bytes allocated via malloc (or realloc, etc) but not yet
freed. Note that this is the number of bytes allocated, not the
number requested. It will be larger than the number requested
because of alignment and bookkeeping overhead. Because it includes
alignment wastage as being in use, this figure may be greater than
zero even when no user-level chunks are allocated.
The reported current and maximum system memory can be inaccurate if
a program makes other calls to system memory allocation functions
(normally sbrk) outside of malloc.
malloc_stats prints only the most commonly interesting statistics.
More information can be obtained by calling mallinfo.
*/
void dlmalloc_stats(void);
#endif /* !ONLY_MSPACES */
#if MSPACES
/*
mspace is an opaque type representing an independent
region of space that supports mspace_malloc, etc.
*/
typedef void* mspace;
/*
create_mspace creates and returns a new independent space with the
given initial capacity, or, if 0, the default granularity size. It
returns null if there is no system memory available to create the
space. If argument locked is non-zero, the space uses a separate
lock to control access. The capacity of the space will grow
dynamically as needed to service mspace_malloc requests. You can
control the sizes of incremental increases of this space by
compiling with a different DEFAULT_GRANULARITY or dynamically
setting with mallopt(M_GRANULARITY, value).
*/
mspace create_mspace(size_t capacity, int locked);
/*
destroy_mspace destroys the given space, and attempts to return all
of its memory back to the system, returning the total number of
bytes freed. After destruction, the results of access to all memory
used by the space become undefined.
*/
size_t destroy_mspace(mspace msp);
/*
create_mspace_with_base uses the memory supplied as the initial base
of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this
space is used for bookkeeping, so the capacity must be at least this
large. (Otherwise 0 is returned.) When this initial space is
exhausted, additional memory will be obtained from the system.
Destroying this space will deallocate all additionally allocated
space (if possible) but not the initial base.
*/
mspace create_mspace_with_base(void* base, size_t capacity, int locked);
/*
mspace_malloc behaves as malloc, but operates within
the given space.
*/
void* mspace_malloc(mspace msp, size_t bytes);
/*
mspace_free behaves as free, but operates within
the given space.
If compiled with FOOTERS==1, mspace_free is not actually needed.
free may be called instead of mspace_free because freed chunks from
any space are handled by their originating spaces.
*/
void mspace_free(mspace msp, void* mem);
/*
mspace_realloc behaves as realloc, but operates within
the given space.
If compiled with FOOTERS==1, mspace_realloc is not actually
needed. realloc may be called instead of mspace_realloc because
realloced chunks from any space are handled by their originating
spaces.
*/
void* mspace_realloc(mspace msp, void* mem, size_t newsize);
/*
mspace_calloc behaves as calloc, but operates within
the given space.
*/
void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
/*
mspace_memalign behaves as memalign, but operates within
the given space.
*/
void* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
/*
mspace_independent_calloc behaves as independent_calloc, but
operates within the given space.
*/
void** mspace_independent_calloc(mspace msp, size_t n_elements,
size_t elem_size, void* chunks[]);
/*
mspace_independent_comalloc behaves as independent_comalloc, but
operates within the given space.
*/
void** mspace_independent_comalloc(mspace msp, size_t n_elements,
size_t sizes[], void* chunks[]);
/*
mspace_footprint() returns the number of bytes obtained from the
system for this space.
*/
size_t mspace_footprint(mspace msp);
#if !NO_MALLINFO
/*
mspace_mallinfo behaves as mallinfo, but reports properties of
the given space.
*/
struct mallinfo mspace_mallinfo(mspace msp);
#endif /* NO_MALLINFO */
/*
mspace_malloc_stats behaves as malloc_stats, but reports
properties of the given space.
*/
void mspace_malloc_stats(mspace msp);
/*
mspace_trim behaves as malloc_trim, but
operates within the given space.
*/
int mspace_trim(mspace msp, size_t pad);
/*
An alias for malloc_usable_size.
*/
size_t mspace_usable_size(void *mem);
/*
An alias for mallopt.
*/
int mspace_mallopt(int, int);
#endif /* MSPACES */
#ifdef __cplusplus
}; /* end of extern "C" */
#endif
#endif /* MALLOC_280_H */
/* ======================== END malloc-2.8.3.h ========================== */
/* The following file is replicated from malloc.c */
#ifndef MALLOC_PRIVATE_H
#define MALLOC_PRIVATE_H
#ifndef MALLOC_ALIGNMENT
# define MALLOC_ALIGNMENT ((size_t)8U)
#endif
#ifndef USE_LOCKS
# define USE_LOCKS 0
#endif
/* The bit mask value corresponding to MALLOC_ALIGNMENT */
#define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE)
/* the number of bytes to offset an address to align it */
#define align_offset(A)\
((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\
((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK))
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
#define MAP_ANONYMOUS MAP_ANON
#endif /* MAP_ANON */
#ifdef MAP_ANONYMOUS
#define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS)
#define CALL_MMAP(s) mmap(0, (s), PROT_READ|PROT_WRITE, MMAP_FLAGS, -1, 0)
#else /* MAP_ANONYMOUS */
/*
Nearly all versions of mmap support MAP_ANONYMOUS, so the following
is unlikely to be needed, but is supplied just in case.
*/
#include <fcntl.h> /* for O_RDWR */
#define MMAP_FLAGS (MAP_PRIVATE)
static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
#define CALL_MMAP(s) ((dev_zero_fd < 0) ? \
(dev_zero_fd = open("/dev/zero", O_RDWR), \
mmap(0, (s), PROT_READ|PROT_WRITE, MMAP_FLAGS, dev_zero_fd, 0)) : \
mmap(0, (s), PROT_READ|PROT_WRITE, MMAP_FLAGS, dev_zero_fd, 0))
#endif /* MAP_ANONYMOUS */
#define CALL_MUNMAP(a, s) munmap((a), (s))
struct malloc_chunk {
size_t prev_foot; /* Size of previous chunk (if free). */
size_t head; /* Size and inuse bits. */
struct malloc_chunk* fd; /* double links -- used only if free. */
struct malloc_chunk* bk;
};
typedef struct malloc_chunk mchunk;
typedef struct malloc_chunk* mchunkptr;
typedef unsigned int binmap_t;
typedef unsigned int flag_t;
struct malloc_tree_chunk;
typedef struct malloc_tree_chunk* tbinptr;
struct malloc_segment {
char* base; /* base address */
size_t size; /* allocated size */
struct malloc_segment* next; /* ptr to next segment */
flag_t sflags; /* mmap and extern flag */
};
typedef struct malloc_segment msegment;
#define NSMALLBINS (32U)
#define NTREEBINS (32U)
struct malloc_state {
binmap_t smallmap;
binmap_t treemap;
size_t dvsize;
size_t topsize;
char* least_addr;
mchunkptr dv;
mchunkptr top;
size_t trim_check;
size_t release_checks;
size_t magic;
mchunkptr smallbins[(NSMALLBINS+1)*2];
tbinptr treebins[NTREEBINS];
size_t footprint;
size_t max_footprint;
flag_t mflags;
#if USE_LOCKS
MLOCK_T mutex;
#endif /* USE_LOCKS */
msegment seg;
void* extp;
size_t exts;
};
/*
TOP_FOOT_SIZE is padding at the end of a segment, including space
that may be needed to place segment records and fenceposts when new
noncontiguous segments are added.
*/
#define TOP_FOOT_SIZE\
(/*align_offset(chunk2mem(0))*/+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE + 2 * MALLOC_ALIGNMENT)
/* ------------------- Chunks sizes and alignments ----------------------- */
#define MCHUNK_SIZE (sizeof(mchunk))
#define CHUNK_OVERHEAD (SIZE_T_SIZE)
/* MMapped chunks need a second word of overhead ... */
#define MMAP_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES)
/* ... and additional padding for fake next-chunk at foot */
#define MMAP_FOOT_PAD (FOUR_SIZE_T_SIZES)
/* The smallest size we can malloc is an aligned minimal chunk */
#define MIN_CHUNK_SIZE\
((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
/* conversion from malloc headers to user pointers, and back */
#define chunk2mem(p) ((void*)((char*)(p) + TWO_SIZE_T_SIZES))
#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - TWO_SIZE_T_SIZES))
/* chunk associated with aligned address A */
#define align_as_chunk(A) (mchunkptr)((A) + align_offset(chunk2mem(A)))
/* pad request bytes into a usable size */
#define pad_request(req) \
(((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
/* The byte and bit size of a size_t */
#define SIZE_T_SIZE (sizeof(size_t))
#define SIZE_T_BITSIZE (sizeof(size_t) << 3)
/* Some constants coerced to size_t */
/* Annoying but necessary to avoid errors on some platforms */
#define SIZE_T_ZERO ((size_t)0)
#define SIZE_T_ONE ((size_t)1)
#define SIZE_T_TWO ((size_t)2)
#define SIZE_T_FOUR ((size_t)4)
#define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1)
#define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2)
#define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES)
#define HALF_MAX_SIZE_T (MAX_SIZE_T / 2U)
#define IS_MMAPPED_BIT (SIZE_T_ONE)
#define PINUSE_BIT (SIZE_T_ONE)
#define CINUSE_BIT (SIZE_T_TWO)
#define FLAG_BITS (PINUSE_BIT|CINUSE_BIT|SIZE_T_FOUR)
/* head field is or'ed with NON_MAIN_ARENA if the chunk was obtained
from a non-main arena. This is only set immediately before handing
the chunk to the user, if necessary. */
#define NON_MAIN_ARENA (SIZE_T_FOUR)
#define cinuse(p) ((p)->head & CINUSE_BIT)
#define pinuse(p) ((p)->head & PINUSE_BIT)
#define chunksize(p) ((p)->head & ~(FLAG_BITS))
#define is_mmapped(p)\
(!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_MMAPPED_BIT))
/* Get the internal overhead associated with chunk p */
#define overhead_for(p)\
(is_mmapped(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD)
#endif /* MALLOC_PRIVATE_H */

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,7 @@ details. */
#ifndef MALLOC_DEBUG
#include <malloc.h>
#endif
extern "C" struct mallinfo ptmallinfo ();
extern "C" struct mallinfo dlmallinfo ();
/* we provide these stubs to call into a user's
provided malloc if there is one - otherwise
@ -42,7 +42,11 @@ free (void *p)
if (!use_internal)
user_data->free (p);
else
ptfree (p);
{
__malloc_lock ();
dlfree (p);
__malloc_unlock ();
}
}
extern "C" void *
@ -52,7 +56,11 @@ malloc (size_t size)
if (!use_internal)
res = user_data->malloc (size);
else
res = ptmalloc (size);
{
__malloc_lock ();
res = dlmalloc (size);
__malloc_unlock ();
}
malloc_printf ("(%d) = %x, called by %p", size, res, __builtin_return_address (0));
return res;
}
@ -64,7 +72,11 @@ realloc (void *p, size_t size)
if (!use_internal)
res = user_data->realloc (p, size);
else
res = ptrealloc (p, size);
{
__malloc_lock ();
res = dlrealloc (p, size);
__malloc_unlock ();
}
malloc_printf ("(%x, %d) = %x, called by %x", p, size, res, __builtin_return_address (0));
return res;
}
@ -87,7 +99,11 @@ calloc (size_t nmemb, size_t size)
if (!use_internal)
res = user_data->calloc (nmemb, size);
else
res = ptcalloc (nmemb, size);
{
__malloc_lock ();
res = dlcalloc (nmemb, size);
__malloc_unlock ();
}
malloc_printf ("(%d, %d) = %x, called by %x", nmemb, size, res, __builtin_return_address (0));
return res;
}
@ -102,7 +118,9 @@ posix_memalign (void **memptr, size_t alignment, size_t bytes)
return ENOSYS;
if ((alignment & (alignment - 1)) != 0)
return EINVAL;
res = ptmemalign (alignment, bytes);
__malloc_lock ();
res = dlmemalign (alignment, bytes);
__malloc_unlock ();
if (!res)
return ENOMEM;
if (memptr)
@ -120,7 +138,11 @@ memalign (size_t alignment, size_t bytes)
res = NULL;
}
else
res = ptmemalign (alignment, bytes);
{
__malloc_lock ();
res = dlmemalign (alignment, bytes);
__malloc_unlock ();
}
return res;
}
@ -135,7 +157,11 @@ valloc (size_t bytes)
res = NULL;
}
else
res = ptvalloc (bytes);
{
__malloc_lock ();
res = dlvalloc (bytes);
__malloc_unlock ();
}
return res;
}
@ -150,7 +176,11 @@ malloc_usable_size (void *p)
res = 0;
}
else
res = ptmalloc_usable_size (p);
{
__malloc_lock ();
res = dlmalloc_usable_size (p);
__malloc_unlock ();
}
return res;
}
@ -165,7 +195,11 @@ malloc_trim (size_t pad)
res = 0;
}
else
res = ptmalloc_trim (pad);
{
__malloc_lock ();
res = dlmalloc_trim (pad);
__malloc_unlock ();
}
return res;
}
@ -180,7 +214,11 @@ mallopt (int p, int v)
res = 0;
}
else
res = ptmallopt (p, v);
{
__malloc_lock ();
res = dlmallopt (p, v);
__malloc_unlock ();
}
return res;
}
@ -191,7 +229,11 @@ malloc_stats ()
if (!use_internal)
set_errno (ENOSYS);
else
ptmalloc_stats ();
{
__malloc_lock ();
dlmalloc_stats ();
__malloc_unlock ();
}
}
extern "C" struct mallinfo
@ -201,7 +243,11 @@ mallinfo ()
if (!use_internal)
set_errno (ENOSYS);
else
m = ptmallinfo ();
{
__malloc_lock ();
m = dlmallinfo ();
__malloc_unlock ();
}
return m;
}
@ -216,9 +262,20 @@ strdup (const char *s)
return p;
}
/* 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. */
muto NO_COPY mallock;
void
malloc_init ()
{
mallock.init ("mallock");
#ifndef MALLOC_DEBUG
/* Check if malloc is provided by application. If so, redirect all
calls to malloc/free/realloc to application provided. This may

View File

@ -1326,9 +1326,11 @@ winpids::enum_processes (bool winpid)
void
winpids::set (bool winpid)
{
__malloc_lock ();
npids = enum_processes (winpid);
if (pidlist)
pidlist[npids] = 0;
__malloc_unlock ();
}
DWORD

File diff suppressed because it is too large Load Diff