gc.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921
  1. /* File gc.c Copyright (c) Codemist Ltd, 1990-2003 */
  2. /*
  3. * Garbage collection.
  4. * Fourth major version - now using Foster-style
  5. * algorithm for relocating vector heap, and support for handling
  6. * BPS via segmented handles. Pointer-reversing mark phase to go
  7. * with same.
  8. *
  9. * Furthermore there is (optionally) a copying 2-space garbage
  10. * collector as well as the mark/slide one. Now do you understand
  11. * why this file seems so very long?
  12. *
  13. * The code in parts of this file (and also in preserve.c & restart.c)
  14. * is painfully sensitive to memory layout and I have some messy
  15. * conditional inclusion of code depending on whether a Lisp_Object is
  16. * a 32 or 64-bit value.
  17. */
  18. /*
  19. * This code may be used and modified, and redistributed in binary
  20. * or source form, subject to the "CCL Public License", which should
  21. * accompany it. This license is a variant on the BSD license, and thus
  22. * permits use of code derived from this in either open and commercial
  23. * projects: but it does require that updates to this code be made
  24. * available back to the originators of the package.
  25. * Before merging other code in with this or linking this code
  26. * with other packages or libraries please check that the license terms
  27. * of the other material are compatible with those of this.
  28. */
  29. /* Signature: 0651f4cb 13-Jul-2003 */
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <math.h>
  34. #include "machine.h"
  35. #include "tags.h"
  36. #include "cslerror.h"
  37. #include "externs.h"
  38. #include "arith.h"
  39. #include "entries.h"
  40. #include "sys.h"
  41. #include "stream.h"
  42. #ifdef SOCKETS
  43. #include "sockhdr.h"
  44. #endif
  45. #ifdef TIMEOUT
  46. #include "timeout.h"
  47. #endif
  48. /*
  49. * This function is provided so that if some C compiler generates bad
  50. * code and I can not see a better way of correcting things, and if the
  51. * bad code goes away when there are lots of function calls to cause the
  52. * compiler to unload its registers etc, then I can write things like
  53. * dummy_function_call("Ha Ha", reference_this_var, and_this);
  54. * without it having any big effect on the results I obtain. I will
  55. * generally want to guard any such un-natural calls by a test that identifies
  56. * the C compiler involved. Of course I must try not to use this as a way
  57. * of permitting system-sensitive code to creep into CSL...
  58. */
  59. void MS_CDECL dummy_function_call(char *why, ...)
  60. {
  61. CSL_IGNORE(why);
  62. return;
  63. }
  64. CSLbool gc_method_is_copying; /* YES if copying, NO if sliding */
  65. int32 gc_number = 0;
  66. static intxx cons_cells, symbol_heads, strings, user_vectors,
  67. big_numbers, box_floats, bytestreams, other_mem,
  68. litvecs, getvecs;
  69. #define is_immed(x) (is_immed_or_cons(x) && !is_cons(x))
  70. #ifndef DEMO_MODE
  71. static void non_recursive_mark(Lisp_Object *top)
  72. {
  73. /*
  74. * This code is written about as neatly as I know how ... I want to think of
  75. * it in terms of three chunks - descending down into lists, regular
  76. * climbing back out, and the special case of climbing back out when I have
  77. * just processed a vector. I like to think of this as a finite state
  78. * machine with three major groups of states, and a bunch of subsidiary
  79. * states that deal with (e.g.) scanning along vectors.
  80. */
  81. Lisp_Object b = (Lisp_Object)top,
  82. p = *top,
  83. w,
  84. nil = C_nil;
  85. Header h, *q;
  86. intxx i;
  87. /*
  88. * When I get to descend I have b as an unmarked address that is either
  89. * equal to top, or is a back-pointer as set up below. p is a normal
  90. * (i.e. unmarked) Lisp pointer, representing a tree to be marked. Only
  91. * at the very top of a call can p be immediate data at descend, and in that
  92. * case no marking is needed.
  93. * NB that the initial back pointer will seem tagged as either a CONS or a
  94. * SYMBOL, but certainly as a pointer datatype.
  95. */
  96. descend:
  97. switch ((int)p & TAG_BITS)
  98. {
  99. /*
  100. * If I have a cons cell I need to check if it has been visited before or
  101. * if one or both components are immediate - and extend my backwards
  102. * chain one step.
  103. */
  104. case TAG_CONS:
  105. #ifdef COMMON
  106. if (p == nil) goto ascend;
  107. #endif
  108. w = qcar(p);
  109. if (is_immed(w))
  110. { if (is_marked_i(w)) goto ascend;
  111. /*
  112. * To test if this cons cell was marked I had to classify the item
  113. * in its car, and if this was immediate data it makes sense to go
  114. * right ahead and probe the cdr.
  115. */
  116. qcar(p) = flip_mark_bit_i(w);
  117. w = qcdr(p);
  118. /*
  119. * Since I am not allowing myself to descend onto immediate data
  120. * I check for it here, and if both car and cdr of p were immediate
  121. * I can ascend forthwith.
  122. */
  123. if (is_immed(w) || w == nil) goto ascend;
  124. /*
  125. * Here I fill in a back-pointer and descend into the cdr of a pair.
  126. */
  127. qcdr(p) = flip_mark_bit_p(b);
  128. b = p;
  129. p = w;
  130. goto descend;
  131. }
  132. else if (is_marked_p(w)) goto ascend;
  133. /*
  134. * Here I fill in a back-pointer and descend into the car of a pair.
  135. * [would it be worth taking a special case on w == nil here?]
  136. */
  137. qcar(p) = flip_mark_bit_p(b);
  138. b = p;
  139. p = w;
  140. goto descend;
  141. /*
  142. * case TAG_FIXNUM:
  143. * case TAG_ODDS:
  144. * case TAG_SFLOAT:
  145. */
  146. default:
  147. return; /* assert (b==(Lisp_Object)top) here. */
  148. case TAG_SYMBOL:
  149. #ifndef COMMON
  150. if (p == nil) goto ascend;
  151. #endif
  152. h = qheader(p);
  153. /*
  154. * When I have finished every item that has been visited must be marked,
  155. * with cons cells marked in their car fields and vectors etc in the header
  156. * word. Furthermore the header of all vectors (including symbols) must
  157. * have been replaced by the start of a back-pointer chain identifying the
  158. * words that started off pointing at the vector. The pointers in this
  159. * chain must be marked, word-aligned pointers. Note a special curiosity:
  160. * the back-chain of references to a vector can thread through the CDR
  161. * field of CONS cells and through either odd or even words within vectors.
  162. * Thus althouh marked with the pointer mark bit the rest of tagging on these
  163. * chain words is a bit funny! Specifically the tag bits will say "0" or "4",
  164. * ie CONS or SYMBOL (and not ODDS).
  165. */
  166. if (!is_odds(h) || is_marked_h(h)) /* Visited before */
  167. { q = &qheader(p); /* where I should chain */
  168. p = h; /* the previous header */
  169. goto ascend_from_vector;
  170. }
  171. /*
  172. * Now this is the first visit to a symbol.
  173. */
  174. qheader(p) = h = flip_mark_bit_h(h);
  175. /*
  176. * When components of a symbol are immediate or nil I do nothing.
  177. * (the test for nil is because I expect it to be cheap and to catch
  178. * common cases)
  179. */
  180. w = qvalue(p);
  181. if (!is_immed(w) && w != nil)
  182. { qvalue(p) = flip_mark_bit_p(b);
  183. b = p;
  184. p = w;
  185. goto descend;
  186. }
  187. w = qenv(p);
  188. if (!is_immed(w) && w != nil)
  189. { qenv(p) = flip_mark_bit_p(b);
  190. b = p;
  191. p = w;
  192. goto descend;
  193. }
  194. w = qpname(p);
  195. if (!is_immed(w) && w != nil)
  196. { qpname(p) = flip_mark_bit_p(b);
  197. b = p;
  198. p = w;
  199. goto descend;
  200. }
  201. w = qplist(p);
  202. if (!is_immed(w) && w != nil)
  203. { qplist(p) = flip_mark_bit_p(b);
  204. b = p;
  205. p = w;
  206. goto descend;
  207. }
  208. w = qfastgets(p);
  209. if (!is_immed(w) && w != nil)
  210. { qfastgets(p) = flip_mark_bit_p(b);
  211. b = p;
  212. p = w;
  213. goto descend;
  214. }
  215. #ifdef COMMON
  216. w = qpackage(p);
  217. if (!is_immed(w) && w != nil)
  218. { qpackage(p) = flip_mark_bit_p(b);
  219. b = p;
  220. p = w;
  221. goto descend;
  222. }
  223. #endif
  224. /*
  225. * When all components of the vector are marked I climb up the
  226. * back-pointer chain.
  227. */
  228. q = &qheader(p);
  229. p = h;
  230. goto ascend_from_vector;
  231. case TAG_NUMBERS:
  232. h = numhdr(p);
  233. if (!is_odds(h) || is_marked_h(h)) /* marked already. */
  234. { q = &numhdr(p);
  235. p = h;
  236. goto ascend_from_vector;
  237. }
  238. /*
  239. * For CSL the only case here is that of big integers, which have just
  240. * binary data in them. For Common Lisp I also have to cope with
  241. * ratios and complex numbers.
  242. */
  243. if (is_bignum_header(h))
  244. { q = &numhdr(p);
  245. p = flip_mark_bit_h(h);
  246. goto ascend_from_vector;
  247. }
  248. #ifdef COMMON
  249. numhdr(p) = h = flip_mark_bit_h(h);
  250. w = real_part(p); /* Or numerator of a ratio! */
  251. if (!is_immed(w))
  252. { real_part(p) = flip_mark_bit_p(b);
  253. b = p;
  254. p = w;
  255. goto descend;
  256. }
  257. w = imag_part(p); /* Or denominator of a ratio! */
  258. if (!is_immed(w))
  259. { imag_part(p) = flip_mark_bit_p(b);
  260. b = p;
  261. p = w;
  262. goto descend;
  263. }
  264. /*
  265. * get here if both components of a ratio/complex are immediate (e.g fixnums)
  266. */
  267. q = &numhdr(p);
  268. p = h;
  269. goto ascend_from_vector;
  270. #else
  271. term_printf("Bad numeric code detected in GC\n");
  272. ensure_screen();
  273. abort(); /* Bad numeric type in CSL mode. */
  274. #endif
  275. case TAG_BOXFLOAT:
  276. h = flthdr(p);
  277. if (!is_odds(h) || is_marked_h(h))
  278. { q = &flthdr(p);
  279. p = h;
  280. goto ascend_from_vector;
  281. }
  282. q = &flthdr(p);
  283. p = flip_mark_bit_h(h);
  284. goto ascend_from_vector;
  285. case TAG_VECTOR:
  286. h = vechdr(p);
  287. if (!is_odds(h) || is_marked_h(h))
  288. { q = &vechdr(p);
  289. p = h;
  290. goto ascend_from_vector;
  291. }
  292. if (vector_holds_binary(h))
  293. { q = &vechdr(p);
  294. p = flip_mark_bit_h(h);
  295. goto ascend_from_vector;
  296. }
  297. vechdr(p) = h = flip_mark_bit_h(h);
  298. i = (intxx)doubleword_align_up(length_of_header(h));
  299. if (is_mixed_header(h))
  300. i = 4*CELL; /* Only use first few pointers */
  301. while (i >= 2*CELL)
  302. { i -= CELL;
  303. q = (Header *)((char *)p - TAG_VECTOR + i);
  304. w = *q;
  305. if (is_immed(w) || w == nil) continue;
  306. /*
  307. * For vectors I have to use all available mark bits to keep track of
  308. * where I am...
  309. */
  310. if (i == CELL)
  311. /*
  312. * When descending into the first (or only) word of a vector I leave the
  313. * back-pointer marked, and note that the header word just before it
  314. * will be marked (either as a header word or as a pointer)
  315. */
  316. { *q = flip_mark_bit_p(b);
  317. b = p;
  318. p = w;
  319. }
  320. #ifndef ADDRESS_64
  321. else if ((i & 4) == 0)
  322. #endif
  323. /*
  324. * When descending a pointer at an even (word) address I leave the
  325. * back-pointer unmarked.
  326. */
  327. { *q = b;
  328. b = (Lisp_Object)((char *)p + i);
  329. p = w;
  330. }
  331. #ifndef ADDRESS_64
  332. else
  333. /*
  334. * Finally when I descend into a pointer at an odd (word) address other
  335. * than the special case of the first such, I leave an unmarked back-pointer
  336. * but mark the word before the one I am following. The effect of all this is
  337. * that when I get back to the vector I am able to discriminate between these
  338. * various cases...
  339. */
  340. { *q = b;
  341. b = (Lisp_Object)((char *)p + i - 4);
  342. p = w;
  343. w = *(Lisp_Object *)((char *)b - TAG_VECTOR);
  344. if (is_immed(w)) w = flip_mark_bit_i(w);
  345. else w = flip_mark_bit_p(w);
  346. *(Lisp_Object *)((char *)b - TAG_VECTOR) = w;
  347. }
  348. #endif /* ADDRESS_64 */
  349. goto descend;
  350. }
  351. /*
  352. * I drop through to here if all items in the vector were in fact
  353. * immediate values (e.g. fixnums), and thus there was no need to
  354. * dig deeper.
  355. */
  356. q = &vechdr(p);
  357. p = h;
  358. goto ascend_from_vector;
  359. }
  360. /*
  361. * When I get to ascend b is a back-pointer, and p is an unmarked pointer
  362. * to be put back into the place I descended through.
  363. */
  364. ascend:
  365. if (b == (Lisp_Object)top) return;
  366. switch ((int)b & TAG_BITS)
  367. {
  368. default:
  369. term_printf("Bad tag bits in GC\n");
  370. ensure_screen();
  371. abort();
  372. case TAG_CONS:
  373. w = qcdr(b);
  374. if (is_immed(w) || w == nil)
  375. { w = qcar(b);
  376. qcar(b) = flip_mark_bit_p(p);
  377. p = b;
  378. b = flip_mark_bit_p(w);
  379. goto ascend;
  380. }
  381. else if (is_marked_p(w))
  382. { qcdr(b) = p;
  383. p = b;
  384. b = flip_mark_bit_p(w);
  385. goto ascend;
  386. }
  387. else
  388. { qcdr(b) = qcar(b);
  389. qcar(b) = flip_mark_bit_p(p);
  390. p = w;
  391. goto descend;
  392. }
  393. case TAG_SYMBOL:
  394. #ifdef COMMON
  395. w = qpackage(b);
  396. if (!is_immed(w) && is_marked_p(w))
  397. { qpackage(b) = p;
  398. goto try_nothing;
  399. }
  400. #endif
  401. w = qfastgets(b);
  402. if (!is_immed(w) && is_marked_p(w))
  403. { qfastgets(b) = p;
  404. goto try_package;
  405. }
  406. w = qplist(b);
  407. if (!is_immed(w) && is_marked_p(w))
  408. { qplist(b) = p;
  409. goto try_fastgets;
  410. }
  411. w = qpname(b);
  412. if (!is_immed(w) && is_marked_p(w))
  413. { qpname(b) = p;
  414. goto try_plist;
  415. }
  416. w = qenv(b);
  417. if (!is_immed(w) && is_marked_p(w))
  418. { qenv(b) = p;
  419. goto try_pname;
  420. }
  421. w = qvalue(b);
  422. if (!is_immed(w) && is_marked_p(w))
  423. { qvalue(b) = p;
  424. goto try_env;
  425. }
  426. term_printf("Backpointer not found in GC\n");
  427. ensure_screen();
  428. abort(); /* backpointer not found */
  429. try_env:
  430. p = qenv(b);
  431. if (!is_immed(p) && p != nil && !is_marked_p(p))
  432. { qenv(b) = w;
  433. goto descend;
  434. }
  435. try_pname:
  436. p = qpname(b);
  437. if (!is_immed(p) && p != nil && !is_marked_p(p))
  438. { qpname(b) = w;
  439. goto descend;
  440. }
  441. try_plist:
  442. p = qplist(b);
  443. if (!is_immed(p) && p != nil && !is_marked_p(p))
  444. { qplist(b) = w;
  445. goto descend;
  446. }
  447. try_fastgets:
  448. p = qfastgets(b);
  449. if (!is_immed(p) && p != nil && !is_marked_p(p))
  450. { qfastgets(b) = w;
  451. goto descend;
  452. }
  453. try_package:
  454. #ifdef COMMON
  455. p = qpackage(b);
  456. if (!is_immed(p) && p != nil && !is_marked_p(p))
  457. { qpackage(b) = w;
  458. goto descend;
  459. }
  460. try_nothing:
  461. #endif
  462. q = &qheader(b);
  463. p = *q;
  464. b = flip_mark_bit_p(w);
  465. goto ascend_from_vector;
  466. #ifdef COMMON
  467. case TAG_NUMBERS:
  468. /*
  469. * If I get back to a NUMBERS than it must have been a ratio or a complex.
  470. */
  471. w = imag_part(b);
  472. if (is_immed(w))
  473. { w = real_part(b);
  474. real_part(b) = p;
  475. q = &numhdr(b);
  476. p = *q;
  477. b = flip_mark_bit_p(w);
  478. goto ascend_from_vector;
  479. }
  480. else if (is_marked_p(w))
  481. { imag_part(b) = p;
  482. q = &numhdr(p);
  483. p = *q;
  484. b = flip_mark_bit_p(w);
  485. goto ascend_from_vector;
  486. }
  487. else
  488. { imag_part(b) = real_part(b);
  489. real_part(b) = p;
  490. p = w;
  491. goto descend;
  492. }
  493. #endif
  494. case TAG_VECTOR:
  495. /*
  496. * If I get back to a vector it must have been a vector of Lisp_Objects,
  497. * not a vector of binary data. My back-pointer points part-way into it.
  498. * The back-pointer will be doubleword aligned, so on 32-bit systems
  499. * it is not quite enough to tell me which cell of the vector was involved,
  500. * and so in that case I do a further inspection of mark bits in the two
  501. * parts of the doubelword concerned.
  502. */
  503. w = *(Lisp_Object *)((char *)b - TAG_VECTOR);
  504. #ifndef ADDRESS_64
  505. if (is_immed(w) || is_marked_p(w))
  506. /*
  507. * Here I had been marking the pointer that was stored at an odd (word)
  508. * address.
  509. */
  510. { Lisp_Object w1 = *(Lisp_Object *)((char *)b - TAG_VECTOR + 4);
  511. *(Lisp_Object *)((char *)b - TAG_VECTOR + 4) = p;
  512. if (is_marked_p(w1)) /* End of this vector */
  513. { q = (Header *)((char *)b - TAG_VECTOR);
  514. p = w;
  515. b = flip_mark_bit_p(w1);
  516. goto ascend_from_vector;
  517. }
  518. p = w;
  519. w = w1;
  520. if (!is_immed(p))
  521. { p = flip_mark_bit_p(p);
  522. if (p != nil)
  523. { *(Lisp_Object *)((char *)b - TAG_VECTOR) = w1;
  524. goto descend;
  525. }
  526. }
  527. else p = flip_mark_bit_i(p);
  528. }
  529. #endif /* ADDRESS_64 */
  530. *(Lisp_Object *)((char *)b - TAG_VECTOR) = p;
  531. /*
  532. * Now the doubleword I returned to has been marked (and tidied up),
  533. * so I need to scan back towards the header.
  534. */
  535. scan_vector_more:
  536. for (;;)
  537. { Lisp_Object w2;
  538. /*
  539. * NB on the next line I step back by 8 on both 32 and 64-bit machines!
  540. * That is because the back-pointers I use can only refer to a doubleword
  541. * so on 32-bit systems I have to go 2 cells at a go. Ugh.
  542. */
  543. b = (Lisp_Object)((char *)b - 8);
  544. w2 = *(Lisp_Object *)((char *)b - TAG_VECTOR);
  545. #ifndef ADDRESS_64
  546. p = *(Lisp_Object *)((char *)b - TAG_VECTOR + CELL);
  547. #endif
  548. if ((is_odds(w2) && is_header(w2)) ||
  549. (!is_immed(w2) && is_marked_p(w2)))
  550. /*
  551. * In this case I have reached the doubleword containing the header.
  552. */
  553. {
  554. #ifndef ADDRESS_64
  555. if (!is_immed(p) && p != nil)
  556. { *(Lisp_Object *)((char *)b - TAG_VECTOR + CELL) =
  557. flip_mark_bit_p(w);
  558. goto descend;
  559. }
  560. else
  561. #endif
  562. { q = (Header *)((char *)b - TAG_VECTOR);
  563. p = w2;
  564. b = w;
  565. goto ascend_from_vector;
  566. }
  567. }
  568. /*
  569. * Otherwise I have another general doubleword to cope with.
  570. */
  571. #ifndef ADDRESS_64
  572. if (!is_immed(p) && p != nil)
  573. { if (is_immed(w2)) w2 = flip_mark_bit_i(w2);
  574. else w2 = flip_mark_bit_p(w2);
  575. *(Lisp_Object *)((char *)b - TAG_VECTOR) = w2;
  576. *(Lisp_Object *)((char *)b - TAG_VECTOR + CELL) = w;
  577. goto descend;
  578. }
  579. #endif
  580. if (!is_immed(w2) && w2 != nil)
  581. { p = w2;
  582. *(Lisp_Object *)((char *)b - TAG_VECTOR) = w;
  583. goto descend;
  584. }
  585. continue; /* Step back another doubleword */
  586. }
  587. }
  588. ascend_from_vector:
  589. /*
  590. * Here the item just marked was a vector. I need to leave a reversed
  591. * chain of pointers through its header word. q points to that header,
  592. * and p contains what used to be in the word at q.
  593. */
  594. if (b == (Lisp_Object)top)
  595. { *q = flip_mark_bit_p(b);
  596. *top = p;
  597. return;
  598. }
  599. switch ((int)b & TAG_BITS)
  600. {
  601. default:
  602. term_printf("Bad tag bits in GC\n");
  603. ensure_screen();
  604. abort();
  605. case TAG_CONS:
  606. w = qcdr(b);
  607. if (is_immed(w) || w == nil)
  608. { w = qcar(b);
  609. qcar(b) = p;
  610. *q = flip_mark_bit_p((Lisp_Object *)&qcar(b));
  611. p = b;
  612. b = flip_mark_bit_p(w);
  613. goto ascend;
  614. }
  615. else if (is_marked_p(w))
  616. { qcdr(b) = p;
  617. *q = flip_mark_bit_p((Lisp_Object *)&qcdr(b));
  618. p = b;
  619. b = flip_mark_bit_p(w);
  620. goto ascend;
  621. }
  622. else
  623. { qcdr(b) = qcar(b);
  624. qcar(b) = p;
  625. *q = flip_mark_bit_p((Lisp_Object *)&qcar(b));
  626. p = w;
  627. goto descend;
  628. }
  629. case TAG_SYMBOL:
  630. #ifdef COMMON
  631. w = qpackage(b);
  632. if (!is_immed(w) && is_marked_p(w))
  633. { qpackage(b) = p;
  634. *q = flip_mark_bit_p((Lisp_Object *)&qpackage(b));
  635. goto try_nothing;
  636. }
  637. #endif
  638. w = qfastgets(b);
  639. if (!is_immed(w) && is_marked_p(w))
  640. { qfastgets(b) = p;
  641. *q = flip_mark_bit_p((Lisp_Object *)&qfastgets(b));
  642. goto try_package;
  643. }
  644. w = qplist(b);
  645. if (!is_immed(w) && is_marked_p(w))
  646. { qplist(b) = p;
  647. *q = flip_mark_bit_p((Lisp_Object *)&qplist(b));
  648. goto try_fastgets;
  649. }
  650. w = qpname(b);
  651. if (!is_immed(w) && is_marked_p(w))
  652. { qpname(b) = p;
  653. *q = flip_mark_bit_p((Lisp_Object *)&qpname(b));
  654. goto try_plist;
  655. }
  656. w = qenv(b);
  657. if (!is_immed(w) && is_marked_p(w))
  658. { qenv(b) = p;
  659. *q = flip_mark_bit_p((Lisp_Object *)&qenv(b));
  660. goto try_pname;
  661. }
  662. w = qvalue(b);
  663. if (!is_immed(w) && is_marked_p(w))
  664. { qvalue(b) = p;
  665. *q = flip_mark_bit_p((Lisp_Object *)&qvalue(b));
  666. goto try_env;
  667. }
  668. term_printf("Failure in GC\n");
  669. ensure_screen();
  670. abort();
  671. #ifdef COMMON
  672. case TAG_NUMBERS:
  673. /*
  674. * If I get back to a NUMBERS than it must have been a ratio or a complex.
  675. */
  676. w = imag_part(b);
  677. if (is_immed(w))
  678. { w = real_part(b);
  679. real_part(b) = p;
  680. *q = flip_mark_bit_p((Lisp_Object *)&real_part(b));
  681. q = &numhdr(b);
  682. p = *q;
  683. b = flip_mark_bit_p(w);
  684. goto ascend_from_vector;
  685. }
  686. else if (is_marked_p(w))
  687. { imag_part(b) = p;
  688. *q = flip_mark_bit_p((Lisp_Object *)&imag_part(b));
  689. q = &numhdr(p);
  690. p = *q;
  691. b = flip_mark_bit_p(w);
  692. goto ascend_from_vector;
  693. }
  694. else
  695. { imag_part(b) = real_part(b);
  696. real_part(b) = p;
  697. *q = flip_mark_bit_p((Lisp_Object *)&real_part(b));
  698. p = w;
  699. goto descend;
  700. }
  701. #endif
  702. case TAG_VECTOR:
  703. /*
  704. * If I get back to a vector it must have been a vector of Lisp_Objects,
  705. * not a vector of binary data. My back-pointer points part-way into it.
  706. * I can tell where I am by inspecting the state of mark bits on both parts
  707. * of the doubleword so identified.
  708. */
  709. w = *(Lisp_Object *)((char *)b - TAG_VECTOR);
  710. #ifndef ADDRESS_64
  711. if (is_immed(w) || is_marked_p(w))
  712. /*
  713. * Here I had been marking the pointer that was stored at an odd (word)
  714. * address.
  715. */
  716. { Lisp_Object w1 = *(Lisp_Object *)((char *)b - TAG_VECTOR + 4);
  717. *(Lisp_Object *)((char *)b - TAG_VECTOR + 4) = p;
  718. *q = flip_mark_bit_p((Lisp_Object)((char *)b - TAG_VECTOR + 4));
  719. if (is_marked_p(w1)) /* End of this vector */
  720. { q = (Header *)((char *)b - TAG_VECTOR);
  721. p = *q; /* May not be same as w still! */
  722. b = flip_mark_bit_p(w1);
  723. goto ascend_from_vector;
  724. }
  725. p = w;
  726. w = w1;
  727. if (!is_immed(p))
  728. { p = flip_mark_bit_p(p);
  729. if (p != nil)
  730. { *(Lisp_Object *)((char *)b - TAG_VECTOR) = w1;
  731. goto descend;
  732. }
  733. }
  734. else p = flip_mark_bit_i(p);
  735. *(Lisp_Object *)((char *)b - TAG_VECTOR) = p;
  736. }
  737. else
  738. #endif /* ADDRESS_64 */
  739. { *(Lisp_Object *)((char *)b - TAG_VECTOR) = p;
  740. *q = flip_mark_bit_p((Lisp_Object)((char *)b - TAG_VECTOR));
  741. }
  742. /*
  743. * Now the doubleword I returned to has been marked (and tidied up),
  744. * so I need to scan back towards the header.
  745. */
  746. goto scan_vector_more;
  747. }
  748. }
  749. static void mark(Lisp_Object *pp)
  750. {
  751. /*
  752. * This mark procedure works by using the regular Lisp stack to
  753. * store things while traversing the lists. A null pointer on the
  754. * stack marks the end of the section that is being used. If too
  755. * much stack is (about to be) used I switch over to the pointer-
  756. * reversing code given above, which is slower but which uses
  757. * bounded workspace. My measurements (on just one computer) show the
  758. * stack-based code only 25% faster than the pointer-reversing version,
  759. * which HARDLY seems enough to justify all this extra code, but then
  760. * fast garbage collection is very desirable and every little speed-up
  761. * will help.
  762. */
  763. Lisp_Object p, q, nil = C_nil;
  764. Lisp_Object *sp = stack, *sl = stacklimit;
  765. Header h;
  766. intxx i;
  767. *++sp = (Lisp_Object)NULL;
  768. top:
  769. /*
  770. * normally here pp is a pointer to a Lisp_Object and hence an even
  771. * number - I exploit this so that if I find an odd number stacked I
  772. * view it as indicating a return into a vector...
  773. */
  774. if (((intxx)pp & 1) != 0)
  775. { i = ((intxx)pp) - 1; /* saved value of i */
  776. p = *sp--;
  777. goto in_vector;
  778. }
  779. p = *pp;
  780. if (is_immed_or_cons(p))
  781. {
  782. #ifdef COMMON
  783. if (!is_cons(p) || p == nil || flip_mark_bit_p(p) == nil)
  784. { pp = (Lisp_Object *)(*sp--);
  785. if (pp == NULL) return;
  786. else goto top;
  787. }
  788. #else
  789. if (!is_cons(p))
  790. { pp = (Lisp_Object *)(*sp--);
  791. if (pp == NULL) return;
  792. else goto top;
  793. }
  794. #endif
  795. /*
  796. * Here, and in analagous places, I have to unset the mark bit - this is
  797. * because I set the mark bit on a cons cell early (as I need to) then
  798. * call mark(&car(p)) [in effect], and the effect is that p here sees the
  799. * marked pointer...
  800. */
  801. if (is_marked_p(p)) p = flip_mark_bit_p(p);
  802. q = qcar(p);
  803. if (is_immed_or_cons(q) && !is_cons(q))
  804. { if (is_marked_i(q))
  805. { pp = (Lisp_Object *)(*sp--);
  806. if (pp == NULL) return;
  807. else goto top;
  808. }
  809. qcar(p) = flip_mark_bit_i(q);
  810. pp = &qcdr(p);
  811. goto top;
  812. }
  813. else if (is_marked_p(q))
  814. { pp = (Lisp_Object *)(*sp--);
  815. if (pp == NULL) return;
  816. else goto top;
  817. }
  818. else
  819. { qcar(p) = flip_mark_bit_p(q);
  820. q = qcdr(p);
  821. if (!is_immed(q) && q != nil)
  822. { if (sp >= sl) non_recursive_mark(&qcdr(p));
  823. else *++sp = (Lisp_Object)&qcdr(p);
  824. }
  825. pp = &qcar(p);
  826. goto top;
  827. }
  828. }
  829. /* here we have a vector of some sort */
  830. switch ((int)p & TAG_BITS)
  831. {
  832. default: /* The case-list is exhaustive! */
  833. case TAG_CONS: /* Already processed */
  834. case TAG_FIXNUM: /* Invalid here */
  835. case TAG_ODDS: /* Invalid here */
  836. #ifdef COMMON
  837. case TAG_SFLOAT: /* Invalid here */
  838. #endif
  839. /* Fatal error really called for here */
  840. term_printf("\nBad object in GC (%.8lx)\n", (long)p);
  841. ensure_screen();
  842. abort();
  843. return;
  844. case TAG_SYMBOL:
  845. if (is_marked_p(p)) p = flip_mark_bit_p(p);
  846. #ifndef COMMON
  847. /*
  848. * NIL is outside the main heap, and so marking it must NOT involve
  849. * the regular pointer-chaining operations.
  850. */
  851. if (p == nil)
  852. { pp = (Lisp_Object *)(*sp--);
  853. if (pp == NULL) return;
  854. else goto top;
  855. }
  856. #endif
  857. h = qheader(p);
  858. if (!is_odds(h)) /* already visited */
  859. { *pp = (Lisp_Object)h;
  860. qheader(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  861. pp = (Lisp_Object *)(*sp--);
  862. if (pp == NULL) return;
  863. else goto top;
  864. }
  865. #ifdef DAMAGED_SYMBOLS
  866. term_printf("Symbol at %.8x ", p);
  867. q = qpname(p);
  868. if (is_vector(q) && is_odds(vechdr(q)))
  869. { term_printf("(%.*s)\n",
  870. (int)(length_of_header(vechdr(q))-CELL),
  871. &celt(q, 0));
  872. }
  873. else term_printf("(pname not available)\n");
  874. ensure_screen();
  875. #endif
  876. *pp = flip_mark_bit_i(h);
  877. qheader(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  878. if (sp >= sl)
  879. { non_recursive_mark(&qvalue(p));
  880. non_recursive_mark(&qenv(p));
  881. non_recursive_mark(&qpname(p));
  882. #ifdef COMMON
  883. non_recursive_mark(&qpackage(p));
  884. #endif
  885. }
  886. else
  887. { q = qvalue(p);
  888. if (!is_immed(q) && q != nil)
  889. *++sp = (Lisp_Object)&qvalue(p);
  890. q = qenv(p);
  891. if (!is_immed(q) && q != nil)
  892. *++sp = (Lisp_Object)&qenv(p);
  893. q = qpname(p);
  894. if (!is_immed(q) && q != nil)
  895. *++sp = (Lisp_Object)&qpname(p);
  896. q = qfastgets(p);
  897. if (!is_immed(q) && q != nil)
  898. *++sp = (Lisp_Object)&qfastgets(p);
  899. #ifdef COMMON
  900. q = qpackage(p);
  901. if (!is_immed(q) && q != nil)
  902. *++sp = (Lisp_Object)&qpackage(p);
  903. #endif
  904. }
  905. pp = &qplist(p); /* iterate into plist not value? */
  906. goto top;
  907. case TAG_NUMBERS:
  908. if (is_marked_p(p)) p = flip_mark_bit_p(p);
  909. h = numhdr(p);
  910. if (!is_odds(h)) /* already visited */
  911. { *pp = (Lisp_Object)h;
  912. numhdr(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  913. pp = (Lisp_Object *)(*sp--);
  914. if (pp == NULL) return;
  915. else goto top;
  916. }
  917. *pp = flip_mark_bit_i(h);
  918. numhdr(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  919. if (is_bignum_header(h))
  920. { pp = (Lisp_Object *)(*sp--);
  921. if (pp == NULL) return;
  922. else goto top;
  923. }
  924. #ifdef COMMON
  925. q = real_part(p);
  926. if (!is_immed(q))
  927. { if (sp >= sl) non_recursive_mark(&real_part(p));
  928. else *++sp = (Lisp_Object)&real_part(p);
  929. }
  930. pp = (Lisp_Object *)&imag_part(p);
  931. goto top;
  932. #else
  933. term_printf("Bad numeric type found %.8lx\n", (long)h);
  934. ensure_screen();
  935. abort();
  936. return;
  937. #endif
  938. case TAG_BOXFLOAT:
  939. if (is_marked_p(p)) p = flip_mark_bit_p(p);
  940. h = flthdr(p);
  941. if (!is_odds(h)) /* already visited */
  942. { *pp = (Lisp_Object)h;
  943. flthdr(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  944. pp = (Lisp_Object *)(*sp--);
  945. if (pp == NULL) return;
  946. else goto top;
  947. }
  948. *pp = flip_mark_bit_i(h);
  949. flthdr(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  950. pp = (Lisp_Object *)(*sp--);
  951. if (pp == NULL) return;
  952. else goto top;
  953. case TAG_VECTOR:
  954. if (is_marked_p(p)) p = flip_mark_bit_p(p);
  955. h = vechdr(p);
  956. if (!is_odds(h)) /* already visited */
  957. { *pp = (Lisp_Object)h;
  958. vechdr(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  959. pp = (Lisp_Object *)(*sp--);
  960. if (pp == NULL) return;
  961. else goto top;
  962. }
  963. *pp = flip_mark_bit_i(h);
  964. vechdr(p) = (Header)flip_mark_bit_p((Lisp_Object)pp);
  965. if (vector_holds_binary(h)) /* strings & bitvecs */
  966. { pp = (Lisp_Object *)(*sp--);
  967. if (pp == NULL) return;
  968. else goto top;
  969. }
  970. i = (intxx)doubleword_align_up(length_of_header(h));
  971. if (is_mixed_header(h))
  972. i = 4*CELL; /* Only use first few pointers */
  973. in_vector:
  974. if (sp >= sl)
  975. { while (i >= 3*CELL)
  976. { i -= CELL;
  977. non_recursive_mark((Lisp_Object *)((char *)p - TAG_VECTOR + i));
  978. }
  979. }
  980. else
  981. { while (i >= 3*CELL)
  982. { i -= CELL;
  983. pp = (Lisp_Object *)((char *)p - TAG_VECTOR + i);
  984. q = *pp;
  985. if (!is_immed(q) && q != nil)
  986. { *++sp = p;
  987. *++sp = i + 1;
  988. #ifdef DAMAGED_SYMBOLS
  989. term_printf("Marking item %d of vector %.8x\n", i, p);
  990. ensure_screen();
  991. #endif
  992. goto top;
  993. }
  994. }
  995. }
  996. /*
  997. * Because we padded up to an even number of words for the vector in total
  998. * there are always an odd number of pointers to trace, and in particular
  999. * always at least one - so it IS reasonable to iterate on the first item in
  1000. * the vector, and there can not be any worries about zero-length vectors
  1001. * to hurt me. WELL actually in ADDRESS_64 mode I might have had a zero
  1002. * length vector! I should have treated that as if it contained binary..
  1003. */
  1004. #ifdef ADDRESS_64
  1005. /* /* # error messed up here */
  1006. #endif
  1007. pp = (Lisp_Object *)((char *)p - TAG_VECTOR + i - CELL);
  1008. goto top;
  1009. }
  1010. }
  1011. #endif /* DEMO_MODE */
  1012. Lisp_Object MS_CDECL Lgc0(Lisp_Object nil, int nargs, ...)
  1013. {
  1014. argcheck(nargs, 0, "reclaim");
  1015. return Lgc(nil, lisp_true);
  1016. }
  1017. Lisp_Object Lgc(Lisp_Object nil, Lisp_Object a)
  1018. {
  1019. /*
  1020. * If GC is called with a non-nil argument the garbage collection
  1021. * will be a full one - otherwise it will be soft and may do hardly
  1022. * anything.
  1023. */
  1024. #ifdef DEMO_MODE
  1025. return onevalue(nil);
  1026. #else
  1027. return reclaim(nil, "user request",
  1028. a != nil ? GC_USER_HARD : GC_USER_SOFT, 0);
  1029. #endif
  1030. }
  1031. Lisp_Object Lverbos(Lisp_Object nil, Lisp_Object a)
  1032. /*
  1033. * (verbos 0) or (verbos nil) silent garbage collection
  1034. * (verbos 1) or (verbos t) standard GC messages
  1035. * (verbos 2) messages when FASL module loaded
  1036. * (verbos 4) extra timing info for GC process
  1037. * These bits can be added to get combination effects, except that
  1038. * "4" has no effect unless "1" is present.
  1039. */
  1040. {
  1041. int32 code, old_code = verbos_flag;
  1042. if (a == nil) code = 0;
  1043. else if (is_fixnum(a)) code = int_of_fixnum(a);
  1044. else code = 1;
  1045. /*
  1046. * -G on the command line makes garbage collection noisy always...
  1047. */
  1048. if (miscflags & ALWAYS_NOISY) code |= 3;
  1049. miscflags = (miscflags & ~GC_MSG_BITS) | (code & GC_MSG_BITS);
  1050. return onevalue(fixnum_of_int(old_code));
  1051. }
  1052. CSLbool volatile already_in_gc, tick_on_gc_exit;
  1053. CSLbool volatile interrupt_pending, tick_pending, polltick_pending;
  1054. Lisp_Object volatile saveheaplimit;
  1055. Lisp_Object volatile savevheaplimit;
  1056. Lisp_Object volatile savecodelimit;
  1057. Lisp_Object * volatile savestacklimit;
  1058. static int stop_after_gc = 0;
  1059. static int fold_cons_heap(void)
  1060. {
  1061. /*
  1062. * This is amazingly messy because the heap is segmented.
  1063. */
  1064. nil_as_base
  1065. int top_page_number = 0,
  1066. bottom_page_number = (int)heap_pages_count - 1;
  1067. void *top_page = heap_pages[top_page_number],
  1068. *bottom_page = heap_pages[bottom_page_number];
  1069. char *top_low = (char *)quadword_align_up((intxx)top_page),
  1070. *bottom_low = (char *)quadword_align_up((intxx)bottom_page);
  1071. char *top_start = top_low + CSL_PAGE_SIZE,
  1072. *bottom_start = bottom_low + CSL_PAGE_SIZE;
  1073. char *top_fringe = top_low + car32(top_low),
  1074. *bottom_fringe = bottom_low + car32(bottom_low);
  1075. if (bottom_fringe != (char *)fringe)
  1076. { term_printf("disaster wrt heap fringe %.8lx %.8lx\n",
  1077. (long)bottom_fringe, (long)fringe);
  1078. my_exit(EXIT_FAILURE);
  1079. }
  1080. fringe -= sizeof(Cons_Cell);
  1081. for (;;)
  1082. {
  1083. Lisp_Object p;
  1084. /* scan up from fringe to find a busy cell */
  1085. for (;;)
  1086. { fringe += sizeof(Cons_Cell);
  1087. if (top_page_number == bottom_page_number &&
  1088. top_start == (char *)fringe)
  1089. /*
  1090. * The cast to (unsigned) on the next line is unexpectedly delicate. The
  1091. * difference between two pointers is of type ptrdiff_t, which is a signed
  1092. * type. If this is implemented as int (and that in turn is a 16 bit value)
  1093. * then the following subtraction can overflow and give a value that appears
  1094. * to have the wrong sign. The implicit widening to (Lisp_Object) could
  1095. * then propagate the sign bit in an unhelpful manner. Going via a variable
  1096. * of type (unsigned) ought to mend things. Ok so 16-bit ints are now a
  1097. * thing of that past so this no longer worried me!
  1098. */
  1099. { unsignedxx len = (unsignedxx)((char *)fringe - top_low);
  1100. car32(top_low) = len;
  1101. return bottom_page_number;
  1102. }
  1103. if ((char *)fringe >= bottom_start)
  1104. {
  1105. /*
  1106. * If the heap were to be left totally empty this would be WRONG
  1107. */
  1108. bottom_page = heap_pages[--bottom_page_number];
  1109. bottom_low = (char *)quadword_align_up((intxx)bottom_page);
  1110. bottom_start = bottom_low + CSL_PAGE_SIZE;
  1111. fringe = (Lisp_Object)(bottom_low + car32(bottom_low));
  1112. heaplimit = (Lisp_Object)(bottom_low + SPARE);
  1113. fringe -= sizeof(Cons_Cell);
  1114. #ifdef TICK_STREAM
  1115. /*
  1116. * From time to time in the GC I poll for clock ticks, just to keep
  1117. * responsiveness of the window system up.
  1118. */
  1119. if (tick_on_gc_exit)
  1120. { Lisp_Object nil;
  1121. inject_randomness((int)clock());
  1122. inject_randomness((int)fringe);
  1123. accept_tick();
  1124. nil = C_nil;
  1125. if (exception_pending())
  1126. { stop_after_gc = 1;
  1127. flip_exception();
  1128. }
  1129. tick_on_gc_exit = NO;
  1130. }
  1131. #endif
  1132. continue;
  1133. }
  1134. p = qcar(fringe);
  1135. if (is_immed_or_cons(p) && !is_cons(p))
  1136. { if (is_marked_i(p))
  1137. { qcar(fringe) = flip_mark_bit_i(p);
  1138. break;
  1139. }
  1140. }
  1141. else if (is_marked_p(p))
  1142. { qcar(fringe) = flip_mark_bit_p(p);
  1143. break;
  1144. }
  1145. }
  1146. /* scan down from the top to find a free cell, unmarking is I go */
  1147. for (;;)
  1148. { top_start -= sizeof(Cons_Cell);
  1149. if (top_page_number == bottom_page_number &&
  1150. top_start == (char *)fringe)
  1151. { unsignedxx len = (unsignedxx)((char *)fringe - top_low);
  1152. car32(top_low) = len;
  1153. return bottom_page_number;
  1154. }
  1155. if (top_start < top_fringe)
  1156. { top_page_number++;
  1157. top_page = heap_pages[top_page_number];
  1158. top_low = (char *)quadword_align_up((intxx)top_page);
  1159. top_start = top_low + CSL_PAGE_SIZE;
  1160. top_fringe = top_low + car32(top_low);
  1161. continue;
  1162. }
  1163. p = qcar(top_start);
  1164. if (is_immed_or_cons(p) && !is_cons(p))
  1165. { if (!is_marked_i(p)) break;
  1166. else qcar(top_start) = flip_mark_bit_i(p);
  1167. }
  1168. else if (!is_marked_p(p)) break;
  1169. else qcar(top_start) = flip_mark_bit_p(p);
  1170. }
  1171. /* Now relocate one cell */
  1172. qcar(top_start) = qcar(fringe);
  1173. qcdr(top_start) = qcdr(fringe);
  1174. { Lisp_Object forward = flip_mark_bit_p(top_start + TAG_VECTOR);
  1175. qcar(fringe) = forward;
  1176. qcdr(fringe) = forward;
  1177. }
  1178. }
  1179. }
  1180. static void adjust_vec_heap(void)
  1181. /*
  1182. * This scans over the vector heap working out where each vector
  1183. * is going to get relocated to, and then changing pointers to reflect
  1184. * where the vectors will end up.
  1185. */
  1186. {
  1187. Lisp_Object nil = C_nil;
  1188. int32 page_number, new_page_number = 0;
  1189. void *new_page = vheap_pages[0];
  1190. char *new_low = (char *)doubleword_align_up((intxx)new_page);
  1191. char *new_high = new_low + (CSL_PAGE_SIZE - 8);
  1192. char *p = new_low + 8;
  1193. for (page_number = 0; page_number < vheap_pages_count; page_number++)
  1194. { void *page = vheap_pages[page_number];
  1195. char *low = (char *)doubleword_align_up((intxx)page);
  1196. char *fr = low + car32(low);
  1197. #ifdef TICK_STREAM
  1198. if (tick_on_gc_exit)
  1199. {
  1200. inject_randomness((int)clock());
  1201. inject_randomness((int)fr);
  1202. accept_tick();
  1203. tick_on_gc_exit = NO;
  1204. }
  1205. #endif
  1206. *(Lisp_Object *)fr = nil;
  1207. low += 8;
  1208. for (;;)
  1209. { Header h;
  1210. Lisp_Object h1;
  1211. char *p1;
  1212. int32 l;
  1213. unsignedxx free;
  1214. /*
  1215. * Here a vector will have an ordinary vector-header (which is tagged
  1216. * as ODDS) if it was not marked.
  1217. */
  1218. while (is_odds(h = *(Header *)low))
  1219. {
  1220. if (is_symbol_header(h)) low += symhdr_length;
  1221. else low += doubleword_align_up(length_of_header(h));
  1222. }
  1223. /*
  1224. * It could be that all (remaining) the vectors in this page are unmarked...
  1225. */
  1226. if (low >= fr) break;
  1227. /*
  1228. * Otherwise I have found an active vector. Its header should have been
  1229. * left with a back-pointer chain through all places that refereed to the
  1230. * vector.
  1231. */
  1232. h1 = h;
  1233. while (!is_odds(h1))
  1234. { Lisp_Object h2;
  1235. h2 = *(Lisp_Object *)clear_mark_bit_p(h1);
  1236. if (is_vector(h2))
  1237. /*
  1238. * Forwarding pointer for relocated cons cell. This is delicate because
  1239. * of the number of spare bits I have on a 32-bit system. The back-pointer
  1240. * chain via the heaver word of a vector can run through other vector
  1241. * cells (in the middle of vectors) and it can also go via either CAR or CDR
  1242. * field of a cons cell. The funny case here is if it is via the CDR field
  1243. * of a CONS cell and that CONS has been relocated. Then the CONS contains
  1244. * a forwarding address that points to the start of the relocated object.
  1245. * Sometimes I want to end up with a pointer to the CDR bit again. The
  1246. * "+ (h1 & CELL)" is there to achieve that. I somewhat feel that I ought to
  1247. * have been able to do something cleaner, but changing it now seems to me
  1248. * to be too delicate.
  1249. */
  1250. h1 = (Lisp_Object)((char *)h2 - TAG_VECTOR + (h1 & CELL));
  1251. else h1 = h2;
  1252. }
  1253. if (is_symbol_header(h1)) l = symhdr_length;
  1254. else l = doubleword_align_up(length_of_header(h1));
  1255. /*
  1256. * I subtract the pointers (new_high - p) into an unsigned int because
  1257. * on a 16-bit machine that might be vital! The type ptrdiff_t is a signed
  1258. * type and in bad cases the subtraction might overflow, but I know that the
  1259. * answer here is supposed to be positive. Hmm I think that these days
  1260. * worry about 16 bit machines is no longer worthwhile...
  1261. */
  1262. free = (unsignedxx)(new_high - p);
  1263. if (l > (intxx)free)
  1264. { new_page_number++;
  1265. new_page = vheap_pages[new_page_number];
  1266. new_low = (char *)doubleword_align_up((intxx)new_page);
  1267. new_high = new_low + (CSL_PAGE_SIZE - 8);
  1268. p = new_low + 8;
  1269. }
  1270. /*
  1271. * Because I did not have enough bits to store the critical information
  1272. * somewhere nicer I have to reconstruct the tag bits to go with the
  1273. * vector out of the header word associated with it.
  1274. * Here is had BETTER be a vector!
  1275. */
  1276. if (is_symbol_header(h1)) p1 = p + TAG_SYMBOL;
  1277. else if (is_numbers_header(h1)) p1 = p + TAG_NUMBERS;
  1278. else if (is_boxfloat_header(h1)) p1 = p + TAG_BOXFLOAT;
  1279. else p1 = p + TAG_VECTOR;
  1280. while (!is_odds(h))
  1281. { h = clear_mark_bit_p(h);
  1282. h1 = *(Lisp_Object *)h;
  1283. /*
  1284. * The two above lines fail if amalgamated - both on Zortech C 3.0.1 and
  1285. * on a VAX/VMS C compiler. Hence two lines of code where once I had one.
  1286. */
  1287. if (is_vector(h1))
  1288. h = (Lisp_Object)((char *)h1 - TAG_VECTOR + (h & CELL));
  1289. else
  1290. { *(Lisp_Object *)h = (Lisp_Object)p1;
  1291. h = h1;
  1292. }
  1293. }
  1294. *(Lisp_Object *)low = set_mark_bit_h(h);
  1295. low += l;
  1296. p += l;
  1297. if (low >= fr) break;
  1298. }
  1299. }
  1300. }
  1301. static void move_vec_heap(void)
  1302. /*
  1303. * This moves data down in the vector heap, supposing that all pointer
  1304. * relocation will be dealt with elsewhere. Calculations made here must remain
  1305. * in step with those in adjust_vecheap.
  1306. */
  1307. {
  1308. nil_as_base
  1309. int32 page_number, new_page_number = 0;
  1310. void *new_page = vheap_pages[0];
  1311. char *new_low = (char *)doubleword_align_up((intxx)new_page);
  1312. char *new_high = new_low + (CSL_PAGE_SIZE - 8);
  1313. char *p = new_low + 8;
  1314. for (page_number = 0; page_number < vheap_pages_count; page_number++)
  1315. { void *page = vheap_pages[page_number];
  1316. char *low = (char *)doubleword_align_up((intxx)page);
  1317. char *fr = low + car32(low);
  1318. #ifdef TICK_STREAM
  1319. if (tick_on_gc_exit)
  1320. {
  1321. inject_randomness((int)clock());
  1322. inject_randomness((int)fr);
  1323. accept_tick();
  1324. tick_on_gc_exit = NO;
  1325. }
  1326. #endif
  1327. *(Lisp_Object *)fr = set_mark_bit_h(TAG_ODDS + (8<<10));
  1328. low += 8;
  1329. for (;;)
  1330. { Header h;
  1331. intxx l;
  1332. unsignedxx free;
  1333. while (!is_marked_h(h = *(Header *)low))
  1334. if (is_symbol_header(h)) low += symhdr_length;
  1335. else low += doubleword_align_up(length_of_header(h));
  1336. if (low >= fr) break;
  1337. if (is_symbol_header(h)) l = symhdr_length;
  1338. else l = doubleword_align_up(length_of_header(h));
  1339. #ifdef DEBUG
  1340. if (l >= CSL_PAGE_SIZE)
  1341. { term_printf("heap mangled - vector length wrong\n");
  1342. ensure_screen();
  1343. abort();
  1344. }
  1345. #endif
  1346. free = (unsignedxx)(new_high - p);
  1347. if (l > (intxx)free)
  1348. { unsignedxx len = (unsignedxx)(p - new_low);
  1349. car32(new_low) = len;
  1350. /*
  1351. * I fill the end of the page with zero words so that the data there is
  1352. * definite in value, and to help file-compression when I dump a heap
  1353. * image.
  1354. */
  1355. #ifdef CLEAR_OUT_MEMORY
  1356. while (free != 0)
  1357. { *(int32 *)p = 0;
  1358. p += 4;
  1359. free -= 4;
  1360. }
  1361. #endif
  1362. new_page_number++;
  1363. new_page = vheap_pages[new_page_number];
  1364. new_low = (char *)doubleword_align_up((intxx)new_page);
  1365. new_high = new_low + (CSL_PAGE_SIZE - 8);
  1366. p = new_low + 8;
  1367. }
  1368. *(Header *)p = clear_mark_bit_h(h);
  1369. p += CELL;
  1370. low += CELL;
  1371. l -= CELL;
  1372. while (l != 0)
  1373. { *(int32 *)p = *(int32 *)low;
  1374. p += 4;
  1375. low += 4;
  1376. l -= 4;
  1377. }
  1378. }
  1379. }
  1380. { unsignedxx len = (unsignedxx)(p - new_low);
  1381. #ifdef CLEAR_OUT_MEMORY
  1382. unsignedxx free = (unsignedxx)(new_high - p);
  1383. #endif
  1384. car32(new_low) = len;
  1385. #ifdef CLEAR_OUT_MEMORY
  1386. while (free != 0)
  1387. { *(int32 *)p = 0;
  1388. p += 4;
  1389. free -= 4;
  1390. }
  1391. #endif
  1392. }
  1393. vfringe = (Lisp_Object)p;
  1394. vheaplimit = (Lisp_Object)(new_low + (CSL_PAGE_SIZE - 8));
  1395. new_page_number++;
  1396. while (vheap_pages_count > new_page_number)
  1397. pages[pages_count++] = vheap_pages[--vheap_pages_count];
  1398. }
  1399. static int compress_heap(void)
  1400. {
  1401. int n = fold_cons_heap();
  1402. adjust_vec_heap();
  1403. move_vec_heap();
  1404. return n;
  1405. }
  1406. static void relocate(Lisp_Object *cp)
  1407. /*
  1408. * If p is a pointer to a cons cell that has been moved, fix it up.
  1409. */
  1410. {
  1411. Lisp_Object nil = C_nil,
  1412. p = (*cp); /* BEWARE "p =* cp;" anachronism here! */
  1413. if (p == nil) return; /* nil is separate from the main heap */
  1414. else if (is_cons(p))
  1415. { Lisp_Object p1;
  1416. p1 = qcar(p);
  1417. if (is_vector(p1) && is_marked_p(p1))
  1418. *cp = clear_mark_bit_p(p1 - TAG_VECTOR + TAG_CONS);
  1419. }
  1420. }
  1421. static void relocate_consheap(int bottom_page_number)
  1422. {
  1423. int page_number;
  1424. for (page_number = 0; page_number <= bottom_page_number; page_number++)
  1425. { void *page = heap_pages[page_number];
  1426. char *low = (char *)quadword_align_up((intxx)page);
  1427. char *start = low + CSL_PAGE_SIZE;
  1428. char *fr = low + car32(low);
  1429. #ifdef TICK_STREAM
  1430. if (tick_on_gc_exit)
  1431. {
  1432. inject_randomness((int)clock());
  1433. inject_randomness((int)fr);
  1434. accept_tick();
  1435. tick_on_gc_exit = NO;
  1436. }
  1437. #endif
  1438. while (fr < start)
  1439. { relocate((Lisp_Object *)fr);
  1440. fr += sizeof(Lisp_Object);
  1441. cons_cells += sizeof(Lisp_Object);
  1442. }
  1443. }
  1444. }
  1445. static void relocate_vecheap(void)
  1446. {
  1447. int page_number;
  1448. intxx i;
  1449. for (page_number = 0; page_number < vheap_pages_count; page_number++)
  1450. { void *page = vheap_pages[page_number];
  1451. char *low = (char *)doubleword_align_up((intxx)page);
  1452. char *fr = low + car32(low);
  1453. #ifdef TICK_STREAM
  1454. if (tick_on_gc_exit)
  1455. {
  1456. inject_randomness((int)clock());
  1457. inject_randomness((int)fr);
  1458. accept_tick();
  1459. tick_on_gc_exit = NO;
  1460. }
  1461. #endif
  1462. low += 8;
  1463. while (low < fr)
  1464. { Header h = *(Header *)low;
  1465. if (is_symbol_header(h))
  1466. { Symbol_Head *s = (Symbol_Head *)low;
  1467. relocate(&(s->value));
  1468. relocate(&(s->env));
  1469. /*
  1470. * To keep track of literal vectors I suppose here that they are never shared,
  1471. * and I then account for things that are either V or (B . V) in an environment
  1472. * cell, where B is binary code and V is a vector.
  1473. */
  1474. { Lisp_Object e = s->env;
  1475. if (is_cons(e) && is_bps(qcar(e))) e = qcdr(e);
  1476. if (is_vector(e))
  1477. litvecs += doubleword_align_up(
  1478. length_of_header(vechdr(e)));
  1479. }
  1480. /* relocate(&(s->pname)); can never be a cons cell */
  1481. relocate(&(s->plist));
  1482. relocate(&(s->fastgets));
  1483. { Lisp_Object e = s->fastgets;
  1484. if (is_vector(e))
  1485. getvecs += doubleword_align_up(
  1486. length_of_header(vechdr(e)));
  1487. }
  1488. #ifdef COMMON
  1489. relocate(&(s->package));
  1490. #endif
  1491. low += symhdr_length;
  1492. symbol_heads += symhdr_length;
  1493. continue;
  1494. }
  1495. else switch (type_of_header(h))
  1496. {
  1497. #ifdef COMMON
  1498. case TYPE_RATNUM:
  1499. case TYPE_COMPLEX_NUM:
  1500. relocate((Lisp_Object *)(low+CELL));
  1501. relocate((Lisp_Object *)(low+2*CELL));
  1502. other_mem += 2*CELL;
  1503. break;
  1504. #endif
  1505. case TYPE_MIXED1:
  1506. case TYPE_MIXED2:
  1507. case TYPE_MIXED3:
  1508. case TYPE_STREAM:
  1509. for (i=CELL; i<4*CELL; i+=CELL)
  1510. relocate((Lisp_Object *)(low+i));
  1511. other_mem += doubleword_align_up(length_of_header(h));
  1512. break;
  1513. case TYPE_HASH:
  1514. case TYPE_SIMPLE_VEC:
  1515. case TYPE_ARRAY:
  1516. case TYPE_STRUCTURE:
  1517. for (i=CELL; i<doubleword_align_up(length_of_header(h)); i+=CELL)
  1518. relocate((Lisp_Object *)(low+i));
  1519. if (type_of_header(h) == TYPE_SIMPLE_VEC)
  1520. user_vectors += doubleword_align_up(length_of_header(h));
  1521. else other_mem += doubleword_align_up(length_of_header(h));
  1522. break;
  1523. case TYPE_STRING:
  1524. strings += doubleword_align_up(length_of_header(h));
  1525. break;
  1526. case TYPE_BIGNUM:
  1527. big_numbers += doubleword_align_up(length_of_header(h));
  1528. break;
  1529. #ifdef COMMON
  1530. case TYPE_SINGLE_FLOAT:
  1531. case TYPE_LONG_FLOAT:
  1532. #endif
  1533. case TYPE_DOUBLE_FLOAT:
  1534. box_floats += doubleword_align_up(length_of_header(h));
  1535. break;
  1536. default:
  1537. break;
  1538. }
  1539. low += doubleword_align_up(length_of_header(h));
  1540. }
  1541. }
  1542. }
  1543. static void abandon_heap_pages(int bottom_page_number)
  1544. {
  1545. bottom_page_number++;
  1546. while (heap_pages_count > bottom_page_number)
  1547. pages[pages_count++] = heap_pages[--heap_pages_count];
  1548. }
  1549. static void zero_out(void *p)
  1550. {
  1551. char *p1 = (char *)doubleword_align_up((intxx)p);
  1552. memset(p1, 0, CSL_PAGE_SIZE);
  1553. }
  1554. #ifndef NO_COPYING_GC
  1555. /*
  1556. * You may like to observe how much more compact the code for the copying
  1557. * garbage collector is when compared with the mark/slide mess. It is
  1558. * naturally and easily non-recursive and does not get involved in any
  1559. * over-dubious punning on bit-patterns... It just requires a lot of spare
  1560. * memory for the new semi-space.
  1561. */
  1562. static int trailing_heap_pages_count,
  1563. trailing_vheap_pages_count;
  1564. static void copy(Lisp_Object *p)
  1565. /*
  1566. * This copies the object pointed at by p from the old to the new semi-space,
  1567. * and returns a copy to the pointer. If scans the copied material to copy
  1568. * all relevent sub-structures to the new semi-space.
  1569. */
  1570. {
  1571. Lisp_Object nil = C_nil;
  1572. char *fr = (char *)fringe, *vfr = (char *)vfringe;
  1573. char *tr_fr = fr, *tr_vfr = vfr;
  1574. void *p1;
  1575. #define CONT 0
  1576. #define DONE_CAR -1
  1577. #define DONE_VALUE -2
  1578. #define DONE_ENV -3
  1579. #define DONE_PNAME -4
  1580. #define DONE_PLIST -5
  1581. #define DONE_FASTGETS -6
  1582. int next = CONT;
  1583. char *tr;
  1584. #ifdef DEBUG_GC
  1585. term_printf("Copy [%.8lx] %.8lx\n", (long)p, (long)*p);
  1586. #endif
  1587. /*
  1588. * The code here is a simulation of multiple procedure calls to the
  1589. * code that copies a single object. What might otherwise have been
  1590. * a "return address" in the calls is handled by the variable "next" which
  1591. * takes positive values while copying vectors, and negative ones in
  1592. * the more common cases. I use "for (;;)" blocks a lot so that I can
  1593. * use "break" and "continue" to leap around in the code - maybe I
  1594. * would do better to be honest and use regular labels and "goto"
  1595. * statements.
  1596. */
  1597. for (;;)
  1598. {
  1599. /*
  1600. * Copy one object, pointed at by p, from the old semi-space into the new
  1601. * one.
  1602. */
  1603. Lisp_Object a = *p;
  1604. #ifdef DEBUG_GC
  1605. term_printf("Next copy [%.8lx] %.8lx\n", (long)p, (long)*p);
  1606. #endif
  1607. for (;;)
  1608. {
  1609. if (a == nil) break; /* common and cheap enough to test here */
  1610. else if (is_immed_or_cons(a))
  1611. { if (is_cons(a))
  1612. {
  1613. Lisp_Object w;
  1614. w = qcar(a);
  1615. if (is_cons(w) && is_marked_p(w)) /* a forwarding address */
  1616. { *p = flip_mark_bit_p(w);
  1617. break;
  1618. }
  1619. fr = fr - sizeof(Cons_Cell);
  1620. cons_cells += 2*CELL;
  1621. /*
  1622. * When I am doing regular calculation I leave myself a bunch of spare
  1623. * words (size SPARE bytes) so that I can afford to do several cons operations
  1624. * between tests. Here I do careful tests on every step, and so I can
  1625. * sail much closer to the wind wrt filling up space.
  1626. */
  1627. if (fr <= (char *)heaplimit - SPARE + 32)
  1628. { char *hl = (char *)heaplimit;
  1629. void *p;
  1630. unsignedxx len = (unsignedxx)(fr - (hl - SPARE) +
  1631. sizeof(Cons_Cell));
  1632. car32(hl - SPARE) = len;
  1633. qcar(fr) = SPID_GCMARK;
  1634. if (pages_count == 0)
  1635. { term_printf("pages_count = 0 in GC\n");
  1636. ensure_screen();
  1637. abort();
  1638. return;
  1639. }
  1640. p = pages[--pages_count];
  1641. zero_out(p);
  1642. new_heap_pages[new_heap_pages_count++] = p;
  1643. heaplimit = quadword_align_up((intxx)p);
  1644. hl = (char *)heaplimit;
  1645. car32(heaplimit) = CSL_PAGE_SIZE;
  1646. fr = hl + CSL_PAGE_SIZE - sizeof(Cons_Cell);
  1647. heaplimit = (Lisp_Object)(hl + SPARE);
  1648. }
  1649. qcar(fr) = w;
  1650. qcdr(fr) = qcdr(a);
  1651. *p = w = (Lisp_Object)(fr + TAG_CONS);
  1652. qcar(a) = flip_mark_bit_p(w);
  1653. break;
  1654. }
  1655. else if (is_bps(a))
  1656. { char *d = data_of_bps(a) - CELL, *rr;
  1657. intxx alloc_size;
  1658. Header h = *(Header *)d;
  1659. intxx len;
  1660. if (is_bps(h)) /* Replacement handle in header field? */
  1661. { *p = h ;
  1662. break;
  1663. }
  1664. len = length_of_header(h);
  1665. alloc_size = (intxx)doubleword_align_up(len);
  1666. bytestreams += alloc_size;
  1667. for (;;)
  1668. { char *cf = (char *)codefringe,
  1669. *cl = (char *)codelimit;
  1670. unsignedxx free = (unsignedxx)(cf - cl);
  1671. if (alloc_size > (intxx)free)
  1672. {
  1673. void *p;
  1674. if (codelimit != 0)
  1675. { unsignedxx len = (unsignedxx)(cf - (cl - 8));
  1676. car32(cl - 8) = len;
  1677. }
  1678. if (pages_count == 0)
  1679. { term_printf("pages_count = 0 in GC\n");
  1680. ensure_screen();
  1681. abort();
  1682. return;
  1683. }
  1684. p = pages[--pages_count];
  1685. zero_out(p);
  1686. new_bps_pages[new_bps_pages_count++] = p;
  1687. cl = (char *)doubleword_align_up((intxx)p);
  1688. codefringe = (Lisp_Object)(cl + CSL_PAGE_SIZE);
  1689. codelimit = (Lisp_Object)(cl + 8);
  1690. continue;
  1691. }
  1692. rr = cf - alloc_size;
  1693. codefringe = (Lisp_Object)rr;
  1694. /*
  1695. * See comments in fns2.c for the curious packing here!
  1696. */
  1697. *(Header *)d = *p = TAG_BPS +
  1698. (((intxx)((rr + CELL) - (cl - 8)) &
  1699. (PAGE_POWER_OF_TWO-4)) << 6) +
  1700. (((intxx)(new_bps_pages_count-1))<<(PAGE_BITS+6));
  1701. /* Wow! How obscure!! */
  1702. *(Header *)rr = h;
  1703. memcpy(rr+CELL, d+CELL, alloc_size-CELL);
  1704. break;
  1705. }
  1706. break;
  1707. }
  1708. else break; /* Immediate data drops out here */
  1709. }
  1710. else /* Here I have a symbol or vector */
  1711. { Header h;
  1712. int tag;
  1713. intxx len;
  1714. tag = ((int)a) & TAG_BITS;
  1715. a = (Lisp_Object)((char *)a - tag);
  1716. h = *(Header *)a;
  1717. #ifdef DEBUG_GC
  1718. term_printf("Header is %.8lx\n", (long)h);
  1719. #endif
  1720. if (!is_odds(h))
  1721. { *p = h;
  1722. break;
  1723. }
  1724. if (tag == TAG_SYMBOL)
  1725. len = symhdr_length, symbol_heads += symhdr_length;
  1726. else
  1727. { len = doubleword_align_up(length_of_header(h));
  1728. switch (type_of_header(h))
  1729. {
  1730. case TYPE_STRING:
  1731. strings += len; break;
  1732. case TYPE_BIGNUM:
  1733. big_numbers += len; break;
  1734. #ifdef COMMON
  1735. case TYPE_SINGLE_FLOAT:
  1736. case TYPE_LONG_FLOAT:
  1737. #endif
  1738. case TYPE_DOUBLE_FLOAT:
  1739. box_floats += len; break;
  1740. case TYPE_SIMPLE_VEC:
  1741. user_vectors += len; break;
  1742. default:
  1743. other_mem += len; break;
  1744. }
  1745. }
  1746. for (;;)
  1747. { char *vl = (char *)vheaplimit;
  1748. unsignedxx free = (unsignedxx)(vl - vfr);
  1749. if (len > (intxx)free)
  1750. { unsignedxx free1 =
  1751. (unsignedxx)(vfr - (vl - (CSL_PAGE_SIZE - 8)));
  1752. car32(vl - (CSL_PAGE_SIZE - 8)) = free1;
  1753. qcar(vfr) = 0; /* sentinel value */
  1754. if (pages_count == 0)
  1755. { term_printf("pages_count = 0 in GC\n");
  1756. ensure_screen();
  1757. abort();
  1758. return;
  1759. }
  1760. p1 = pages[--pages_count];
  1761. zero_out(p1);
  1762. new_vheap_pages[new_vheap_pages_count++] = p1;
  1763. vfr = (char *)doubleword_align_up((intxx)p1) + 8;
  1764. vl = vfr + (CSL_PAGE_SIZE - 16);
  1765. vheaplimit = (Lisp_Object)vl;
  1766. free1 = (unsignedxx)(vfr - (vl - (CSL_PAGE_SIZE - 8)));
  1767. car32(vl - (CSL_PAGE_SIZE - 8)) = free1;
  1768. continue;
  1769. }
  1770. *(Lisp_Object *)a = *p = (Lisp_Object)(vfr + tag);
  1771. *(Header *)vfr = h;
  1772. memcpy((char *)vfr+CELL, (char *)a+CELL, len-CELL);
  1773. vfr += len;
  1774. break;
  1775. }
  1776. break;
  1777. }
  1778. }
  1779. /*
  1780. * Now I have copied one object - the next thing to do is to scan to see
  1781. * if any further items are in the new space, and if so I will copy
  1782. * their offspring.
  1783. */
  1784. for (;;)
  1785. {
  1786. switch (next)
  1787. {
  1788. case CONT:
  1789. if (tr_fr != fr)
  1790. { tr_fr = tr_fr - sizeof(Cons_Cell);
  1791. if (qcar(tr_fr) == SPID_GCMARK)
  1792. { char *w;
  1793. p1 = new_heap_pages[trailing_heap_pages_count++];
  1794. w = (char *)quadword_align_up((intxx)p1);
  1795. tr_fr = w + (CSL_PAGE_SIZE - sizeof(Cons_Cell));
  1796. }
  1797. next = DONE_CAR;
  1798. p = &qcar(tr_fr);
  1799. break; /* Takes me to the outer loop */
  1800. }
  1801. else if (tr_vfr != vfr)
  1802. { Header h;
  1803. h = *(Header *)tr_vfr;
  1804. if (h == 0)
  1805. { char *w;
  1806. p1 = new_vheap_pages[trailing_vheap_pages_count++];
  1807. w = (char *)doubleword_align_up((intxx)p1);
  1808. tr_vfr = w + 8;
  1809. h = *(Header *)tr_vfr;
  1810. }
  1811. if (is_symbol_header(h))
  1812. { next = DONE_VALUE;
  1813. p = &(((Symbol_Head *)tr_vfr)->value);
  1814. break;
  1815. }
  1816. else
  1817. { intxx len = doubleword_align_up(length_of_header(h));
  1818. tr = tr_vfr;
  1819. tr_vfr = tr_vfr + len;
  1820. switch (type_of_header(h))
  1821. {
  1822. #ifdef COMMON
  1823. case TYPE_SINGLE_FLOAT:
  1824. case TYPE_LONG_FLOAT:
  1825. #endif
  1826. case TYPE_DOUBLE_FLOAT:
  1827. case TYPE_BIGNUM:
  1828. continue;
  1829. case TYPE_MIXED1: case TYPE_MIXED2:
  1830. case TYPE_MIXED3: case TYPE_STREAM:
  1831. next = 2*CELL;
  1832. break;
  1833. /*
  1834. * There is a slight delight here. The test "vector_holds_binary" is only
  1835. * applicable if the header to be checked is a header of a genuine vector,
  1836. * ie something that would have TAG_VECTOR in the pointer to it. But here
  1837. * various numeric data types also live in the vector heap, so I need to
  1838. * separate them out explicitly. The switch block here does slightly more than
  1839. * it actually HAS to, since the vector_holds_binary test would happen to
  1840. * deal with several of the numeric types "by accident", but I feel that
  1841. * the security of listing them as separate cases is more important than the
  1842. * minor speed-up that might come from exploiting such marginal behaviour.
  1843. */
  1844. default:
  1845. if (vector_holds_binary(h)) continue;
  1846. #ifdef COMMON
  1847. case TYPE_RATNUM:
  1848. case TYPE_COMPLEX_NUM:
  1849. #endif
  1850. next = len - 2*CELL;
  1851. break;
  1852. }
  1853. p = (Lisp_Object *)(tr + next + CELL);
  1854. break;
  1855. }
  1856. }
  1857. else
  1858. { fringe = (Lisp_Object)fr;
  1859. vfringe = (Lisp_Object)vfr;
  1860. return; /* Final exit when all has been copied */
  1861. }
  1862. case DONE_CAR:
  1863. next = CONT;
  1864. p = &qcdr(tr_fr);
  1865. break;
  1866. case DONE_VALUE:
  1867. next = DONE_ENV;
  1868. p = &(((Symbol_Head *)tr_vfr)->env);
  1869. break;
  1870. case DONE_ENV:
  1871. next = DONE_FASTGETS;
  1872. p = &(((Symbol_Head *)tr_vfr)->fastgets);
  1873. break;
  1874. case DONE_FASTGETS:
  1875. next = DONE_PNAME;
  1876. p = &(((Symbol_Head *)tr_vfr)->pname);
  1877. break;
  1878. case DONE_PNAME:
  1879. #ifndef COMMON
  1880. next = CONT;
  1881. p = &(((Symbol_Head *)tr_vfr)->plist);
  1882. tr_vfr = tr_vfr + symhdr_length;
  1883. break;
  1884. #else
  1885. next = DONE_PLIST;
  1886. p = &(((Symbol_Head *)tr_vfr)->plist);
  1887. break;
  1888. case DONE_PLIST:
  1889. next = CONT;
  1890. p = &(((Symbol_Head *)tr_vfr)->package);
  1891. tr_vfr = tr_vfr + symhdr_length;
  1892. break;
  1893. #endif
  1894. default:
  1895. p = (Lisp_Object *)(tr + next);
  1896. next -= CELL;
  1897. break;
  1898. }
  1899. break;
  1900. }
  1901. }
  1902. }
  1903. #endif /* NO_COPYING_GC */
  1904. #ifndef DEMO_MODE
  1905. typedef struct mapstore_item
  1906. {
  1907. double w;
  1908. double n;
  1909. unsigned32 n1;
  1910. Lisp_Object p;
  1911. } mapstore_item;
  1912. int profile_count_mode;
  1913. static int MS_CDECL profile_cf(const void *a, const void *b)
  1914. {
  1915. mapstore_item *aa = (mapstore_item *)a,
  1916. *bb = (mapstore_item *)b;
  1917. if (profile_count_mode)
  1918. { if (aa->n1 == bb->n1) return 0;
  1919. if (aa->n1 < bb->n1) return 1;
  1920. else return -1;
  1921. }
  1922. if (aa->w == bb->w) return 0;
  1923. else if (aa->w < bb->w) return 1;
  1924. else return -1;
  1925. }
  1926. #endif
  1927. Lisp_Object Lmapstore(Lisp_Object nil, Lisp_Object a)
  1928. /*
  1929. * Argument controls what happens:
  1930. * nil or 0 print statistics and reset to zero
  1931. * 1 print, but do not reset
  1932. * 2 return list of stats, reset to zero
  1933. * 3 return list, do not reset
  1934. * 4 reset to zero, do not print, return nil
  1935. * 8 Toggle call count mode
  1936. */
  1937. {
  1938. #ifdef DEMO_MODE
  1939. return onevalue(nil);
  1940. #else
  1941. int pass, what;
  1942. int32 j, gcn;
  1943. double itotal = 0.0, total = 0.0;
  1944. Lisp_Object res = nil;
  1945. mapstore_item *buff;
  1946. int32 buffp, buffn;
  1947. if (a == nil) a = fixnum_of_int(0);
  1948. if (is_fixnum(a)) what = int_of_fixnum(a);
  1949. else what = 0;
  1950. if ((what & 6) == 0)
  1951. { buff = (mapstore_item *)(*malloc_hook)(100*sizeof(mapstore_item));
  1952. if (buff == NULL) return onevalue(nil); /* fail */
  1953. buffp = 0;
  1954. buffn = 100;
  1955. }
  1956. if ((what & 2) != 0)
  1957. { Lgc0(nil, 0); /* Force GC at start to avoid one in the middle */
  1958. nil = C_nil;
  1959. if (exception_pending()) return nil;
  1960. gcn = gc_number;
  1961. }
  1962. if ((what & 8) != 0)
  1963. profile_count_mode = !profile_count_mode;
  1964. #ifdef PROFILED
  1965. /*
  1966. * PROFILED is intended to be defined if we were compiled with a -p option,
  1967. * and we take system dependent action to dump out results (e.g. on some systems
  1968. * it may be useful to invoke monitor() or moncontrol() here.
  1969. */
  1970. #ifdef SHOW_COUNTS_AVAILABLE
  1971. show_counts();
  1972. write_profile("counts"); /* Useful if -px option to compiler */
  1973. #endif
  1974. #endif /* PROFILED */
  1975. { char *vf = (char *)vfringe,
  1976. *vl = (char *)vheaplimit;
  1977. unsignedxx len = (unsignedxx)(vf - (vl - (CSL_PAGE_SIZE - 8)));
  1978. /*
  1979. * Set up the current page so I can tell where the active data is.
  1980. */
  1981. car32(vl - (CSL_PAGE_SIZE - 8)) = len;
  1982. }
  1983. for (pass=0; pass<2; pass++)
  1984. { for (j=0; j<vheap_pages_count; j++)
  1985. { void *page = vheap_pages[j];
  1986. char *low = (char *)doubleword_align_up((intxx)page);
  1987. char *high = low + car32(low);
  1988. low += 8;
  1989. while (low<high)
  1990. { Header h = *(Header *)low;
  1991. if (is_symbol_header(h))
  1992. { Lisp_Object e = qenv(low + TAG_SYMBOL);
  1993. intxx clen = 0;
  1994. unsignedxx n;
  1995. if (is_cons(e))
  1996. { e = qcar(e);
  1997. if (is_bps(e))
  1998. { Header ch = *(Header *)(data_of_bps(e) - CELL);
  1999. clen = length_of_header(ch);
  2000. }
  2001. }
  2002. n = qcount(low + TAG_SYMBOL);
  2003. if (n != 0 && clen != 0)
  2004. { double w = (double)n/(double)clen;
  2005. /*
  2006. * Here I want a measure that will give a good idea of how worthwhile it
  2007. * would be to compile the given function into C - what I have chosen is
  2008. * a count of bytecodes executed scaled by the length
  2009. * of the bytestream code defining the function. This will cause "good value"
  2010. * cases to show up best. I scale this relative to the total across all
  2011. * functions recorded to make the numbers less sensitive to details of
  2012. * how I generate test cases. For interest I also display the proportion
  2013. * of actual bytecodes interpreted. In each case I record these out of
  2014. * a total of 100.0 (percent) to give comfortable ranges of numbers to admire.
  2015. */
  2016. if (pass == 0) itotal += (double)n, total += w;
  2017. else
  2018. { if (w/total > 0.00001 ||
  2019. (double)n/itotal > 0.0001)
  2020. { if ((what & 6) == 0)
  2021. { if (buffp == buffn)
  2022. { buffn += 100;
  2023. buff = (mapstore_item *)
  2024. (*realloc_hook)((void *)buff,
  2025. sizeof(mapstore_item)*buffn);
  2026. if (buff == NULL) return onevalue(nil);
  2027. }
  2028. buff[buffp].w = 100.0*w/total;
  2029. buff[buffp].n = 100.0*(double)n/itotal;
  2030. buff[buffp].n1 = n;
  2031. buff[buffp].p = (Lisp_Object)(low + TAG_SYMBOL);
  2032. buffp++;
  2033. }
  2034. if ((what & 2) != 0)
  2035. { Lisp_Object w1;
  2036. /* Result is a list of items ((name size bytes-executed) ...).
  2037. * You might think that I needed to push res here - but I abort if there
  2038. * is a GC, so it is not necessary after all.
  2039. */
  2040. w1 = list3((Lisp_Object)(low + TAG_SYMBOL),
  2041. fixnum_of_int(clen),
  2042. fixnum_of_int(n));
  2043. nil = C_nil;
  2044. if (exception_pending() || gcn != gc_number)
  2045. return nil;
  2046. res = cons(w1, res);
  2047. nil = C_nil;
  2048. if (exception_pending() || gcn != gc_number)
  2049. return nil;
  2050. }
  2051. }
  2052. /*
  2053. * Reset count unless 1 bit of arg is set
  2054. */
  2055. if ((what & 1) == 0)
  2056. qcount(low + TAG_SYMBOL) = 0;
  2057. }
  2058. }
  2059. low += symhdr_length;
  2060. }
  2061. else low += (intxx)doubleword_align_up(length_of_header(h));
  2062. }
  2063. }
  2064. }
  2065. if ((what & 6) == 0)
  2066. { double running = 0.0;
  2067. qsort((void *)buff, buffp, sizeof(buff[0]), profile_cf);
  2068. trace_printf("\n Value %%bytes (So far) Bytecodes Function name\n");
  2069. for (j=0; j<buffp; j++)
  2070. { running += buff[j].n;
  2071. trace_printf("%7.2f %7.2f (%6.2f) %9lu: ",
  2072. buff[j].w, buff[j].n, running, (long unsigned)buff[j].n1);
  2073. prin_to_trace(buff[j].p);
  2074. trace_printf("\n");
  2075. }
  2076. trace_printf("\n");
  2077. (*free_hook)((void *)buff);
  2078. }
  2079. return onevalue(res);
  2080. #endif /* DEMO_MODE */
  2081. }
  2082. Lisp_Object MS_CDECL Lmapstore0(Lisp_Object nil, int nargs, ...)
  2083. {
  2084. argcheck(nargs, 0, "mapstore");
  2085. return Lmapstore(nil, nil);
  2086. }
  2087. static CSLbool reset_limit_registers(intxx vheap_need,
  2088. intxx bps_need,
  2089. intxx native_need)
  2090. /*
  2091. * returns YES if after resetting the limit registers there was
  2092. * enough space left for me to proceed. Return NO on failure, ie
  2093. * need for a more genuine GC.
  2094. */
  2095. {
  2096. void *p;
  2097. nil_as_base
  2098. unsignedxx len;
  2099. CSLbool full;
  2100. #ifndef NO_COPYING_GC
  2101. if (gc_method_is_copying)
  2102. full = (pages_count <=
  2103. heap_pages_count + vheap_pages_count +
  2104. bps_pages_count + native_pages_count);
  2105. else
  2106. #endif
  2107. full = (pages_count == 0);
  2108. if (fringe <= heaplimit)
  2109. { if (full) return NO;
  2110. p = pages[--pages_count];
  2111. zero_out(p);
  2112. heap_pages[heap_pages_count++] = p;
  2113. heaplimit = quadword_align_up((intxx)p);
  2114. car32(heaplimit) = CSL_PAGE_SIZE;
  2115. fringe = (Lisp_Object)((char *)heaplimit + CSL_PAGE_SIZE);
  2116. heaplimit = (Lisp_Object)((char *)heaplimit + SPARE);
  2117. }
  2118. { char *vh = (char *)vheaplimit,
  2119. *vf = (char *)vfringe;
  2120. len = (unsignedxx)(vh - vf);
  2121. }
  2122. if (vheap_need > (intxx)len)
  2123. { char *vf, *vh;
  2124. if (full) return NO;
  2125. p = pages[--pages_count];
  2126. zero_out(p);
  2127. vheap_pages[vheap_pages_count++] = p;
  2128. vf = (char *)doubleword_align_up((intxx)p) + 8;
  2129. vfringe = (Lisp_Object)vf;
  2130. vh = vf + (CSL_PAGE_SIZE - 16);
  2131. vheaplimit = (Lisp_Object)vh;
  2132. len = (unsignedxx)(vf - (vh - (CSL_PAGE_SIZE - 8)));
  2133. car32(vh - (CSL_PAGE_SIZE - 8)) = len;
  2134. }
  2135. { char *cl = (char *)codelimit,
  2136. *cf = (char *)codefringe;
  2137. len = (unsignedxx)(cf - cl);
  2138. }
  2139. if (bps_need != 0 && bps_need >= (intxx)len)
  2140. { char *cl;
  2141. if (full || bps_pages_count >= MAX_BPS_PAGES - 1) return NO;
  2142. p = pages[--pages_count];
  2143. zero_out(p);
  2144. bps_pages[bps_pages_count++] = p;
  2145. cl = (char *)doubleword_align_up((intxx)p);
  2146. codefringe = (Lisp_Object)(cl + CSL_PAGE_SIZE);
  2147. codelimit = (Lisp_Object)(cl + 8);
  2148. }
  2149. if (native_need != 0)
  2150. { if (full || native_pages_count >= MAX_NATIVE_PAGES - 1) return NO;
  2151. p = pages[--pages_count];
  2152. zero_out(p);
  2153. native_pages[native_pages_count++] = p;
  2154. native_fringe = 8;
  2155. }
  2156. return (stack < stacklimit);
  2157. }
  2158. static void tidy_fringes(void)
  2159. /*
  2160. * heaplimit was SPARE bytes above the actual base of the page,
  2161. * so the next line dumps fringe somewhere where it can be found
  2162. * later on when needed while scanning a page of heap. Similarly
  2163. * vfringe is stashed away at the end of its page.
  2164. */
  2165. { nil_as_base
  2166. char *fr = (char *)fringe,
  2167. *vf = (char *)vfringe,
  2168. *cf = (char *)codefringe,
  2169. *hl = (char *)heaplimit,
  2170. *vl = (char *)vheaplimit,
  2171. *cl = (char *)codelimit;
  2172. unsignedxx len = (unsignedxx)(fr - (hl - SPARE));
  2173. car32(hl - SPARE) = len;
  2174. len = (unsignedxx)(vf - (vl - (CSL_PAGE_SIZE - 8)));
  2175. car32(vl - (CSL_PAGE_SIZE - 8)) = (Lisp_Object)len;
  2176. if (codelimit != 0)
  2177. { len = (unsignedxx)(cf - (cl - 8));
  2178. car32(cl - 8) = len;
  2179. }
  2180. }
  2181. static void lose_dead_hashtables(void)
  2182. /*
  2183. * This splices out from the list of hash tables all entries that point to
  2184. * tables that have not been marked or copied this garbage collection.
  2185. */
  2186. {
  2187. Lisp_Object *p = &eq_hash_tables, q, r;
  2188. while ((q = *p) != C_nil)
  2189. { Header h;
  2190. r = qcar(q);
  2191. h = vechdr(r);
  2192. if (is_odds(h) && !is_marked_h(h)) *p = qcdr(q);
  2193. else p = &qcdr(q);
  2194. }
  2195. p = &equal_hash_tables;
  2196. while ((q = *p) != C_nil)
  2197. { Header h;
  2198. r = qcar(q);
  2199. h = vechdr(r);
  2200. if (is_odds(h) && !is_marked_h(h)) *p = qcdr(q);
  2201. else p = &qcdr(q);
  2202. }
  2203. }
  2204. #ifdef DEMO_MODE
  2205. extern CSLbool terminal_pushed;
  2206. void give_up()
  2207. {
  2208. Lisp_Object nil;
  2209. #define m(s) err_printf(s)
  2210. m("\n+++ DEMONSTRATION VERSION OF REDUCE - RESOURCE LIMIT EXCEEDED +++\n");
  2211. m("This version of REDUCE has been provided for testing and\n");
  2212. m("demonstration purposes. It has a built-in cut-out that will\n");
  2213. m("terminate processing after a time that should be sufficient for\n");
  2214. m("various small tests to run, but which will probably stop it\n");
  2215. m("from being useful as a serious tool. You are permitted to copy\n");
  2216. m("the demonstration version and pass it on to friends subject to\n");
  2217. m("not changing it, and in particular neither changing the various\n");
  2218. m("messages it prints nor attempting to circumvent the time-out\n");
  2219. m("mechanism. Full versions of REDUCE are available to run on a\n");
  2220. m("wide range of types of computer, and a machine-readable file\n");
  2221. m("listing suppliers was provided with the documentation that goes\n");
  2222. m("with this version. Some suppliers are:\n");
  2223. m(" Codemist Ltd, Alta, Horsecombe Vale, Combe Down, Bath BA2 5QR,\n");
  2224. m(" England. Phone and fax +44-225-837430,\n");
  2225. m(" http://www.codemist.co.uk\n");
  2226. m(" Winfried Neun, Konrad-Zuse-Zentrum fuer Informationstechnik Berlin\n");
  2227. m(" Heilbronner Str. 10, D 10711 Berlin-Wilmersdorf, GERMANY\n");
  2228. m(" Phone: +44-30-89604-195 Fax +49-30-89604-125.\n");
  2229. m(" (Codemist provided this version, the ZIB differs slightly)\n");
  2230. m("<Close window/type RETURN to exit>\n");
  2231. #undef m
  2232. nil = C_nil;
  2233. prompt_thing = CHAR_EOF; /* Disables the prompt */
  2234. ensure_screen();
  2235. terminal_pushed = NOT_CHAR;
  2236. tty_count = 0;
  2237. char_from_terminal(0);
  2238. my_exit(EXIT_FAILURE);
  2239. }
  2240. #endif
  2241. Lisp_Object reclaim(Lisp_Object p, char *why, int stg_class, intxx size)
  2242. {
  2243. intxx i;
  2244. clock_t t0, t1, t2, t3;
  2245. int bottom_page_number;
  2246. Lisp_Object *sp, nil = C_nil;
  2247. intxx vheap_need = 0, bps_need = 0, native_need = 0;
  2248. #if defined(FLEX) && defined(UNIX)
  2249. extern int timnag_();
  2250. timnag_();
  2251. #endif
  2252. stop_after_gc = 0;
  2253. if (stg_class == GC_VEC) vheap_need = size;
  2254. else if (stg_class == GC_BPS) bps_need = size;
  2255. else if (stg_class == GC_NATIVE) native_need = size;
  2256. already_in_gc = YES;
  2257. #ifdef SOCKETS
  2258. if (socket_server != 0)
  2259. { time_t tt0 = time(NULL);
  2260. t0 = clock();
  2261. tt0 = time(NULL);
  2262. if (t0 > cpu_timeout ||
  2263. tt0 > elapsed_timeout)
  2264. { cpu_timeout = t0 + 20;
  2265. elapsed_timeout = tt0 + 20;
  2266. term_printf("\nSorry: timeout on this session. Closing down\n");
  2267. return Lstop(nil, fixnum_of_int(1));
  2268. }
  2269. }
  2270. #endif
  2271. push_clock(); t0 = base_time;
  2272. #ifdef TICK_STREAM
  2273. if (tick_pending != 0)
  2274. { tick_pending = 0;
  2275. heaplimit = saveheaplimit;
  2276. vheaplimit = savevheaplimit;
  2277. codelimit = savecodelimit;
  2278. stacklimit = savestacklimit;
  2279. tidy_fringes();
  2280. if (stg_class != GC_PRESERVE &&
  2281. stg_class != GC_USER_HARD &&
  2282. reset_limit_registers(vheap_need, bps_need, native_need))
  2283. { already_in_gc = NO;
  2284. tick_on_gc_exit = NO;
  2285. #ifdef POLL_FOR_ATTN
  2286. poll_for_attn();
  2287. nil = C_nil;
  2288. if (exception_pending())
  2289. { pop_clock();
  2290. return nil;
  2291. }
  2292. #endif
  2293. if (interrupt_pending)
  2294. { interrupt_pending = NO;
  2295. pop_clock();
  2296. return interrupted(p);
  2297. }
  2298. else
  2299. {
  2300. inject_randomness((int)clock());
  2301. accept_tick();
  2302. pop_clock();
  2303. return onevalue(p);
  2304. }
  2305. }
  2306. else tick_on_gc_exit = YES;
  2307. }
  2308. else
  2309. #else
  2310. if (interrupt_pending)
  2311. { if (tick_pending)
  2312. { tick_pending = 0;
  2313. heaplimit = saveheaplimit;
  2314. vheaplimit = savevheaplimit;
  2315. codelimit = savecodelimit;
  2316. stacklimit = savestacklimit;
  2317. }
  2318. tidy_fringes();
  2319. interrupt_pending = NO;
  2320. pop_clock();
  2321. return interrupted(p);
  2322. }
  2323. #endif
  2324. { tidy_fringes();
  2325. if (stg_class != GC_PRESERVE &&
  2326. stg_class != GC_USER_HARD &&
  2327. reset_limit_registers(vheap_need, bps_need, native_need))
  2328. { already_in_gc = NO;
  2329. pop_clock();
  2330. return onevalue(p); /* Soft GC */
  2331. }
  2332. }
  2333. if (stack >= stacklimit)
  2334. { stacklimit = &stacklimit[50]; /* Allow a bit of slack */
  2335. pop_clock();
  2336. return error(0, err_stack_overflow);
  2337. }
  2338. #ifdef MEMORY_TRACE
  2339. #ifndef CHECK_ONLY
  2340. identify_page_types();
  2341. memory_comment(4);
  2342. #endif
  2343. #endif
  2344. #ifdef DEMO_MODE
  2345. give_up();
  2346. pop_clock();
  2347. return nil;
  2348. #else
  2349. push(p);
  2350. gc_number++;
  2351. #ifdef WINDOW_SYSTEM
  2352. /*
  2353. * If I have a window system I tell it the current time every so often
  2354. * just to keep things cheery...
  2355. */
  2356. { long int t = (long int)(100.0 * consolidated_time[0]);
  2357. long int gct = (long int)(100.0 * gc_time);
  2358. /* /*
  2359. * I guess that I want garbage collection messages, if any, to
  2360. * be sent to stderr rather than whatever output stream happens to
  2361. * be selected at the time of the garbage collection?
  2362. * At present messages go to the normal output stream, which only makes
  2363. * sense if GC messages are almost always disabled - maybe that will
  2364. * be the case!
  2365. */
  2366. report_time(t, gct);
  2367. if (verbos_flag & 1)
  2368. { freshline_trace();
  2369. trace_printf(
  2370. "+++ Garbage collection %ld (%s) after %ld.%.2ld+%ld.%.2ld seconds\n",
  2371. (long)gc_number, why, t/100, t%100, gct/100, gct%100);
  2372. }
  2373. }
  2374. #else
  2375. if (verbos_flag & 1)
  2376. { long int t = (long int)(100.0 * consolidated_time[0]);
  2377. long int gct = (long int)(100.0 * gc_time);
  2378. /* /* I guess that I want garbage collection messages, if any, to
  2379. * be sent to stderr rather than whatever output stream happens to
  2380. * be selected at the time of the garbage collection?
  2381. * At present messages go to the normal output stream, which only makes
  2382. * sense if GC messages are almost always disabled - maybe that will
  2383. * be the case!
  2384. */
  2385. freshline_trace();
  2386. trace_printf(
  2387. "+++ Garbage collection %ld (%s) after %ld.%.2ld+%ld.%.2ld seconds\n",
  2388. (long)gc_number, why, t/100, t%100, gct/100, gct%100);
  2389. }
  2390. #endif
  2391. /*
  2392. * If things crash really badly maybe I would rather have my output up
  2393. * to date.
  2394. */
  2395. ensure_screen();
  2396. nil = C_nil;
  2397. if (exception_pending())
  2398. { stop_after_gc = 1;
  2399. flip_exception();
  2400. }
  2401. if (spool_file != NULL) fflush(spool_file);
  2402. copy_into_nilseg(NO);
  2403. cons_cells = symbol_heads = strings = user_vectors =
  2404. big_numbers = box_floats = bytestreams = other_mem =
  2405. litvecs = getvecs = 0;
  2406. #ifndef NO_COPYING_GC
  2407. if (gc_method_is_copying)
  2408. {
  2409. t2 = t1 = t0; /* Time is not split down in this case */
  2410. /*
  2411. * Set up the new half-space initially empty.
  2412. */
  2413. new_heap_pages_count = 0;
  2414. new_vheap_pages_count = 0;
  2415. new_bps_pages_count = 0;
  2416. trailing_heap_pages_count = 1;
  2417. trailing_vheap_pages_count = 1;
  2418. { void *pp = pages[--pages_count];
  2419. char *vf, *vl;
  2420. unsignedxx len;
  2421. /*
  2422. * A first page of (cons-)heap
  2423. */
  2424. zero_out(pp);
  2425. new_heap_pages[new_heap_pages_count++] = pp;
  2426. heaplimit = quadword_align_up((intxx)pp);
  2427. car32(heaplimit) = CSL_PAGE_SIZE;
  2428. vl = (char *)heaplimit;
  2429. fringe = (Lisp_Object)(vl + CSL_PAGE_SIZE);
  2430. heaplimit = (Lisp_Object)(vl + SPARE);
  2431. #ifdef DEBUG_GC
  2432. term_printf("fr = %.8lx, hl = %.8lx\n", (long)fringe, (long)heaplimit);
  2433. #endif
  2434. /*
  2435. * A first page of vector heap.
  2436. */
  2437. pp = pages[--pages_count];
  2438. zero_out(pp);
  2439. new_vheap_pages[new_vheap_pages_count++] = pp;
  2440. vf = (char *)doubleword_align_up((intxx)pp) + 8;
  2441. vfringe = (Lisp_Object)vf;
  2442. vl = vf + (CSL_PAGE_SIZE - 16);
  2443. vheaplimit = (Lisp_Object)vl;
  2444. len = (unsignedxx)(vf - (vl - (CSL_PAGE_SIZE - 8)));
  2445. car32(vl - (CSL_PAGE_SIZE - 8)) = (Lisp_Object)len;
  2446. /*
  2447. * The BPS heap can start of utterly non-existent.
  2448. */
  2449. codefringe = codelimit = 0;
  2450. }
  2451. /*
  2452. * The very first thing that I will copy will be the main object-vector,
  2453. * this is done early to ensure that it gets a full empty page of vector
  2454. * heap to fit into.
  2455. */
  2456. copy(&BASE[current_package_offset]);
  2457. /*
  2458. * The above line is "really"
  2459. * copy(&current_package);
  2460. * but I use an offset into the nilseg in explicit for because otherwise
  2461. * there is a big foul-up with the NILSEG_EXTERNS option... Sorry!
  2462. */
  2463. /*
  2464. * I should remind you, gentle reader, that the value cell
  2465. * and env cells of nil will always contain nil, which does not move,
  2466. * and so I do not need to copy them here.
  2467. */
  2468. copy(&(qplist(nil)));
  2469. copy(&(qpname(nil)));
  2470. copy(&(qfastgets(nil)));
  2471. #ifdef COMMON
  2472. copy(&(qpackage(nil)));
  2473. #endif
  2474. /*
  2475. * I dislike the special treatment of current_package that follows. Maybe
  2476. * I should arrange something totally different for copying the package
  2477. * structure...
  2478. */
  2479. for (i = first_nil_offset; i<last_nil_offset; i++)
  2480. if (i != current_package_offset)
  2481. /* current-package - already copied by hand */
  2482. copy(&BASE[i]);
  2483. for (sp=stack; sp>(Lisp_Object *)stackbase; sp--) copy(sp);
  2484. /*
  2485. * Now I need to perform some magic on the list of hash tables...
  2486. */
  2487. lose_dead_hashtables();
  2488. copy(&eq_hash_tables);
  2489. copy(&equal_hash_tables);
  2490. tidy_fringes();
  2491. /*
  2492. * Throw away the old semi-space - it is now junk.
  2493. */
  2494. while (heap_pages_count!=0)
  2495. pages[pages_count++] = heap_pages[--heap_pages_count];
  2496. while (vheap_pages_count!=0)
  2497. pages[pages_count++] = vheap_pages[--vheap_pages_count];
  2498. while (bps_pages_count!=0)
  2499. pages[pages_count++] = bps_pages[--bps_pages_count];
  2500. /*
  2501. * Flip the descriptors for the old and new semi-spaces.
  2502. */
  2503. { void **w = heap_pages;
  2504. heap_pages = new_heap_pages;
  2505. new_heap_pages = w;
  2506. w = vheap_pages;
  2507. vheap_pages = new_vheap_pages;
  2508. new_vheap_pages = w;
  2509. w = bps_pages;
  2510. bps_pages = new_bps_pages;
  2511. new_bps_pages = w;
  2512. heap_pages_count = new_heap_pages_count;
  2513. new_heap_pages_count = 0;
  2514. vheap_pages_count = new_vheap_pages_count;
  2515. new_vheap_pages_count = 0;
  2516. bps_pages_count = new_bps_pages_count;
  2517. new_bps_pages_count = 0;
  2518. }
  2519. }
  2520. else
  2521. #endif /* NO_COPYING_GC */
  2522. {
  2523. /*
  2524. * The list bases to mark from are
  2525. * (a) nil [NB: mark(nil) would be ineffective],
  2526. * (b) the special ones addressed relative to nil,
  2527. * (c) everything on the Lisp stack,
  2528. * (d) the package structure,
  2529. * (e) the argument (p) passed to reclaim().
  2530. */
  2531. qheader(nil) = set_mark_bit_h(qheader(nil));
  2532. /* nil has nil as value & env ... */
  2533. mark(&qplist(nil)); /* ... thus only its plist and ... */
  2534. mark(&qpname(nil)); /* ... pname cell need marking, */
  2535. /* ... since packages are done later */
  2536. mark(&qfastgets(nil)); /* + the fastgets vector, if any */
  2537. for (i = first_nil_offset; i<last_nil_offset; i++)
  2538. {
  2539. mark(&BASE[i]);
  2540. #ifdef TICK_STREAM
  2541. if (tick_on_gc_exit)
  2542. {
  2543. inject_randomness((int)clock());
  2544. accept_tick();
  2545. tick_on_gc_exit = NO;
  2546. }
  2547. #endif
  2548. }
  2549. for (sp=stack; sp>(Lisp_Object *)stackbase; sp--)
  2550. {
  2551. mark(sp);
  2552. #ifdef TICK_STREAM
  2553. if (tick_on_gc_exit)
  2554. {
  2555. inject_randomness((int)clock());
  2556. accept_tick();
  2557. tick_on_gc_exit = NO;
  2558. }
  2559. #endif
  2560. }
  2561. /*
  2562. * Now I need to perform some magic on the list of hash tables...
  2563. */
  2564. lose_dead_hashtables();
  2565. mark(&eq_hash_tables);
  2566. mark(&equal_hash_tables);
  2567. /*
  2568. * What about the package structure... ? I assume it has been marked by
  2569. * what I have just done.
  2570. */
  2571. qheader(nil) = clear_mark_bit_h(qheader(nil));
  2572. t1 = read_clock();
  2573. bottom_page_number = compress_heap(); /* Folds cons cells upwards */
  2574. t2 = read_clock();
  2575. /*
  2576. * Again I should remind you, gentle reader, that the value cell
  2577. * and env cells of nil will always contain nil, which does not move,
  2578. * and so I do not need to relocate them here.
  2579. */
  2580. relocate(&(qplist(nil)));
  2581. /* relocate(&(qpname(nil))); never a cons cell */
  2582. relocate(&(qfastgets(nil)));
  2583. #ifdef COMMON
  2584. relocate(&(qpackage(nil)));
  2585. #endif
  2586. for (i = first_nil_offset; i<last_nil_offset; i++)
  2587. relocate(&BASE[i]);
  2588. for (sp=stack; sp>(Lisp_Object *)stackbase; sp--) relocate(sp);
  2589. relocate_consheap(bottom_page_number);
  2590. relocate(&eq_hash_tables);
  2591. relocate(&equal_hash_tables);
  2592. relocate_vecheap();
  2593. { char *fr = (char *)fringe,
  2594. *vf = (char *)vfringe,
  2595. *cf = (char *)codefringe,
  2596. *hl = (char *)heaplimit,
  2597. *vl = (char *)vheaplimit,
  2598. *cl = (char *)codelimit;
  2599. unsignedxx len = (unsignedxx)(fr - (hl - SPARE));
  2600. car32(hl - SPARE) = len;
  2601. len = (unsignedxx)(vf - (vl - (CSL_PAGE_SIZE - 8)));
  2602. car32(vl - (CSL_PAGE_SIZE - 8)) = len;
  2603. if (codelimit != 0)
  2604. { len = (unsignedxx)(cf - (cl - 8));
  2605. car32(cl - 8) = len;
  2606. }
  2607. }
  2608. abandon_heap_pages(bottom_page_number);
  2609. }
  2610. { Lisp_Object qq;
  2611. /*
  2612. * Note that EQUAL hash tables do not need to be rehashed here, though
  2613. * they do if a heap image is exported from one system to another.
  2614. */
  2615. for (qq = eq_hash_tables; qq!=nil; qq=qcdr(qq))
  2616. rehash_this_table(qcar(qq));
  2617. }
  2618. gc_time += pop_clock();
  2619. t3 = base_time;
  2620. copy_out_of_nilseg(NO);
  2621. if ((verbos_flag & 5) == 5)
  2622. /*
  2623. * (verbos 4) gets the system to tell me how long each phase of GC took,
  2624. * but (verbos 1) must be ORd in too.
  2625. */
  2626. {
  2627. #ifndef NO_COPYING_GC
  2628. if (gc_method_is_copying)
  2629. trace_printf("Copy %ld ms\n",
  2630. (long int)(1000.0 * (double)(t3-t0)/(double)CLOCKS_PER_SEC));
  2631. else
  2632. #endif
  2633. trace_printf("Mark %ld, compact %ld, relocate %ld ms\n",
  2634. (long int)(1000.0 * (double)(t1-t0)/(double)CLOCKS_PER_SEC),
  2635. (long int)(1000.0 * (double)(t2-t1)/(double)CLOCKS_PER_SEC),
  2636. (long int)(1000.0 * (double)(t3-t2)/(double)CLOCKS_PER_SEC));
  2637. }
  2638. /* (verbos 5) causes a display breaking down how space is used */
  2639. if ((verbos_flag & 5) == 5)
  2640. { trace_printf(
  2641. "cons_cells=%d, symbol_heads=%d, strings=%d, user_vectors=%d\n",
  2642. cons_cells, symbol_heads, strings, user_vectors-litvecs-getvecs);
  2643. trace_printf(
  2644. "bignums=%d, floats=%d, bytestreams=%d, other=%d, litvecs=%d\n",
  2645. big_numbers, box_floats, bytestreams, other_mem, litvecs);
  2646. trace_printf("getvecs=%d\n", getvecs);
  2647. }
  2648. pop(p);
  2649. if (!reset_limit_registers(vheap_need, bps_need, native_need))
  2650. { term_printf("\n+++ No space left at all\n");
  2651. my_exit(EXIT_FAILURE); /* totally drastic... */
  2652. }
  2653. #ifndef HOLD_BACK_MEMORY
  2654. #ifndef MEMORY_TRACE
  2655. /*
  2656. * Here I grab more memory (if I am allowed to).
  2657. * An initial version here, and one still suitable on machines that will
  2658. * have plenty of real memory, will be to defined ok_to_grab_memory(n) as
  2659. * 3*n + 2. This expands until the proportion of the heap active at the
  2660. * end of garbage collection is less than 1/4.
  2661. * If the attempt to grab more memory fails I clear the bit in init_flags
  2662. * that allows me to try to expand, so I will not waste time again. If
  2663. * HOLD_BACK_MEMORY was asserted (for machines where grabbing all seemingly
  2664. * available memory may cause a crash) I do not try this operation. The
  2665. * aim of keeping the heap less than half full is an heuristic and could be
  2666. * adjusted on the basis of experience with this code.
  2667. * The "+2" at the end of calculating the ideal heap size is intended
  2668. * to keep us (mostly) in the copying GC domain. If it is omitted the
  2669. * heap tends to stay just 25% full and sliding GC is used. Overall this is
  2670. * roughly as expensive as copying, but it is more disruptive since it comes
  2671. * in larger gulps.
  2672. * On systems where it is possible to measure the amount of available
  2673. * real memory more sophisticated calculations may be possible.
  2674. */
  2675. if (init_flags & INIT_EXPANDABLE)
  2676. { int32 ideal = ok_to_grab_memory(heap_pages_count +
  2677. vheap_pages_count +
  2678. bps_pages_count);
  2679. int32 more;
  2680. if (ideal > MAX_PAGES) ideal = MAX_PAGES;
  2681. more = ideal - pages_count;
  2682. while (more-- > 0)
  2683. { void *page = (void *)my_malloc((size_t)(CSL_PAGE_SIZE + 8));
  2684. intxx pun, pun1;
  2685. /*
  2686. * When I first grabbed memory in restart.c I used my_malloc_1(), which
  2687. * gobbles a large stack frame and then called regular malloc - the idea
  2688. * there was to avoid malloc grabbing space needed for the stack. I can
  2689. * not properly do that here since reclaim() may be called with a deep
  2690. * stack already active. There is thus a danger that expanding the heap here
  2691. * may cause me to run out of stack elsewhere. Oh well, I guess I can not
  2692. * win in all ways.
  2693. */
  2694. /*
  2695. * Verify that new block does not span zero & has correct sign bit
  2696. */
  2697. pun = (intxx)page;
  2698. pun1 = (intxx)((char *)page + CSL_PAGE_SIZE + 8);
  2699. if ((pun ^ pun1) < 0) page = NULL;
  2700. #ifdef ADDRESS_SIGN_UNKNOWN
  2701. if ((pun + address_sign) < 0) page = NULL;
  2702. #else
  2703. #ifdef ADDRESSES_HAVE_TOP_BIT_SET
  2704. if (pun > 0) page = NULL;
  2705. #else
  2706. if (pun < 0) page = NULL;
  2707. #endif
  2708. #endif
  2709. if (page == NULL)
  2710. { init_flags &= ~INIT_EXPANDABLE;
  2711. break;
  2712. }
  2713. else pages[pages_count++] = page;
  2714. }
  2715. }
  2716. #endif /* MEMORY_TRACE */
  2717. #endif /* HOLD_BACK_MEMORY */
  2718. #ifdef WINDOW_SYSTEM
  2719. { int32 n = heap_pages_count + vheap_pages_count + bps_pages_count;
  2720. int32 n1 = n + pages_count;
  2721. double z = (100.0*n)/n1;
  2722. report_space(gc_number, z);
  2723. if (verbos_flag & 1) trace_printf(
  2724. "At gc end about %.1f Mbytes of %.1f (%.1f%%) of heap is in use\n",
  2725. ((double)n)*(CSL_PAGE_SIZE/(1024.0*1024.0)),
  2726. ((double)n1)*(CSL_PAGE_SIZE/(1024.0*1024.0)), z);
  2727. }
  2728. #else
  2729. if (verbos_flag & 1)
  2730. { int32 n = heap_pages_count + vheap_pages_count + bps_pages_count;
  2731. int32 n1 = n + pages_count;
  2732. trace_printf(
  2733. "At gc end about %.1f Mbytes of %.1f (%.1f%%) of heap is in use\n",
  2734. (double)n*(CSL_PAGE_SIZE/(1024.0*1024.0)),
  2735. (double)n1*(CSL_PAGE_SIZE/(1024.0*1024.0)),
  2736. (100.0*n)/n1);
  2737. }
  2738. #endif
  2739. #ifndef NO_COPYING_GC
  2740. /*
  2741. * I will make the next garbage collection a copying one if the heap is
  2742. * at most 25% full, or a sliding one if it is more full than that.
  2743. */
  2744. gc_method_is_copying = (pages_count >
  2745. 3*(heap_pages_count + vheap_pages_count + bps_pages_count));
  2746. #endif
  2747. if (stop_after_gc)
  2748. {
  2749. #ifdef MEMORY_TRACE
  2750. #ifndef CHECK_ONLY
  2751. memory_comment(15);
  2752. #endif
  2753. #endif
  2754. return Lstop(nil, fixnum_of_int(0));
  2755. }
  2756. #ifdef POLL_FOR_ATTN
  2757. poll_for_attn(); /* Should cause ctrl-C checking */
  2758. nil = C_nil;
  2759. if (exception_pending())
  2760. {
  2761. #ifdef MEMORY_TRACE
  2762. #ifndef CHECK_ONLY
  2763. memory_comment(15);
  2764. #endif
  2765. #endif
  2766. return nil;
  2767. }
  2768. #endif
  2769. #ifdef MEMORY_TRACE
  2770. #ifndef CHECK_ONLY
  2771. memory_comment(15);
  2772. #endif
  2773. #endif
  2774. if (interrupt_pending)
  2775. { interrupt_pending = NO;
  2776. already_in_gc = NO;
  2777. tick_on_gc_exit = NO;
  2778. return interrupted(p);
  2779. }
  2780. already_in_gc = NO;
  2781. if (tick_on_gc_exit)
  2782. {
  2783. inject_randomness((int)clock());
  2784. accept_tick();
  2785. nil = C_nil;
  2786. if (exception_pending()) return nil;
  2787. tick_on_gc_exit = NO;
  2788. return onevalue(p);
  2789. }
  2790. return onevalue(p);
  2791. #endif /* DEMO_MODE */
  2792. }
  2793. /* end of file gc.c */