|
|
|
@ -23,9 +23,8 @@ namespace detail {
|
|
|
|
|
class MetadataCache;
|
|
|
|
|
|
|
|
|
|
// MemoryBlock represents Each allocated memory block, which contains
|
|
|
|
|
// Metadata and the payload.
|
|
|
|
|
class MemoryBlock {
|
|
|
|
|
public:
|
|
|
|
|
// MemoryBlock::Desc and the payload.
|
|
|
|
|
struct MemoryBlock {
|
|
|
|
|
enum Type {
|
|
|
|
|
FREE_CHUNK, // memory is free and idle
|
|
|
|
|
ARENA_CHUNK, // memory is being occupied
|
|
|
|
@ -33,17 +32,17 @@ class MemoryBlock {
|
|
|
|
|
INVALID_CHUNK // memory is invalid
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init saves the Metadata of the memory block in a MetadataCache.
|
|
|
|
|
// init saves the MemoryBlock::Desc of the memory block in a MetadataCache.
|
|
|
|
|
// If it is a CPU memory block, the MetadataCache writes the
|
|
|
|
|
// Metadata to the beginning of the block; or, if it is a GPU memory
|
|
|
|
|
// MemoryBlock::Desc to the beginning of the block; or, if it is a GPU memory
|
|
|
|
|
// block, the MetadataCache writes the Meatadata to a std::map in
|
|
|
|
|
// the CPU.
|
|
|
|
|
void init(MetadataCache* cache, Type t, size_t index, size_t size,
|
|
|
|
|
void init(MemoryBlock::DescCache* cache, Type t, size_t index, size_t size,
|
|
|
|
|
void* left_buddy, void* right_buddy);
|
|
|
|
|
|
|
|
|
|
// All these accessors returns fields in the Metadata of the memory
|
|
|
|
|
// All these accessors returns fields in the MemoryBlock::Desc of the memory
|
|
|
|
|
// block. They all need a MetadataCache instance as their first
|
|
|
|
|
// parameter because they read the Metadata from the cache.
|
|
|
|
|
// parameter because they read the MemoryBlock::Desc from the cache.
|
|
|
|
|
Type type(const MetadataCache& cache) const;
|
|
|
|
|
size_t size(const MetadataCache& cache) const;
|
|
|
|
|
size_t index(const MetadataCache& cache) const;
|
|
|
|
@ -68,12 +67,11 @@ class MemoryBlock {
|
|
|
|
|
void* data() const;
|
|
|
|
|
MemoryBlock* metadata() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Metadata describes a MemoryBlock.
|
|
|
|
|
struct Metadata {
|
|
|
|
|
Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts, MemoryBlock* l,
|
|
|
|
|
MemoryBlock* r);
|
|
|
|
|
Metadata();
|
|
|
|
|
// MemoryBlock::Desc describes a MemoryBlock.
|
|
|
|
|
struct Desc {
|
|
|
|
|
Desc(MemoryBlock::Type t, size_t i, size_t s, size_t ts, MemoryBlock* l,
|
|
|
|
|
MemoryBlock* r);
|
|
|
|
|
Desc();
|
|
|
|
|
|
|
|
|
|
// Updates guard_begin and guard_end by hashes of the Metadata object.
|
|
|
|
|
void update_guards();
|
|
|
|
@ -94,9 +92,10 @@ class MemoryBlock {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// A cache for accessing memory block meta-data that may be expensive
|
|
|
|
|
// to access directly. This class exists to unify the metadata format
|
|
|
|
|
// between GPU and CPU allocations. It should be removed when the CPU
|
|
|
|
|
// can access all GPU allocations directly via UVM.
|
|
|
|
|
// to access directly. This class exists to unify the
|
|
|
|
|
// MemoryBlock::Desc format between GPU and CPU allocations. It should
|
|
|
|
|
// be removed when the CPU can access all GPU allocations directly via
|
|
|
|
|
// UVM.
|
|
|
|
|
class MetadataCache {
|
|
|
|
|
public:
|
|
|
|
|
explicit MetadataCache(bool uses_gpu);
|
|
|
|
@ -105,22 +104,22 @@ class MetadataCache {
|
|
|
|
|
MetadataCache(const MetadataCache&) = delete;
|
|
|
|
|
MetadataCache& operator=(const MetadataCache&) = delete;
|
|
|
|
|
|
|
|
|
|
// Returns the Metadata for a memory block. When MetadataCache is
|
|
|
|
|
// used to manage CPU memory, the Metadata resides at the beginning
|
|
|
|
|
// Returns the MemoryBlock::Desc for a memory block. When MetadataCache is
|
|
|
|
|
// used to manage CPU memory, the MemoryBlock::Desc resides at the beginning
|
|
|
|
|
// of the memory block; when used to manage GPU memory, the
|
|
|
|
|
// Meatadata resides in CPU memory indexed by cache_.
|
|
|
|
|
Metadata load(const MemoryBlock* memory_block) const;
|
|
|
|
|
MemoryBlock::Desc load(const MemoryBlock* memory_block) const;
|
|
|
|
|
|
|
|
|
|
// Saves the Metadata of a memory block into the cache. For CPU
|
|
|
|
|
// memory block, writes the Metadata to the beginning of the memory
|
|
|
|
|
// Saves the MemoryBlock::Desc of a memory block into the cache. For CPU
|
|
|
|
|
// memory block, writes the MemoryBlock::Desc to the beginning of the memory
|
|
|
|
|
// block; whereas for GPU memory, writes it to cache_.
|
|
|
|
|
void save(MemoryBlock* memory_block, const Metadata& meta_data);
|
|
|
|
|
void save(MemoryBlock* memory_block, const MemoryBlock::Desc& meta_data);
|
|
|
|
|
|
|
|
|
|
// For GPU memory block, erases its Metadata from cache_.
|
|
|
|
|
// For GPU memory block, erases its MemoryBlock::Desc from cache_.
|
|
|
|
|
void invalidate(MemoryBlock* memory_block);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
typedef std::unordered_map<const MemoryBlock*, Metadata> MetadataMap;
|
|
|
|
|
typedef std::unordered_map<const MemoryBlock*, MemoryBlock::Desc> MetadataMap;
|
|
|
|
|
MetadataMap cache_;
|
|
|
|
|
bool uses_gpu_;
|
|
|
|
|
};
|
|
|
|
|