1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "av_virtual_memory.h"
17 #include "av_virtual_allocator.h"
18 #include "avbuffer_utils.h"
19 #include "buffer/avallocator.h"
20 #include "common/log.h"
21 #include "common/status.h"
22 #include "message_parcel.h"
23 
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "AVVirtualMemory" };
26 }
27 
28 namespace OHOS {
29 namespace Media {
CreateVirtualAllocator()30 std::shared_ptr<AVAllocator> AVAllocatorFactory::CreateVirtualAllocator()
31 {
32     auto allocator = std::shared_ptr<AVVirtualAllocator>(new AVVirtualAllocator());
33     FALSE_RETURN_V_MSG_E(allocator != nullptr, nullptr, "Create AVVirtualAllocator failed, no memory");
34     return allocator;
35 }
36 
AVVirtualAllocator()37 AVVirtualAllocator::AVVirtualAllocator(){};
38 
Alloc(int32_t capacity)39 void *AVVirtualAllocator::Alloc(int32_t capacity)
40 {
41     uint8_t *ptr = new uint8_t[capacity];
42     FALSE_RETURN_V_MSG_E(ptr != nullptr, nullptr, "Alloc memory failed, no memory");
43 
44     return static_cast<void *>(ptr);
45 }
46 
Free(void * ptr)47 bool AVVirtualAllocator::Free(void *ptr)
48 {
49     uint8_t *dataPtr = static_cast<uint8_t *>(ptr);
50     FALSE_RETURN_V_MSG_E(dataPtr != nullptr, false, "Invalid ptr, ptr = 0x%{public}06" PRIXPTR, FAKE_POINTER(dataPtr));
51     delete dataPtr;
52     return true;
53 }
54 
GetMemoryType()55 MemoryType AVVirtualAllocator::GetMemoryType()
56 {
57     return MemoryType::VIRTUAL_MEMORY;
58 }
59 
AVVirtualMemory()60 AVVirtualMemory::AVVirtualMemory() {}
61 
~AVVirtualMemory()62 AVVirtualMemory::~AVVirtualMemory()
63 {
64     if (allocator_ == nullptr) {
65         return;
66     }
67     bool ret = allocator_->Free(static_cast<void *>(base_));
68     FALSE_RETURN_MSG(ret, "Free memory failed, instance: 0x%{public}06" PRIXPTR, FAKE_POINTER(this));
69     base_ = nullptr;
70 }
71 
Init()72 Status AVVirtualMemory::Init()
73 {
74     int32_t allocSize = align_ ? (capacity_ + align_ - 1) : capacity_;
75     base_ = static_cast<uint8_t *>(allocator_->Alloc(allocSize));
76     FALSE_RETURN_V_MSG_E(base_ != nullptr, Status::ERROR_NO_MEMORY, "Alloc AVVirtualMemory failed");
77 
78     uintptr_t addrBase = reinterpret_cast<uintptr_t>(base_);
79     offset_ = static_cast<int32_t>(AlignUp(addrBase, static_cast<uintptr_t>(offset_)) - addrBase);
80     return Status::OK;
81 }
82 
GetMemoryType()83 MemoryType AVVirtualMemory::GetMemoryType()
84 {
85     return MemoryType::VIRTUAL_MEMORY;
86 }
87 } // namespace Media
88 } // namespace OHOS