typd_mlc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  3. * opyright (c) 1999-2000 by Hewlett-Packard Company. All rights reserved.
  4. *
  5. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  6. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  7. *
  8. * Permission is hereby granted to use or copy this program
  9. * for any purpose, provided the above notices are retained on all copies.
  10. * Permission to modify the code and to distribute modified code is granted,
  11. * provided the above notices are retained, and a notice that the code was
  12. * modified is included with the above copyright notice.
  13. *
  14. */
  15. /*
  16. * Some simple primitives for allocation with explicit type information.
  17. * Simple objects are allocated such that they contain a GC_descr at the
  18. * end (in the last allocated word). This descriptor may be a procedure
  19. * which then examines an extended descriptor passed as its environment.
  20. *
  21. * Arrays are treated as simple objects if they have sufficiently simple
  22. * structure. Otherwise they are allocated from an array kind that supplies
  23. * a special mark procedure. These arrays contain a pointer to a
  24. * complex_descriptor as their last word.
  25. * This is done because the environment field is too small, and the collector
  26. * must trace the complex_descriptor.
  27. *
  28. * Note that descriptors inside objects may appear cleared, if we encounter a
  29. * false refrence to an object on a free list. In the GC_descr case, this
  30. * is OK, since a 0 descriptor corresponds to examining no fields.
  31. * In the complex_descriptor case, we explicitly check for that case.
  32. *
  33. * MAJOR PARTS OF THIS CODE HAVE NOT BEEN TESTED AT ALL and are not testable,
  34. * since they are not accessible through the current interface.
  35. */
  36. #include "private/gc_pmark.h"
  37. #include "gc_typed.h"
  38. # define TYPD_EXTRA_BYTES (sizeof(word) - EXTRA_BYTES)
  39. GC_bool GC_explicit_typing_initialized = FALSE;
  40. int GC_explicit_kind; /* Object kind for objects with indirect */
  41. /* (possibly extended) descriptors. */
  42. int GC_array_kind; /* Object kind for objects with complex */
  43. /* descriptors and GC_array_mark_proc. */
  44. /* Extended descriptors. GC_typed_mark_proc understands these. */
  45. /* These are used for simple objects that are larger than what */
  46. /* can be described by a BITMAP_BITS sized bitmap. */
  47. typedef struct {
  48. word ed_bitmap; /* lsb corresponds to first word. */
  49. GC_bool ed_continued; /* next entry is continuation. */
  50. } ext_descr;
  51. /* Array descriptors. GC_array_mark_proc understands these. */
  52. /* We may eventually need to add provisions for headers and */
  53. /* trailers. Hence we provide for tree structured descriptors, */
  54. /* though we don't really use them currently. */
  55. typedef union ComplexDescriptor {
  56. struct LeafDescriptor { /* Describes simple array */
  57. word ld_tag;
  58. # define LEAF_TAG 1
  59. word ld_size; /* bytes per element */
  60. /* multiple of ALIGNMENT */
  61. word ld_nelements; /* Number of elements. */
  62. GC_descr ld_descriptor; /* A simple length, bitmap, */
  63. /* or procedure descriptor. */
  64. } ld;
  65. struct ComplexArrayDescriptor {
  66. word ad_tag;
  67. # define ARRAY_TAG 2
  68. word ad_nelements;
  69. union ComplexDescriptor * ad_element_descr;
  70. } ad;
  71. struct SequenceDescriptor {
  72. word sd_tag;
  73. # define SEQUENCE_TAG 3
  74. union ComplexDescriptor * sd_first;
  75. union ComplexDescriptor * sd_second;
  76. } sd;
  77. } complex_descriptor;
  78. #define TAG ld.ld_tag
  79. ext_descr * GC_ext_descriptors; /* Points to array of extended */
  80. /* descriptors. */
  81. word GC_ed_size = 0; /* Current size of above arrays. */
  82. # define ED_INITIAL_SIZE 100;
  83. word GC_avail_descr = 0; /* Next available slot. */
  84. int GC_typed_mark_proc_index; /* Indices of my mark */
  85. int GC_array_mark_proc_index; /* procedures. */
  86. /* Add a multiword bitmap to GC_ext_descriptors arrays. Return */
  87. /* starting index. */
  88. /* Returns -1 on failure. */
  89. /* Caller does not hold allocation lock. */
  90. signed_word GC_add_ext_descriptor(bm, nbits)
  91. GC_bitmap bm;
  92. word nbits;
  93. {
  94. register size_t nwords = divWORDSZ(nbits + WORDSZ-1);
  95. register signed_word result;
  96. register word i;
  97. register word last_part;
  98. register int extra_bits;
  99. DCL_LOCK_STATE;
  100. DISABLE_SIGNALS();
  101. LOCK();
  102. while (GC_avail_descr + nwords >= GC_ed_size) {
  103. ext_descr * new;
  104. size_t new_size;
  105. word ed_size = GC_ed_size;
  106. UNLOCK();
  107. ENABLE_SIGNALS();
  108. if (ed_size == 0) {
  109. new_size = ED_INITIAL_SIZE;
  110. } else {
  111. new_size = 2 * ed_size;
  112. if (new_size > MAX_ENV) return(-1);
  113. }
  114. new = (ext_descr *) GC_malloc_atomic(new_size * sizeof(ext_descr));
  115. if (new == 0) return(-1);
  116. DISABLE_SIGNALS();
  117. LOCK();
  118. if (ed_size == GC_ed_size) {
  119. if (GC_avail_descr != 0) {
  120. BCOPY(GC_ext_descriptors, new,
  121. GC_avail_descr * sizeof(ext_descr));
  122. }
  123. GC_ed_size = new_size;
  124. GC_ext_descriptors = new;
  125. } /* else another thread already resized it in the meantime */
  126. }
  127. result = GC_avail_descr;
  128. for (i = 0; i < nwords-1; i++) {
  129. GC_ext_descriptors[result + i].ed_bitmap = bm[i];
  130. GC_ext_descriptors[result + i].ed_continued = TRUE;
  131. }
  132. last_part = bm[i];
  133. /* Clear irrelevant bits. */
  134. extra_bits = nwords * WORDSZ - nbits;
  135. last_part <<= extra_bits;
  136. last_part >>= extra_bits;
  137. GC_ext_descriptors[result + i].ed_bitmap = last_part;
  138. GC_ext_descriptors[result + i].ed_continued = FALSE;
  139. GC_avail_descr += nwords;
  140. UNLOCK();
  141. ENABLE_SIGNALS();
  142. return(result);
  143. }
  144. /* Table of bitmap descriptors for n word long all pointer objects. */
  145. GC_descr GC_bm_table[WORDSZ/2];
  146. /* Return a descriptor for the concatenation of 2 nwords long objects, */
  147. /* each of which is described by descriptor. */
  148. /* The result is known to be short enough to fit into a bitmap */
  149. /* descriptor. */
  150. /* Descriptor is a GC_DS_LENGTH or GC_DS_BITMAP descriptor. */
  151. GC_descr GC_double_descr(descriptor, nwords)
  152. register GC_descr descriptor;
  153. register word nwords;
  154. {
  155. if ((descriptor & GC_DS_TAGS) == GC_DS_LENGTH) {
  156. descriptor = GC_bm_table[BYTES_TO_WORDS((word)descriptor)];
  157. };
  158. descriptor |= (descriptor & ~GC_DS_TAGS) >> nwords;
  159. return(descriptor);
  160. }
  161. complex_descriptor * GC_make_sequence_descriptor();
  162. /* Build a descriptor for an array with nelements elements, */
  163. /* each of which can be described by a simple descriptor. */
  164. /* We try to optimize some common cases. */
  165. /* If the result is COMPLEX, then a complex_descr* is returned */
  166. /* in *complex_d. */
  167. /* If the result is LEAF, then we built a LeafDescriptor in */
  168. /* the structure pointed to by leaf. */
  169. /* The tag in the leaf structure is not set. */
  170. /* If the result is SIMPLE, then a GC_descr */
  171. /* is returned in *simple_d. */
  172. /* If the result is NO_MEM, then */
  173. /* we failed to allocate the descriptor. */
  174. /* The implementation knows that GC_DS_LENGTH is 0. */
  175. /* *leaf, *complex_d, and *simple_d may be used as temporaries */
  176. /* during the construction. */
  177. # define COMPLEX 2
  178. # define LEAF 1
  179. # define SIMPLE 0
  180. # define NO_MEM (-1)
  181. int GC_make_array_descriptor(nelements, size, descriptor,
  182. simple_d, complex_d, leaf)
  183. word size;
  184. word nelements;
  185. GC_descr descriptor;
  186. GC_descr *simple_d;
  187. complex_descriptor **complex_d;
  188. struct LeafDescriptor * leaf;
  189. {
  190. # define OPT_THRESHOLD 50
  191. /* For larger arrays, we try to combine descriptors of adjacent */
  192. /* descriptors to speed up marking, and to reduce the amount */
  193. /* of space needed on the mark stack. */
  194. if ((descriptor & GC_DS_TAGS) == GC_DS_LENGTH) {
  195. if ((word)descriptor == size) {
  196. *simple_d = nelements * descriptor;
  197. return(SIMPLE);
  198. } else if ((word)descriptor == 0) {
  199. *simple_d = (GC_descr)0;
  200. return(SIMPLE);
  201. }
  202. }
  203. if (nelements <= OPT_THRESHOLD) {
  204. if (nelements <= 1) {
  205. if (nelements == 1) {
  206. *simple_d = descriptor;
  207. return(SIMPLE);
  208. } else {
  209. *simple_d = (GC_descr)0;
  210. return(SIMPLE);
  211. }
  212. }
  213. } else if (size <= BITMAP_BITS/2
  214. && (descriptor & GC_DS_TAGS) != GC_DS_PROC
  215. && (size & (sizeof(word)-1)) == 0) {
  216. int result =
  217. GC_make_array_descriptor(nelements/2, 2*size,
  218. GC_double_descr(descriptor,
  219. BYTES_TO_WORDS(size)),
  220. simple_d, complex_d, leaf);
  221. if ((nelements & 1) == 0) {
  222. return(result);
  223. } else {
  224. struct LeafDescriptor * one_element =
  225. (struct LeafDescriptor *)
  226. GC_malloc_atomic(sizeof(struct LeafDescriptor));
  227. if (result == NO_MEM || one_element == 0) return(NO_MEM);
  228. one_element -> ld_tag = LEAF_TAG;
  229. one_element -> ld_size = size;
  230. one_element -> ld_nelements = 1;
  231. one_element -> ld_descriptor = descriptor;
  232. switch(result) {
  233. case SIMPLE:
  234. {
  235. struct LeafDescriptor * beginning =
  236. (struct LeafDescriptor *)
  237. GC_malloc_atomic(sizeof(struct LeafDescriptor));
  238. if (beginning == 0) return(NO_MEM);
  239. beginning -> ld_tag = LEAF_TAG;
  240. beginning -> ld_size = size;
  241. beginning -> ld_nelements = 1;
  242. beginning -> ld_descriptor = *simple_d;
  243. *complex_d = GC_make_sequence_descriptor(
  244. (complex_descriptor *)beginning,
  245. (complex_descriptor *)one_element);
  246. break;
  247. }
  248. case LEAF:
  249. {
  250. struct LeafDescriptor * beginning =
  251. (struct LeafDescriptor *)
  252. GC_malloc_atomic(sizeof(struct LeafDescriptor));
  253. if (beginning == 0) return(NO_MEM);
  254. beginning -> ld_tag = LEAF_TAG;
  255. beginning -> ld_size = leaf -> ld_size;
  256. beginning -> ld_nelements = leaf -> ld_nelements;
  257. beginning -> ld_descriptor = leaf -> ld_descriptor;
  258. *complex_d = GC_make_sequence_descriptor(
  259. (complex_descriptor *)beginning,
  260. (complex_descriptor *)one_element);
  261. break;
  262. }
  263. case COMPLEX:
  264. *complex_d = GC_make_sequence_descriptor(
  265. *complex_d,
  266. (complex_descriptor *)one_element);
  267. break;
  268. }
  269. return(COMPLEX);
  270. }
  271. }
  272. {
  273. leaf -> ld_size = size;
  274. leaf -> ld_nelements = nelements;
  275. leaf -> ld_descriptor = descriptor;
  276. return(LEAF);
  277. }
  278. }
  279. complex_descriptor * GC_make_sequence_descriptor(first, second)
  280. complex_descriptor * first;
  281. complex_descriptor * second;
  282. {
  283. struct SequenceDescriptor * result =
  284. (struct SequenceDescriptor *)
  285. GC_malloc(sizeof(struct SequenceDescriptor));
  286. /* Can't result in overly conservative marking, since tags are */
  287. /* very small integers. Probably faster than maintaining type */
  288. /* info. */
  289. if (result != 0) {
  290. result -> sd_tag = SEQUENCE_TAG;
  291. result -> sd_first = first;
  292. result -> sd_second = second;
  293. }
  294. return((complex_descriptor *)result);
  295. }
  296. #ifdef UNDEFINED
  297. complex_descriptor * GC_make_complex_array_descriptor(nelements, descr)
  298. word nelements;
  299. complex_descriptor * descr;
  300. {
  301. struct ComplexArrayDescriptor * result =
  302. (struct ComplexArrayDescriptor *)
  303. GC_malloc(sizeof(struct ComplexArrayDescriptor));
  304. if (result != 0) {
  305. result -> ad_tag = ARRAY_TAG;
  306. result -> ad_nelements = nelements;
  307. result -> ad_element_descr = descr;
  308. }
  309. return((complex_descriptor *)result);
  310. }
  311. #endif
  312. ptr_t * GC_eobjfreelist;
  313. ptr_t * GC_arobjfreelist;
  314. mse * GC_typed_mark_proc GC_PROTO((register word * addr,
  315. register mse * mark_stack_ptr,
  316. mse * mark_stack_limit,
  317. word env));
  318. mse * GC_array_mark_proc GC_PROTO((register word * addr,
  319. register mse * mark_stack_ptr,
  320. mse * mark_stack_limit,
  321. word env));
  322. /* Caller does not hold allocation lock. */
  323. void GC_init_explicit_typing()
  324. {
  325. register int i;
  326. DCL_LOCK_STATE;
  327. # ifdef PRINTSTATS
  328. if (sizeof(struct LeafDescriptor) % sizeof(word) != 0)
  329. ABORT("Bad leaf descriptor size");
  330. # endif
  331. DISABLE_SIGNALS();
  332. LOCK();
  333. if (GC_explicit_typing_initialized) {
  334. UNLOCK();
  335. ENABLE_SIGNALS();
  336. return;
  337. }
  338. GC_explicit_typing_initialized = TRUE;
  339. /* Set up object kind with simple indirect descriptor. */
  340. GC_eobjfreelist = (ptr_t *)GC_new_free_list_inner();
  341. GC_explicit_kind = GC_new_kind_inner(
  342. (void **)GC_eobjfreelist,
  343. (((word)WORDS_TO_BYTES(-1)) | GC_DS_PER_OBJECT),
  344. TRUE, TRUE);
  345. /* Descriptors are in the last word of the object. */
  346. GC_typed_mark_proc_index = GC_new_proc_inner(GC_typed_mark_proc);
  347. /* Set up object kind with array descriptor. */
  348. GC_arobjfreelist = (ptr_t *)GC_new_free_list_inner();
  349. GC_array_mark_proc_index = GC_new_proc_inner(GC_array_mark_proc);
  350. GC_array_kind = GC_new_kind_inner(
  351. (void **)GC_arobjfreelist,
  352. GC_MAKE_PROC(GC_array_mark_proc_index, 0),
  353. FALSE, TRUE);
  354. for (i = 0; i < WORDSZ/2; i++) {
  355. GC_descr d = (((word)(-1)) >> (WORDSZ - i)) << (WORDSZ - i);
  356. d |= GC_DS_BITMAP;
  357. GC_bm_table[i] = d;
  358. }
  359. UNLOCK();
  360. ENABLE_SIGNALS();
  361. }
  362. # if defined(__STDC__) || defined(__cplusplus)
  363. mse * GC_typed_mark_proc(register word * addr,
  364. register mse * mark_stack_ptr,
  365. mse * mark_stack_limit,
  366. word env)
  367. # else
  368. mse * GC_typed_mark_proc(addr, mark_stack_ptr, mark_stack_limit, env)
  369. register word * addr;
  370. register mse * mark_stack_ptr;
  371. mse * mark_stack_limit;
  372. word env;
  373. # endif
  374. {
  375. register word bm = GC_ext_descriptors[env].ed_bitmap;
  376. register word * current_p = addr;
  377. register word current;
  378. register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
  379. register ptr_t least_ha = GC_least_plausible_heap_addr;
  380. for (; bm != 0; bm >>= 1, current_p++) {
  381. if (bm & 1) {
  382. current = *current_p;
  383. FIXUP_POINTER(current);
  384. if ((ptr_t)current >= least_ha && (ptr_t)current <= greatest_ha) {
  385. PUSH_CONTENTS((ptr_t)current, mark_stack_ptr,
  386. mark_stack_limit, current_p, exit1);
  387. }
  388. }
  389. }
  390. if (GC_ext_descriptors[env].ed_continued) {
  391. /* Push an entry with the rest of the descriptor back onto the */
  392. /* stack. Thus we never do too much work at once. Note that */
  393. /* we also can't overflow the mark stack unless we actually */
  394. /* mark something. */
  395. mark_stack_ptr++;
  396. if (mark_stack_ptr >= mark_stack_limit) {
  397. mark_stack_ptr = GC_signal_mark_stack_overflow(mark_stack_ptr);
  398. }
  399. mark_stack_ptr -> mse_start = addr + WORDSZ;
  400. mark_stack_ptr -> mse_descr =
  401. GC_MAKE_PROC(GC_typed_mark_proc_index, env+1);
  402. }
  403. return(mark_stack_ptr);
  404. }
  405. /* Return the size of the object described by d. It would be faster to */
  406. /* store this directly, or to compute it as part of */
  407. /* GC_push_complex_descriptor, but hopefully it doesn't matter. */
  408. word GC_descr_obj_size(d)
  409. register complex_descriptor *d;
  410. {
  411. switch(d -> TAG) {
  412. case LEAF_TAG:
  413. return(d -> ld.ld_nelements * d -> ld.ld_size);
  414. case ARRAY_TAG:
  415. return(d -> ad.ad_nelements
  416. * GC_descr_obj_size(d -> ad.ad_element_descr));
  417. case SEQUENCE_TAG:
  418. return(GC_descr_obj_size(d -> sd.sd_first)
  419. + GC_descr_obj_size(d -> sd.sd_second));
  420. default:
  421. ABORT("Bad complex descriptor");
  422. /*NOTREACHED*/ return 0; /*NOTREACHED*/
  423. }
  424. }
  425. /* Push descriptors for the object at addr with complex descriptor d */
  426. /* onto the mark stack. Return 0 if the mark stack overflowed. */
  427. mse * GC_push_complex_descriptor(addr, d, msp, msl)
  428. word * addr;
  429. register complex_descriptor *d;
  430. register mse * msp;
  431. mse * msl;
  432. {
  433. register ptr_t current = (ptr_t) addr;
  434. register word nelements;
  435. register word sz;
  436. register word i;
  437. switch(d -> TAG) {
  438. case LEAF_TAG:
  439. {
  440. register GC_descr descr = d -> ld.ld_descriptor;
  441. nelements = d -> ld.ld_nelements;
  442. if (msl - msp <= (ptrdiff_t)nelements) return(0);
  443. sz = d -> ld.ld_size;
  444. for (i = 0; i < nelements; i++) {
  445. msp++;
  446. msp -> mse_start = (word *)current;
  447. msp -> mse_descr = descr;
  448. current += sz;
  449. }
  450. return(msp);
  451. }
  452. case ARRAY_TAG:
  453. {
  454. register complex_descriptor *descr = d -> ad.ad_element_descr;
  455. nelements = d -> ad.ad_nelements;
  456. sz = GC_descr_obj_size(descr);
  457. for (i = 0; i < nelements; i++) {
  458. msp = GC_push_complex_descriptor((word *)current, descr,
  459. msp, msl);
  460. if (msp == 0) return(0);
  461. current += sz;
  462. }
  463. return(msp);
  464. }
  465. case SEQUENCE_TAG:
  466. {
  467. sz = GC_descr_obj_size(d -> sd.sd_first);
  468. msp = GC_push_complex_descriptor((word *)current, d -> sd.sd_first,
  469. msp, msl);
  470. if (msp == 0) return(0);
  471. current += sz;
  472. msp = GC_push_complex_descriptor((word *)current, d -> sd.sd_second,
  473. msp, msl);
  474. return(msp);
  475. }
  476. default:
  477. ABORT("Bad complex descriptor");
  478. /*NOTREACHED*/ return 0; /*NOTREACHED*/
  479. }
  480. }
  481. /*ARGSUSED*/
  482. # if defined(__STDC__) || defined(__cplusplus)
  483. mse * GC_array_mark_proc(register word * addr,
  484. register mse * mark_stack_ptr,
  485. mse * mark_stack_limit,
  486. word env)
  487. # else
  488. mse * GC_array_mark_proc(addr, mark_stack_ptr, mark_stack_limit, env)
  489. register word * addr;
  490. register mse * mark_stack_ptr;
  491. mse * mark_stack_limit;
  492. word env;
  493. # endif
  494. {
  495. register hdr * hhdr = HDR(addr);
  496. register word sz = hhdr -> hb_sz;
  497. register complex_descriptor * descr = (complex_descriptor *)(addr[sz-1]);
  498. mse * orig_mark_stack_ptr = mark_stack_ptr;
  499. mse * new_mark_stack_ptr;
  500. if (descr == 0) {
  501. /* Found a reference to a free list entry. Ignore it. */
  502. return(orig_mark_stack_ptr);
  503. }
  504. /* In use counts were already updated when array descriptor was */
  505. /* pushed. Here we only replace it by subobject descriptors, so */
  506. /* no update is necessary. */
  507. new_mark_stack_ptr = GC_push_complex_descriptor(addr, descr,
  508. mark_stack_ptr,
  509. mark_stack_limit-1);
  510. if (new_mark_stack_ptr == 0) {
  511. /* Doesn't fit. Conservatively push the whole array as a unit */
  512. /* and request a mark stack expansion. */
  513. /* This cannot cause a mark stack overflow, since it replaces */
  514. /* the original array entry. */
  515. GC_mark_stack_too_small = TRUE;
  516. new_mark_stack_ptr = orig_mark_stack_ptr + 1;
  517. new_mark_stack_ptr -> mse_start = addr;
  518. new_mark_stack_ptr -> mse_descr = WORDS_TO_BYTES(sz) | GC_DS_LENGTH;
  519. } else {
  520. /* Push descriptor itself */
  521. new_mark_stack_ptr++;
  522. new_mark_stack_ptr -> mse_start = addr + sz - 1;
  523. new_mark_stack_ptr -> mse_descr = sizeof(word) | GC_DS_LENGTH;
  524. }
  525. return(new_mark_stack_ptr);
  526. }
  527. #if defined(__STDC__) || defined(__cplusplus)
  528. GC_descr GC_make_descriptor(GC_bitmap bm, size_t len)
  529. #else
  530. GC_descr GC_make_descriptor(bm, len)
  531. GC_bitmap bm;
  532. size_t len;
  533. #endif
  534. {
  535. register signed_word last_set_bit = len - 1;
  536. register word result;
  537. register int i;
  538. # define HIGH_BIT (((word)1) << (WORDSZ - 1))
  539. if (!GC_explicit_typing_initialized) GC_init_explicit_typing();
  540. while (last_set_bit >= 0 && !GC_get_bit(bm, last_set_bit)) last_set_bit --;
  541. if (last_set_bit < 0) return(0 /* no pointers */);
  542. # if ALIGNMENT == CPP_WORDSZ/8
  543. {
  544. register GC_bool all_bits_set = TRUE;
  545. for (i = 0; i < last_set_bit; i++) {
  546. if (!GC_get_bit(bm, i)) {
  547. all_bits_set = FALSE;
  548. break;
  549. }
  550. }
  551. if (all_bits_set) {
  552. /* An initial section contains all pointers. Use length descriptor. */
  553. return(WORDS_TO_BYTES(last_set_bit+1) | GC_DS_LENGTH);
  554. }
  555. }
  556. # endif
  557. if (last_set_bit < BITMAP_BITS) {
  558. /* Hopefully the common case. */
  559. /* Build bitmap descriptor (with bits reversed) */
  560. result = HIGH_BIT;
  561. for (i = last_set_bit - 1; i >= 0; i--) {
  562. result >>= 1;
  563. if (GC_get_bit(bm, i)) result |= HIGH_BIT;
  564. }
  565. result |= GC_DS_BITMAP;
  566. return(result);
  567. } else {
  568. signed_word index;
  569. index = GC_add_ext_descriptor(bm, (word)last_set_bit+1);
  570. if (index == -1) return(WORDS_TO_BYTES(last_set_bit+1) | GC_DS_LENGTH);
  571. /* Out of memory: use conservative */
  572. /* approximation. */
  573. result = GC_MAKE_PROC(GC_typed_mark_proc_index, (word)index);
  574. return(result);
  575. }
  576. }
  577. ptr_t GC_clear_stack();
  578. #define GENERAL_MALLOC(lb,k) \
  579. (GC_PTR)GC_clear_stack(GC_generic_malloc((word)lb, k))
  580. #define GENERAL_MALLOC_IOP(lb,k) \
  581. (GC_PTR)GC_clear_stack(GC_generic_malloc_ignore_off_page(lb, k))
  582. #if defined(__STDC__) || defined(__cplusplus)
  583. void * GC_malloc_explicitly_typed(size_t lb, GC_descr d)
  584. #else
  585. char * GC_malloc_explicitly_typed(lb, d)
  586. size_t lb;
  587. GC_descr d;
  588. #endif
  589. {
  590. register ptr_t op;
  591. register ptr_t * opp;
  592. register word lw;
  593. DCL_LOCK_STATE;
  594. lb += TYPD_EXTRA_BYTES;
  595. if( SMALL_OBJ(lb) ) {
  596. # ifdef MERGE_SIZES
  597. lw = GC_size_map[lb];
  598. # else
  599. lw = ALIGNED_WORDS(lb);
  600. # endif
  601. opp = &(GC_eobjfreelist[lw]);
  602. FASTLOCK();
  603. if( !FASTLOCK_SUCCEEDED() || (op = *opp) == 0 ) {
  604. FASTUNLOCK();
  605. op = (ptr_t)GENERAL_MALLOC((word)lb, GC_explicit_kind);
  606. if (0 == op) return 0;
  607. # ifdef MERGE_SIZES
  608. lw = GC_size_map[lb]; /* May have been uninitialized. */
  609. # endif
  610. } else {
  611. *opp = obj_link(op);
  612. obj_link(op) = 0;
  613. GC_words_allocd += lw;
  614. FASTUNLOCK();
  615. }
  616. } else {
  617. op = (ptr_t)GENERAL_MALLOC((word)lb, GC_explicit_kind);
  618. if (op != NULL)
  619. lw = BYTES_TO_WORDS(GC_size(op));
  620. }
  621. if (op != NULL)
  622. ((word *)op)[lw - 1] = d;
  623. return((GC_PTR) op);
  624. }
  625. #if defined(__STDC__) || defined(__cplusplus)
  626. void * GC_malloc_explicitly_typed_ignore_off_page(size_t lb, GC_descr d)
  627. #else
  628. char * GC_malloc_explicitly_typed_ignore_off_page(lb, d)
  629. size_t lb;
  630. GC_descr d;
  631. #endif
  632. {
  633. register ptr_t op;
  634. register ptr_t * opp;
  635. register word lw;
  636. DCL_LOCK_STATE;
  637. lb += TYPD_EXTRA_BYTES;
  638. if( SMALL_OBJ(lb) ) {
  639. # ifdef MERGE_SIZES
  640. lw = GC_size_map[lb];
  641. # else
  642. lw = ALIGNED_WORDS(lb);
  643. # endif
  644. opp = &(GC_eobjfreelist[lw]);
  645. FASTLOCK();
  646. if( !FASTLOCK_SUCCEEDED() || (op = *opp) == 0 ) {
  647. FASTUNLOCK();
  648. op = (ptr_t)GENERAL_MALLOC_IOP(lb, GC_explicit_kind);
  649. # ifdef MERGE_SIZES
  650. lw = GC_size_map[lb]; /* May have been uninitialized. */
  651. # endif
  652. } else {
  653. *opp = obj_link(op);
  654. obj_link(op) = 0;
  655. GC_words_allocd += lw;
  656. FASTUNLOCK();
  657. }
  658. } else {
  659. op = (ptr_t)GENERAL_MALLOC_IOP(lb, GC_explicit_kind);
  660. if (op != NULL)
  661. lw = BYTES_TO_WORDS(GC_size(op));
  662. }
  663. if (op != NULL)
  664. ((word *)op)[lw - 1] = d;
  665. return((GC_PTR) op);
  666. }
  667. #if defined(__STDC__) || defined(__cplusplus)
  668. void * GC_calloc_explicitly_typed(size_t n,
  669. size_t lb,
  670. GC_descr d)
  671. #else
  672. char * GC_calloc_explicitly_typed(n, lb, d)
  673. size_t n;
  674. size_t lb;
  675. GC_descr d;
  676. #endif
  677. {
  678. register ptr_t op;
  679. register ptr_t * opp;
  680. register word lw;
  681. GC_descr simple_descr;
  682. complex_descriptor *complex_descr;
  683. register int descr_type;
  684. struct LeafDescriptor leaf;
  685. DCL_LOCK_STATE;
  686. descr_type = GC_make_array_descriptor((word)n, (word)lb, d,
  687. &simple_descr, &complex_descr, &leaf);
  688. switch(descr_type) {
  689. case NO_MEM: return(0);
  690. case SIMPLE: return(GC_malloc_explicitly_typed(n*lb, simple_descr));
  691. case LEAF:
  692. lb *= n;
  693. lb += sizeof(struct LeafDescriptor) + TYPD_EXTRA_BYTES;
  694. break;
  695. case COMPLEX:
  696. lb *= n;
  697. lb += TYPD_EXTRA_BYTES;
  698. break;
  699. }
  700. if( SMALL_OBJ(lb) ) {
  701. # ifdef MERGE_SIZES
  702. lw = GC_size_map[lb];
  703. # else
  704. lw = ALIGNED_WORDS(lb);
  705. # endif
  706. opp = &(GC_arobjfreelist[lw]);
  707. FASTLOCK();
  708. if( !FASTLOCK_SUCCEEDED() || (op = *opp) == 0 ) {
  709. FASTUNLOCK();
  710. op = (ptr_t)GENERAL_MALLOC((word)lb, GC_array_kind);
  711. if (0 == op) return(0);
  712. # ifdef MERGE_SIZES
  713. lw = GC_size_map[lb]; /* May have been uninitialized. */
  714. # endif
  715. } else {
  716. *opp = obj_link(op);
  717. obj_link(op) = 0;
  718. GC_words_allocd += lw;
  719. FASTUNLOCK();
  720. }
  721. } else {
  722. op = (ptr_t)GENERAL_MALLOC((word)lb, GC_array_kind);
  723. if (0 == op) return(0);
  724. lw = BYTES_TO_WORDS(GC_size(op));
  725. }
  726. if (descr_type == LEAF) {
  727. /* Set up the descriptor inside the object itself. */
  728. VOLATILE struct LeafDescriptor * lp =
  729. (struct LeafDescriptor *)
  730. ((word *)op
  731. + lw - (BYTES_TO_WORDS(sizeof(struct LeafDescriptor)) + 1));
  732. lp -> ld_tag = LEAF_TAG;
  733. lp -> ld_size = leaf.ld_size;
  734. lp -> ld_nelements = leaf.ld_nelements;
  735. lp -> ld_descriptor = leaf.ld_descriptor;
  736. ((VOLATILE word *)op)[lw - 1] = (word)lp;
  737. } else {
  738. extern unsigned GC_finalization_failures;
  739. unsigned ff = GC_finalization_failures;
  740. ((word *)op)[lw - 1] = (word)complex_descr;
  741. /* Make sure the descriptor is cleared once there is any danger */
  742. /* it may have been collected. */
  743. (void)
  744. GC_general_register_disappearing_link((GC_PTR *)
  745. ((word *)op+lw-1),
  746. (GC_PTR) op);
  747. if (ff != GC_finalization_failures) {
  748. /* Couldn't register it due to lack of memory. Punt. */
  749. /* This will probably fail too, but gives the recovery code */
  750. /* a chance. */
  751. return(GC_malloc(n*lb));
  752. }
  753. }
  754. return((GC_PTR) op);
  755. }