| 24 | JitRuntime::~JitRuntime() noexcept {} |
| 25 | |
| 26 | Error JitRuntime::_add(void** dst, CodeHolder* code) noexcept { |
| 27 | *dst = nullptr; |
| 28 | |
| 29 | ASMJIT_PROPAGATE(code->flatten()); |
| 30 | ASMJIT_PROPAGATE(code->resolve_cross_section_fixups()); |
| 31 | |
| 32 | size_t estimated_code_size = code->code_size(); |
| 33 | if (ASMJIT_UNLIKELY(estimated_code_size == 0)) { |
| 34 | return make_error(Error::kNoCodeGenerated); |
| 35 | } |
| 36 | |
| 37 | JitAllocator::Span span; |
| 38 | ASMJIT_PROPAGATE(_allocator.alloc(Out(span), estimated_code_size)); |
| 39 | |
| 40 | // Relocate the code. |
| 41 | CodeHolder::RelocationSummary relocation_summary; |
| 42 | Error err = code->relocate_to_base(uintptr_t(span.rx()), &relocation_summary); |
| 43 | if (ASMJIT_UNLIKELY(err != Error::kOk)) { |
| 44 | _allocator.release(span.rx()); |
| 45 | return err; |
| 46 | } |
| 47 | |
| 48 | // Recalculate the final code size and shrink the memory we allocated for it |
| 49 | // in case that some relocations didn't require records in an address table. |
| 50 | size_t code_size = estimated_code_size - relocation_summary.code_size_reduction; |
| 51 | |
| 52 | // If not true it means that `relocate_to_base()` filled wrong information in `relocation_summary`. |
| 53 | ASMJIT_ASSERT(code_size == code->code_size()); |
| 54 | |
| 55 | _allocator.write(span, [&](JitAllocator::Span& span) noexcept -> Error { |
| 56 | uint8_t* rw = static_cast<uint8_t*>(span.rw()); |
| 57 | |
| 58 | for (Section* section : code->_sections) { |
| 59 | size_t offset = size_t(section->offset()); |
| 60 | size_t buffer_size = size_t(section->buffer_size()); |
| 61 | size_t virtual_size = size_t(section->virtual_size()); |
| 62 | |
| 63 | ASMJIT_ASSERT(offset + buffer_size <= span.size()); |
| 64 | memcpy(rw + offset, section->data(), buffer_size); |
| 65 | |
| 66 | if (virtual_size > buffer_size) { |
| 67 | ASMJIT_ASSERT(offset + virtual_size <= span.size()); |
| 68 | memset(rw + offset + buffer_size, 0, virtual_size - buffer_size); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | span.shrink(code_size); |
| 73 | return Error::kOk; |
| 74 | }); |
| 75 | |
| 76 | *dst = span.rx(); |
| 77 | return Error::kOk; |
| 78 | } |
| 79 | |
| 80 | Error JitRuntime::_release(void* p) noexcept { |
| 81 | return _allocator.release(p); |
nothing calls this directly
no test coverage detected