/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2026-2026. All rights reserved.
 * MindIE 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.
 */

/*!
 * \file buffer_manager.h
 * \brief buffer内存管理
 */
#ifndef BUFFER_MANAGER_H
#define BUFFER_MANAGER_H

#if (__NPU_ARCH__ == 5102)
#include "buffer_mix_core.h"
#else
#include "buffer.h"
#endif

// L1  TPosition::A1
// L0A TPosition::A2
// L0B TPosition::B2
// L0C TPosition::CO1
// UB  TPosition::VECIN
namespace fa_base_matmul {
template <BufferType bufferType> class BufferManager {
    using TensorType = std::conditional_t<bufferType == BufferType::GM, GlobalTensor<uint8_t>, LocalTensor<uint8_t>>;

  public:
    __aicore__ inline void Init(TPipe *pipe, uint32_t size) {
        static_assert(bufferType != BufferType::GM, "GM should use workspace.");
        TBuf<BufferInfo<bufferType>::Position> tbuf;
        pipe->InitBuffer(tbuf, size);
        mem_ = tbuf.template Get<uint8_t>();
    }

    __aicore__ inline void Init(__gm__ uint8_t *workspace) {
        static_assert(bufferType == BufferType::GM, "BufferType should be GM.");
        mem_.SetGlobalBuffer((__gm__ uint8_t *)workspace);
    }

    template <SyncType syncType = SyncType::INNER_CORE_SYNC>
    __aicore__ inline Buffer<bufferType, syncType> AllocBuffer(uint32_t size) {
        TensorType temp = mem_[offset_];
        offset_ += size;
        return Buffer<bufferType, syncType>(temp, size);
    }

    template <SyncType syncType = SyncType::INNER_CORE_SYNC>
    __aicore__ inline void FreeBuffer(Buffer<bufferType, syncType> &buffer) {}

  private:
    uint32_t offset_ = 0;
    TensorType mem_;
};
}
#endif