• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "avsharedmemorypool.h"
17 #include "avsharedmemorybase.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 
21 namespace {
22     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVShMemPool"};
23     constexpr int32_t MAX_MEM_SIZE = 100 * 1024 * 1024;
24 }
25 
26 namespace OHOS {
27 namespace Media {
AVSharedMemoryPool(const std::string & name)28 AVSharedMemoryPool::AVSharedMemoryPool(const std::string &name) : name_(name)
29 {
30     MEDIA_LOGD("enter ctor, 0x%{public}06" PRIXPTR ", name: %{public}s", FAKE_POINTER(this), name_.c_str());
31 }
32 
~AVSharedMemoryPool()33 AVSharedMemoryPool::~AVSharedMemoryPool()
34 {
35     MEDIA_LOGD("enter dtor, 0x%{public}06" PRIXPTR ", name: %{public}s", FAKE_POINTER(this), name_.c_str());
36     Reset();
37 }
38 
Init(const InitializeOption & option)39 int32_t AVSharedMemoryPool::Init(const InitializeOption &option)
40 {
41     std::unique_lock<std::mutex> lock(mutex_);
42 
43     CHECK_AND_RETURN_RET(!inited_, MSERR_INVALID_OPERATION);
44     CHECK_AND_RETURN_RET(option.memSize < MAX_MEM_SIZE, MSERR_INVALID_VAL);
45     CHECK_AND_RETURN_RET(option.maxMemCnt != 0, MSERR_INVALID_VAL);
46 
47     option_ = option;
48     if (option.preAllocMemCnt > option.maxMemCnt) {
49         option_.preAllocMemCnt = option.maxMemCnt;
50     }
51 
52     MEDIA_LOGI("name: %{public}s, init option: preAllocMemCnt = %{public}u, memSize = %{public}d, "
53                "maxMemCnt = %{public}u, enableFixedSize = %{public}d",
54                name_.c_str(), option_.preAllocMemCnt, option_.memSize, option_.maxMemCnt,
55                option_.enableFixedSize);
56     bool ret = true;
57     for (uint32_t i = 0; i < option_.preAllocMemCnt; ++i) {
58         auto memory = AllocMemory(option_.memSize);
59         if (memory == nullptr) {
60             MEDIA_LOGE("alloc memory failed");
61             ret = false;
62             break;
63         }
64         idleList_.push_back(memory);
65     }
66 
67     if (!ret) {
68         for (auto iter = idleList_.begin(); iter != idleList_.end(); ++iter) {
69             delete *iter;
70             *iter = nullptr;
71         }
72         return MSERR_NO_MEMORY;
73     }
74 
75     inited_ = true;
76     notifier_ = option.notifier;
77     return MSERR_OK;
78 }
79 
AllocMemory(int32_t size)80 AVSharedMemory *AVSharedMemoryPool::AllocMemory(int32_t size)
81 {
82     AVSharedMemoryBase *memory = new (std::nothrow) AVSharedMemoryBase(size, option_.flags, name_);
83     CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "create object failed");
84 
85     if (memory->Init() != MSERR_OK) {
86         delete memory;
87         memory = nullptr;
88         MEDIA_LOGE("init avsharedmemorybase failed");
89     }
90 
91     return memory;
92 }
93 
ReleaseMemory(AVSharedMemory * memory)94 void AVSharedMemoryPool::ReleaseMemory(AVSharedMemory *memory)
95 {
96     CHECK_AND_RETURN_LOG(memory != nullptr, "memory is nullptr");
97     std::unique_lock<std::mutex> lock(mutex_);
98 
99     for (auto iter = busyList_.begin(); iter != busyList_.end(); ++iter) {
100         if (*iter != memory) {
101             continue;
102         }
103 
104         busyList_.erase(iter);
105         idleList_.push_back(memory);
106         cond_.notify_all();
107         MEDIA_LOGD("0x%{public}06" PRIXPTR " released back to pool %{public}s",
108                     FAKE_POINTER(memory), name_.c_str());
109 
110         lock.unlock();
111         if (notifier_ != nullptr) {
112             notifier_();
113         }
114         return;
115     }
116 
117     MEDIA_LOGE("0x%{public}06" PRIXPTR " is no longer managed by this pool", FAKE_POINTER(memory));
118     delete memory;
119 }
120 
DoAcquireMemory(int32_t size,AVSharedMemory ** outMemory)121 bool AVSharedMemoryPool::DoAcquireMemory(int32_t size, AVSharedMemory **outMemory)
122 {
123     MEDIA_LOGD("busylist size %{public}zu, idlelist size %{public}zu", busyList_.size(), idleList_.size());
124 
125     AVSharedMemory *result = nullptr;
126     std::list<AVSharedMemory *>::iterator minSizeIdleMem = idleList_.end();
127     int32_t minIdleSize = std::numeric_limits<int32_t>::max();
128 
129     for (auto iter = idleList_.begin(); iter != idleList_.end(); ++iter) {
130         if ((*iter)->GetSize() >= size) {
131             result = *iter;
132             idleList_.erase(iter);
133             break;
134         }
135 
136         if ((*iter)->GetSize() < minIdleSize) {
137             minIdleSize = (*iter)->GetSize();
138             minSizeIdleMem = iter;
139         }
140     }
141 
142     if (result == nullptr) {
143         auto totalCnt = busyList_.size() + idleList_.size();
144         if (totalCnt < option_.maxMemCnt) {
145             result = AllocMemory(size);
146             CHECK_AND_RETURN_RET(result != nullptr, false);
147         }
148 
149         if (!option_.enableFixedSize && minSizeIdleMem != idleList_.end()) {
150             delete *minSizeIdleMem;
151             *minSizeIdleMem = nullptr;
152             idleList_.erase(minSizeIdleMem);
153             result = AllocMemory(size);
154             CHECK_AND_RETURN_RET(result != nullptr, false);
155         }
156     }
157 
158     *outMemory = result;
159     return true;
160 }
161 
CheckSize(int32_t size)162 bool AVSharedMemoryPool::CheckSize(int32_t size)
163 {
164     if (size <= 0 && size != -1) {
165         return false;
166     }
167 
168     if (!option_.enableFixedSize && size == -1) {
169         return false;
170     }
171 
172     if (option_.enableFixedSize) {
173         if (size > option_.memSize) {
174             return false;
175         }
176 
177         if (size <= 0 && size != -1) {
178             return false;
179         }
180     }
181 
182     return true;
183 }
184 
AcquireMemory(int32_t size,bool blocking)185 std::shared_ptr<AVSharedMemory> AVSharedMemoryPool::AcquireMemory(int32_t size, bool blocking)
186 {
187     MEDIA_LOGD("acquire memory for size: %{public}d from pool %{public}s, blocking: %{public}d",
188                size, name_.c_str(), blocking);
189 
190     std::unique_lock<std::mutex> lock(mutex_);
191     if (!CheckSize(size)) {
192         MEDIA_LOGE("invalid size: %{public}d", size);
193         return nullptr;
194     }
195 
196     if (option_.enableFixedSize) {
197         size = option_.memSize;
198     }
199 
200     AVSharedMemory *memory = nullptr;
201     do {
202         if (!DoAcquireMemory(size, &memory) || memory != nullptr) {
203             break;
204         }
205 
206         if (!blocking || forceNonBlocking_) {
207             break;
208         }
209 
210         cond_.wait(lock);
211     } while (inited_ && !forceNonBlocking_);
212 
213     if (memory == nullptr) {
214         MEDIA_LOGD("acquire memory failed for size: %{public}d", size);
215         return nullptr;
216     }
217 
218     busyList_.push_back(memory);
219 
220     auto result = std::shared_ptr<AVSharedMemory>(memory, [weakPool = weak_from_this()](AVSharedMemory *memory) {
221         std::shared_ptr<AVSharedMemoryPool> pool = weakPool.lock();
222         if (pool != nullptr) {
223             pool->ReleaseMemory(memory);
224         } else {
225             MEDIA_LOGI("release memory 0x%{public}06" PRIXPTR ", but the pool is destroyed", FAKE_POINTER(memory));
226             delete memory;
227         }
228     });
229 
230     MEDIA_LOGD("0x%{public}06" PRIXPTR " acquired from pool", FAKE_POINTER(memory));
231     return result;
232 }
233 
SetNonBlocking(bool enable)234 void AVSharedMemoryPool::SetNonBlocking(bool enable)
235 {
236     std::unique_lock<std::mutex> lock(mutex_);
237     MEDIA_LOGD("SetNonBlocking: %{public}d", enable);
238     forceNonBlocking_ = enable;
239     if (forceNonBlocking_) {
240         cond_.notify_all();
241     }
242 }
243 
Reset()244 void AVSharedMemoryPool::Reset()
245 {
246     MEDIA_LOGD("Reset");
247 
248     std::unique_lock<std::mutex> lock(mutex_);
249     for (auto &memory : idleList_) {
250         delete memory;
251         memory = nullptr;
252     }
253     idleList_.clear();
254     inited_ = false;
255     forceNonBlocking_ = false;
256     notifier_ = nullptr;
257     cond_.notify_all();
258     // for busylist, the memory will be released when the refcount of shared_ptr is zero.
259 }
260 } // namespace Media
261 } // namespace OHOS