#include "MemoryAllocator/CodeBuffer/CodeBufferBase.h" #include "xnucxx/LiteMemOpt.h" // alk #include #include CodeBufferBase *CodeBufferBase::Copy() { CodeBufferBase *result = new CodeBufferBase(this->getCapacity()); result->EmitBuffer(this->getRawBuffer(), this->getSize()); return result; } void CodeBufferBase::Emit8(uint8_t value) { DLOG(0, "[code buffer] Emit8 with %x, cursor %x", value, buffer_cursor); // Ensure the free space enough for the template T value this->ensureCapacity(sizeof(uint8_t) + this->getSize()); *reinterpret_cast(buffer_cursor) = value; buffer_cursor += sizeof(uint8_t); } void CodeBufferBase::Emit16(uint16_t value) { DLOG(0, "[code buffer] Emit16 with %x, cursor %x", value, buffer_cursor); // Ensure the free space enough for the template T value this->ensureCapacity(sizeof(uint16_t) + this->getSize()); *reinterpret_cast(buffer_cursor) = value; buffer_cursor += sizeof(uint16_t); } void CodeBufferBase::Emit32(uint32_t value) { DLOG(0, "[code buffer] Emit32 with %x, cursor %x", value, buffer_cursor); // Ensure the free space enough for the template T value this->ensureCapacity(sizeof(uint32_t) + this->getSize()); *reinterpret_cast(buffer_cursor) = value; buffer_cursor += sizeof(uint32_t); } void CodeBufferBase::Emit64(uint64_t value) { DLOG(0, "[code buffer] Emit64 with %x, cursor %x", value, buffer_cursor); // Ensure the free space enough for the template T value this->ensureCapacity(sizeof(uint64_t) + this->getSize()); *reinterpret_cast(buffer_cursor) = value; buffer_cursor += sizeof(uint64_t); } void CodeBufferBase::EmitBuffer(void *buffer, int buffer_size) { DLOG(0, "[code buffer] EmitBuffer with size %d, cursor %x", buffer_size, buffer_cursor); // Ensure the free space enough for the template T value this->ensureCapacity(buffer_size + this->getSize()); _memcpy(buffer_cursor, buffer, buffer_size); buffer_cursor += buffer_size; } #if 0 // Template Advanced won't enable even in userspace template T CodeBufferBase::Load(int offset) { return *reinterpret_cast(buffer + offset); } template void CodeBufferBase::Store(int offset, T value) { *reinterpret_cast(buffer + offset) = value; } template void CodeBufferBase::Emit(T value) { // Ensure the free space enough for the template T value ensureCapacity(sizeof(T) + getSize()); *reinterpret_cast(buffer_cursor) = value; buffer_cursor += sizeof(T); } #endif