#pragma once
#include<cstdlib>
#include<ostream>
#ifdef Z3DEBUG
#include "util/vector.h"
class region {
ptr_vector<char> m_chunks;
unsigned_vector m_scopes;
public:
~region() {
reset();
}
void * allocate(size_t size);
void reset();
void push_scope() {
m_scopes.push_back(m_chunks.size());
}
void pop_scope();
void pop_scope(unsigned num_scopes) {
for (unsigned i = 0; i < num_scopes; ++i) {
pop_scope();
}
}
void display_mem_stats(std::ostream & out) const;
};
#else
class region {
struct mark {
char * m_curr_page;
char * m_curr_ptr;
mark * m_prev_mark;
mark(char * page, char * ptr, mark * m):m_curr_page(page), m_curr_ptr(ptr), m_prev_mark(m) {}
};
char * m_curr_page;
char * m_curr_ptr; char * m_curr_end_ptr; char * m_free_pages;
mark * m_mark;
void allocate_page();
void recycle_curr_page();
public:
region();
~region();
void * allocate(size_t size);
void reset();
void push_scope();
void pop_scope();
void pop_scope(unsigned num_scopes) {
for (unsigned i = 0; i < num_scopes; ++i) {
pop_scope();
}
}
void display_mem_stats(std::ostream & out) const;
};
#endif
inline void * operator new(size_t s, region & r) { return r.allocate(s); }
inline void * operator new[](size_t s, region & r) { return r.allocate(s); }
inline void operator delete(void *, region & ) { }
inline void operator delete[](void *, region & ) { }