gcj_mlc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  3. * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
  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. #include "private/gc_pmark.h" /* includes gc_priv.h */
  16. #ifdef GC_GCJ_SUPPORT
  17. /*
  18. * This is an allocator interface tuned for gcj (the GNU static
  19. * java compiler).
  20. *
  21. * Each allocated object has a pointer in its first word to a vtable,
  22. * which for our purposes is simply a structure describing the type of
  23. * the object.
  24. * This descriptor structure contains a GC marking descriptor at offset
  25. * MARK_DESCR_OFFSET.
  26. *
  27. * It is hoped that this interface may also be useful for other systems,
  28. * possibly with some tuning of the constants. But the immediate goal
  29. * is to get better gcj performance.
  30. *
  31. * We assume:
  32. * 1) Counting on explicit initialization of this interface is OK;
  33. * 2) FASTLOCK is not a significant win.
  34. */
  35. #include "gc_gcj.h"
  36. #include "private/dbg_mlc.h"
  37. #ifdef GC_ASSERTIONS
  38. GC_INNER /* variable is also used in thread_local_alloc.c */
  39. #else
  40. STATIC
  41. #endif
  42. GC_bool GC_gcj_malloc_initialized = FALSE;
  43. int GC_gcj_kind = 0; /* Object kind for objects with descriptors */
  44. /* in "vtable". */
  45. int GC_gcj_debug_kind = 0;
  46. /* The kind of objects that is always marked */
  47. /* with a mark proc call. */
  48. GC_INNER ptr_t * GC_gcjobjfreelist = NULL;
  49. STATIC ptr_t * GC_gcjdebugobjfreelist = NULL;
  50. STATIC struct GC_ms_entry * GC_gcj_fake_mark_proc(word * addr GC_ATTR_UNUSED,
  51. struct GC_ms_entry *mark_stack_ptr,
  52. struct GC_ms_entry * mark_stack_limit GC_ATTR_UNUSED,
  53. word env GC_ATTR_UNUSED)
  54. {
  55. ABORT_RET("No client gcj mark proc is specified");
  56. return mark_stack_ptr;
  57. }
  58. /* Caller does not hold allocation lock. */
  59. GC_API void GC_CALL GC_init_gcj_malloc(int mp_index,
  60. void * /* really GC_mark_proc */mp)
  61. {
  62. GC_bool ignore_gcj_info;
  63. DCL_LOCK_STATE;
  64. if (mp == 0) /* In case GC_DS_PROC is unused. */
  65. mp = (void *)(word)GC_gcj_fake_mark_proc;
  66. GC_init(); /* In case it's not already done. */
  67. LOCK();
  68. if (GC_gcj_malloc_initialized) {
  69. UNLOCK();
  70. return;
  71. }
  72. GC_gcj_malloc_initialized = TRUE;
  73. # ifdef GC_IGNORE_GCJ_INFO
  74. /* This is useful for debugging on platforms with missing getenv(). */
  75. ignore_gcj_info = 1;
  76. # else
  77. ignore_gcj_info = (0 != GETENV("GC_IGNORE_GCJ_INFO"));
  78. # endif
  79. if (ignore_gcj_info) {
  80. GC_COND_LOG_PRINTF("Gcj-style type information is disabled!\n");
  81. }
  82. GC_ASSERT(GC_mark_procs[mp_index] == (GC_mark_proc)0); /* unused */
  83. GC_mark_procs[mp_index] = (GC_mark_proc)(word)mp;
  84. if ((unsigned)mp_index >= GC_n_mark_procs)
  85. ABORT("GC_init_gcj_malloc: bad index");
  86. /* Set up object kind gcj-style indirect descriptor. */
  87. GC_gcjobjfreelist = (ptr_t *)GC_new_free_list_inner();
  88. if (ignore_gcj_info) {
  89. /* Use a simple length-based descriptor, thus forcing a fully */
  90. /* conservative scan. */
  91. GC_gcj_kind = GC_new_kind_inner((void **)GC_gcjobjfreelist,
  92. (0 | GC_DS_LENGTH),
  93. TRUE, TRUE);
  94. } else {
  95. GC_gcj_kind = GC_new_kind_inner(
  96. (void **)GC_gcjobjfreelist,
  97. (((word)(-(signed_word)MARK_DESCR_OFFSET
  98. - GC_INDIR_PER_OBJ_BIAS))
  99. | GC_DS_PER_OBJECT),
  100. FALSE, TRUE);
  101. }
  102. /* Set up object kind for objects that require mark proc call. */
  103. if (ignore_gcj_info) {
  104. GC_gcj_debug_kind = GC_gcj_kind;
  105. GC_gcjdebugobjfreelist = GC_gcjobjfreelist;
  106. } else {
  107. GC_gcjdebugobjfreelist = (ptr_t *)GC_new_free_list_inner();
  108. GC_gcj_debug_kind = GC_new_kind_inner(
  109. (void **)GC_gcjdebugobjfreelist,
  110. GC_MAKE_PROC(mp_index,
  111. 1 /* allocated with debug info */),
  112. FALSE, TRUE);
  113. }
  114. UNLOCK();
  115. }
  116. #define GENERAL_MALLOC_INNER(lb,k) \
  117. GC_clear_stack(GC_generic_malloc_inner(lb, k))
  118. #define GENERAL_MALLOC_INNER_IOP(lb,k) \
  119. GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
  120. /* We need a mechanism to release the lock and invoke finalizers. */
  121. /* We don't really have an opportunity to do this on a rarely executed */
  122. /* path on which the lock is not held. Thus we check at a */
  123. /* rarely executed point at which it is safe to release the lock. */
  124. /* We do this even where we could just call GC_INVOKE_FINALIZERS, */
  125. /* since it's probably cheaper and certainly more uniform. */
  126. /* FIXME - Consider doing the same elsewhere? */
  127. static void maybe_finalize(void)
  128. {
  129. static word last_finalized_no = 0;
  130. DCL_LOCK_STATE;
  131. if (GC_gc_no == last_finalized_no ||
  132. !EXPECT(GC_is_initialized, TRUE)) return;
  133. UNLOCK();
  134. GC_INVOKE_FINALIZERS();
  135. LOCK();
  136. last_finalized_no = GC_gc_no;
  137. }
  138. /* Allocate an object, clear it, and store the pointer to the */
  139. /* type structure (vtable in gcj). */
  140. /* This adds a byte at the end of the object if GC_malloc would.*/
  141. #ifdef THREAD_LOCAL_ALLOC
  142. GC_INNER void * GC_core_gcj_malloc(size_t lb,
  143. void * ptr_to_struct_containing_descr)
  144. #else
  145. GC_API void * GC_CALL GC_gcj_malloc(size_t lb,
  146. void * ptr_to_struct_containing_descr)
  147. #endif
  148. {
  149. ptr_t op;
  150. ptr_t * opp;
  151. word lg;
  152. DCL_LOCK_STATE;
  153. GC_DBG_COLLECT_AT_MALLOC(lb);
  154. if(SMALL_OBJ(lb)) {
  155. lg = GC_size_map[lb];
  156. opp = &(GC_gcjobjfreelist[lg]);
  157. LOCK();
  158. op = *opp;
  159. if(EXPECT(op == 0, FALSE)) {
  160. maybe_finalize();
  161. op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
  162. if (0 == op) {
  163. GC_oom_func oom_fn = GC_oom_fn;
  164. UNLOCK();
  165. return((*oom_fn)(lb));
  166. }
  167. } else {
  168. *opp = obj_link(op);
  169. GC_bytes_allocd += GRANULES_TO_BYTES(lg);
  170. }
  171. *(void **)op = ptr_to_struct_containing_descr;
  172. GC_ASSERT(((void **)op)[1] == 0);
  173. UNLOCK();
  174. } else {
  175. LOCK();
  176. maybe_finalize();
  177. op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
  178. if (0 == op) {
  179. GC_oom_func oom_fn = GC_oom_fn;
  180. UNLOCK();
  181. return((*oom_fn)(lb));
  182. }
  183. *(void **)op = ptr_to_struct_containing_descr;
  184. UNLOCK();
  185. }
  186. return((void *) op);
  187. }
  188. /* Similar to GC_gcj_malloc, but add debug info. This is allocated */
  189. /* with GC_gcj_debug_kind. */
  190. GC_API void * GC_CALL GC_debug_gcj_malloc(size_t lb,
  191. void * ptr_to_struct_containing_descr, GC_EXTRA_PARAMS)
  192. {
  193. void * result;
  194. DCL_LOCK_STATE;
  195. /* We're careful to avoid extra calls, which could */
  196. /* confuse the backtrace. */
  197. LOCK();
  198. maybe_finalize();
  199. result = GC_generic_malloc_inner(lb + DEBUG_BYTES, GC_gcj_debug_kind);
  200. if (result == 0) {
  201. GC_oom_func oom_fn = GC_oom_fn;
  202. UNLOCK();
  203. GC_err_printf("GC_debug_gcj_malloc(%lu, %p) returning NULL (%s:%d)\n",
  204. (unsigned long)lb, ptr_to_struct_containing_descr, s, i);
  205. return((*oom_fn)(lb));
  206. }
  207. *((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
  208. UNLOCK();
  209. if (!GC_debugging_started) {
  210. GC_start_debugging();
  211. }
  212. ADD_CALL_CHAIN(result, ra);
  213. return (GC_store_debug_info(result, (word)lb, s, i));
  214. }
  215. /* There is no THREAD_LOCAL_ALLOC for GC_gcj_malloc_ignore_off_page(). */
  216. GC_API void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb,
  217. void * ptr_to_struct_containing_descr)
  218. {
  219. ptr_t op;
  220. ptr_t * opp;
  221. word lg;
  222. DCL_LOCK_STATE;
  223. GC_DBG_COLLECT_AT_MALLOC(lb);
  224. if(SMALL_OBJ(lb)) {
  225. lg = GC_size_map[lb];
  226. opp = &(GC_gcjobjfreelist[lg]);
  227. LOCK();
  228. op = *opp;
  229. if (EXPECT(0 == op, FALSE)) {
  230. maybe_finalize();
  231. op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
  232. if (0 == op) {
  233. GC_oom_func oom_fn = GC_oom_fn;
  234. UNLOCK();
  235. return((*oom_fn)(lb));
  236. }
  237. } else {
  238. *opp = obj_link(op);
  239. GC_bytes_allocd += GRANULES_TO_BYTES(lg);
  240. }
  241. } else {
  242. LOCK();
  243. maybe_finalize();
  244. op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
  245. if (0 == op) {
  246. GC_oom_func oom_fn = GC_oom_fn;
  247. UNLOCK();
  248. return((*oom_fn)(lb));
  249. }
  250. }
  251. *(void **)op = ptr_to_struct_containing_descr;
  252. UNLOCK();
  253. return((void *) op);
  254. }
  255. #endif /* GC_GCJ_SUPPORT */