• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "ecmascript/shared_mm/shared_mm.h"
17 
18 namespace panda::ecmascript {
19 static constexpr size_t MALLOC_SIZE_LIMIT = 2147483648; // Max internal memory used by the VM declared in options
20 
~JSSharedMemoryManager()21 JSSharedMemoryManager::~JSSharedMemoryManager()
22 {
23     os::memory::LockHolder lock(jsSharedMemoryLock_);
24     auto iter = loadedJSSharedMemory_.begin();
25     while (iter != loadedJSSharedMemory_.end()) {
26         const void *pointer = ToVoidPtr(iter->first);
27         FreeBuffer(const_cast<void *>(pointer));
28         iter = loadedJSSharedMemory_.erase(iter);
29     }
30 }
31 
CreateOrLoad(void ** pointer,size_t size)32 bool JSSharedMemoryManager::CreateOrLoad(void **pointer, size_t size)
33 {
34     if (*pointer != nullptr) {
35         if (loadedJSSharedMemory_.find((uint64_t)*pointer) != loadedJSSharedMemory_.end()) {
36             IncreaseRefSharedMemory(*pointer);
37         }
38         return false;
39     }
40     *pointer = AllocateBuffer(size);
41     InsertSharedMemory(*pointer);
42     return true;
43 }
44 
InsertSharedMemory(const void * pointer)45 void JSSharedMemoryManager::InsertSharedMemory(const void *pointer)
46 {
47     os::memory::LockHolder lock(jsSharedMemoryLock_);
48     if (loadedJSSharedMemory_.find((uint64_t)pointer) == loadedJSSharedMemory_.end()) {
49         loadedJSSharedMemory_[(uint64_t)pointer] = 1;
50     }
51 }
52 
IncreaseRefSharedMemory(const void * pointer)53 void JSSharedMemoryManager::IncreaseRefSharedMemory(const void *pointer)
54 {
55     os::memory::LockHolder lock(jsSharedMemoryLock_);
56     if (loadedJSSharedMemory_.find((uint64_t)pointer) != loadedJSSharedMemory_.end()) {
57         loadedJSSharedMemory_[(uint64_t)pointer]++;
58     }
59 }
60 
DecreaseRefSharedMemory(const void * pointer)61 void JSSharedMemoryManager::DecreaseRefSharedMemory(const void *pointer)
62 {
63     os::memory::LockHolder lock(jsSharedMemoryLock_);
64     auto iter = loadedJSSharedMemory_.find((uint64_t)pointer);
65     if (iter != loadedJSSharedMemory_.end()) {
66         if (iter->second > 1) {
67             iter->second--;
68             return;
69         }
70         loadedJSSharedMemory_.erase(iter);
71         FreeBuffer(const_cast<void *>(pointer));
72     }
73 }
74 
AllocateBuffer(size_t size)75 void *JSSharedMemoryManager::AllocateBuffer(size_t size)
76 {
77     if (size == 0) {
78         LOG_ECMA_MEM(FATAL) << "size must have a size bigger than 0";
79         UNREACHABLE();
80     }
81     if (size >= MALLOC_SIZE_LIMIT) {
82         LOG_ECMA_MEM(FATAL) << "size must be less than the maximum";
83         UNREACHABLE();
84     }
85     // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
86     void *ptr = malloc(size);
87     if (ptr == nullptr) {
88         LOG_ECMA_MEM(FATAL) << "malloc failed";
89         UNREACHABLE();
90     }
91 #if ECMASCRIPT_ENABLE_ZAP_MEM
92     if (memset_s(ptr, size, INVALID_VALUE, size) != EOK) {
93         LOG_ECMA_MEM(FATAL) << "memset failed";
94         UNREACHABLE();
95     }
96 #endif
97     return ptr;
98 }
99 
FreeBuffer(void * mem)100 void JSSharedMemoryManager::FreeBuffer(void *mem)
101 {
102     if (mem == nullptr) {
103         return;
104     }
105     // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
106     free(mem);
107 }
108 
RemoveSharedMemory(void * pointer,void * data)109 void JSSharedMemoryManager::RemoveSharedMemory(void *pointer, void *data)
110 {
111     if (pointer == nullptr || data == nullptr) {
112         return;
113     }
114     // dec ref in menorymanager
115     JSSharedMemoryManager *jsSharedMemoryManager = static_cast<JSSharedMemoryManager *>(data);
116     jsSharedMemoryManager->DecreaseRefSharedMemory(pointer);
117 }
118 } // namespace panda::ecmascript
119