memgraph/memory/memory.hpp

59 lines
1.1 KiB
C++
Raw Normal View History

#ifndef MEMGRAPH_MEMORY_MEMORY_HPP
#define MEMGRAPH_MEMORY_MEMORY_HPP
2015-07-07 22:18:26 +08:00
#include <atomic>
#include <mutex>
#include "storage/model/record.hpp"
#include "storage/model/vertex.hpp"
#include "storage/model/edge.hpp"
// TODO implement the memory engine using the allocator style allocation to
// make this class non-dependent on the memory allocation strategy
// TODO implement real recycling of vertices and edges to improve performance
class MemoryEngine
{
public:
template <class T,
typename... Args>
T* create(Args&&... args)
{
return new T(std::forward<Args>(args)...);
}
template<class T>
T* allocate()
{
return static_cast<T*>(malloc(sizeof(T)));
}
template <class T>
2015-07-31 18:33:12 +08:00
void recycle(Record<T>* record)
2015-07-07 22:18:26 +08:00
{
recycle(&record->derived());
}
2015-07-31 18:33:12 +08:00
void recycle(Vertex* v)
2015-07-07 22:18:26 +08:00
{
delete v;
}
2015-07-31 18:33:12 +08:00
void recycle(Edge* e)
2015-07-07 22:18:26 +08:00
{
delete e;
}
private:
2015-07-31 18:33:12 +08:00
std::unique_lock<SpinLock> acquire()
2015-07-07 22:18:26 +08:00
{
2015-07-31 18:33:12 +08:00
return std::unique_lock<SpinLock>(lock);
2015-07-07 22:18:26 +08:00
}
2015-07-31 18:33:12 +08:00
SpinLock lock;
2015-07-07 22:18:26 +08:00
};
#endif