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"

// 内存管理类,管理每个Device上的内存池
class MemoryManager {
public:
    MemoryManager();

    // 在每个Device上创建对应的内存池
    void CreateMemoryPool(size_t poolSize);

    // 获取当前线程对应的Device
    int32_t GetDeviceId();

    // 获取当前线程对应设备上的内存池
    std::shared_ptr<MemoryPool> &GetMemoryPool();

    // 分配内存块
    void AllocateBlock(uint32_t size, int &blockId);

    // 释放内存块
    void FreeBlock(int blockId);

    // 获取内存块的物理地址
    void GetBlockPtr(int blockId, void *&addr);

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

// 获取全局MemoryManager实例
MemoryManager &GetMemoryManager();

#endif