15490a39dSopenharmony_ci/* 25490a39dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 35490a39dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45490a39dSopenharmony_ci * you may not use this file except in compliance with the License. 55490a39dSopenharmony_ci * You may obtain a copy of the License at 65490a39dSopenharmony_ci * 75490a39dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85490a39dSopenharmony_ci * 95490a39dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105490a39dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115490a39dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125490a39dSopenharmony_ci * See the License for the specific language governing permissions and 135490a39dSopenharmony_ci * limitations under the License. 145490a39dSopenharmony_ci */ 155490a39dSopenharmony_ci#include "array_buffer_util.h" 165490a39dSopenharmony_ci#include "securec.h" 175490a39dSopenharmony_ci#include "intell_voice_log.h" 185490a39dSopenharmony_ci 195490a39dSopenharmony_ci#define LOG_TAG "ArrayBufferUtil" 205490a39dSopenharmony_ci 215490a39dSopenharmony_cinamespace OHOS { 225490a39dSopenharmony_cinamespace IntellVoiceUtils { 235490a39dSopenharmony_citemplate<class T> bool ArrayBufferUtil<T>::Init(uint32_t size, const T *data) 245490a39dSopenharmony_ci{ 255490a39dSopenharmony_ci if (size == 0) { 265490a39dSopenharmony_ci INTELL_VOICE_LOG_ERROR("size is zero"); 275490a39dSopenharmony_ci return false; 285490a39dSopenharmony_ci } 295490a39dSopenharmony_ci 305490a39dSopenharmony_ci data_ = std::make_unique<T[]>(size); 315490a39dSopenharmony_ci if (data_ == nullptr) { 325490a39dSopenharmony_ci INTELL_VOICE_LOG_ERROR("allocate data failed"); 335490a39dSopenharmony_ci return false; 345490a39dSopenharmony_ci } 355490a39dSopenharmony_ci 365490a39dSopenharmony_ci if (data == nullptr) { 375490a39dSopenharmony_ci (void)memset_s(data_.get(), size * sizeof(T), 0, size * sizeof(T)); 385490a39dSopenharmony_ci } else { 395490a39dSopenharmony_ci (void)memcpy_s(data_.get(), size * sizeof(T), data, size * sizeof(T)); 405490a39dSopenharmony_ci } 415490a39dSopenharmony_ci 425490a39dSopenharmony_ci size_ = size; 435490a39dSopenharmony_ci return true; 445490a39dSopenharmony_ci} 455490a39dSopenharmony_ci 465490a39dSopenharmony_citemplate<class T> std::unique_ptr<ArrayBufferUtil<T>> CreateArrayBuffer(uint32_t size, const T *data) 475490a39dSopenharmony_ci{ 485490a39dSopenharmony_ci auto ret = std::unique_ptr<ArrayBufferUtil<T>>(new (std::nothrow) ArrayBufferUtil<T>()); 495490a39dSopenharmony_ci if (ret == nullptr) { 505490a39dSopenharmony_ci INTELL_VOICE_LOG_ERROR("allocate array buffer failed"); 515490a39dSopenharmony_ci return nullptr; 525490a39dSopenharmony_ci } 535490a39dSopenharmony_ci 545490a39dSopenharmony_ci if (!ret->Init(size, data)) { 555490a39dSopenharmony_ci INTELL_VOICE_LOG_ERROR("init array buffer failed"); 565490a39dSopenharmony_ci return nullptr; 575490a39dSopenharmony_ci } 585490a39dSopenharmony_ci 595490a39dSopenharmony_ci return ret; 605490a39dSopenharmony_ci} 615490a39dSopenharmony_ci 625490a39dSopenharmony_citemplate class ArrayBufferUtil<uint8_t>; 635490a39dSopenharmony_citemplate std::unique_ptr<Uint8ArrayBuffer> CreateArrayBuffer(uint32_t size, const uint8_t *data = nullptr); 645490a39dSopenharmony_ci} 655490a39dSopenharmony_ci}