allocator.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef __ALLOCATOR_H
  2. #define __ALLOCATOR_H
  3. #include "fixed_types.h"
  4. #include "FSBAllocator.hh"
  5. #include <typeinfo>
  6. #include <cxxabi.h>
  7. // Pool allocator
  8. class Allocator
  9. {
  10. private:
  11. struct DataElement
  12. {
  13. Allocator *allocator;
  14. char data[];
  15. };
  16. public:
  17. virtual ~Allocator() {}
  18. virtual void *alloc(size_t bytes) = 0;
  19. virtual void _dealloc(void *ptr) = 0;
  20. static void dealloc(void* ptr)
  21. {
  22. DataElement *elem = (DataElement*)(((char*)ptr) - sizeof(DataElement));
  23. elem->allocator->_dealloc(elem);
  24. }
  25. };
  26. template <typename T, unsigned MaxItems = 0> class TypedAllocator : public Allocator
  27. {
  28. private:
  29. UInt64 m_items;
  30. FSBAllocator_ElemAllocator<sizeof(DataElement) + sizeof(T), MaxItems, T> m_alloc;
  31. // In ROB-SMT, DynamicMicroOps are allocated by their own thread but free'd in simulate() which can be called by anyone
  32. Lock m_lock;
  33. public:
  34. TypedAllocator()
  35. : m_items(0)
  36. {}
  37. virtual ~TypedAllocator()
  38. {
  39. if (m_items)
  40. {
  41. int status;
  42. char *nameoftype = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
  43. printf("[ALLOC] %" PRIu64 " items of type %s not freed\n", m_items, nameoftype);
  44. free(nameoftype);
  45. }
  46. }
  47. virtual void* alloc(size_t bytes)
  48. {
  49. ScopedLock sl(m_lock);
  50. //LOG_ASSERT_ERROR(bytes == sizeof(T), "");
  51. ++m_items;
  52. DataElement *elem = (DataElement *)m_alloc.allocate();
  53. elem->allocator = this;
  54. return elem->data;
  55. }
  56. virtual void _dealloc(void* ptr)
  57. {
  58. ScopedLock sl(m_lock);
  59. --m_items;
  60. m_alloc.deallocate((T*)ptr);
  61. }
  62. };
  63. #endif // __ALLOCATOR_H