|
| 1 | +// This is an example of native API for the single instance MMTk. |
| 2 | + |
| 3 | +// Note: the mmtk core does not directly provide this API. However, it provides |
| 4 | +// a similar multi-instance Rust API. A VM binding should write their own C |
| 5 | +// header file (possibly based on this example with their own extension and |
| 6 | +// modification), and expose the Rust API based on their native API. |
| 7 | + |
| 8 | +#ifndef MMTK_H |
| 9 | +#define MMTK_H |
| 10 | + |
| 11 | +#include <stdbool.h> |
| 12 | +#include <stddef.h> |
| 13 | + |
| 14 | +#ifdef __cplusplus |
| 15 | +extern "C" { |
| 16 | +#endif |
| 17 | + |
| 18 | +typedef void* MMTk_Mutator; |
| 19 | + |
| 20 | +// Initialize an MMTk instance |
| 21 | +extern void mmtk_init(size_t heap_size); |
| 22 | + |
| 23 | +// Request MMTk to create a new mutator for the given `tls` thread |
| 24 | +extern MMTk_Mutator mmtk_bind_mutator(void* tls); |
| 25 | + |
| 26 | +// Reclaim mutator that is no longer needed |
| 27 | +extern void mmtk_destroy_mutator(MMTk_Mutator mutator); |
| 28 | + |
| 29 | +// Flush mutator local state |
| 30 | +extern void mmtk_flush_mutator(MMTk_Mutator mutator); |
| 31 | + |
| 32 | +// Initialize MMTk scheduler and GC workers |
| 33 | +extern void mmtk_initialize_collection(void* tls); |
| 34 | + |
| 35 | +// Allow MMTk to perform a GC when the heap is full |
| 36 | +extern void mmtk_enable_collection(); |
| 37 | + |
| 38 | +// Disallow MMTk to perform a GC when the heap is full |
| 39 | +extern void mmtk_disable_collection(); |
| 40 | + |
| 41 | +// Allocate memory for an object |
| 42 | +extern void* mmtk_alloc(MMTk_Mutator mutator, |
| 43 | + size_t size, |
| 44 | + size_t align, |
| 45 | + size_t offset, |
| 46 | + int allocator); |
| 47 | + |
| 48 | +// Slowpath allocation for an object |
| 49 | +extern void* mmtk_alloc_slow(MMTk_Mutator mutator, |
| 50 | + size_t size, |
| 51 | + size_t align, |
| 52 | + size_t offset, |
| 53 | + int allocator); |
| 54 | + |
| 55 | +// Perform post-allocation hooks or actions such as initializing object metadata |
| 56 | +extern void mmtk_post_alloc(MMTk_Mutator mutator, |
| 57 | + void* refer, |
| 58 | + int bytes, |
| 59 | + int allocator); |
| 60 | + |
| 61 | +// Return if the object pointed to by `ref` is live |
| 62 | +extern bool mmtk_is_live_object(void* ref); |
| 63 | + |
| 64 | +// Return if the object pointed to by `ref` is in mapped memory |
| 65 | +extern bool mmtk_is_mapped_object(void* ref); |
| 66 | + |
| 67 | +// Return if the address pointed to by `addr` is in mapped memory |
| 68 | +extern bool mmtk_is_mapped_address(void* addr); |
| 69 | + |
| 70 | +// Return if object pointed to by `object` will never move |
| 71 | +extern bool mmtk_will_never_move(void* object); |
| 72 | + |
| 73 | +// Process an MMTk option. Return true if option was processed successfully |
| 74 | +extern bool mmtk_process(char* name, char* value); |
| 75 | + |
| 76 | +// Process MMTk options. Return true if all options were processed successfully |
| 77 | +extern bool mmtk_process_bulk(char* options); |
| 78 | + |
| 79 | +// Sanity only. Scan heap for discrepancies and errors |
| 80 | +extern void mmtk_scan_region(); |
| 81 | + |
| 82 | +// Request MMTk to trigger a GC. Note that this may not actually trigger a GC |
| 83 | +extern void mmtk_handle_user_collection_request(void* tls); |
| 84 | + |
| 85 | +// Run the main loop for the GC controller thread. Does not return |
| 86 | +extern void mmtk_start_control_collector(void* tls, void* worker); |
| 87 | + |
| 88 | +// Run the main loop for a GC worker. Does not return |
| 89 | +extern void mmtk_start_worker(void* tls, void* worker); |
| 90 | + |
| 91 | +// Return the current amount of free memory in bytes |
| 92 | +extern size_t mmtk_free_bytes(); |
| 93 | + |
| 94 | +// Return the current amount of used memory in bytes |
| 95 | +extern size_t mmtk_used_bytes(); |
| 96 | + |
| 97 | +// Return the current amount of total memory in bytes |
| 98 | +extern size_t mmtk_total_bytes(); |
| 99 | + |
| 100 | +// Return the starting address of MMTk's heap |
| 101 | +extern void* mmtk_starting_heap_address(); |
| 102 | + |
| 103 | +// Return the ending address of MMTk's heap |
| 104 | +extern void* mmtk_last_heap_address(); |
| 105 | + |
| 106 | +// Add a reference to the list of weak references |
| 107 | +extern void mmtk_add_weak_candidate(void* ref); |
| 108 | + |
| 109 | +// Add a reference to the list of soft references |
| 110 | +extern void mmtk_add_soft_candidate(void* ref); |
| 111 | + |
| 112 | +// Add a reference to the list of phantom references |
| 113 | +extern void mmtk_add_phantom_candidate(void* ref); |
| 114 | + |
| 115 | +// Generic hook to allow benchmarks to be harnessed |
| 116 | +extern void mmtk_harness_begin(void* tls); |
| 117 | + |
| 118 | +// Generic hook to allow benchmarks to be harnessed |
| 119 | +extern void mmtk_harness_end(); |
| 120 | + |
| 121 | +#ifdef __cplusplus |
| 122 | +} |
| 123 | +#endif |
| 124 | + |
| 125 | +#endif // MMTK_H |
0 commit comments