PageRenderTime 85ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/kits/scc/heap.h

http://github.com/pablomarx/Thomas
C Header | 404 lines | 96 code | 50 blank | 258 comment | 11 complexity | 7442ba87fb8ec913e8503d300275f85a MD5 | raw file
  1. /* SCHEME->C */
  2. /* Copyright 1989 Digital Equipment Corporation
  3. * All Rights Reserved
  4. *
  5. * Permission to use, copy, and modify this software and its documentation is
  6. * hereby granted only under the following terms and conditions. Both the
  7. * above copyright notice and this permission notice must appear in all copies
  8. * of the software, derivative works or modified versions, and any portions
  9. * thereof, and both notices must appear in supporting documentation.
  10. *
  11. * Users of this software agree to the terms and conditions set forth herein,
  12. * and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  13. * right and license under any changes, enhancements or extensions made to the
  14. * core functions of the software, including but not limited to those affording
  15. * compatibility with other hardware or software environments, but excluding
  16. * applications which incorporate this software. Users further agree to use
  17. * their best efforts to return to Digital any such changes, enhancements or
  18. * extensions that they make and inform Digital of noteworthy uses of this
  19. * software. Correspondence should be provided to Digital at:
  20. *
  21. * Director of Licensing
  22. * Western Research Laboratory
  23. * Digital Equipment Corporation
  24. * 250 University Avenue
  25. * Palo Alto, California 94301
  26. *
  27. * This software may be distributed (but not offered for sale or transferred
  28. * for compensation) to third parties, provided such third parties agree to
  29. * abide by the terms and conditions of this notice.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  32. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  34. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  35. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  36. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  37. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  38. * SOFTWARE.
  39. */
  40. /* Import definitions */
  41. #ifndef rusage
  42. #include <time.h>
  43. #include <sys/resource.h>
  44. #endif
  45. /* This module implements the object storage storage system for SCHEME->C.
  46. Unlike most Lisp systems, it is not intended that SCHEME->C provide a
  47. "one language" environment divorced from other programming languages.
  48. Instead, it is intended that SCHEME->C co-exist with other languages
  49. and share their development tools and runtime environment.
  50. Nor, is it intended that SCHEME->C have detailed knowledge and intimate
  51. control over the hardware. Instead of generating actual instructions,
  52. it generates C intermediate language.
  53. By adhering to these two design goals, SCHEME->C can be a powerful tool
  54. for delivering Lisp based technology to non-Lisp environments. However,
  55. these design goals also place some significant constraints on the design
  56. of the storage system. For example:
  57. 1. The system must tolerate both Scheme and non-Scheme storage and
  58. data types.
  59. 2. The system will not have any control over register allocation or
  60. instruction sequences.
  61. 3. In examining register or stack contents, one can make a statement that
  62. something is not a pointer, but one cannot say that something is a
  63. pointer. At best, one can say that it looks exactly like a pointer.
  64. Given these constraints, a conventional "stop-and-copy" garbage collector
  65. cannot be used. Instead, a storage allocation method called
  66. "mostly-copying" is used (see WRL Research Report 88/2, Compacting Garbage
  67. Collection with Ambiguous Roots).
  68. In its simplist form, the algorithm works as follows. The heap is divided
  69. into pages (which need not be the same size as the processor's page).
  70. Objects are allocated entirely within a page, or in a dedicated set of
  71. pages. When half the storage in the heap has been allocated, the garbage
  72. collector is invoked.
  73. Garbage collection is divided into three phases. The first is the a copy
  74. phase similar to that of the Minsky-Fenichel-Yochelson-Chaeny-Arnborg
  75. collector. Items will be copied from the oldspace (pages in the current
  76. generation) to the newspace (pages in the next generation). Indirect
  77. pointers to new objects will be placed in the old objects, but pointers
  78. to new objects are never stored in new objects.
  79. During this phase, the contents of continuations (including the current
  80. continuation which is in the registers and the stack) get special
  81. processing. Each word in them is examined to see if it might be a pointer.
  82. If it is a pointer, then the object that it points to is copied, and the
  83. page is marked as locked.
  84. Thus at the end of the phase, all accessibile storage has been copied, and
  85. all pointers are indirect through the old space. All pages which have items
  86. which must be left in place are marked as locked.
  87. The next phase is the correction phase which turns all indirect pointers
  88. into their correct values. At the end of this phase, all pointers will
  89. point to the correct place, but items which were locked will be located in
  90. the newspace.
  91. The final phase is the copy back phase where items that are locked are
  92. copied back from the newspace to their correct position in the locked
  93. pages. At this time, locked pages are unlocked and promoted to newspace.
  94. At this point, garbage collection is done and the generation number is
  95. advanced. As with the classical "stop-and-copy" algorithm, the time used
  96. is proportional to the amount of storage retained, rather than the total
  97. amount of storage. It needs somewhat more storage as it must retain locked
  98. pages, and has duplicate copies of items of locked pages.
  99. In order to avoid repeated copying of retained data, the collector
  100. implements a generational version of the algorithm. Objects that survive
  101. a collection are retained and not moved until more than SCLIMIT of the heap
  102. is allocated following a collection. At this point, the entire heap is
  103. collected (see WRL Technical Note TN-12, Mostly-Copying Garbage Collection
  104. Picks Up Generations and C++).
  105. A few simple changes to the previously described algorithm result in a
  106. generational collector. Even generation numbers represent retained storage
  107. and storage is always allocated out of an odd numbered generation when the
  108. user program is executing. During garbage collection, all retained
  109. objects in the odd generation are copied into a new even numbered space.
  110. During this copy phase, pointers into an object in an even numbered space
  111. need not be followed. A total collection is done by changing the space
  112. number on all even numbered pages to the current odd generation and then
  113. doing a collection.
  114. In order for a generational scheme to work, all stores of pointers to new
  115. objects in old pages must be detected. This is done by explicit checks
  116. in: SET-CAR!, SET-CDR!, VECTOR-SET!, SET!, SET-TOP-LEVEL-VALUE!, PUTPROP,
  117. and SCHEME-TSCP-SET!. While at first glance, explicit checks seem a slow
  118. way of doing things, the reduction in copying more than makes up for them.
  119. The garbage collector may be configured by the user setting any of the
  120. following environment variables:
  121. name: range: default: action:
  122. SCHEAP [1:1000] 8 Number of megabytes to
  123. initially allocate for the heap
  124. (total).
  125. SCMAXHEAP [SCHEAP:1000] SCHEAP Number of megabytes to allow
  126. the heap to grow to.
  127. SCLIMIT [10:45] 33 Cause of total collection of
  128. the heap when more than this %
  129. of the heap is allocated
  130. following a generational
  131. collection.
  132. SCGCINFO [0:2] 0 C boolean indicating that
  133. garbage collection statistics
  134. should be printed on stderr.
  135. When set to 2, additional
  136. debugging information is
  137. printed and additional tests
  138. are done.
  139. */
  140. /* Page related definitions. The page size is defined as a power of 2, where
  141. 2**PAGEPOWER = PAGEBYTES.
  142. */
  143. #define PAGEPOWER 9 /* 512 bytes/page */
  144. #define PAGEBYTES (1<<PAGEPOWER)
  145. #define PAGEWORDS (PAGEBYTES/4)
  146. #define ONEMB 1048576
  147. #define PAGEBIT PAGEPOWER
  148. #define PAGEBITLEN (32-PAGEPOWER)
  149. /* Page number to address conversion is handled by the following defines */
  150. #define ADDRESS_PAGE( adr ) ((int) (((unsigned)(adr)) >> PAGEBIT))
  151. #define PAGE_ADDRESS( page ) ((page) << PAGEBIT)
  152. #define ADDRESS_OFFSET( adr ) (((int)(adr)) & (PAGEBYTES-1))
  153. /* Each page in the pool has the following flags associated with it:
  154. PAGEGENERATION generation number associated with the page. Even
  155. numbered generations are objects that survived a
  156. garbage collection. Odd numbered generations are
  157. where storage is allocated during the execution of the
  158. user's program. A zero value indicates that the page
  159. is not available for garbage collection.
  160. PAGETYPE tag field indicating the type of data stored in the
  161. page. It is either PAIRTAG, EXTENDEDTAG, or
  162. BIGEXTENDEDTAG.
  163. PAGELOCK boolean indicating whether or not the page is locked
  164. by the garbage collector.
  165. PAGELINK next page (or 0) of the lock list whose head is kept in
  166. LOCKLIST, and length in LOCKCNT (only during gc).
  167. -or-
  168. OKTOSET or ~OKTOSET (-1 or 0) indicating status of
  169. a just allocated page (value of INITIALLINK).
  170. -or-
  171. next page (or -1) of the GENLIST, whose head is kept
  172. in GENLIST. This list contains all pages in older
  173. generations that might contain a pointer to a newer
  174. generation.
  175. If this value is non-zero, then it is possible to set
  176. pointers in the page without going through
  177. sc_setgeneration.
  178. It is possible to pack these fields into 1-2 words, but this has not been
  179. done.
  180. Objects which are longer than one page are allocated on an integral number
  181. of pages. Pages other than the head are marked with a BIGEXTENDEDTAG in
  182. pagetype field to indicate that they are related to the previous page.
  183. CURRENT_GENERATION holds the generation number that is presently being
  184. allocated. NEXT_GENERATION holds the obvious during garbage collection.
  185. N.B.: There may be pages in the range sc_firstheappage through
  186. sc_lastheappage that are not managed by the garbage collector. This
  187. is why the 0 flag is used in PAGEGENERATION.
  188. */
  189. extern int *sc_pagegeneration,
  190. *sc_pagetype,
  191. *sc_pagelock,
  192. *sc_pagelink,
  193. sc_initiallink,
  194. sc_locklist,
  195. sc_genlist,
  196. sc_lockcnt,
  197. sc_current_generation,
  198. sc_next_generation;
  199. #define INC_GENERATION( g ) (g + 1) /* 1 collection/second will take over 32
  200. years to overflow g */
  201. #define NEXTPAGE( page ) ((page==sc_lastheappage) ? sc_firstheappage : page+1)
  202. #define BIGEXTENDEDTAG -1
  203. #define OKTOSET -1
  204. #define S2CPAGE( p ) (p >= sc_firstheappage && p <= sc_lastheappage && \
  205. sc_pagegeneration[ p ] != 0)
  206. #define NOT_S2CPAGE( p ) (p < sc_firstheappage || p > sc_lastheappage || \
  207. sc_pagegeneration[ p ] == 0)
  208. extern int sc_firstheappage, /* first page in the Scheme heap */
  209. sc_lastheappage, /* last page in the Scheme heap */
  210. sc_limit, /* % of heap allocated after collecton
  211. that forces total collection */
  212. sc_freepage, /* Free page index */
  213. sc_heappages, /* # of pages in the Scheme heap */
  214. sc_maxheappages, /* Maximum # of pages in Scheme heap */
  215. sc_allocatedheappages, /* # of pages currently allocated */
  216. *sc_firstheapp, /* ptr to first word in the heap */
  217. *sc_lastheapp; /* ptr to last word in the heap */
  218. /* In order to speed up allocation of CONS cells, blocks of potential CONS
  219. cells are preallocated. CONSP points to the next free cell, and CONSCNT
  220. is the number of free cells left.
  221. */
  222. extern int sc_conscnt;
  223. extern SCP sc_consp;
  224. /* In order to speed up allocation of extended objects, EXTOBJWORDS is the
  225. number of words available in the current page pointed to by EXTOBJP.
  226. EXTWASTE keeps track of the number of words lost because of page alignment
  227. problems. When only a part of the page is used, the first unused word is
  228. marked with ENDOFPAGE.
  229. */
  230. extern int sc_extobjwords,
  231. sc_extwaste;
  232. extern SCP sc_extobjp;
  233. #define ENDOFPAGE 0xAAAAAAAA
  234. /* Some implementations require extended storage always be allocated so that
  235. double objects in it are on double word boundaries (addr mod 8 = 0). This
  236. is handled by the following define that is used to force pointer alignment.
  237. */
  238. #ifdef DOUBLE_ALIGN
  239. #define ODD_EXTOBJP( e ) if ((e) && sc_extobjwords &&\
  240. (sc_extobjwords & 1) == 0) {\
  241. sc_extobjp->unsi.gned = WORDALIGNTAG;\
  242. sc_extobjp = (SCP)(((int*)sc_extobjp)+1);\
  243. sc_extobjwords = sc_extobjwords-1;\
  244. }
  245. #define EVEN_EXTOBJP( e ) if ((e) && sc_extobjwords & 1) {\
  246. sc_extobjp->unsi.gned = WORDALIGNTAG;\
  247. sc_extobjp = (SCP)(((int*)sc_extobjp)+1);\
  248. sc_extobjwords = sc_extobjwords-1;\
  249. }
  250. #endif
  251. #ifndef DOUBLE_ALIGN
  252. #define ODD_EXTOBJP( e )
  253. #define EVEN_EXTOBJP( e )
  254. #endif
  255. /* A running total of garbage collection resource usage in kept in GCRU.
  256. Garbage collection statistics are printed on stderr following each
  257. collection when SCGCINFO is true (set by the environment variable SCGCINFO,
  258. or by the command line flag -scgc, default = 0).
  259. */
  260. extern int sc_gcinfo;
  261. /* Garbage collection and call-with-current-continuation need to know the
  262. base of the stack, i.e. the value of the stack pointer when the stack is
  263. empty. It is computed at initialization time and stored in SC_STACKBASE.
  264. STACKPTR is the address of the current top of stack.
  265. */
  266. extern int *sc_stackbase;
  267. #ifdef MIPS
  268. #define STACKPTR sc_processor_register( 29 )
  269. #endif
  270. #ifdef TITAN
  271. extern int *zzReadRegister();
  272. #define STACKPTR (zzReadRegister( 63 )+1)
  273. #endif
  274. #ifdef VAX
  275. #define STACKPTR sc_processor_register( 14 )
  276. #endif
  277. /* Some objects require cleanup actions when they are freed. For example,
  278. when a file port is recovered, the corresponding file needs to be closed.
  279. Such objects are noted by the procedure (WHEN-UNREFERENCED object action),
  280. where object is any Scheme object and action is either #F indicating that
  281. nothing should be done, or a procedure that takes one argument. When a
  282. procedure is supplied, it will be called when a garbage collection occurs
  283. and there are no references to that object. In order to implement this
  284. function, the runtime system will keep two alists, SC_WHENFREED and
  285. SC_FREED. The first list is those items requiring cleanup when they
  286. become free, and the second list is those items freed that require
  287. cleanup now.
  288. */
  289. extern TSCP sc_whenfreed,
  290. sc_freed;
  291. /* A Scheme program can register a callback with the garbage collector that
  292. will be called following each collection. This is done by setting the
  293. value of AFTER-COLLECT to a procedure that takes three arguments: the
  294. heap size in bytes, the current allocation in bytes, and the percent of
  295. allocation that will force a total collection.
  296. */
  297. extern TSCP sc_after_2dcollect_v;
  298. /* Objects on the *FROZEN-OBJECTS* list are never moved by the garbage
  299. collector. User programs can use this to "lock" objects that are passed to
  300. other languages.
  301. */
  302. extern TSCP sc__2afrozen_2dobjects_2a_v;
  303. /* The procedural interfaces to this module are: */
  304. extern int *sc_processor_register();
  305. extern TSCP sc_my_2drusage_v;
  306. extern TSCP sc_my_2drusage();
  307. extern TSCP sc_collect_2drusage_v;
  308. extern TSCP sc_collect_2drusage();
  309. extern TSCP sc_collect_v;
  310. extern TSCP sc_collect();
  311. extern TSCP sc_collect_2dall_v;
  312. extern TSCP sc_collect_2dall();
  313. extern TSCP sc_setgeneration();
  314. extern SCP sc_allocateheap();
  315. extern TSCP sc_makefloat32();
  316. extern TSCP sc_makefloat64();
  317. extern TSCP sc_cons_v;
  318. extern TSCP sc_cons();
  319. extern TSCP sc_weak_2dcons_v;
  320. extern TSCP sc_weak_2dcons();