📄 readme
字号:
Copyright 1988, 1989 Hans-J. Boehm, Alan J. DemersThis material may be freely distributed, provided this notice is retained.This material is provided as is, with no warranty expressed or implied.Use at your own risk. This collector was developed as a part of research projects supported inpart by the National Science Foundation and the Defense Advance ResearchProjects Agency. The SPARC specific code was contributed by Mark Weiser(weiser.pa@xerox.com). The Encore Multimax modifications were supplied byKevin Kenny (kenny@m.cs.uiuc.edu). The adaptation to the RT is largely dueto Vernon Lee, on machines made available by IBM. (Blame for misinstallationof those modifications goes to the first author, however.) Some of theimprovements incorporated in this version were suggested by David Chase atOlivetti Research. This is intended to be a general purpose, garbage collecting storageallocator. The algorithms used are described in:Boehm, H., and M. Weiser, "Garbage Collection in an Uncooperative Environment",Software Practice & Experience, September 1988, pp. 807-820. Many of the ideas underlying the collector have previously been exploredby others. (We discovered recently that Doug McIlroy wrote a more or lesssimilar collector that is part of version 8 UNIX (tm).) However none of thiswork appears to have been widely disseminated. The tools for detecting storage leaks described in the above paperare not included here. There is some hope that they might be releasedby Xerox in the future. Since the collector does not require pointers to be tagged, it does notattempt to insure that all inaccessible storage is reclaimed. However,in our experience, it is typically more successful at reclaiming unusedmemory than most C programs using explicit deallocation. In the following, an "object" is defined to be a region of memory allocatedby the routines described below. Any objects not intended to be collected must be pointed to eitherfrom other such accessible objects, or from the registers,stack, data, or statically allocated bss segments. It is usually assumedthat all such pointers point to the beginning of the object. (This doesnot disallow interior pointers; it simply requires that there must be apointer to the beginning of every accessible object, in addition to anyinterior pointers. Conditionally compiled code to check for pointers to theinteriors of objects is supplied. As explained in "runtime.h", thismay create other problems, however.) Note that pointers inside memory allocated by the standard "malloc" are notseen by the garbage collector. Thus objects pointed to only from such aregion may be prematurely deallocated. It is thus suggested that thestandard "malloc" be used only for memory regions, such as I/O buffers, thatare guaranteed not to contain pointers. Pointers in C language automatic,static, or register variables, are correctly recognized. The collector is designed to minimize stack growth if link fields insidestructures are allocated first. (Normally only linked lists of lengthsexceeding about 100000 will cause this to be noticable.) Signal processing for most signals is deferred during collection. (Thisis not done on the MIPS machine under System V, where this seem to requiremany system calls. If signal handling is desired, the user will probablyfind it necessary to suspend those signals that are actually used.) As distributed, the collector produces garbage collection statisticsduring every collection. Once the collector is known to operate properly,these can be suppressed by undefining the appropriate macros at the topof "runtime.h". (The given statistics exhibit a few peculiarities.Things don't appear to add up for a variety of reasons, most notablyfragmentation losses. These are probably much more significant for thecontrived program "test.c" than for your application.) The collector currently is designed to run essentially unmodified onthe following machines: Sun 3 Sun 4 (except under some versions of 3.2) Vax under Berkeley UNIX Sequent Symmetry (no concurrency) Encore Multimax (no concurrency) MIPS M/120 (and presumably M/2000) (System V) IBM PC/RT (Berkeley UNIX) For these machines you should check the beginning of runtime.hto verify that the machine type is correctly defined. On an Encore Multimax,MIPS M/120, or a PC/RT, you will also need to make changes to theMakefile, as described by comments there. In all cases we assume that pointer alignment is consistent with thatenforced by the standard C compilers. If you use a nonstandard compileryou may have to adjust the alignment parameters defined in runtime.h. On a MIPS machine or PC/RT, we assume that no calls to sbrk occur during acollection. (This is necessary due to the way stack expansion works on thesemachines.) This may become false if certain kinds of I/O calls are insertedinto the collector. For machines not already mentioned, the following are likely to requirechange:1. The parameters at the top of runtime.h and the definition of TMP_POINTER_MASK further down in the same file.2. mach_dep.c. This includes routines to mark from registers, and to save registers not normally preserved by the C compiler. (The latter should not be necessary unless assembly language calls to the allocator are used.) If your machine does not allow in-line assembly code, this may be replaced by a .s file (as we did for the MIPS machine and the PC/RT). For a different UN*X version or different machine using the Motorola 68000,Vax, SPARC, 80386, NS 32000, PC/RT, or MIPS architecture, it should frequentlysuffice to change definitions in runtime.h. The following routines are intended to be directly called by the user.Note that only gc_malloc and gc_init are necessary. The remaining routinesare used solely to enhance performance. It is suggested that they be usedonly after initial debugging.1) gc_init() - called once before allocation to initialize the collector.2) gc_malloc(nbytes) - allocate an object of size nbytes. Unlike malloc, the object is cleared before being returned to the user. (For even better performance, it may help to expand the relevant part of gc_malloc in line. This is done by the Russell compiler, for example.) Gc_malloc will invoke the garbage collector when it determines this to be appropriate. (A number of previous collector bugs resulted in objects not getting completely cleared. We claim these are all fixed. But if you encounter problems, this is a likely source to check for. The collector tries hard to avoid clearing any words that it doesn't have to. Thus this is a bit subtle.) 3) gc_malloc_atomic(nbytes) - allocate an object of size nbytes that is guaranteed not to contain any pointers. The returned object is not guaranteed to be cleeared. (Can always be replaced by gc_malloc, but results in faster collection times. The collector will probably run faster if large character arrays, etc. are allocated with gc_malloc_atomic than if they are statically allocated.)4) gc_free(object) - explicitly deallocate an object returned by gc_malloc or gc_malloc_atomic. Not necessary, but can be used to minimize collections if performance is critical.5) expand_hp(number_of_4K_blocks) - Explicitly increase the heap size. (This is normally done automatically if a garbage collection failed to reclaim enough memory. Explicit calls to expand_hp may prevent unnecessarily frequent collections at program startup.) The global variable dont_gc can be set to a non-zero value to inhibitcollections, e.g. during a time-critical section of code. (This may causeotherwise unnecessary exansion of the process' memory.) The variable non_gc_bytes, which is normally 0, may be changed to reflectthe amount of memory allocated by the above routines that should not beconsidered as a candidate for collection. Collections are inhibitedif this exceeds a given fraction (currently 3/4) of the total heap size.The heap is simply expanded instead. Careless use may, of course, resultin excessive memory consumption. Some additional tuning is possible through the parameters definednear the top of runtime.h. The two gc_malloc routines may be declared to return a suitable pointertype. It is not intended that runtime.h be included by the user program.If only gc_malloc is intended to be used, it might be appropriate to define:#define malloc(n) gc_malloc(n)#define calloc(m,n) gc_malloc((m)*(n)) No attempt is made to use obscure names for garbage collector routinesand data structures. Name conflicts are possible. (Running "nm gc.o"should identify names to be avoided.) Please address bug reports to boehm@rice.edu.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -