Added back interface to override default functions for memory allocation (ASMJIT_ALLOC, ASMJIT_REALLOC, ASMJIT_FREE).

This commit is contained in:
kobalicek
2014-06-05 11:02:38 +02:00
parent db322d5dc1
commit df618fa24d
7 changed files with 46 additions and 36 deletions

View File

@@ -35,7 +35,7 @@ BaseAssembler::BaseAssembler(Runtime* runtime) :
BaseAssembler::~BaseAssembler() {
if (_buffer != NULL)
::free(_buffer);
ASMJIT_FREE(_buffer);
}
// ============================================================================
@@ -51,7 +51,7 @@ void BaseAssembler::reset() {
_baseZone.reset();
if (_buffer != NULL) {
::free(_buffer);
ASMJIT_FREE(_buffer);
_buffer = NULL;
_end = NULL;
@@ -124,9 +124,9 @@ Error BaseAssembler::_reserve(size_t n) {
uint8_t* newBuffer;
if (_buffer == NULL)
newBuffer = static_cast<uint8_t*>(::malloc(n));
newBuffer = static_cast<uint8_t*>(ASMJIT_ALLOC(n));
else
newBuffer = static_cast<uint8_t*>(::realloc(_buffer, n));
newBuffer = static_cast<uint8_t*>(ASMJIT_REALLOC(_buffer, n));
if (newBuffer == NULL)
return setError(kErrorNoHeapMemory);

View File

@@ -73,13 +73,13 @@ Error PodVectorBase::_reserve(size_t n, size_t sizeOfT) {
return kErrorNoHeapMemory;
if (d == &_nullData) {
d = static_cast<PodVectorData*>(::malloc(nBytes));
d = static_cast<PodVectorData*>(ASMJIT_ALLOC(nBytes));
if (d == NULL)
return kErrorNoHeapMemory;
d->length = 0;
}
else {
d = static_cast<PodVectorData*>(::realloc(d, nBytes));
d = static_cast<PodVectorData*>(ASMJIT_REALLOC(d, nBytes));
if (d == NULL)
return kErrorNoHeapMemory;
}

View File

@@ -55,7 +55,7 @@ struct PodVectorBase {
//! Destroy the `PodVectorBase` and data.
ASMJIT_INLINE ~PodVectorBase() {
if (_d != &_nullData)
::free(_d);
ASMJIT_FREE(_d);
}
// --------------------------------------------------------------------------
@@ -140,7 +140,7 @@ struct PodVector : PodVectorBase {
//! Clear vector data and free internal buffer.
ASMJIT_INLINE void reset() {
if (_d != &_nullData) {
::free(_d);
ASMJIT_FREE(_d);
_d = const_cast<PodVectorData*>(&_nullData);
}
}

View File

@@ -31,7 +31,7 @@ StringBuilder::StringBuilder() :
StringBuilder::~StringBuilder() {
if (_canFree)
::free(_data);
ASMJIT_FREE(_data);
}
// ============================================================================
@@ -62,14 +62,14 @@ char* StringBuilder::prepare(uint32_t op, size_t len) {
if (to < 256 - sizeof(intptr_t))
to = 256 - sizeof(intptr_t);
char* newData = static_cast<char*>(::malloc(to + sizeof(intptr_t)));
char* newData = static_cast<char*>(ASMJIT_ALLOC(to + sizeof(intptr_t)));
if (newData == NULL) {
clear();
return NULL;
}
if (_canFree)
::free(_data);
ASMJIT_FREE(_data);
_data = newData;
_capacity = to + sizeof(intptr_t) - 1;
@@ -114,14 +114,14 @@ char* StringBuilder::prepare(uint32_t op, size_t len) {
}
to = IntUtil::alignTo<size_t>(to, sizeof(intptr_t));
char* newData = static_cast<char*>(::malloc(to + sizeof(intptr_t)));
char* newData = static_cast<char*>(ASMJIT_ALLOC(to + sizeof(intptr_t)));
if (newData == NULL)
return NULL;
::memcpy(newData, _data, _length);
if (_canFree)
::free(_data);
ASMJIT_FREE(_data);
_data = newData;
_capacity = to + sizeof(intptr_t) - 1;
@@ -146,13 +146,13 @@ bool StringBuilder::reserve(size_t to) {
to = IntUtil::alignTo<size_t>(to, sizeof(intptr_t));
char* newData = static_cast<char*>(::malloc(to + sizeof(intptr_t)));
char* newData = static_cast<char*>(ASMJIT_ALLOC(to + sizeof(intptr_t)));
if (newData == NULL)
return false;
::memcpy(newData, _data, _length + 1);
if (_canFree)
::free(_data);
ASMJIT_FREE(_data);
_data = newData;
_capacity = to + sizeof(intptr_t) - 1;

View File

@@ -458,7 +458,7 @@ VMemPrivate::~VMemPrivate() {
PermanentNode* node = _permanent;
while (node) {
PermanentNode* prev = node->prev;
::free(node);
ASMJIT_FREE(node);
node = prev;
}
}
@@ -480,14 +480,14 @@ MemNode* VMemPrivate::createNode(size_t size, size_t density) {
size_t blocks = (vsize / density);
size_t bsize = (((blocks + 7) >> 3) + sizeof(size_t) - 1) & ~(size_t)(sizeof(size_t) - 1);
MemNode* node = static_cast<MemNode*>(::malloc(sizeof(MemNode)));
uint8_t* data = static_cast<uint8_t*>(::malloc(bsize * 2));
MemNode* node = static_cast<MemNode*>(ASMJIT_ALLOC(sizeof(MemNode)));
uint8_t* data = static_cast<uint8_t*>(ASMJIT_ALLOC(bsize * 2));
// Out of memory.
if (node == NULL || data == NULL) {
freeVirtualMemory(vmem, vsize);
if (node) ::free(node);
if (data) ::free(data);
if (node) ASMJIT_FREE(node);
if (data) ASMJIT_FREE(data);
return NULL;
}
@@ -523,8 +523,8 @@ void VMemPrivate::reset(bool keepVirtualMemory) {
if (!keepVirtualMemory)
freeVirtualMemory(node->mem, node->size);
::free(node->baUsed);
::free(node);
ASMJIT_FREE(node->baUsed);
ASMJIT_FREE(node);
node = next;
}
@@ -562,7 +562,7 @@ void* VMemPrivate::allocPermanent(size_t vsize) {
if (vsize > nodeSize)
nodeSize = vsize;
node = static_cast<PermanentNode*>(::malloc(sizeof(PermanentNode)));
node = static_cast<PermanentNode*>(ASMJIT_ALLOC(sizeof(PermanentNode)));
// Out of memory.
if (node == NULL)
@@ -572,7 +572,7 @@ void* VMemPrivate::allocPermanent(size_t vsize) {
// Out of memory.
if (node->mem == NULL) {
::free(node);
ASMJIT_FREE(node);
return NULL;
}
@@ -775,7 +775,7 @@ Error VMemPrivate::release(void* address) {
// Free memory associated with node (this memory is not accessed
// anymore so it's safe).
freeVirtualMemory(node->mem, node->size);
::free(node->baUsed);
ASMJIT_FREE(node->baUsed);
node->baUsed = NULL;
node->baCont = NULL;
@@ -785,7 +785,7 @@ Error VMemPrivate::release(void* address) {
// Remove node. This function can return different node than
// passed into, but data is copied into previous node if needed.
::free(removeNode(node));
ASMJIT_FREE(removeNode(node));
ASMJIT_ASSERT(checkTree());
}
@@ -1274,8 +1274,8 @@ UNIT(base_vmem) {
INFO("Memory alloc/free test - %d allocations.", static_cast<int>(kCount));
void** a = (void**)::malloc(sizeof(void*) * kCount);
void** b = (void**)::malloc(sizeof(void*) * kCount);
void** a = (void**)ASMJIT_ALLOC(sizeof(void*) * kCount);
void** b = (void**)ASMJIT_ALLOC(sizeof(void*) * kCount);
EXPECT(a != NULL && b != NULL,
"Couldn't allocate %u bytes on heap.", kCount * 2);
@@ -1306,7 +1306,7 @@ UNIT(base_vmem) {
EXPECT(a[i] != NULL,
"Couldn't allocate %d bytes of virtual memory.", r);
b[i] = ::malloc(r);
b[i] = ASMJIT_ALLOC(r);
EXPECT(b[i] != NULL,
"Couldn't allocate %d bytes on heap.", r);
@@ -1322,7 +1322,7 @@ UNIT(base_vmem) {
VMemTest_verify(a[i], b[i]);
EXPECT(memmgr.release(a[i]) == kErrorOk,
"Failed to free %p.", a[i]);
::free(b[i]);
ASMJIT_FREE(b[i]);
}
VMemTest_stats(memmgr);
@@ -1334,7 +1334,7 @@ UNIT(base_vmem) {
EXPECT(a[i] != NULL,
"Couldn't allocate %d bytes of virtual memory.", r);
b[i] = ::malloc(r);
b[i] = ASMJIT_ALLOC(r);
EXPECT(b[i] != NULL,
"Couldn't allocate %d bytes on heap.");
@@ -1347,12 +1347,12 @@ UNIT(base_vmem) {
VMemTest_verify(a[i], b[i]);
EXPECT(memmgr.release(a[i]) == kErrorOk,
"Failed to free %p.", a[i]);
::free(b[i]);
ASMJIT_FREE(b[i]);
}
VMemTest_stats(memmgr);
::free(a);
::free(b);
ASMJIT_FREE(a);
ASMJIT_FREE(b);
}
#endif // ASMJIT_TEST

View File

@@ -64,7 +64,7 @@ void Zone::reset() {
do {
Block* prev = cur->prev;
::free(cur);
ASMJIT_FREE(cur);
cur = prev;
} while (cur != NULL);
@@ -98,7 +98,7 @@ void* Zone::_alloc(size_t size) {
if (blockSize > ~static_cast<size_t>(0) - sizeof(Block))
return NULL;
Block* newBlock = static_cast<Block*>(::malloc(sizeof(Block) - sizeof(void*) + blockSize));
Block* newBlock = static_cast<Block*>(ASMJIT_ALLOC(sizeof(Block) - sizeof(void*) + blockSize));
if (newBlock == NULL)
return NULL;

View File

@@ -185,6 +185,16 @@
# define ASMJIT_ENUM(_Name_) enum _Name_
#endif
// ============================================================================
// [asmjit::build - Memory Management]
// ============================================================================
#if !defined(ASMJIT_ALLOC) && !defined(ASMJIT_REALLOC) && !defined(ASMJIT_FREE)
# define ASMJIT_ALLOC(_Size_) ::malloc(_Size_)
# define ASMJIT_REALLOC(_Ptr_, _Size_) ::realloc(_Ptr_, _Size_)
# define ASMJIT_FREE(_Ptr_) ::free(_Ptr_)
#endif // !ASMJIT_ALLOC && !ASMJIT_REALLOC && !ASMJIT_FREE
// ============================================================================
// [asmjit::build - _ASMJIT_HOST_INDEX]
// ============================================================================