Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
Xbyak::CodeArray Class Reference

#include <xbyak.h>

Inheritance diagram for Xbyak::CodeArray:
Collaboration diagram for Xbyak::CodeArray:

Public Member Functions

 CodeArray (size_t maxSize, void *userPtr=0, Allocator *allocator=0)
 
virtual ~CodeArray ()
 
void resetSize ()
 
void db (int code)
 
void db (const uint8 *code, size_t codeSize)
 
void db (uint64 code, size_t codeSize)
 
void dw (uint32 code)
 
void dd (uint32 code)
 
void dq (uint64 code)
 
const uint8getCode () const
 
template<class F >
const F getCode () const
 
const uint8getCurr () const
 
template<class F >
const F getCurr () const
 
size_t getSize () const
 
void setSize (size_t size)
 
void dump () const
 
void rewrite (size_t offset, uint64 disp, size_t size)
 
void save (size_t offset, size_t val, int size, inner::LabelMode mode)
 
bool isAutoGrow () const
 
bool isCalledCalcJmpAddress () const
 

Static Public Member Functions

static bool protect (const void *addr, size_t size, bool canExec)
 
static uint8getAlignedAddress (uint8 *addr, size_t alignedSize=16)
 

Protected Member Functions

void growMemory ()
 
void calcJmpAddress ()
 

Protected Attributes

size_t maxSize_
 
uint8top_
 
size_t size_
 
bool isCalledCalcJmpAddress_
 

Detailed Description

Definition at line 790 of file xbyak.h.

Constructor & Destructor Documentation

◆ CodeArray()

Xbyak::CodeArray::CodeArray ( size_t maxSize,
void * userPtr = 0,
Allocator * allocator = 0 )
inlineexplicit

Definition at line 855 of file xbyak.h.

856 : type_(userPtr == AutoGrow ? AUTO_GROW : userPtr ? USER_BUF : ALLOC_BUF)
857 , alloc_(allocator ? allocator : (Allocator*)&defaultAllocator_)
858 , maxSize_(maxSize)
859 , top_(type_ == USER_BUF ? reinterpret_cast<uint8*>(userPtr) : alloc_->alloc((std::max<size_t>)(maxSize, 1)))
860 , size_(0)
862 {
863 if (maxSize_ > 0 && top_ == 0) throw Error(ERR_CANT_ALLOC);
864 if ((type_ == ALLOC_BUF && alloc_->useProtect()) && !protect(top_, maxSize, true)) {
865 alloc_->free(top_);
866 throw Error(ERR_CANT_PROTECT);
867 }
868 }
Error
Definition calc.cpp:23
size_t size_
Definition xbyak.h:825
uint8 * top_
Definition xbyak.h:824
size_t maxSize_
Definition xbyak.h:823
bool isCalledCalcJmpAddress_
Definition xbyak.h:826
static bool protect(const void *addr, size_t size, bool canExec)
Definition xbyak.h:966
Concept for allocating, resizing and freeing memory block.
@ ERR_CANT_ALLOC
Definition xbyak.h:159
@ ERR_CANT_PROTECT
Definition xbyak.h:151
void *const AutoGrow
Definition xbyak.h:788
unsigned char uint8
virtual uint8 * alloc(size_t size)
Definition xbyak.h:310
virtual bool useProtect() const
Definition xbyak.h:314
virtual void free(uint8 *p)
Definition xbyak.h:311

◆ ~CodeArray()

virtual Xbyak::CodeArray::~CodeArray ( )
inlinevirtual

Definition at line 869 of file xbyak.h.

870 {
871 if (isAllocType()) {
872 if (alloc_->useProtect()) protect(top_, maxSize_, false);
873 alloc_->free(top_);
874 }
875 }

Member Function Documentation

◆ calcJmpAddress()

void Xbyak::CodeArray::calcJmpAddress ( )
inlineprotected

Definition at line 844 of file xbyak.h.

845 {
846 if (isCalledCalcJmpAddress_) return;
847 for (AddrInfoList::const_iterator i = addrInfoList_.begin(), ie = addrInfoList_.end(); i != ie; ++i) {
848 uint64 disp = i->getVal(top_);
849 rewrite(i->codeOffset, disp, i->jmpSize);
850 }
851 if (alloc_->useProtect() && !protect(top_, size_, true)) throw Error(ERR_CANT_PROTECT);
853 }
void rewrite(size_t offset, uint64 disp, size_t size)
Definition xbyak.h:944
Xbyak::uint64 uint64
Definition quantize.cpp:51
Here is the caller graph for this function:

◆ db() [1/3]

void Xbyak::CodeArray::db ( const uint8 * code,
size_t codeSize )
inline

Definition at line 893 of file xbyak.h.

894 {
895 for (size_t i = 0; i < codeSize; i++) db(code[i]);
896 }
void db(int code)
Definition xbyak.h:882

◆ db() [2/3]

void Xbyak::CodeArray::db ( int code)
inline

Definition at line 882 of file xbyak.h.

883 {
884 if (size_ >= maxSize_) {
885 if (type_ == AUTO_GROW) {
886 growMemory();
887 } else {
889 }
890 }
891 top_[size_++] = static_cast<uint8>(code);
892 }
void growMemory()
Definition xbyak.h:831
@ ERR_CODE_IS_TOO_BIG
Definition xbyak.h:139
Here is the caller graph for this function:

◆ db() [3/3]

void Xbyak::CodeArray::db ( uint64 code,
size_t codeSize )
inline

Definition at line 897 of file xbyak.h.

898 {
899 if (codeSize > 8) throw Error(ERR_BAD_PARAMETER);
900 for (size_t i = 0; i < codeSize; i++) db(static_cast<uint8>(code >> (i * 8)));
901 }
@ ERR_BAD_PARAMETER
Definition xbyak.h:150

◆ dd()

void Xbyak::CodeArray::dd ( uint32 code)
inline

Definition at line 903 of file xbyak.h.

903{ db(code, 4); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dq()

void Xbyak::CodeArray::dq ( uint64 code)
inline

Definition at line 904 of file xbyak.h.

904{ db(code, 8); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump()

void Xbyak::CodeArray::dump ( ) const
inline

Definition at line 917 of file xbyak.h.

918 {
919 const uint8 *p = getCode();
920 size_t bufSize = getSize();
921 size_t remain = bufSize;
922 for (int i = 0; i < 4; i++) {
923 size_t disp = 16;
924 if (remain < 16) {
925 disp = remain;
926 }
927 for (size_t j = 0; j < 16; j++) {
928 if (j < disp) {
929 printf("%02X", p[i * 16 + j]);
930 }
931 }
932 putchar('\n');
933 remain -= disp;
934 if (remain == 0) {
935 break;
936 }
937 }
938 }
const mie::Vuint & p
Definition bn.cpp:27
size_t getSize() const
Definition xbyak.h:911
const uint8 * getCode() const
Definition xbyak.h:905
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
uint16_t j

◆ dw()

void Xbyak::CodeArray::dw ( uint32 code)
inline

Definition at line 902 of file xbyak.h.

902{ db(code, 2); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAlignedAddress()

static uint8 * Xbyak::CodeArray::getAlignedAddress ( uint8 * addr,
size_t alignedSize = 16 )
inlinestatic

get aligned memory pointer

Parameters
addr[in] address
alignedSize[in] power of two
Returns
aligned addr by alingedSize

Definition at line 987 of file xbyak.h.

988 {
989 return reinterpret_cast<uint8*>((reinterpret_cast<size_t>(addr) + alignedSize - 1) & ~(alignedSize - static_cast<size_t>(1)));
990 }
Here is the caller graph for this function:

◆ getCode() [1/2]

const uint8 * Xbyak::CodeArray::getCode ( ) const
inline

Definition at line 905 of file xbyak.h.

905{ return top_; }
Here is the caller graph for this function:

◆ getCode() [2/2]

template<class F >
const F Xbyak::CodeArray::getCode ( ) const
inline

Definition at line 907 of file xbyak.h.

907{ return CastTo<F>(top_); }
const To CastTo(From p)
Definition xbyak.h:279
Here is the call graph for this function:

◆ getCurr() [1/2]

const uint8 * Xbyak::CodeArray::getCurr ( ) const
inline

Definition at line 908 of file xbyak.h.

908{ return &top_[size_]; }
Here is the caller graph for this function:

◆ getCurr() [2/2]

template<class F >
const F Xbyak::CodeArray::getCurr ( ) const
inline

Definition at line 910 of file xbyak.h.

910{ return CastTo<F>(&top_[size_]); }
Here is the call graph for this function:

◆ getSize()

size_t Xbyak::CodeArray::getSize ( ) const
inline

Definition at line 911 of file xbyak.h.

911{ return size_; }
Here is the caller graph for this function:

◆ growMemory()

void Xbyak::CodeArray::growMemory ( )
inlineprotected

Definition at line 831 of file xbyak.h.

832 {
833 const size_t newSize = (std::max<size_t>)(DEFAULT_MAX_CODE_SIZE, maxSize_ * 2);
834 uint8 *newTop = alloc_->alloc(newSize);
835 if (newTop == 0) throw Error(ERR_CANT_ALLOC);
836 for (size_t i = 0; i < size_; i++) newTop[i] = top_[i];
837 alloc_->free(top_);
838 top_ = newTop;
839 maxSize_ = newSize;
840 }
@ DEFAULT_MAX_CODE_SIZE
Definition xbyak.h:107

◆ isAutoGrow()

bool Xbyak::CodeArray::isAutoGrow ( ) const
inline

Definition at line 957 of file xbyak.h.

957{ return type_ == AUTO_GROW; }
Here is the caller graph for this function:

◆ isCalledCalcJmpAddress()

bool Xbyak::CodeArray::isCalledCalcJmpAddress ( ) const
inline

Definition at line 958 of file xbyak.h.

958{ return isCalledCalcJmpAddress_; }
Here is the caller graph for this function:

◆ protect()

static bool Xbyak::CodeArray::protect ( const void * addr,
size_t size,
bool canExec )
inlinestatic

change exec permission of memory

Parameters
addr[in] buffer address
size[in] buffer size
canExec[in] true(enable to exec), false(disable to exec)
Returns
true(success), false(failure)

Definition at line 966 of file xbyak.h.

967 {
968#if defined(_WIN32)
969 DWORD oldProtect;
970 return VirtualProtect(const_cast<void*>(addr), size, canExec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldProtect) != 0;
971#elif defined(__GNUC__)
972 size_t pageSize = sysconf(_SC_PAGESIZE);
973 size_t iaddr = reinterpret_cast<size_t>(addr);
974 size_t roundAddr = iaddr & ~(pageSize - static_cast<size_t>(1));
975 int mode = PROT_READ | PROT_WRITE | (canExec ? PROT_EXEC : 0);
976 return mprotect(reinterpret_cast<void*>(roundAddr), size + (iaddr - roundAddr), mode) == 0;
977#else
978 return true;
979#endif
980 }
Here is the caller graph for this function:

◆ resetSize()

void Xbyak::CodeArray::resetSize ( )
inline

Definition at line 876 of file xbyak.h.

877 {
878 size_ = 0;
879 addrInfoList_.clear();
881 }
Here is the caller graph for this function:

◆ rewrite()

void Xbyak::CodeArray::rewrite ( size_t offset,
uint64 disp,
size_t size )
inline

Definition at line 944 of file xbyak.h.

945 {
946 assert(offset < maxSize_);
947 if (size != 1 && size != 2 && size != 4 && size != 8) throw Error(ERR_BAD_PARAMETER);
948 uint8 *const data = top_ + offset;
949 for (size_t i = 0; i < size; i++) {
950 data[i] = static_cast<uint8>(disp >> (i * 8));
951 }
952 }

◆ save()

void Xbyak::CodeArray::save ( size_t offset,
size_t val,
int size,
inner::LabelMode mode )
inline

Definition at line 953 of file xbyak.h.

954 {
955 addrInfoList_.push_back(AddrInfo(offset, val, size, mode));
956 }

◆ setSize()

void Xbyak::CodeArray::setSize ( size_t size)
inline

Definition at line 912 of file xbyak.h.

913 {
914 if (size > maxSize_) throw Error(ERR_OFFSET_IS_TOO_BIG);
915 size_ = size;
916 }
@ ERR_OFFSET_IS_TOO_BIG
Definition xbyak.h:153
Here is the caller graph for this function:

Member Data Documentation

◆ isCalledCalcJmpAddress_

bool Xbyak::CodeArray::isCalledCalcJmpAddress_
protected

Definition at line 826 of file xbyak.h.

◆ maxSize_

size_t Xbyak::CodeArray::maxSize_
protected

Definition at line 823 of file xbyak.h.

◆ size_

size_t Xbyak::CodeArray::size_
protected

Definition at line 825 of file xbyak.h.

◆ top_

uint8* Xbyak::CodeArray::top_
protected

Definition at line 824 of file xbyak.h.


The documentation for this class was generated from the following file: