LCOV - code coverage report
Current view: top level - acl/aclrt_c/runtime - memory.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 64 64
Test Date: 2026-07-28 10:53:01 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : #include <stdlib.h>
      11              : #include "acl/acl_base.h"
      12              : #include "acl/acl_rt.h"
      13              : #include "log_inner.h"
      14              : #include "runtime/dev.h"
      15              : #include "runtime/mem.h"
      16              : #include "model_config_rt.h"
      17              : 
      18            9 : static aclError GetAlignedSize(const size_t size, size_t* alignedSize)
      19              : {
      20            9 :     const size_t DATA_MEMORY_ALIGN_SIZE = 32UL;
      21            9 :     if ((size + (DATA_MEMORY_ALIGN_SIZE * 2UL)) < size) {
      22            1 :         return ACL_ERROR_INVALID_PARAM;
      23              :     }
      24            8 :     *alignedSize = (size + (DATA_MEMORY_ALIGN_SIZE * 2UL) - 1UL) / DATA_MEMORY_ALIGN_SIZE * DATA_MEMORY_ALIGN_SIZE;
      25            8 :     return ACL_SUCCESS;
      26              : }
      27              : 
      28           11 : aclError aclrtMalloc(void** devPtr, size_t size, aclrtMemMallocPolicy policy)
      29              : {
      30           11 :     size_t alignedSize = 0;
      31           11 :     if ((devPtr == NULL) || (size == 0UL) || (GetAlignedSize(size, &alignedSize) != ACL_SUCCESS)) {
      32            3 :         ACL_LOG_ERROR("%s", devPtr == NULL ? "devPtr is NULL" : "size invalid");
      33            3 :         return ACL_ERROR_INVALID_PARAM;
      34              :     }
      35            8 :     rtMemType_t type = RT_MEMORY_DEFAULT;
      36            8 :     aclError ret = GetMemTypeFromPolicy(policy, &type);
      37            8 :     if (ret != ACL_SUCCESS) {
      38            1 :         return ret;
      39              :     }
      40            7 :     return rtMalloc(devPtr, alignedSize, type, (uint16_t)(APP));
      41              : }
      42              : 
      43            6 : aclError aclrtFree(void* devPtr)
      44              : {
      45            6 :     if (devPtr == NULL) {
      46            1 :         ACL_LOG_ERROR("devPtr is NULL.");
      47            1 :         return ACL_ERROR_INVALID_PARAM;
      48              :     }
      49            5 :     return rtFree(devPtr);
      50              : }
      51              : 
      52            6 : static aclError MemcpyKindTranslate(const aclrtMemcpyKind kind, rtMemcpyKind_t* rtKind)
      53              : {
      54            6 :     switch (kind) {
      55            1 :         case ACL_MEMCPY_HOST_TO_DEVICE: {
      56            1 :             *rtKind = RT_MEMCPY_HOST_TO_DEVICE;
      57            1 :             break;
      58              :         }
      59            1 :         case ACL_MEMCPY_DEVICE_TO_DEVICE: {
      60            1 :             *rtKind = RT_MEMCPY_DEVICE_TO_DEVICE;
      61            1 :             break;
      62              :         }
      63            1 :         case ACL_MEMCPY_DEVICE_TO_HOST: {
      64            1 :             *rtKind = RT_MEMCPY_DEVICE_TO_HOST;
      65            1 :             break;
      66              :         }
      67            2 :         case ACL_MEMCPY_HOST_TO_HOST: {
      68            2 :             *rtKind = RT_MEMCPY_HOST_TO_HOST;
      69            2 :             break;
      70              :         }
      71            1 :         default: {
      72            1 :             return ACL_ERROR_INVALID_PARAM;
      73              :         }
      74              :     }
      75            5 :     return ACL_SUCCESS;
      76              : }
      77              : 
      78           13 : aclError aclrtMemcpy(void* dst, size_t destMax, const void* src, size_t count, aclrtMemcpyKind kind)
      79              : {
      80           13 :     if (count == 0UL) {
      81            3 :         ACL_LOG_INFO("count is 0, no need to copy, just return success.");
      82            3 :         return ACL_SUCCESS;
      83              :     }
      84           10 :     if (dst == NULL || src == NULL) {
      85            4 :         ACL_LOG_ERROR("%s", dst == NULL ? "dst is NULL" : "src is NULL.");
      86            4 :         return ACL_ERROR_INVALID_PARAM;
      87              :     }
      88            6 :     rtMemcpyKind_t rtKind = RT_MEMCPY_RESERVED;
      89            6 :     const aclError ret = MemcpyKindTranslate(kind, &rtKind);
      90            6 :     if (ret != ACL_SUCCESS) {
      91            1 :         ACL_LOG_INNER_ERROR("invalid kind[%d]", (int32_t)(kind));
      92            1 :         return ret;
      93              :     }
      94            5 :     return rtMemcpy(dst, destMax, src, count, rtKind);
      95              : }
      96              : 
      97            4 : aclError aclrtMemset(void* devPtr, size_t maxCount, int32_t value, size_t count)
      98              : {
      99            4 :     if (count == 0UL) {
     100            1 :         ACL_LOG_INFO("count is 0, no need to set, just return success.");
     101            1 :         return ACL_SUCCESS;
     102              :     }
     103            3 :     if (devPtr == NULL) {
     104            1 :         ACL_LOG_ERROR("devPtr is NULL.");
     105            1 :         return ACL_ERROR_INVALID_PARAM;
     106              :     }
     107            2 :     return rtMemset(devPtr, maxCount, (uint32_t)(value), count);
     108              : }
     109              : 
     110            4 : aclError aclrtGetMemInfo(aclrtMemAttr attr, size_t* free, size_t* total)
     111              : {
     112            4 :     if (free == NULL || total == NULL) {
     113            2 :         ACL_LOG_ERROR("%s", free == NULL ? "free is NULL" : "total is NULL.");
     114            2 :         return ACL_ERROR_INVALID_PARAM;
     115              :     }
     116            2 :     return rtMemGetInfoEx((rtMemInfoType_t)(attr), free, total);
     117              : }
        

Generated by: LCOV version 2.0-1