mirror of
https://github.com/asmjit/asmjit.git
synced 2025-12-17 20:44:37 +03:00
[ABI] Reworked JitAllocator to alloc spans and to use explicit write API
This commit is contained in:
@@ -127,28 +127,26 @@ int main() {
|
||||
}
|
||||
|
||||
// Allocate memory for the function and relocate it there.
|
||||
void* rxPtr;
|
||||
void* rwPtr;
|
||||
err = allocator.alloc(&rxPtr, &rwPtr, codeSize);
|
||||
JitAllocator::Span span;
|
||||
err = allocator.alloc(span, codeSize);
|
||||
if (err)
|
||||
fail("Failed to allocate executable memory", err);
|
||||
|
||||
// Relocate to the base-address of the allocated memory.
|
||||
code.relocateToBase(uint64_t(uintptr_t(rxPtr)));
|
||||
|
||||
{
|
||||
VirtMem::ProtectJitReadWriteScope scope(rxPtr, code.codeSize());
|
||||
code.relocateToBase(uint64_t(uintptr_t(span.rx())));
|
||||
|
||||
allocator.write(span, [&](JitAllocator::Span& span) noexcept -> Error {
|
||||
// Copy the flattened code into `mem.rw`. There are two ways. You can either copy
|
||||
// everything manually by iterating over all sections or use `copyFlattenedData`.
|
||||
// This code is similar to what `copyFlattenedData(p, codeSize, 0)` would do:
|
||||
for (Section* section : code.sectionsByOrder())
|
||||
memcpy(static_cast<uint8_t*>(rwPtr) + size_t(section->offset()), section->data(), section->bufferSize());
|
||||
}
|
||||
memcpy(static_cast<uint8_t*>(span.rw()) + size_t(section->offset()), section->data(), section->bufferSize());
|
||||
return kErrorOk;
|
||||
});
|
||||
|
||||
// Execute the function and test whether it works.
|
||||
typedef size_t (*Func)(size_t idx);
|
||||
Func fn = (Func)rxPtr;
|
||||
Func fn = (Func)span.rx();
|
||||
|
||||
printf("\n");
|
||||
if (fn(0) != dataArray[0] ||
|
||||
|
||||
Reference in New Issue
Block a user