3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-08-19 17:50:23 +00:00

add reallocate() function and use it in bit_vector and vector containers

give a speedup of 1-4%

Signed-off-by: Nuno Lopes <a-nlopes@microsoft.com>
This commit is contained in:
Nuno Lopes 2015-03-10 16:53:47 +00:00
parent 55ca6ce44b
commit 44e647e72b
5 changed files with 51 additions and 18 deletions

View file

@ -252,6 +252,24 @@ void * memory::allocate(size_t s) {
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
}
void* memory::reallocate(void *p, size_t s) {
size_t *sz_p = reinterpret_cast<size_t*>(p)-1;
size_t sz = *sz_p;
void *real_p = reinterpret_cast<void*>(sz_p);
s = s + sizeof(size_t); // we allocate an extra field!
g_memory_thread_alloc_size += s - sz;
if (g_memory_thread_alloc_size > SYNCH_THRESHOLD) {
synchronize_counters(true);
}
void *r = realloc(real_p, s);
if (r == 0)
throw_out_of_memory();
*(static_cast<size_t*>(r)) = s;
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
}
#else
// ==================================
// ==================================
@ -292,5 +310,29 @@ void * memory::allocate(size_t s) {
*(static_cast<size_t*>(r)) = s;
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
}
void* memory::reallocate(void *p, size_t s) {
size_t * sz_p = reinterpret_cast<size_t*>(p) - 1;
size_t sz = *sz_p;
void * real_p = reinterpret_cast<void*>(sz_p);
s = s + sizeof(size_t); // we allocate an extra field!
bool out_of_mem = false;
#pragma omp critical (z3_memory_manager)
{
g_memory_alloc_size += s - sz;
if (g_memory_alloc_size > g_memory_max_used_size)
g_memory_max_used_size = g_memory_alloc_size;
if (g_memory_max_size != 0 && g_memory_alloc_size > g_memory_max_size)
out_of_mem = true;
}
if (out_of_mem)
throw_out_of_memory();
void *r = realloc(real_p, s);
if (r == 0)
throw_out_of_memory();
*(static_cast<size_t*>(r)) = s;
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
}
#endif