⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readme

📁 这是一个内存分配管理的程序
💻
字号:
ptmalloc - a multi-thread malloc implementation===============================================Wolfram Gloger (wg@malloc.de)19 Dec 1999Introduction============ptmalloc.c is a modified version of Doug Lea's malloc-2.6.4implementation (available seperately from ftp://g.oswego.edu/pub/misc)that I adapted for multiple threads, while trying to avoid lockcontention as much as possible.  Many thanks should go to Doug Lea(dl@cs.oswego.edu) for the great original malloc implementation.As part of the GNU C library, the source files are available under theGNU Library General Public License (see the comments in the files).But as part of this stand-alone package, the code is available underthe (probably less restrictive) conditions described in the file`COPYRIGHT'.  In any case, there is no warranty whatsoever for thispackage.Compilation and usage=====================It should be possible to compile ptmalloc.c on any UN*X-like systemthat implements the sbrk(), mmap(), munmap() and mprotect() calls.  Ifmmap() is not available, it is only possible to produce anon-threadsafe implementation from the source file.  See the commentsin the source file for descriptions of the compile-time options.Several thread interfaces are supported: o Posix threads (pthreads), compile with `-DUSE_PTHREADS=1'   (and possibly with `-DUSE_TSD_DATA_HACK', see below) o Solaris threads, compile with `-DUSE_THR=1' o SGI sproc() threads, compile with `-DUSE_SPROC=1' o When compiling ptmalloc.c as part of the GNU C library,   i.e. when _LIBC is defined (no other defines necessary) o no threads, compile without any of the above definitionsThe distributed Makefile includes several targets (e.g. `solaris' forSolaris threads, but you probably want `posix' for recent Solarisversions) which cause ptmalloc.c to be compiled with the appropriateflags.  The default is to compile for Posix threads.  Some additionaltargets, ending in `-libc', are also provided, to compare performanceof the test programs to the case when linking with the standard mallocimplementation in libc.A potential problem remains: If any of the system-specific functionsfor getting/setting thread-specific data or for locking a mutex callone of the malloc-related functions internally, the implementationcannot work at all due to infinite recursion.  One example seems to beSolaris 2.4; a workaround for thr_getspecific() has been inserted intothe thread-m.h file.  I would like to hear if this problem occurs onother 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 coursenot generally guaranteed).  USE_TSD_DATA_HACK is now the defaultbecause I haven't yet found a non-glibc pthreads system where thishack is _not_ needed.To use ptmalloc (i.e. when linking ptmalloc.o into applications), nospecial precautions are necessary except calling an initializationroutine, ptmalloc_init(), once before the first call to malloc() (orcalloc(), etc.).  This call happens automatically when: o compiling ptmalloc with MALLOC_HOOKS defined (this is the default   when using the supplied Makefile) o using the GNU C librarySo in any of these cases, you can omit the explicit ptmalloc_init()call from applications using ptmalloc.o.On some systems, when overriding malloc and linking against sharedlibraries, the link order becomes very important.  E.g., when linkingC++ programs on Solaris, don't rely on libC being included by default,but instead put `-lthread' behind `-lC' on the command line:  CC ... ptmalloc.o -lC -lthreadThis is because there are global constructors in libC that needmalloc/ptmalloc, which in turn needs to have the thread library to bealready initialized.Debugging hooks===============When the ptmalloc.c source is compiled with MALLOC_HOOKS defined (thisis recommended), all calls to malloc(), realloc(), free() andmemalign() are routed through the global function pointers__malloc_hook, __realloc_hook, __free_hook and __memalign_hook if theyare not NULL (see the ptmalloc.h header file for declarations of thesepointers).  Therefore the malloc implementation can be changed atruntime, if care is taken not to call free() or realloc() on pointersobtained with a different implementation than the one currently ineffect.  (The easiest way to guarantee this is to set up the hooksbefore any malloc call, e.g.  with a function pointed to by the globalvariable __malloc_initialize_hook).A useful application of the hooks is built-in into ptmalloc: Theimplementation is usually very unforgiving with respect to misuse,such as free()ing a pointer twice or free()ing a pointer not obtainedwith malloc() (these will typically crash the applicationimmediately).  To debug in such situations, you can set theenvironment variable `MALLOC_CHECK_' (note the trailing underscore).Performance will suffer somewhat, but you will get more controlledbehaviour in the case of misuse.  If MALLOC_CHECK_=0, wrong free()swill be silently ignored, if MALLOC_CHECK_=1, diagnostics will beprinted on stderr, and if MALLOC_CHECK_=2, abort() will be called onany error.You can now also tune other malloc parameters (normally adjused viamallopt() calls from the application) with environment variables:    MALLOC_TRIM_THRESHOLD_    for deciding to shrink the heap (in bytes)    MALLOC_TOP_PAD_           how much extra memory to allocate on                              each system call (in bytes)    MALLOC_MMAP_THRESHOLD_    min. size for chunks allocated via                              mmap() (in bytes)    MALLOC_MMAP_MAX_          max. number of mmapped regions to useTests=====Two testing applications, t-test1 and t-test2, are included in thissource distribution.  Both perform pseudo-random sequences ofallocations/frees, and can be given numeric arguments (all argumentsare 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 maintainThe first test `t-test1' maintains a completely seperate pool ofallocated bins for each thread, and should therefore show fullparallelism.  On the other hand, `t-test2' creates only a single poolof bins, and each thread randomly allocates/frees any bin.  Some lockcontention is to be expected in this case, as the threads frequentlycross each others arena.Performance results from t-test1 should be quite repeatable, while thebehaviour of t-test2 depends on scheduling variations.Some performance data from t-test1==================================The times given are complete program execution times, obtained with`time t-test1 ...'.1. SGI Octane, one R12000 300MHz CPU, Irix 6.5, `sproc' threads:20 threads (4 in parallel), 3000000 malloc calls per thread, max. size5000 bytes, 5000 bins:ptmalloc:                       malloc from libc:real    3m0.521s                real    30m27.240suser    2m45.336s               user    10m7.592ssys     0m3.014s                sys     17m6.502s2. Same as 1., but with POSIX threads:20 threads (4 in parallel), 3000000 malloc calls per thread, max. size5000 bytes, 5000 bins:ptmalloc:                       malloc from libc:real    3m10.667s               real    5m51.588suser    2m57.052s               user    5m27.986ssys     0m2.399s                sys     0m3.098s(Comparing the two ptmalloc results probably shows the slightperformance penalty from having to compile with USE_TSD_DATA_HACK whenusing pthreads on Irix.)Special section on use of ptmalloc with Linux=============================================On Linux, ptmalloc should work with the libpthreads library that isincluded with Linux libc-5.x (but this is untested).  Thanks to theefforts of H.J. Lu and Ulrich Drepper, it is now an integral part ofthe GNU C library 2.x releases (libc-6.x), so you don't need tocompile and link ptmalloc.o with glibc.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -