gc_mark.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  3. * Copyright (c) 2001 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. * This contains interfaces to the GC marker that are likely to be useful to
  17. * clients that provide detailed heap layout information to the collector.
  18. * This interface should not be used by normal C or C++ clients.
  19. * It will be useful to runtimes for other languages.
  20. *
  21. * This is an experts-only interface! There are many ways to break the
  22. * collector in subtle ways by using this functionality.
  23. */
  24. #ifndef GC_MARK_H
  25. #define GC_MARK_H
  26. #ifndef GC_H
  27. # include "gc.h"
  28. #endif
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* A client supplied mark procedure. Returns new mark stack pointer. */
  33. /* Primary effect should be to push new entries on the mark stack. */
  34. /* Mark stack pointer values are passed and returned explicitly. */
  35. /* Global variables describing mark stack are not necessarily valid. */
  36. /* (This usually saves a few cycles by keeping things in registers.) */
  37. /* Assumed to scan about GC_PROC_BYTES on average. If it needs to do */
  38. /* much more work than that, it should do it in smaller pieces by */
  39. /* pushing itself back on the mark stack. */
  40. /* Note that it should always do some work (defined as marking some */
  41. /* objects) before pushing more than one entry on the mark stack. */
  42. /* This is required to ensure termination in the event of mark stack */
  43. /* overflows. */
  44. /* This procedure is always called with at least one empty entry on the */
  45. /* mark stack. */
  46. /* Currently we require that mark procedures look for pointers in a */
  47. /* subset of the places the conservative marker would. It must be safe */
  48. /* to invoke the normal mark procedure instead. */
  49. /* WARNING: Such a mark procedure may be invoked on an unused object */
  50. /* residing on a free list. Such objects are cleared, except for a */
  51. /* free list link field in the first word. Thus mark procedures may */
  52. /* not count on the presence of a type descriptor, and must handle this */
  53. /* case correctly somehow. */
  54. #define GC_PROC_BYTES 100
  55. #ifdef GC_BUILD
  56. struct GC_ms_entry;
  57. #else
  58. struct GC_ms_entry { void *opaque; };
  59. #endif
  60. typedef struct GC_ms_entry * (*GC_mark_proc)(GC_word * /* addr */,
  61. struct GC_ms_entry * /* mark_stack_ptr */,
  62. struct GC_ms_entry * /* mark_stack_limit */,
  63. GC_word /* env */);
  64. #define GC_LOG_MAX_MARK_PROCS 6
  65. #define GC_MAX_MARK_PROCS (1 << GC_LOG_MAX_MARK_PROCS)
  66. /* In a few cases it's necessary to assign statically known indices to */
  67. /* certain mark procs. Thus we reserve a few for well known clients. */
  68. /* (This is necessary if mark descriptors are compiler generated.) */
  69. #define GC_RESERVED_MARK_PROCS 8
  70. #define GC_GCJ_RESERVED_MARK_PROC_INDEX 0
  71. /* Object descriptors on mark stack or in objects. Low order two */
  72. /* bits are tags distinguishing among the following 4 possibilities */
  73. /* for the high order 30 bits. */
  74. #define GC_DS_TAG_BITS 2
  75. #define GC_DS_TAGS ((1 << GC_DS_TAG_BITS) - 1)
  76. #define GC_DS_LENGTH 0 /* The entire word is a length in bytes that */
  77. /* must be a multiple of 4. */
  78. #define GC_DS_BITMAP 1 /* 30 (62) bits are a bitmap describing pointer */
  79. /* fields. The msb is 1 if the first word */
  80. /* is a pointer. */
  81. /* (This unconventional ordering sometimes */
  82. /* makes the marker slightly faster.) */
  83. /* Zeroes indicate definite nonpointers. Ones */
  84. /* indicate possible pointers. */
  85. /* Only usable if pointers are word aligned. */
  86. #define GC_DS_PROC 2
  87. /* The objects referenced by this object can be */
  88. /* pushed on the mark stack by invoking */
  89. /* PROC(descr). ENV(descr) is passed as the */
  90. /* last argument. */
  91. #define GC_MAKE_PROC(proc_index, env) \
  92. (((((env) << GC_LOG_MAX_MARK_PROCS) \
  93. | (proc_index)) << GC_DS_TAG_BITS) | GC_DS_PROC)
  94. #define GC_DS_PER_OBJECT 3 /* The real descriptor is at the */
  95. /* byte displacement from the beginning of the */
  96. /* object given by descr & ~GC_DS_TAGS. */
  97. /* If the descriptor is negative, the real */
  98. /* descriptor is at (*<object_start>) - */
  99. /* (descr&~GC_DS_TAGS) - GC_INDIR_PER_OBJ_BIAS */
  100. /* The latter alternative can be used if each */
  101. /* object contains a type descriptor in the */
  102. /* first word. */
  103. /* Note that in the multi-threaded environments */
  104. /* per-object descriptors must be located in */
  105. /* either the first two or last two words of */
  106. /* the object, since only those are guaranteed */
  107. /* to be cleared while the allocation lock is */
  108. /* held. */
  109. #define GC_INDIR_PER_OBJ_BIAS 0x10
  110. GC_API void * GC_least_plausible_heap_addr;
  111. GC_API void * GC_greatest_plausible_heap_addr;
  112. /* Bounds on the heap. Guaranteed valid */
  113. /* Likely to include future heap expansion. */
  114. /* Hence usually includes not-yet-mapped */
  115. /* memory. */
  116. /* Handle nested references in a custom mark procedure. */
  117. /* Check if obj is a valid object. If so, ensure that it is marked. */
  118. /* If it was not previously marked, push its contents onto the mark */
  119. /* stack for future scanning. The object will then be scanned using */
  120. /* its mark descriptor. */
  121. /* Returns the new mark stack pointer. */
  122. /* Handles mark stack overflows correctly. */
  123. /* Since this marks first, it makes progress even if there are mark */
  124. /* stack overflows. */
  125. /* Src is the address of the pointer to obj, which is used only */
  126. /* for back pointer-based heap debugging. */
  127. /* It is strongly recommended that most objects be handled without mark */
  128. /* procedures, e.g. with bitmap descriptors, and that mark procedures */
  129. /* be reserved for exceptional cases. That will ensure that */
  130. /* performance of this call is not extremely performance critical. */
  131. /* (Otherwise we would need to inline GC_mark_and_push completely, */
  132. /* which would tie the client code to a fixed collector version.) */
  133. /* Note that mark procedures should explicitly call FIXUP_POINTER() */
  134. /* if required. */
  135. GC_API struct GC_ms_entry * GC_CALL GC_mark_and_push(void * /* obj */,
  136. struct GC_ms_entry * /* mark_stack_ptr */,
  137. struct GC_ms_entry * /* mark_stack_limit */,
  138. void ** /* src */);
  139. #define GC_MARK_AND_PUSH(obj, msp, lim, src) \
  140. ((GC_word)(obj) >= (GC_word)GC_least_plausible_heap_addr && \
  141. (GC_word)(obj) <= (GC_word)GC_greatest_plausible_heap_addr ? \
  142. GC_mark_and_push(obj, msp, lim, src) : (msp))
  143. GC_API struct GC_ms_entry * GC_CALL GC_custom_push_range(void * /* bottom */, void * /* top */,
  144. struct GC_ms_entry * /* mark_stack_ptr */,
  145. struct GC_ms_entry * /* mark_stack_limit */);
  146. GC_API struct GC_ms_entry * GC_CALL GC_custom_push_proc(GC_word /* proc */, void * /* start */,
  147. struct GC_ms_entry * /* mark_stack_ptr */,
  148. struct GC_ms_entry * /* mark_stack_limit */);
  149. GC_API size_t GC_debug_header_size;
  150. /* The size of the header added to objects allocated through */
  151. /* the GC_debug routines. */
  152. /* Defined as a variable so that client mark procedures don't */
  153. /* need to be recompiled for collector version changes. */
  154. #define GC_USR_PTR_FROM_BASE(p) ((void *)((char *)(p) + GC_debug_header_size))
  155. /* And some routines to support creation of new "kinds", e.g. with */
  156. /* custom mark procedures, by language runtimes. */
  157. /* The _inner versions assume the caller holds the allocation lock. */
  158. /* Return a new free list array. */
  159. GC_API void ** GC_CALL GC_new_free_list(void);
  160. GC_API void ** GC_CALL GC_new_free_list_inner(void);
  161. /* Return a new kind, as specified. */
  162. GC_API unsigned GC_CALL GC_new_kind(void ** /* free_list */,
  163. GC_word /* mark_descriptor_template */,
  164. int /* add_size_to_descriptor */,
  165. int /* clear_new_objects */) GC_ATTR_NONNULL(1);
  166. /* The last two parameters must be zero or one. */
  167. GC_API unsigned GC_CALL GC_new_kind_inner(void ** /* free_list */,
  168. GC_word /* mark_descriptor_template */,
  169. int /* add_size_to_descriptor */,
  170. int /* clear_new_objects */) GC_ATTR_NONNULL(1);
  171. /* Return a new mark procedure identifier, suitable for use as */
  172. /* the first argument in GC_MAKE_PROC. */
  173. GC_API unsigned GC_CALL GC_new_proc(GC_mark_proc);
  174. GC_API unsigned GC_CALL GC_new_proc_inner(GC_mark_proc);
  175. /* Allocate an object of a given kind. By default, there are only */
  176. /* a few kinds: composite (pointer-free), atomic, uncollectible, etc. */
  177. /* We claim it is possible for clever client code that understands the */
  178. /* GC internals to add more, e.g. to communicate object layout */
  179. /* information to the collector. Note that in the multi-threaded */
  180. /* contexts, this is usually unsafe for kinds that have the descriptor */
  181. /* in the object itself, since there is otherwise a window in which */
  182. /* the descriptor is not correct. Even in the single-threaded case, */
  183. /* we need to be sure that cleared objects on a free list don't */
  184. /* cause a GC crash if they are accidentally traced. */
  185. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL GC_generic_malloc(
  186. size_t /* lb */,
  187. int /* knd */);
  188. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  189. GC_generic_malloc_ignore_off_page(
  190. size_t /* lb */, int /* knd */);
  191. /* As above, but pointers to past the */
  192. /* first page of the resulting object */
  193. /* are ignored. */
  194. /* Generalized version of GC_malloc_[atomic_]uncollectable. */
  195. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  196. GC_generic_malloc_uncollectable(
  197. size_t /* lb */, int /* knd */);
  198. /* Same as above but primary for allocating an object of the same kind */
  199. /* as an existing one (kind obtained by GC_get_kind_and_size). */
  200. /* Not suitable for GCJ and typed-malloc kinds. */
  201. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  202. GC_generic_or_special_malloc(
  203. size_t /* size */, int /* knd */);
  204. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  205. GC_debug_generic_or_special_malloc(
  206. size_t /* size */, int /* knd */,
  207. GC_EXTRA_PARAMS);
  208. #ifdef GC_DEBUG
  209. # define GC_GENERIC_OR_SPECIAL_MALLOC(sz, knd) \
  210. GC_debug_generic_or_special_malloc(sz, knd, GC_EXTRAS)
  211. #else
  212. # define GC_GENERIC_OR_SPECIAL_MALLOC(sz, knd) \
  213. GC_generic_or_special_malloc(sz, knd)
  214. #endif /* !GC_DEBUG */
  215. /* Similar to GC_size but returns object kind. Size is returned too */
  216. /* if psize is not NULL. */
  217. GC_API int GC_CALL GC_get_kind_and_size(const void *, size_t * /* psize */)
  218. GC_ATTR_NONNULL(1);
  219. typedef void (GC_CALLBACK * GC_describe_type_fn)(void * /* p */,
  220. char * /* out_buf */);
  221. /* A procedure which */
  222. /* produces a human-readable */
  223. /* description of the "type" of object */
  224. /* p into the buffer out_buf of length */
  225. /* GC_TYPE_DESCR_LEN. This is used by */
  226. /* the debug support when printing */
  227. /* objects. */
  228. /* These functions should be as robust */
  229. /* as possible, though we do avoid */
  230. /* invoking them on objects on the */
  231. /* global free list. */
  232. #define GC_TYPE_DESCR_LEN 40
  233. GC_API void GC_CALL GC_register_describe_type_fn(int /* kind */,
  234. GC_describe_type_fn);
  235. /* Register a describe_type function */
  236. /* to be used when printing objects */
  237. /* of a particular kind. */
  238. /* Clear some of the inaccessible part of the stack. Returns its */
  239. /* argument, so it can be used in a tail call position, hence clearing */
  240. /* another frame. Argument may be NULL. */
  241. GC_API void * GC_CALL GC_clear_stack(void *);
  242. /* Set and get the client notifier on collections. The client function */
  243. /* is called at the start of every full GC (called with the allocation */
  244. /* lock held). May be 0. This is a really tricky interface to use */
  245. /* correctly. Unless you really understand the collector internals, */
  246. /* the callback should not, directly or indirectly, make any GC_ or */
  247. /* potentially blocking calls. In particular, it is not safe to */
  248. /* allocate memory using the garbage collector from within the callback */
  249. /* function. Both the setter and getter acquire the GC lock. */
  250. typedef void (GC_CALLBACK * GC_start_callback_proc)(void);
  251. GC_API void GC_CALL GC_set_start_callback(GC_start_callback_proc);
  252. GC_API GC_start_callback_proc GC_CALL GC_get_start_callback(void);
  253. /* Slow/general mark bit manipulation. The caller must hold the */
  254. /* allocation lock. GC_is_marked returns 1 (TRUE) or 0. */
  255. GC_API int GC_CALL GC_is_marked(const void *) GC_ATTR_NONNULL(1);
  256. GC_API void GC_CALL GC_clear_mark_bit(const void *) GC_ATTR_NONNULL(1);
  257. GC_API void GC_CALL GC_set_mark_bit(const void *) GC_ATTR_NONNULL(1);
  258. /* Push everything in the given range onto the mark stack. */
  259. /* (GC_push_conditional pushes either all or only dirty pages depending */
  260. /* on the third argument.) GC_push_all_eager also ensures that stack */
  261. /* is scanned immediately, not just scheduled for scanning. */
  262. GC_API void GC_CALL GC_push_all(void * /* bottom */, void * /* top */);
  263. GC_API void GC_CALL GC_push_all_eager(void * /* bottom */, void * /* top */);
  264. GC_API void GC_CALL GC_push_conditional(void * /* bottom */, void * /* top */,
  265. int /* bool all */);
  266. GC_API void GC_CALL GC_push_finalizer_structures(void);
  267. GC_API void GC_CALL GC_push_proc(GC_word /* proc */, void * /* start */);
  268. /* Set and get the client push-other-roots procedure. A client */
  269. /* supplied procedure should also call the original procedure. */
  270. /* Note that both the setter and getter require some external */
  271. /* synchronization to avoid data race. */
  272. typedef void (GC_CALLBACK * GC_push_other_roots_proc)(void);
  273. GC_API void GC_CALL GC_set_push_other_roots(GC_push_other_roots_proc);
  274. GC_API GC_push_other_roots_proc GC_CALL GC_get_push_other_roots(void);
  275. /* Walk the GC heap visiting all reachable objects. Assume the caller */
  276. /* holds the allocation lock. Object base pointer, object size and */
  277. /* client custom data are passed to the callback (holding the lock). */
  278. typedef void (GC_CALLBACK *GC_reachable_object_proc)(void * /* obj */,
  279. size_t /* bytes */,
  280. void * /* client_data */);
  281. GC_API void GC_CALL GC_enumerate_reachable_objects_inner(
  282. GC_reachable_object_proc,
  283. void * /* client_data */) GC_ATTR_NONNULL(1);
  284. GC_API int GC_CALL GC_is_tmp_root(void *);
  285. GC_API void GC_CALL GC_print_trace(GC_word /* gc_no */);
  286. GC_API void GC_CALL GC_print_trace_inner(GC_word /* gc_no */);
  287. /* Set the client for when mark stack is empty. A client can use */
  288. /* this callback to process (un)marked objects and push additional */
  289. /* work onto the stack. Useful for implementing ephemerons. */
  290. typedef struct GC_ms_entry* (GC_CALLBACK* GC_mark_stack_empty_proc)(
  291. struct GC_ms_entry* /* mark_stack_ptr */,
  292. struct GC_ms_entry* /* mark_stack_limit */);
  293. GC_API void GC_CALL GC_set_mark_stack_empty (GC_mark_stack_empty_proc);
  294. GC_API GC_mark_stack_empty_proc GC_CALL GC_get_mark_stack_empty (void);
  295. #ifdef __cplusplus
  296. } /* extern "C" */
  297. #endif
  298. #endif /* GC_MARK_H */