gcj_mlc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 struct GC_ms_entry * GC_gcj_fake_mark_proc(word * addr GC_ATTR_UNUSED,
  50. struct GC_ms_entry *mark_stack_ptr,
  51. struct GC_ms_entry * mark_stack_limit GC_ATTR_UNUSED,
  52. word env GC_ATTR_UNUSED)
  53. {
  54. ABORT_RET("No client gcj mark proc is specified");
  55. return mark_stack_ptr;
  56. }
  57. /* Caller does not hold allocation lock. */
  58. GC_API void GC_CALL GC_init_gcj_malloc(int mp_index,
  59. void * /* really GC_mark_proc */mp)
  60. {
  61. GC_bool ignore_gcj_info;
  62. DCL_LOCK_STATE;
  63. if (mp == 0) /* In case GC_DS_PROC is unused. */
  64. mp = (void *)(word)GC_gcj_fake_mark_proc;
  65. GC_init(); /* In case it's not already done. */
  66. LOCK();
  67. if (GC_gcj_malloc_initialized) {
  68. UNLOCK();
  69. return;
  70. }
  71. GC_gcj_malloc_initialized = TRUE;
  72. # ifdef GC_IGNORE_GCJ_INFO
  73. /* This is useful for debugging on platforms with missing getenv(). */
  74. ignore_gcj_info = 1;
  75. # else
  76. ignore_gcj_info = (0 != GETENV("GC_IGNORE_GCJ_INFO"));
  77. # endif
  78. if (ignore_gcj_info) {
  79. GC_COND_LOG_PRINTF("Gcj-style type information is disabled!\n");
  80. }
  81. GC_ASSERT(GC_mark_procs[mp_index] == (GC_mark_proc)0); /* unused */
  82. GC_mark_procs[mp_index] = (GC_mark_proc)(word)mp;
  83. if ((unsigned)mp_index >= GC_n_mark_procs)
  84. ABORT("GC_init_gcj_malloc: bad index");
  85. /* Set up object kind gcj-style indirect descriptor. */
  86. GC_gcjobjfreelist = (ptr_t *)GC_new_free_list_inner();
  87. if (ignore_gcj_info) {
  88. /* Use a simple length-based descriptor, thus forcing a fully */
  89. /* conservative scan. */
  90. GC_gcj_kind = GC_new_kind_inner((void **)GC_gcjobjfreelist,
  91. /* 0 | */ GC_DS_LENGTH,
  92. TRUE, TRUE);
  93. } else {
  94. GC_gcj_kind = GC_new_kind_inner(
  95. (void **)GC_gcjobjfreelist,
  96. (((word)(-(signed_word)MARK_DESCR_OFFSET
  97. - GC_INDIR_PER_OBJ_BIAS))
  98. | GC_DS_PER_OBJECT),
  99. FALSE, TRUE);
  100. }
  101. /* Set up object kind for objects that require mark proc call. */
  102. if (ignore_gcj_info) {
  103. GC_gcj_debug_kind = GC_gcj_kind;
  104. } else {
  105. GC_gcj_debug_kind = GC_new_kind_inner(GC_new_free_list_inner(),
  106. GC_MAKE_PROC(mp_index,
  107. 1 /* allocated with debug info */),
  108. FALSE, TRUE);
  109. }
  110. UNLOCK();
  111. }
  112. #define GENERAL_MALLOC_INNER(lb,k) \
  113. GC_clear_stack(GC_generic_malloc_inner(lb, k))
  114. #define GENERAL_MALLOC_INNER_IOP(lb,k) \
  115. GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
  116. /* We need a mechanism to release the lock and invoke finalizers. */
  117. /* We don't really have an opportunity to do this on a rarely executed */
  118. /* path on which the lock is not held. Thus we check at a */
  119. /* rarely executed point at which it is safe to release the lock. */
  120. /* We do this even where we could just call GC_INVOKE_FINALIZERS, */
  121. /* since it's probably cheaper and certainly more uniform. */
  122. /* FIXME - Consider doing the same elsewhere? */
  123. static void maybe_finalize(void)
  124. {
  125. static word last_finalized_no = 0;
  126. DCL_LOCK_STATE;
  127. if (GC_gc_no == last_finalized_no ||
  128. !EXPECT(GC_is_initialized, TRUE)) return;
  129. UNLOCK();
  130. GC_INVOKE_FINALIZERS();
  131. LOCK();
  132. last_finalized_no = GC_gc_no;
  133. }
  134. /* Allocate an object, clear it, and store the pointer to the */
  135. /* type structure (vtable in gcj). */
  136. /* This adds a byte at the end of the object if GC_malloc would.*/
  137. #if !IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  138. #ifdef THREAD_LOCAL_ALLOC
  139. GC_INNER void * GC_core_gcj_malloc(size_t lb,
  140. void * ptr_to_struct_containing_descr)
  141. #else
  142. GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc(size_t lb,
  143. void * ptr_to_struct_containing_descr)
  144. #endif
  145. {
  146. ptr_t op;
  147. DCL_LOCK_STATE;
  148. GC_DBG_COLLECT_AT_MALLOC(lb);
  149. if(SMALL_OBJ(lb)) {
  150. word lg;
  151. LOCK();
  152. lg = GC_size_map[lb];
  153. op = GC_gcjobjfreelist[lg];
  154. if(EXPECT(0 == op, FALSE)) {
  155. maybe_finalize();
  156. op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
  157. if (0 == op) {
  158. GC_oom_func oom_fn = GC_oom_fn;
  159. UNLOCK();
  160. return((*oom_fn)(lb));
  161. }
  162. } else {
  163. GC_gcjobjfreelist[lg] = (ptr_t)obj_link(op);
  164. GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
  165. }
  166. GC_ASSERT(((void **)op)[1] == 0);
  167. } else {
  168. LOCK();
  169. maybe_finalize();
  170. op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
  171. if (0 == op) {
  172. GC_oom_func oom_fn = GC_oom_fn;
  173. UNLOCK();
  174. return((*oom_fn)(lb));
  175. }
  176. }
  177. *(void **)op = ptr_to_struct_containing_descr;
  178. UNLOCK();
  179. GC_dirty(op);
  180. return((void *) op);
  181. }
  182. #endif
  183. /* Similar to GC_gcj_malloc, but add debug info. This is allocated */
  184. /* with GC_gcj_debug_kind. */
  185. GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb,
  186. void * ptr_to_struct_containing_descr, GC_EXTRA_PARAMS)
  187. {
  188. void * result;
  189. DCL_LOCK_STATE;
  190. /* We're careful to avoid extra calls, which could */
  191. /* confuse the backtrace. */
  192. LOCK();
  193. maybe_finalize();
  194. result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES),
  195. GC_gcj_debug_kind);
  196. if (result == 0) {
  197. GC_oom_func oom_fn = GC_oom_fn;
  198. UNLOCK();
  199. GC_err_printf("GC_debug_gcj_malloc(%lu, %p) returning NULL (%s:%d)\n",
  200. (unsigned long)lb, ptr_to_struct_containing_descr, s, i);
  201. return((*oom_fn)(lb));
  202. }
  203. *((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
  204. if (!GC_debugging_started) {
  205. GC_start_debugging_inner();
  206. }
  207. ADD_CALL_CHAIN(result, ra);
  208. result = GC_store_debug_info_inner(result, (word)lb, s, i);
  209. UNLOCK();
  210. GC_dirty(result);
  211. return result;
  212. }
  213. /* There is no THREAD_LOCAL_ALLOC for GC_gcj_malloc_ignore_off_page(). */
  214. GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb,
  215. void * ptr_to_struct_containing_descr)
  216. {
  217. ptr_t op;
  218. DCL_LOCK_STATE;
  219. GC_DBG_COLLECT_AT_MALLOC(lb);
  220. if(SMALL_OBJ(lb)) {
  221. word lg;
  222. LOCK();
  223. lg = GC_size_map[lb];
  224. op = GC_gcjobjfreelist[lg];
  225. if (EXPECT(0 == op, FALSE)) {
  226. maybe_finalize();
  227. op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
  228. if (0 == op) {
  229. GC_oom_func oom_fn = GC_oom_fn;
  230. UNLOCK();
  231. return((*oom_fn)(lb));
  232. }
  233. } else {
  234. GC_gcjobjfreelist[lg] = (ptr_t)obj_link(op);
  235. GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
  236. }
  237. } else {
  238. LOCK();
  239. maybe_finalize();
  240. op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
  241. if (0 == op) {
  242. GC_oom_func oom_fn = GC_oom_fn;
  243. UNLOCK();
  244. return((*oom_fn)(lb));
  245. }
  246. }
  247. *(void **)op = ptr_to_struct_containing_descr;
  248. UNLOCK();
  249. GC_dirty(op);
  250. return((void *) op);
  251. }
  252. #endif /* GC_GCJ_SUPPORT */