/* * Memory.h * */ #ifndef GC_MEMORY_H_ #define GC_MEMORY_H_ #include #include #include #include using namespace std; #include "Tools/Exceptions.h" #include "Clear.h" #include "config.h" class NoMemory { public: void resize_min(size_t, const char*) {} }; namespace GC { template class Memory : public vector { public: void resize(size_t size, const char* name = ""); void resize_min(size_t size, const char* name = ""); void check_index(Integer index) const; T& operator[] (Integer i); const T& operator[] (Integer i) const; size_t capacity_in_bytes() const { return this->capacity() * sizeof(T); } }; template inline void Memory::check_index(Integer index) const { (void)index; #ifdef CHECK_SIZE size_t i = index.get(); if (i >= vector::size()) { stringstream ss; ss << T::type_string() << " memory overflow: " << i << "/" << vector::size(); throw Processor_Error(ss.str()); } #ifdef DEBUG_MEMORY cout << typeid(T).name() << " at " << this << " index " << i << ": " << vector::operator[](i) << endl; #endif #endif } template inline T& Memory::operator[] (Integer i) { check_index(i); return vector::operator[](i.get()); } template inline const T& Memory::operator[] (Integer i) const { check_index(i); return vector::operator[](i.get()); } template inline void Memory::resize(size_t size, const char* name) { (void) name; #ifdef DEBUG_MEMORY if (size > 1000) cerr << "Resizing " << T::type_string() << " " << name << " to " << size << endl; #endif vector::resize(size); } template inline void Memory::resize_min(size_t size, const char* name) { if (this->size() < size) resize(size, name); } template inline ostream& operator<<(ostream& s, const Memory& memory) { for (auto& x : memory) x.output(s, false); return s; } } /* namespace GC */ #endif /* GC_MEMORY_H_ */