LCOV - code coverage report
Current view: top level - acl/aclrt_c/runtime - memory.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.9 % 81 72
Test Date: 2026-08-13 17:07:01 Functions: 100.0 % 8 8

            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 "securec.h"
      15              : #include "runtime/dev.h"
      16              : #include "runtime/mem.h"
      17              : #include "model_config_rt.h"
      18              : 
      19              : #define ACL_MEMCPY_KIND_DESC_LEN 48U
      20              : 
      21            1 : static const char* GetUnsupportedMemcpyKindDesc(
      22              :     const aclrtMemcpyKind kind, char* const unknownDesc, const size_t unknownDescLen)
      23              : {
      24            1 :     switch (kind) {
      25            0 :         case ACL_MEMCPY_DEFAULT:
      26            0 :             return "MEMCPY_DEFAULT(4)";
      27            0 :         case ACL_MEMCPY_HOST_TO_BUF_TO_DEVICE:
      28            0 :             return "MEMCPY_HOST_TO_BUF_TO_DEVICE(5)";
      29            0 :         case ACL_MEMCPY_INNER_DEVICE_TO_DEVICE:
      30            0 :             return "MEMCPY_INNER_DEVICE_TO_DEVICE(6)";
      31            0 :         case ACL_MEMCPY_INTER_DEVICE_TO_DEVICE:
      32            0 :             return "MEMCPY_INTER_DEVICE_TO_DEVICE(7)";
      33            1 :         default:
      34            1 :             if ((unknownDesc == NULL) || (unknownDescLen == 0U)) {
      35            0 :                 return "UNKNOWN";
      36              :             }
      37            1 :             (void)snprintf_s(unknownDesc, unknownDescLen, unknownDescLen - 1U, "UNKNOWN(%d)", (int32_t)kind);
      38            1 :             unknownDesc[unknownDescLen - 1U] = '\0';
      39            1 :             return unknownDesc;
      40              :     }
      41              : }
      42              : 
      43            9 : static aclError GetAlignedSize(const size_t size, size_t* alignedSize)
      44              : {
      45            9 :     const size_t DATA_MEMORY_ALIGN_SIZE = 32UL;
      46            9 :     if ((size + (DATA_MEMORY_ALIGN_SIZE * 2UL)) < size) {
      47            1 :         return ACL_ERROR_INVALID_PARAM;
      48              :     }
      49            8 :     *alignedSize = (size + (DATA_MEMORY_ALIGN_SIZE * 2UL) - 1UL) / DATA_MEMORY_ALIGN_SIZE * DATA_MEMORY_ALIGN_SIZE;
      50            8 :     return ACL_SUCCESS;
      51              : }
      52              : 
      53           11 : aclError aclrtMalloc(void** devPtr, size_t size, aclrtMemMallocPolicy policy)
      54              : {
      55           11 :     size_t alignedSize = 0;
      56           11 :     if ((devPtr == NULL) || (size == 0UL) || (GetAlignedSize(size, &alignedSize) != ACL_SUCCESS)) {
      57            3 :         ACL_LOG_ERROR("%s", devPtr == NULL ? "devPtr is NULL" : "size invalid");
      58            3 :         return ACL_ERROR_INVALID_PARAM;
      59              :     }
      60            8 :     rtMemType_t type = RT_MEMORY_DEFAULT;
      61            8 :     aclError ret = GetMemTypeFromPolicy(policy, &type);
      62            8 :     if (ret != ACL_SUCCESS) {
      63            1 :         return ret;
      64              :     }
      65            7 :     return rtMalloc(devPtr, alignedSize, type, (uint16_t)(APP));
      66              : }
      67              : 
      68            6 : aclError aclrtFree(void* devPtr)
      69              : {
      70            6 :     if (devPtr == NULL) {
      71            1 :         ACL_LOG_ERROR("devPtr is NULL.");
      72            1 :         return ACL_ERROR_INVALID_PARAM;
      73              :     }
      74            5 :     return rtFree(devPtr);
      75              : }
      76              : 
      77            6 : static aclError MemcpyKindTranslate(const aclrtMemcpyKind kind, rtMemcpyKind_t* rtKind)
      78              : {
      79            6 :     switch (kind) {
      80            1 :         case ACL_MEMCPY_HOST_TO_DEVICE: {
      81            1 :             *rtKind = RT_MEMCPY_HOST_TO_DEVICE;
      82            1 :             break;
      83              :         }
      84            1 :         case ACL_MEMCPY_DEVICE_TO_DEVICE: {
      85            1 :             *rtKind = RT_MEMCPY_DEVICE_TO_DEVICE;
      86            1 :             break;
      87              :         }
      88            1 :         case ACL_MEMCPY_DEVICE_TO_HOST: {
      89            1 :             *rtKind = RT_MEMCPY_DEVICE_TO_HOST;
      90            1 :             break;
      91              :         }
      92            2 :         case ACL_MEMCPY_HOST_TO_HOST: {
      93            2 :             *rtKind = RT_MEMCPY_HOST_TO_HOST;
      94            2 :             break;
      95              :         }
      96            1 :         default: {
      97            1 :             return ACL_ERROR_INVALID_PARAM;
      98              :         }
      99              :     }
     100            5 :     return ACL_SUCCESS;
     101              : }
     102              : 
     103           13 : aclError aclrtMemcpy(void* dst, size_t destMax, const void* src, size_t count, aclrtMemcpyKind kind)
     104              : {
     105           13 :     if (count == 0UL) {
     106            3 :         ACL_LOG_INFO("count is 0, no need to copy, just return success.");
     107            3 :         return ACL_SUCCESS;
     108              :     }
     109           10 :     if (dst == NULL || src == NULL) {
     110            4 :         ACL_LOG_ERROR("%s", dst == NULL ? "dst is NULL" : "src is NULL.");
     111            4 :         return ACL_ERROR_INVALID_PARAM;
     112              :     }
     113            6 :     rtMemcpyKind_t rtKind = RT_MEMCPY_RESERVED;
     114            6 :     const aclError ret = MemcpyKindTranslate(kind, &rtKind);
     115            6 :     if (ret != ACL_SUCCESS) {
     116            1 :         char kindDesc[ACL_MEMCPY_KIND_DESC_LEN] = {0};
     117            1 :         ACL_LOG_INNER_ERROR("invalid kind[%s]", GetUnsupportedMemcpyKindDesc(kind, kindDesc, sizeof(kindDesc)));
     118            1 :         return ret;
     119              :     }
     120            5 :     return rtMemcpy(dst, destMax, src, count, rtKind);
     121              : }
     122              : 
     123            4 : aclError aclrtMemset(void* devPtr, size_t maxCount, int32_t value, size_t count)
     124              : {
     125            4 :     if (count == 0UL) {
     126            1 :         ACL_LOG_INFO("count is 0, no need to set, just return success.");
     127            1 :         return ACL_SUCCESS;
     128              :     }
     129            3 :     if (devPtr == NULL) {
     130            1 :         ACL_LOG_ERROR("devPtr is NULL.");
     131            1 :         return ACL_ERROR_INVALID_PARAM;
     132              :     }
     133            2 :     return rtMemset(devPtr, maxCount, (uint32_t)(value), count);
     134              : }
     135              : 
     136            4 : aclError aclrtGetMemInfo(aclrtMemAttr attr, size_t* free, size_t* total)
     137              : {
     138            4 :     if (free == NULL || total == NULL) {
     139            2 :         ACL_LOG_ERROR("%s", free == NULL ? "free is NULL" : "total is NULL.");
     140            2 :         return ACL_ERROR_INVALID_PARAM;
     141              :     }
     142            2 :     return rtMemGetInfoEx((rtMemInfoType_t)(attr), free, total);
     143              : }
        

Generated by: LCOV version 2.0-1