mem.h (836B)
1 #ifndef MEM_H 2 #define MEM_H 3 4 #include <string> // std::string 5 6 7 namespace citrun { 8 9 // 10 // Class that handles operating system independent access to blocks of memory 11 // that are efficiently sized for the underlying system. 12 // 13 class mem 14 { 15 size_t m_off; 16 17 // Allocation size is system dependent. 18 virtual size_t alloc_size() = 0; 19 20 protected: 21 void *m_base; 22 size_t m_size; 23 24 public: 25 mem() : m_off(0) {}; 26 27 void increment(size_t size) 28 { 29 size_t page_mask; 30 size_t rounded_size; 31 32 // Round up to next allocation size. 33 page_mask = alloc_size() - 1; 34 rounded_size = (size + page_mask) & ~page_mask; 35 36 m_off += rounded_size; 37 } 38 39 void *get_ptr() { return (char *)m_base + m_off; } 40 bool at_end() { return m_off >= m_size; } 41 bool at_end_exactly() { return m_off == m_size; } 42 43 }; 44 45 } // namespace citrun 46 #endif // MEM_H