memory_utils.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef MEMORY_UTILS_H
#define MEMORY_UTILS_H

#include <memory>
#include <vector>
#include "memorypool.h"

// Memory management class for managing the memory pool on each device.
class MemoryManager {
public:
    MemoryManager();

    // Create a memory pool on each device.
    void CreateMemoryPool(size_t poolSize);

    // Obtain the device corresponding to the current thread.
    int32_t GetDeviceId();

    // Obtain the memory pool on the device corresponding to the current thread.
    std::shared_ptr<MemoryPool> &GetMemoryPool();

    // Allocate a memory block.
    void AllocateBlock(uint32_t size, int &blockId);

    // Release a memory block.
    void FreeBlock(int blockId);

    // Obtain the physical address of a memory block.
    void GetBlockPtr(int blockId, void *&addr);

private:
    std::vector<std::shared_ptr<MemoryPool>> memoryPools_;
};

// Obtain the global MemoryManager instance.
MemoryManager &GetMemoryManager();

#endif