first commit

This commit is contained in:
Jose Caban
2025-06-07 11:34:38 -04:00
commit 0eb2d7c07d
4708 changed files with 1500614 additions and 0 deletions

19
extern/ptmalloc3/COPYRIGHT vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright (c) 2001-2006 Wolfram Gloger
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.

33
extern/ptmalloc3/ChangeLog vendored Normal file
View File

@@ -0,0 +1,33 @@
2006-05-31 Wolfram Gloger <wg@malloc.de>
* sysdeps/pthread/malloc-machine.h (mutex_unlock): Unlock needs
full memory barrier (thanks Bart Robinson).
2006-03-31 Wolfram Gloger <wg@malloc.de>
* ptmalloc3.c (public_iCALLOc, public_iCOMALLOc): New functions.
2006-03-30 Wolfram Gloger <wg@malloc.de>
* malloc/malloc.c: Upgrade to version pre-2.8.4-29mar06.
* malloc/malloc-private.h: New fields in malloc_state.
2004-03-29 Wolfram Gloger <wg@malloc.de>
* malloc/malloc.c (mmap_alloc): Use page_align instead of
granularity_align.
(mmap_resize): Likewise.
* malloc/ptmalloc3.c (ptmalloc_init):
Add MALLOC_GRANULARITY_ and synonymous MALLOC_TOP_PAD_ environment
parameters.
2006-03-25 Wolfram Gloger <wg@malloc.de>
* malloc/malloc-private.h: New file.
2005-12-31 Wolfram Gloger <wg@malloc.de>
* malloc/malloc.c: Imported from Doug Lea's malloc-2.8.3.
* malloc/malloc.h-2.8.3.h: Likewise.
* malloc/ptmalloc3.c: New file.

211
extern/ptmalloc3/Makefile vendored Normal file
View File

@@ -0,0 +1,211 @@
# Makefile for ptmalloc, version 3
# by Wolfram Gloger 1996-1999, 2001-2005, 2006
DIST_FILES0 = ChangeLog malloc-2.8.3.h malloc.c \
malloc-private.h ptmalloc3.c \
sysdeps \
#tst-mallocstate.c tst-mstats.c
DIST_FILES1 = COPYRIGHT README Makefile \
$(DIST_FILES0) \
lran2.h t-test.h t-test1.c t-test2.c \
tst-independent-alloc.c \
#debian
DIST_FILES2 = $(DIST_FILES1) \
m-test1.c \
RCS/*,v
TAR_FLAGS = --numeric-owner --exclude "*~" --exclude "debian/tmp*"
#CC = /pkg/gcc-2.95.2-wg/bin/gcc
CC = gcc
SYS_FLAGS =
OPT_FLAGS = -g -O2 #-O # -O2
WARN_FLAGS = -Wall -Wstrict-prototypes
SH_FLAGS = -shared -fpic
INC_FLAGS = -Isysdeps/generic
# Flags for the test programs
T_FLAGS = -DUSE_MALLOC=1 -DTEST=1
# Flags for the compilation of malloc.c
M_FLAGS = -DTHREAD_STATS=1 #-DMALLOC_DEBUG=1 -DDEBUG=1
# Thread flags.
# See the platform-specific targets below.
THR_FLAGS = -DUSE_TSD_DATA_HACK -D_REENTRANT
THR_LIBS = -lpthread
RM = rm -f
AR = ar
RANLIB = ranlib
MALLOC_OBJ = ptmalloc3.o malloc.o
LIB_MALLOC = libptmalloc3.a
T_SUF =
TESTS = t-test1$(T_SUF) t-test2$(T_SUF) \
tst-independent-alloc$(T_SUF)
#m-test1$(T_SUF) tst-mallocstate$(T_SUF) tst-mstats$(T_SUF)
CFLAGS = $(SYS_FLAGS) $(OPT_FLAGS) $(WARN_FLAGS) $(THR_FLAGS) $(INC_FLAGS)
.c.o:
$(CC) -c $(CFLAGS) $<
all: $(LIB_MALLOC) $(TESTS)
ptmalloc3.o: ptmalloc3.c malloc-2.8.3.h
$(CC) -c $(CFLAGS) $(M_FLAGS) -DMSPACES=1 $<
malloc.o: malloc.c
$(CC) -c $(CFLAGS) $(M_FLAGS) -DONLY_MSPACES -DUSE_LOCKS=0 $<
#malloc-stats.o: malloc-stats.c malloc.h
# $(CC) -c $(CFLAGS) $(M_FLAGS) $<
libptmalloc3.a: $(MALLOC_OBJ)
$(AR) cr $@ $(MALLOC_OBJ)
$(RANLIB) $@
libptmalloc3.so: $(MALLOC_OBJ)
$(CC) $(SH_FLAGS) $(CFLAGS) $(M_FLAGS) $(MALLOC_OBJ) -o $@
again:
$(RM) $(TESTS)
$(MAKE) $(TESTS)
clean:
$(RM) $(MALLOC_OBJ) libptmalloc3.a libptmalloc3.so $(TESTS) \
core core.[0-9]*
m-test1$(T_SUF): m-test1.c $(LIB_MALLOC)
$(CC) $(CFLAGS) $(T_FLAGS) m-test1.c $(LIB_MALLOC) $(THR_LIBS) -o $@
t-test1$(T_SUF): t-test1.c t-test.h $(LIB_MALLOC)
$(CC) $(CFLAGS) $(T_FLAGS) t-test1.c $(LIB_MALLOC) $(THR_LIBS) -o $@
t-test2$(T_SUF): t-test2.c t-test.h $(LIB_MALLOC)
$(CC) $(CFLAGS) $(T_FLAGS) t-test2.c $(LIB_MALLOC) $(THR_LIBS) -o $@
tst-mallocstate$(T_SUF): tst-mallocstate.c $(LIB_MALLOC)
$(CC) $(CFLAGS) $(T_FLAGS) tst-mallocstate.c $(LIB_MALLOC) \
$(THR_LIBS) -o $@
tst-mstats$(T_SUF): tst-mstats.c $(LIB_MALLOC)
$(CC) $(CFLAGS) $(T_FLAGS) tst-mstats.c $(LIB_MALLOC) \
$(THR_LIBS) -o $@
tst-independent-alloc$(T_SUF): tst-independent-alloc.c $(LIB_MALLOC)
$(CC) $(CFLAGS) $(T_FLAGS) tst-independent-alloc.c $(LIB_MALLOC) \
$(THR_LIBS) -o $@
############################################################################
# Platform-specific targets. The ones ending in `-libc' are provided
# to enable comparison with the standard malloc implementation from
# the system's native C library. The option USE_TSD_DATA_HACK is now
# the default for pthreads systems, as most (Irix 6, Solaris 2) seem
# to need it. Try with USE_TSD_DATA_HACK undefined only if you're
# confident that your systems's thread specific data functions do _not_
# use malloc themselves.
# posix threads with TSD data hack
posix:
$(MAKE) THR_FLAGS='-DUSE_TSD_DATA_HACK -D_REENTRANT' \
OPT_FLAGS='$(OPT_FLAGS)' SYS_FLAGS='$(SYS_FLAGS)' CC='$(CC)' \
INC_FLAGS='-Isysdeps/pthread -Isysdeps/generic -I.'
THR_LIBS=-lpthread
# posix threads with explicit initialization. Known to be needed on HPUX.
posix-explicit:
$(MAKE) THR_FLAGS='-D_REENTRANT -DUSE_TSD_DATA_HACK -DUSE_STARTER=2' \
THR_LIBS=-lpthread \
OPT_FLAGS='$(OPT_FLAGS)' SYS_FLAGS='$(SYS_FLAGS)' CC='$(CC)' \
INC_FLAGS='-Isysdeps/pthread -Isysdeps/generic -I.' \
M_FLAGS='$(M_FLAGS)'
# posix threads without TSD data hack -- not known to work
posix-with-tsd:
$(MAKE) THR_FLAGS='-D_REENTRANT' THR_LIBS=-lpthread \
INC_FLAGS='-Isysdeps/pthread -Isysdeps/generic -I.' \
M_FLAGS='$(M_FLAGS)'
posix-libc:
$(MAKE) THR_FLAGS='-D_REENTRANT' THR_LIBS=-lpthread \
INC_FLAGS='-Isysdeps/pthread -Isysdeps/generic -I.' \
M_FLAGS='$(M_FLAGS)' LIB_MALLOC= T_FLAGS= T_SUF=-libc
linux-pthread:
$(MAKE) SYS_FLAGS='-D_GNU_SOURCE=1' \
WARN_FLAGS='-Wall -Wstrict-prototypes' \
OPT_FLAGS='$(OPT_FLAGS)' THR_FLAGS='-DUSE_TSD_DATA_HACK' \
INC_FLAGS='-Isysdeps/pthread -Isysdeps/generic -I.' M_FLAGS='$(M_FLAGS)' \
TESTS='$(TESTS)'
linux-shared:
$(MAKE) SYS_FLAGS='-D_GNU_SOURCE=1 -fpic' \
WARN_FLAGS='-Wall -Wstrict-prototypes' \
OPT_FLAGS='$(OPT_FLAGS)' THR_FLAGS='-DUSE_TSD_DATA_HACK' \
INC_FLAGS='-Isysdeps/pthread -Isysdeps/generic -I.' M_FLAGS='$(M_FLAGS)' \
LIB_MALLOC=libptmalloc3.so
sproc:
$(MAKE) THR_FLAGS='' THR_LIBS='' OPT_FLAGS='$(OPT_FLAGS)' CC='$(CC)' \
INC_FLAGS='-Isysdeps/sproc -Isysdeps/generic -I.' \
M_FLAGS='$(M_FLAGS) -Dmalloc_getpagesize=4096'
sproc-shared:
$(MAKE) THR_FLAGS='' THR_LIBS= \
SH_FLAGS='-shared -check_registry /usr/lib/so_locations' \
INC_FLAGS='-Isysdeps/sproc -Isysdeps/generic -I.' \
LIB_MALLOC=libptmalloc3.so M_FLAGS='$(M_FLAGS)'
sproc-libc:
$(MAKE) THR_FLAGS='' THR_LIBS= LIB_MALLOC= T_FLAGS= \
INC_FLAGS='-Isysdeps/sproc -Isysdeps/generic -I.' \
T_SUF=-libc M_FLAGS='$(M_FLAGS)'
solaris:
$(MAKE) THR_FLAGS='-D_REENTRANT' OPT_FLAGS='$(OPT_FLAGS)' \
INC_FLAGS='-Isysdeps/solaris -Isysdeps/generic -I.' \
THR_LIBS=-lthread M_FLAGS='$(M_FLAGS)'
solaris-libc:
$(MAKE) THR_FLAGS='-D_REENTRANT' OPT_FLAGS='$(OPT_FLAGS)' \
INC_FLAGS='-Isysdeps/solaris -Isysdeps/generic -I.' \
THR_LIBS=-lthread LIB_MALLOC= T_FLAGS= T_SUF=-libc M_FLAGS='$(M_FLAGS)'
nothreads:
$(MAKE) OPT_FLAGS='$(OPT_FLAGS)' SYS_FLAGS='$(SYS_FLAGS)' \
INC_FLAGS='-Isysdeps/generic -I.' \
THR_FLAGS='' THR_LIBS='' M_FLAGS='$(M_FLAGS)'
gcc-nothreads:
$(MAKE) CC='gcc' WARN_FLAGS='-Wall' OPT_FLAGS='$(OPT_FLAGS)' \
INC_FLAGS='-Isysdeps/generic -I.' \
SYS_FLAGS='$(SYS_FLAGS)' THR_FLAGS='' THR_LIBS='' M_FLAGS='$(M_FLAGS)'
linux-nothreads:
$(MAKE) CC='gcc' WARN_FLAGS='-Wall' OPT_FLAGS='$(OPT_FLAGS)' \
INC_FLAGS='-Isysdeps/generic -I.' \
SYS_FLAGS='-D_GNU_SOURCE' THR_FLAGS='' THR_LIBS='' M_FLAGS='$(M_FLAGS)'
############################################################################
check: $(TESTS)
./t-test1
./t-test2
#./tst-mallocstate || echo "Test mallocstate failed!"
#./tst-mstats || echo "Test mstats failed!"
snap:
cd ..; tar $(TAR_FLAGS) -c -f - $(DIST_FILES1:%=ptmalloc3/%) | \
gzip -9 >ptmalloc3-current.tar.gz
dist:
cd ..; tar $(TAR_FLAGS) -c -f - $(DIST_FILES2:%=ptmalloc3/%) | \
gzip -9 >ptmalloc3.tar.gz
# dependencies
ptmalloc3.o: malloc-private.h

186
extern/ptmalloc3/README vendored Normal file
View File

@@ -0,0 +1,186 @@
ptmalloc3 - a multi-thread malloc implementation
================================================
Wolfram Gloger (wg@malloc.de)
Jan 2006
Thanks
======
This release was partly funded by Pixar Animation Studios. I would
like to thank David Baraff of Pixar for his support and Doug Lea
(dl@cs.oswego.edu) for the great original malloc implementation.
Introduction
============
This package is a modified version of Doug Lea's malloc-2.8.3
implementation (available seperately from ftp://g.oswego.edu/pub/misc)
that I adapted for multiple threads, while trying to avoid lock
contention as much as possible.
As part of the GNU C library, the source files may be available under
the GNU Library General Public License (see the comments in the
files). But as part of this stand-alone package, the code is also
available under the (probably less restrictive) conditions described
in the file 'COPYRIGHT'. In any case, there is no warranty whatsoever
for this package.
The current distribution should be available from:
http://www.malloc.de/malloc/ptmalloc3.tar.gz
Compilation
===========
It should be possible to build ptmalloc3 on any UN*X-like system that
implements the sbrk(), mmap(), munmap() and mprotect() calls. Since
there are now several source files, a library (libptmalloc3.a) is
generated. See the Makefile for examples of the compile-time options.
Note that support for non-ANSI compilers is no longer there.
Several example targets are provided in the Makefile:
o Posix threads (pthreads), compile with "make posix"
o Posix threads with explicit initialization, compile with
"make posix-explicit" (known to be required on HPUX)
o Posix threads without "tsd data hack" (see below), compile with
"make posix-with-tsd"
o Solaris threads, compile with "make solaris"
o SGI sproc() threads, compile with "make sproc"
o no threads, compile with "make nothreads" (currently out of order?)
For Linux:
o make "linux-pthread" (almost the same as "make posix") or
make "linux-shared"
Note that some compilers need special flags for multi-threaded code,
e.g. with Solaris cc with Posix threads, one should use:
% make posix SYS_FLAGS='-mt'
Some additional targets, ending in `-libc', are also provided in the
Makefile, to compare performance of the test programs to the case when
linking with the standard malloc implementation in libc.
A potential problem remains: If any of the system-specific functions
for getting/setting thread-specific data or for locking a mutex call
one of the malloc-related functions internally, the implementation
cannot work at all due to infinite recursion. One example seems to be
Solaris 2.4. I would like to hear if this problem occurs on other
systems, and whether similar workarounds could be applied.
For Posix threads, too, an optional hack like that has been integrated
(activated when defining USE_TSD_DATA_HACK) which depends on
`pthread_t' being convertible to an integral type (which is of course
not generally guaranteed). USE_TSD_DATA_HACK is now the default
because I haven't yet found a non-glibc pthreads system where this
hack is _not_ needed.
*NEW* and _important_: In (currently) one place in the ptmalloc3
source, a write memory barrier is needed, named
atomic_write_barrier(). This macro needs to be defined at the end of
malloc-machine.h. For gcc, a fallback in the form of a full memory
barrier is already defined, but you may need to add another definition
if you don't use gcc.
Usage
=====
Just link libptmalloc3 into your application.
Some wicked systems (e.g. HPUX apparently) won't let malloc call _any_
thread-related functions before main(). On these systems,
USE_STARTER=2 must be defined during compilation (see "make
posix-explicit" above) and the global initialization function
ptmalloc_init() must be called explicitly, preferably at the start of
main().
Otherwise, when using ptmalloc3, no special precautions are necessary.
Link order is important
=======================
On some systems, when overriding malloc and linking against shared
libraries, the link order becomes very important. E.g., when linking
C++ programs on Solaris with Solaris threads [this is probably now
obsolete], don't rely on libC being included by default, but instead
put `-lthread' behind `-lC' on the command line:
CC ... libptmalloc3.a -lC -lthread
This is because there are global constructors in libC that need
malloc/ptmalloc, which in turn needs to have the thread library to be
already initialized.
Debugging hooks
===============
All calls to malloc(), realloc(), free() and memalign() are routed
through the global function pointers __malloc_hook, __realloc_hook,
__free_hook and __memalign_hook if they are not NULL (see the malloc.h
header file for declarations of these pointers). Therefore the malloc
implementation can be changed at runtime, if care is taken not to call
free() or realloc() on pointers obtained with a different
implementation than the one currently in effect. (The easiest way to
guarantee this is to set up the hooks before any malloc call, e.g.
with a function pointed to by the global variable
__malloc_initialize_hook).
You can now also tune other malloc parameters (normally adjused via
mallopt() calls from the application) with environment variables:
MALLOC_TRIM_THRESHOLD_ for deciding to shrink the heap (in bytes)
MALLOC_GRANULARITY_ The unit for allocating and deallocating
MALLOC_TOP_PAD_ memory from the system. The default
is 64k and this parameter _must_ be a
power of 2.
MALLOC_MMAP_THRESHOLD_ min. size for chunks allocated via
mmap() (in bytes)
Tests
=====
Two testing applications, t-test1 and t-test2, are included in this
source distribution. Both perform pseudo-random sequences of
allocations/frees, and can be given numeric arguments (all arguments
are optional):
% t-test[12] <n-total> <n-parallel> <n-allocs> <size-max> <bins>
n-total = total number of threads executed (default 10)
n-parallel = number of threads running in parallel (2)
n-allocs = number of malloc()'s / free()'s per thread (10000)
size-max = max. size requested with malloc() in bytes (10000)
bins = number of bins to maintain
The first test `t-test1' maintains a completely seperate pool of
allocated bins for each thread, and should therefore show full
parallelism. On the other hand, `t-test2' creates only a single pool
of bins, and each thread randomly allocates/frees any bin. Some lock
contention is to be expected in this case, as the threads frequently
cross each others arena.
Performance results from t-test1 should be quite repeatable, while the
behaviour of t-test2 depends on scheduling variations.
Conclusion
==========
I'm always interested in performance data and feedback, just send mail
to ptmalloc@malloc.de.
Good luck!

51
extern/ptmalloc3/lran2.h vendored Normal file
View File

@@ -0,0 +1,51 @@
/* lran2.h
* by Wolfram Gloger 1996.
*
* A small, portable pseudo-random number generator.
*/
#ifndef _LRAN2_H
#define _LRAN2_H
#define LRAN2_MAX 714025l /* constants for portable */
#define IA 1366l /* random number generator */
#define IC 150889l /* (see e.g. `Numerical Recipes') */
struct lran2_st {
long x, y, v[97];
};
static void
lran2_init(struct lran2_st* d, long seed)
{
long x;
int j;
x = (IC - seed) % LRAN2_MAX;
if(x < 0) x = -x;
for(j=0; j<97; j++) {
x = (IA*x + IC) % LRAN2_MAX;
d->v[j] = x;
}
d->x = (IA*x + IC) % LRAN2_MAX;
d->y = d->x;
}
#ifdef __GNUC__
__inline__
#endif
static long
lran2(struct lran2_st* d)
{
int j = (d->y % 97);
d->y = d->v[j];
d->x = (IA*d->x + IC) % LRAN2_MAX;
d->v[j] = d->x;
return d->y;
}
#undef IA
#undef IC
#endif

534
extern/ptmalloc3/malloc-2.8.3.h vendored Normal file
View File

@@ -0,0 +1,534 @@
/*
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 !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);
#define M_TRIM_THRESHOLD (-1)
#define M_GRANULARITY (-2)
#define M_MMAP_THRESHOLD (-3)
/*
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
/*
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 */
};
#endif /* _MALLOC_H */
#endif /* HAVE_USR_INCLUDE_MALLOC_H */
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 */

170
extern/ptmalloc3/malloc-private.h vendored Normal file
View File

@@ -0,0 +1,170 @@
/*
$Id: malloc-private.h,v 1.4 2006/03/31 12:56:52 wg Exp $
Private header file for ptmalloc3, created by Wolfram Gloger
and released to the public domain, as explained at
http://creativecommons.org/licenses/publicdomain.
*/
/* 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)
/* ------------------- 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 */

5515
extern/ptmalloc3/malloc.c vendored Normal file

File diff suppressed because it is too large Load Diff

1135
extern/ptmalloc3/ptmalloc3.c vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
/* Empty placeholder */

View File

@@ -0,0 +1,68 @@
/* Basic platform-independent macro definitions for mutexes,
thread-specific data and parameters for malloc.
Copyright (C) 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#ifndef _GENERIC_MALLOC_MACHINE_H
#define _GENERIC_MALLOC_MACHINE_H
#include <atomic.h>
#ifndef mutex_init /* No threads, provide dummy macros */
# define NO_THREADS
/* The mutex functions used to do absolutely nothing, i.e. lock,
trylock and unlock would always just return 0. However, even
without any concurrently active threads, a mutex can be used
legitimately as an `in use' flag. To make the code that is
protected by a mutex async-signal safe, these macros would have to
be based on atomic test-and-set operations, for example. */
typedef int mutex_t;
# define mutex_init(m) (*(m) = 0)
# define mutex_lock(m) ((*(m) = 1), 0)
# define mutex_trylock(m) (*(m) ? 1 : ((*(m) = 1), 0))
# define mutex_unlock(m) (*(m) = 0)
typedef void *tsd_key_t;
# define tsd_key_create(key, destr) do {} while(0)
# define tsd_setspecific(key, data) ((key) = (data))
# define tsd_getspecific(key, vptr) (vptr = (key))
# define thread_atfork(prepare, parent, child) do {} while(0)
#endif /* !defined mutex_init */
#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(_GENERIC_MALLOC_MACHINE_H) */

View File

@@ -0,0 +1,48 @@
/*
* $Id:$
* Generic version: no threads.
* by Wolfram Gloger 2004
*/
#include <stdio.h>
struct thread_st {
char *sp; /* stack pointer, can be 0 */
void (*func)(struct thread_st* st); /* must be set by user */
int id;
int flags;
struct user_data u;
};
static void
thread_init(void)
{
printf("No threads.\n");
}
/* Create a thread. */
static int
thread_create(struct thread_st *st)
{
st->flags = 0;
st->id = 1;
st->func(st);
return 0;
}
/* Wait for one of several subthreads to finish. */
static void
wait_for_thread(struct thread_st st[], int n_thr,
int (*end_thr)(struct thread_st*))
{
int i;
for(i=0; i<n_thr; i++)
if(end_thr)
end_thr(&st[i]);
}
/*
* Local variables:
* tab-width: 4
* End:
*/

View File

@@ -0,0 +1,131 @@
/* 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)
#include <sysdeps/generic/malloc-machine.h>
#endif /* !defined(_MALLOC_MACHINE_H) */

View File

@@ -0,0 +1,111 @@
/*
* $Id: thread-st.h$
* pthread version
* by Wolfram Gloger 2004
*/
#include <pthread.h>
#include <stdio.h>
pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER;
#ifndef USE_PTHREADS_STACKS
#define USE_PTHREADS_STACKS 0
#endif
#ifndef STACKSIZE
#define STACKSIZE 32768
#endif
struct thread_st {
char *sp; /* stack pointer, can be 0 */
void (*func)(struct thread_st* st); /* must be set by user */
pthread_t id;
int flags;
struct user_data u;
};
static void
thread_init(void)
{
printf("Using posix threads.\n");
pthread_cond_init(&finish_cond, NULL);
pthread_mutex_init(&finish_mutex, NULL);
}
static void *
thread_wrapper(void *ptr)
{
struct thread_st *st = (struct thread_st*)ptr;
/*printf("begin %p\n", st->sp);*/
st->func(st);
pthread_mutex_lock(&finish_mutex);
st->flags = 1;
pthread_mutex_unlock(&finish_mutex);
pthread_cond_signal(&finish_cond);
/*printf("end %p\n", st->sp);*/
return NULL;
}
/* Create a thread. */
static int
thread_create(struct thread_st *st)
{
st->flags = 0;
{
pthread_attr_t* attr_p = 0;
#if USE_PTHREADS_STACKS
pthread_attr_t attr;
pthread_attr_init (&attr);
if(!st->sp)
st->sp = malloc(STACKSIZE+16);
if(!st->sp)
return -1;
if(pthread_attr_setstacksize(&attr, STACKSIZE))
fprintf(stderr, "error setting stacksize");
else
pthread_attr_setstackaddr(&attr, st->sp + STACKSIZE);
/*printf("create %p\n", st->sp);*/
attr_p = &attr;
#endif
return pthread_create(&st->id, attr_p, thread_wrapper, st);
}
return 0;
}
/* Wait for one of several subthreads to finish. */
static void
wait_for_thread(struct thread_st st[], int n_thr,
int (*end_thr)(struct thread_st*))
{
int i;
pthread_mutex_lock(&finish_mutex);
for(;;) {
int term = 0;
for(i=0; i<n_thr; i++)
if(st[i].flags) {
/*printf("joining %p\n", st[i].sp);*/
if(pthread_join(st[i].id, NULL) == 0) {
st[i].flags = 0;
if(end_thr)
end_thr(&st[i]);
} else
fprintf(stderr, "can't join\n");
++term;
}
if(term > 0)
break;
pthread_cond_wait(&finish_cond, &finish_mutex);
}
pthread_mutex_unlock(&finish_mutex);
}
/*
* Local variables:
* tab-width: 4
* End:
*/

View File

@@ -0,0 +1,51 @@
/* Basic platform-independent macro definitions for mutexes,
thread-specific data and parameters for malloc.
Solaris threads 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 _SOLARIS_MALLOC_MACHINE_H
#define _SOLARIS_MALLOC_MACHINE_H
#include <thread.h>
typedef thread_t thread_id;
#define MUTEX_INITIALIZER { 0 }
#define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
/*
* Hack for thread-specific data on Solaris. We can't use thr_setspecific
* because that function calls malloc() itself.
*/
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)thr_self() % 256] = (data))
#define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
#define thread_atfork(prepare, parent, child) do {} while(0)
#include <sysdeps/generic/malloc-machine.h>
#endif /* !defined(_SOLARIS_MALLOC_MACHINE_H) */

View File

@@ -0,0 +1,72 @@
/*
* $Id:$
* Solaris version
* by Wolfram Gloger 2004
*/
#include <thread.h>
#include <stdio.h>
#ifndef STACKSIZE
#define STACKSIZE 32768
#endif
struct thread_st {
char *sp; /* stack pointer, can be 0 */
void (*func)(struct thread_st* st); /* must be set by user */
thread_id id;
int flags;
struct user_data u;
};
static void
thread_init(void)
{
printf("Using Solaris threads.\n");
}
static void *
thread_wrapper(void *ptr)
{
struct thread_st *st = (struct thread_st*)ptr;
/*printf("begin %p\n", st->sp);*/
st->func(st);
/*printf("end %p\n", st->sp);*/
return NULL;
}
/* Create a thread. */
static int
thread_create(struct thread_st *st)
{
st->flags = 0;
if(!st->sp)
st->sp = malloc(STACKSIZE);
if(!st->sp) return -1;
thr_create(st->sp, STACKSIZE, thread_wrapper, st, THR_NEW_LWP, &st->id);
return 0;
}
/* Wait for one of several subthreads to finish. */
static void
wait_for_thread(struct thread_st st[], int n_thr,
int (*end_thr)(struct thread_st*))
{
int i;
thread_t id;
thr_join(0, &id, NULL);
for(i=0; i<n_thr; i++)
if(id == st[i].id) {
if(end_thr)
end_thr(&st[i]);
break;
}
}
/*
* Local variables:
* tab-width: 4
* End:
*/

View File

@@ -0,0 +1,51 @@
/* Basic platform-independent macro definitions for mutexes,
thread-specific data and parameters for malloc.
SGI threads (sprocs) 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 _SPROC_MALLOC_MACHINE_H
#define _SPROC_MALLOC_MACHINE_H
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <abi_mutex.h>
typedef abilock_t mutex_t;
#define MUTEX_INITIALIZER { 0 }
#define mutex_init(m) init_lock(m)
#define mutex_lock(m) (spin_lock(m), 0)
#define mutex_trylock(m) acquire_lock(m)
#define mutex_unlock(m) release_lock(m)
typedef int tsd_key_t;
int tsd_key_next;
#define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
#define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
#define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
#define thread_atfork(prepare, parent, child) do {} while(0)
#include <sysdeps/generic/malloc-machine.h>
#endif /* !defined(_SPROC_MALLOC_MACHINE_H) */

View File

@@ -0,0 +1,85 @@
/*
* $Id:$
* sproc version
* by Wolfram Gloger 2001, 2004, 2006
*/
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/prctl.h>
#ifndef STACKSIZE
#define STACKSIZE 32768
#endif
struct thread_st {
char *sp; /* stack pointer, can be 0 */
void (*func)(struct thread_st* st); /* must be set by user */
int id;
int flags;
struct user_data u;
};
static void
thread_init(void)
{
printf("Using sproc() threads.\n");
}
static void
thread_wrapper(void *ptr, size_t stack_len)
{
struct thread_st *st = (struct thread_st*)ptr;
/*printf("begin %p\n", st->sp);*/
st->func(st);
/*printf("end %p\n", st->sp);*/
}
/* Create a thread. */
static int
thread_create(struct thread_st *st)
{
st->flags = 0;
if(!st->sp)
st->sp = malloc(STACKSIZE);
if(!st->sp) return -1;
st->id = sprocsp(thread_wrapper, PR_SALL, st, st->sp+STACKSIZE, STACKSIZE);
if(st->id < 0) {
return -1;
}
return 0;
}
/* Wait for one of several subthreads to finish. */
static void
wait_for_thread(struct thread_st st[], int n_thr,
int (*end_thr)(struct thread_st*))
{
int i;
int id;
int status = 0;
id = wait(&status);
if(status != 0) {
if(WIFSIGNALED(status))
printf("thread %id terminated by signal %d\n",
id, WTERMSIG(status));
else
printf("thread %id exited with status %d\n",
id, WEXITSTATUS(status));
}
for(i=0; i<n_thr; i++)
if(id == st[i].id) {
if(end_thr)
end_thr(&st[i]);
break;
}
}
/*
* Local variables:
* tab-width: 4
* End:
*/

143
extern/ptmalloc3/t-test.h vendored Normal file
View File

@@ -0,0 +1,143 @@
/*
* $Id: t-test.h,v 1.1 2004/11/04 14:32:21 wg Exp $
* by Wolfram Gloger 1996.
* Common data structures and functions for testing malloc performance.
*/
/* Testing level */
#ifndef TEST
#define TEST 0
#endif
/* For large allocation sizes, the time required by copying in
realloc() can dwarf all other execution times. Avoid this with a
size threshold. */
#ifndef REALLOC_MAX
#define REALLOC_MAX 2000
#endif
struct bin {
unsigned char *ptr;
unsigned long size;
};
#if TEST > 0
static void
mem_init(unsigned char *ptr, unsigned long size)
{
unsigned long i, j;
if(size == 0) return;
for(i=0; i<size; i+=2047) {
j = (unsigned long)ptr ^ i;
ptr[i] = ((j ^ (j>>8)) & 0xFF);
}
j = (unsigned long)ptr ^ (size-1);
ptr[size-1] = ((j ^ (j>>8)) & 0xFF);
}
static int
mem_check(unsigned char *ptr, unsigned long size)
{
unsigned long i, j;
if(size == 0) return 0;
for(i=0; i<size; i+=2047) {
j = (unsigned long)ptr ^ i;
if(ptr[i] != ((j ^ (j>>8)) & 0xFF)) return 1;
}
j = (unsigned long)ptr ^ (size-1);
if(ptr[size-1] != ((j ^ (j>>8)) & 0xFF)) return 2;
return 0;
}
static int
zero_check(unsigned* ptr, unsigned long size)
{
unsigned char* ptr2;
while(size >= sizeof(*ptr)) {
if(*ptr++ != 0)
return -1;
size -= sizeof(*ptr);
}
ptr2 = (unsigned char*)ptr;
while(size > 0) {
if(*ptr2++ != 0)
return -1;
--size;
}
return 0;
}
#endif /* TEST > 0 */
/* Allocate a bin with malloc(), realloc() or memalign(). r must be a
random number >= 1024. */
static void
bin_alloc(struct bin *m, unsigned long size, int r)
{
#if TEST > 0
if(mem_check(m->ptr, m->size)) {
printf("memory corrupt!\n");
exit(1);
}
#endif
r %= 1024;
/*printf("%d ", r);*/
if(r < 4) { /* memalign */
if(m->size > 0) free(m->ptr);
m->ptr = (unsigned char *)memalign(sizeof(int) << r, size);
} else if(r < 20) { /* calloc */
if(m->size > 0) free(m->ptr);
m->ptr = (unsigned char *)calloc(size, 1);
#if TEST > 0
if(zero_check((unsigned*)m->ptr, size)) {
long i;
for(i=0; i<size; i++)
if(m->ptr[i] != 0)
break;
printf("calloc'ed memory non-zero (ptr=%p, i=%ld)!\n", m->ptr, i);
exit(1);
}
#endif
} else if(r < 100 && m->size < REALLOC_MAX) { /* realloc */
if(m->size == 0) m->ptr = NULL;
m->ptr = realloc(m->ptr, size);
} else { /* plain malloc */
if(m->size > 0) free(m->ptr);
m->ptr = (unsigned char *)malloc(size);
}
if(!m->ptr) {
printf("out of memory (r=%d, size=%ld)!\n", r, (long)size);
exit(1);
}
m->size = size;
#if TEST > 0
mem_init(m->ptr, m->size);
#endif
}
/* Free a bin. */
static void
bin_free(struct bin *m)
{
if(m->size == 0) return;
#if TEST > 0
if(mem_check(m->ptr, m->size)) {
printf("memory corrupt!\n");
exit(1);
}
#endif
free(m->ptr);
m->size = 0;
}
/*
* Local variables:
* tab-width: 4
* End:
*/

285
extern/ptmalloc3/t-test1.c vendored Normal file
View File

@@ -0,0 +1,285 @@
/*
* $Id: t-test1.c,v 1.2 2006/03/27 16:05:13 wg Exp $
* by Wolfram Gloger 1996-1999, 2001, 2004, 2006
* A multi-thread test for malloc performance, maintaining one pool of
* allocated bins per thread.
*/
#if (defined __STDC__ && __STDC__) || defined __cplusplus
# include <stdlib.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/mman.h>
#if !USE_MALLOC
#include <malloc.h>
#else
#include "malloc-2.8.3.h"
#endif
#include "lran2.h"
#include "t-test.h"
struct user_data {
int bins, max;
unsigned long size;
long seed;
};
#include "thread-st.h"
#define N_TOTAL 10
#ifndef N_THREADS
#define N_THREADS 2
#endif
#ifndef N_TOTAL_PRINT
#define N_TOTAL_PRINT 50
#endif
#ifndef MEMORY
#define MEMORY 8000000l
#endif
#define SIZE 10000
#define I_MAX 10000
#define ACTIONS_MAX 30
#ifndef TEST_FORK
#define TEST_FORK 0
#endif
#define RANDOM(d,s) (lran2(d) % (s))
struct bin_info {
struct bin *m;
unsigned long size, bins;
};
#if TEST > 0
void
bin_test(struct bin_info *p)
{
int b;
for(b=0; b<p->bins; b++) {
if(mem_check(p->m[b].ptr, p->m[b].size)) {
printf("memory corrupt!\n");
abort();
}
}
}
#endif
void
malloc_test(struct thread_st *st)
{
int b, i, j, actions, pid = 1;
struct bin_info p;
struct lran2_st ld; /* data for random number generator */
lran2_init(&ld, st->u.seed);
#if TEST_FORK>0
if(RANDOM(&ld, TEST_FORK) == 0) {
int status;
#if !USE_THR
pid = fork();
#else
pid = fork1();
#endif
if(pid > 0) {
/*printf("forked, waiting for %d...\n", pid);*/
waitpid(pid, &status, 0);
printf("done with %d...\n", pid);
if(!WIFEXITED(status)) {
printf("child term with signal %d\n", WTERMSIG(status));
exit(1);
}
return;
}
exit(0);
}
#endif
p.m = (struct bin *)malloc(st->u.bins*sizeof(*p.m));
p.bins = st->u.bins;
p.size = st->u.size;
for(b=0; b<p.bins; b++) {
p.m[b].size = 0;
p.m[b].ptr = NULL;
if(RANDOM(&ld, 2) == 0)
bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld));
}
for(i=0; i<=st->u.max;) {
#if TEST > 1
bin_test(&p);
#endif
actions = RANDOM(&ld, ACTIONS_MAX);
#if USE_MALLOC && MALLOC_DEBUG
if(actions < 2) { mallinfo(); }
#endif
for(j=0; j<actions; j++) {
b = RANDOM(&ld, p.bins);
bin_free(&p.m[b]);
}
i += actions;
actions = RANDOM(&ld, ACTIONS_MAX);
for(j=0; j<actions; j++) {
b = RANDOM(&ld, p.bins);
bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld));
#if TEST > 2
bin_test(&p);
#endif
}
#if 0 /* Test illegal free()s while setting MALLOC_CHECK_ */
for(j=0; j<8; j++) {
b = RANDOM(&ld, p.bins);
if(p.m[b].ptr) {
int offset = (RANDOM(&ld, 11) - 5)*8;
char *rogue = (char*)(p.m[b].ptr) + offset;
/*printf("p=%p rogue=%p\n", p.m[b].ptr, rogue);*/
free(rogue);
}
}
#endif
i += actions;
}
for(b=0; b<p.bins; b++)
bin_free(&p.m[b]);
free(p.m);
if(pid == 0)
exit(0);
}
int n_total=0, n_total_max=N_TOTAL, n_running;
int
my_end_thread(struct thread_st *st)
{
/* Thread st has finished. Start a new one. */
#if 0
printf("Thread %lx terminated.\n", (long)st->id);
#endif
if(n_total >= n_total_max) {
n_running--;
} else if(st->u.seed++, thread_create(st)) {
printf("Creating thread #%d failed.\n", n_total);
} else {
n_total++;
if(n_total%N_TOTAL_PRINT == 0)
printf("n_total = %d\n", n_total);
}
return 0;
}
#if 0
/* Protect address space for allocation of n threads by LinuxThreads. */
static void
protect_stack(int n)
{
char buf[2048*1024];
char* guard;
size_t guard_size = 2*2048*1024UL*(n+2);
buf[0] = '\0';
guard = (char*)(((unsigned long)buf - 4096)& ~4095UL) - guard_size;
printf("Setting up stack guard at %p\n", guard);
if(mmap(guard, guard_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,
-1, 0)
!= guard)
printf("failed!\n");
}
#endif
int
main(int argc, char *argv[])
{
int i, bins;
int n_thr=N_THREADS;
int i_max=I_MAX;
unsigned long size=SIZE;
struct thread_st *st;
#if USE_MALLOC && USE_STARTER==2
ptmalloc_init();
printf("ptmalloc_init\n");
#endif
if(argc > 1) n_total_max = atoi(argv[1]);
if(n_total_max < 1) n_thr = 1;
if(argc > 2) n_thr = atoi(argv[2]);
if(n_thr < 1) n_thr = 1;
if(n_thr > 100) n_thr = 100;
if(argc > 3) i_max = atoi(argv[3]);
if(argc > 4) size = atol(argv[4]);
if(size < 2) size = 2;
bins = MEMORY/(size*n_thr);
if(argc > 5) bins = atoi(argv[5]);
if(bins < 4) bins = 4;
/*protect_stack(n_thr);*/
thread_init();
printf("total=%d threads=%d i_max=%d size=%ld bins=%d\n",
n_total_max, n_thr, i_max, size, bins);
st = (struct thread_st *)malloc(n_thr*sizeof(*st));
if(!st) exit(-1);
#if !defined NO_THREADS && (defined __sun__ || defined sun)
/* I know of no other way to achieve proper concurrency with Solaris. */
thr_setconcurrency(n_thr);
#endif
/* Start all n_thr threads. */
for(i=0; i<n_thr; i++) {
st[i].u.bins = bins;
st[i].u.max = i_max;
st[i].u.size = size;
st[i].u.seed = ((long)i_max*size + i) ^ bins;
st[i].sp = 0;
st[i].func = malloc_test;
if(thread_create(&st[i])) {
printf("Creating thread #%d failed.\n", i);
n_thr = i;
break;
}
printf("Created thread %lx.\n", (long)st[i].id);
}
/* Start an extra thread so we don't run out of stacks. */
if(0) {
struct thread_st lst;
lst.u.bins = 10; lst.u.max = 20; lst.u.size = 8000; lst.u.seed = 8999;
lst.sp = 0;
lst.func = malloc_test;
if(thread_create(&lst)) {
printf("Creating thread #%d failed.\n", i);
} else {
wait_for_thread(&lst, 1, NULL);
}
}
for(n_running=n_total=n_thr; n_running>0;) {
wait_for_thread(st, n_thr, my_end_thread);
}
for(i=0; i<n_thr; i++) {
free(st[i].sp);
}
free(st);
#if USE_MALLOC
malloc_stats();
#endif
printf("Done.\n");
return 0;
}
/*
* Local variables:
* tab-width: 4
* End:
*/

231
extern/ptmalloc3/t-test2.c vendored Normal file
View File

@@ -0,0 +1,231 @@
/*
* $Id: t-test2.c,v 1.2 2006/03/27 16:06:20 wg Exp $
* by Wolfram Gloger 1996-1999, 2001, 2004
* A multi-thread test for malloc performance, maintaining a single
* global pool of allocated bins.
*/
#if (defined __STDC__ && __STDC__) || defined __cplusplus
# include <stdlib.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#if !USE_MALLOC
#include <malloc.h>
#else
#include "malloc-2.8.3.h"
#endif
#include "lran2.h"
#include "t-test.h"
struct user_data {
int max;
unsigned long size;
long seed;
};
#include "thread-st.h"
#include "malloc-machine.h" /* for mutex */
#define N_TOTAL 10
#ifndef N_THREADS
#define N_THREADS 2
#endif
#ifndef N_TOTAL_PRINT
#define N_TOTAL_PRINT 50
#endif
#define STACKSIZE 32768
#ifndef MEMORY
#define MEMORY 8000000l
#endif
#define SIZE 10000
#define I_MAX 10000
#define BINS_PER_BLOCK 20
#define RANDOM(d,s) (lran2(d) % (s))
struct block {
struct bin b[BINS_PER_BLOCK];
mutex_t mutex;
} *blocks;
int n_blocks;
#if TEST > 0
void
bin_test(void)
{
int b, i;
for(b=0; b<n_blocks; b++) {
mutex_lock(&blocks[b].mutex);
for(i=0; i<BINS_PER_BLOCK; i++) {
if(mem_check(blocks[b].b[i].ptr, blocks[b].b[i].size)) {
printf("memory corrupt!\n");
exit(1);
}
}
mutex_unlock(&blocks[b].mutex);
}
}
#endif
void
malloc_test(struct thread_st *st)
{
struct block *bl;
int i, b, r;
struct lran2_st ld; /* data for random number generator */
unsigned long rsize[BINS_PER_BLOCK];
int rnum[BINS_PER_BLOCK];
lran2_init(&ld, st->u.seed);
for(i=0; i<=st->u.max;) {
#if TEST > 1
bin_test();
#endif
bl = &blocks[RANDOM(&ld, n_blocks)];
r = RANDOM(&ld, 1024);
if(r < 200) { /* free only */
mutex_lock(&bl->mutex);
for(b=0; b<BINS_PER_BLOCK; b++)
bin_free(&bl->b[b]);
mutex_unlock(&bl->mutex);
i += BINS_PER_BLOCK;
} else { /* alloc/realloc */
/* Generate random numbers in advance. */
for(b=0; b<BINS_PER_BLOCK; b++) {
rsize[b] = RANDOM(&ld, st->u.size) + 1;
rnum[b] = lran2(&ld);
}
mutex_lock(&bl->mutex);
for(b=0; b<BINS_PER_BLOCK; b++)
bin_alloc(&bl->b[b], rsize[b], rnum[b]);
mutex_unlock(&bl->mutex);
i += BINS_PER_BLOCK;
}
#if TEST > 2
bin_test();
#endif
}
}
int n_total=0, n_total_max=N_TOTAL, n_running;
int
my_end_thread(struct thread_st *st)
{
/* Thread st has finished. Start a new one. */
#if 0
printf("Thread %lx terminated.\n", (long)st->id);
#endif
if(n_total >= n_total_max) {
n_running--;
} else if(st->u.seed++, thread_create(st)) {
printf("Creating thread #%d failed.\n", n_total);
} else {
n_total++;
if(n_total%N_TOTAL_PRINT == 0)
printf("n_total = %d\n", n_total);
}
return 0;
}
int
main(int argc, char *argv[])
{
int i, j, bins;
int n_thr=N_THREADS;
int i_max=I_MAX;
unsigned long size=SIZE;
struct thread_st *st;
#if USE_MALLOC && USE_STARTER==2
ptmalloc_init();
printf("ptmalloc_init\n");
#endif
if(argc > 1) n_total_max = atoi(argv[1]);
if(n_total_max < 1) n_thr = 1;
if(argc > 2) n_thr = atoi(argv[2]);
if(n_thr < 1) n_thr = 1;
if(n_thr > 100) n_thr = 100;
if(argc > 3) i_max = atoi(argv[3]);
if(argc > 4) size = atol(argv[4]);
if(size < 2) size = 2;
bins = MEMORY/size;
if(argc > 5) bins = atoi(argv[5]);
if(bins < BINS_PER_BLOCK) bins = BINS_PER_BLOCK;
n_blocks = bins/BINS_PER_BLOCK;
blocks = (struct block *)malloc(n_blocks*sizeof(*blocks));
if(!blocks)
exit(1);
thread_init();
printf("total=%d threads=%d i_max=%d size=%ld bins=%d\n",
n_total_max, n_thr, i_max, size, n_blocks*BINS_PER_BLOCK);
for(i=0; i<n_blocks; i++) {
mutex_init(&blocks[i].mutex);
for(j=0; j<BINS_PER_BLOCK; j++) blocks[i].b[j].size = 0;
}
st = (struct thread_st *)malloc(n_thr*sizeof(*st));
if(!st) exit(-1);
#if !defined NO_THREADS && (defined __sun__ || defined sun)
/* I know of no other way to achieve proper concurrency with Solaris. */
thr_setconcurrency(n_thr);
#endif
/* Start all n_thr threads. */
for(i=0; i<n_thr; i++) {
st[i].u.max = i_max;
st[i].u.size = size;
st[i].u.seed = ((long)i_max*size + i) ^ n_blocks;
st[i].sp = 0;
st[i].func = malloc_test;
if(thread_create(&st[i])) {
printf("Creating thread #%d failed.\n", i);
n_thr = i;
break;
}
printf("Created thread %lx.\n", (long)st[i].id);
}
for(n_running=n_total=n_thr; n_running>0;) {
wait_for_thread(st, n_thr, my_end_thread);
}
for(i=0; i<n_blocks; i++) {
for(j=0; j<BINS_PER_BLOCK; j++)
bin_free(&blocks[i].b[j]);
}
for(i=0; i<n_thr; i++) {
free(st[i].sp);
}
free(st);
free(blocks);
#if USE_MALLOC
malloc_stats();
#endif
printf("Done.\n");
return 0;
}
/*
* Local variables:
* tab-width: 4
* End:
*/

View File

@@ -0,0 +1,98 @@
/* Test for special independant_... allocation functions in ptmalloc3.
Contributed by Wolfram Gloger <wg@malloc.de>, 2006.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <stdio.h>
#include "malloc-2.8.3.h"
#define CSIZE 104
static int errors = 0;
static void
merror (const char *msg)
{
++errors;
printf ("Error: %s\n", msg);
}
int
main (void)
{
char *p1, **p2, **p3;
int i, j;
size_t sizes[10] = { 8, 104, 12, 333, 7, 4098, 119, 331, 1, 707 };
printf("---- Start:\n");
malloc_stats();
p1 = malloc (18);
if (p1 == NULL)
merror ("malloc (10) failed.");
p2 = (char **)independent_calloc (25, CSIZE, 0);
if (p2 == NULL)
merror ("independent_calloc (25, ...) failed.");
p3 = (char **)independent_comalloc(10, sizes, 0);
if (p3 == NULL)
merror ("independent_comalloc (10, ...) failed.");
for (i=0; i<25; ++i) {
for (j=0; j<CSIZE; ++j)
if (p2[i][j] != '\0')
merror("independent_calloc memory not zeroed.");
p2[i][CSIZE-1] = 'x';
if (i < 10) {
p3[i][sizes[i]-1] = 'y';
if (i % 4 == 0) {
p3[i] = realloc (p3[i], i*11 + sizes[9-i]);
if (p3[i] == NULL)
merror ("realloc (i*11 + sizes[9-i]) failed.");
} else if (i % 4 == 1) {
free(p3[i]);
p3[i] = 0;
}
}
if (i % 4 == 0) {
p2[i] = realloc (p2[i], i*7 + 3);
if (p2[i] == NULL)
merror ("realloc (i*7 + 3) failed.");
} else if (i % 4 == 1) {
free(p2[i]);
p2[i] = 0;
}
}
printf("---- Before free:\n");
malloc_stats();
free(p1);
for (i=0; i<10; ++i)
free(p3[i]);
free(p3);
for (i=0; i<25; ++i)
free(p2[i]);
free(p2);
printf("---- After free:\n");
malloc_stats();
return errors != 0;
}
/*
* Local variables:
* c-basic-offset: 2
* End:
*/