block_of_code: Provide an alloc function to allocate space in the code block

This commit is contained in:
MerryMage 2016-12-11 15:27:26 +00:00 committed by Merry
parent f467589346
commit 179a3388f9
2 changed files with 13 additions and 0 deletions

View file

@ -4,6 +4,7 @@
* General Public License version 2 or any later version. * General Public License version 2 or any later version.
*/ */
#include <cstring>
#include <limits> #include <limits>
#include <xbyak.h> #include <xbyak.h>
@ -227,6 +228,17 @@ void BlockOfCode::nop(size_t size) {
} }
} }
void* BlockOfCode::alloc(size_t alloc_size) {
if (size_ + alloc_size >= maxSize_) {
throw Xbyak::Error(Xbyak::ERR_CODE_IS_TOO_BIG);
}
void* ret = getCurr<void*>();
size_ += alloc_size;
memset(ret, 0, alloc_size);
return ret;
}
void BlockOfCode::SetCodePtr(CodePtr code_ptr) { void BlockOfCode::SetCodePtr(CodePtr code_ptr) {
// The "size" defines where top_, the insertion point, is. // The "size" defines where top_, the insertion point, is.
size_t required_size = reinterpret_cast<const u8*>(code_ptr) - getCode(); size_t required_size = reinterpret_cast<const u8*>(code_ptr) - getCode();

View file

@ -127,6 +127,7 @@ public:
void int3() { db(0xCC); } void int3() { db(0xCC); }
void nop(size_t size = 1); void nop(size_t size = 1);
void* alloc(size_t size);
void SetCodePtr(CodePtr code_ptr); void SetCodePtr(CodePtr code_ptr);
void EnsurePatchLocationSize(CodePtr begin, size_t size); void EnsurePatchLocationSize(CodePtr begin, size_t size);