/*
Copyright (c) 2025-2025 Huawei Technologies Co., Ltd.

sysHAX-adapter is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
    http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
PURPOSE.
See the Mulan PSL v2 for more details.
Created: 2026-1-31
Desc: CPU inference memory manager
*/

#ifndef MEMORY_MANAGER_H
#define MEMORY_MANAGER_H

#include <vector>
#include <unordered_map>
#include <iostream>
#include <algorithm>
#include <mutex>
#include <numeric>
#include <cstring>

#include "tensor.h"
#include "config.h"

namespace cpu_inference {

class MemoryManager {
public:
    static MemoryManager& get();
    ~MemoryManager();
    Tensor& alloc_single_numa(const std::string& name, 
                                const std::vector<int>& shape, 
                                int numa_node, 
                                const std::string& dtype,
                                const std::string& quant_type);

    Tensor& alloc_multi_numa(const std::string& name, 
                                const std::vector<int>& shape, 
                                const std::vector<int>& numas, 
                                const std::string& dtype,
                                const std::string& quant_type);
    
    Tensor& alloc_all_numa(const std::string& name, 
                                const std::vector<int>& shape, 
                                const std::string& dtype,
                                const std::string& quant_type);

    Tensor& get_memory(const std::string& name);
    
    int get_tot_numa_cnt() const;

    void clear_all();

private:
    MemoryManager();

    MemoryManager(const MemoryManager&) = delete;
    MemoryManager& operator=(const MemoryManager&) = delete;
    MemoryManager(MemoryManager&&) = delete;
    MemoryManager& operator=(MemoryManager&&) = delete;

    int tot_numa_cnt;
    std::unordered_map<std::string, Tensor> alloc_memory;
    
};

} // namespace cpu_inference

#endif // MEMORY_MANAGER_H