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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_ETS_PROXY_SHARED_REFERENCE_STORAGE_H 17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_ETS_PROXY_SHARED_REFERENCE_STORAGE_H 18 19 #include "libpandabase/macros.h" 20 #include "plugins/ets/runtime/interop_js/ets_proxy/shared_reference.h" 21 #include "plugins/ets/runtime/interop_js/ets_proxy/mem/items_pool.h" 22 #include "plugins/ets/runtime/types/ets_object.h" 23 #include "runtime/mark_word.h" 24 25 namespace panda::ets::interop::js { 26 class InteropCtx; 27 } // namespace panda::ets::interop::js 28 29 namespace panda::ets::interop::js::ets_proxy { 30 31 namespace testing { 32 class SharedReferenceStorage1GTest; 33 } // namespace testing 34 35 using SharedReferencePool = ItemsPool<SharedReference, SharedReference::MAX_MARK_BITS>; 36 37 class SharedReferenceStorage : private SharedReferencePool { 38 public: 39 static std::unique_ptr<SharedReferenceStorage> Create(); 40 ~SharedReferenceStorage() = default; 41 HasReference(EtsObject * etsObject)42 static bool HasReference(EtsObject *etsObject) 43 { 44 return SharedReference::HasReference(etsObject); 45 } 46 47 SharedReference *CreateETSObjectRef(InteropCtx *ctx, EtsObject *etsObject, napi_value jsObject); 48 SharedReference *CreateJSObjectRef(InteropCtx *ctx, EtsObject *etsObject, napi_value jsObject); 49 SharedReference *CreateHybridObjectRef(InteropCtx *ctx, EtsObject *etsObject, napi_value jsObject); 50 51 SharedReference *GetReference(napi_env env, napi_value jsObject); 52 SharedReference *GetReference(EtsObject *etsObject); 53 GetNextAlloc()54 SharedReference *GetNextAlloc() const 55 { 56 return SharedReferencePool::GetNextAlloc(); 57 } 58 59 static constexpr size_t MAX_POOL_SIZE = (sizeof(void *) > 4) ? 1_GB : 64_MB; 60 61 private: SharedReferenceStorage(void * data,size_t size)62 SharedReferenceStorage(void *data, size_t size) : SharedReferencePool(data, size) {} 63 NO_COPY_SEMANTIC(SharedReferenceStorage); 64 NO_MOVE_SEMANTIC(SharedReferenceStorage); 65 66 template <SharedReference::InitFn REF_INIT> 67 inline SharedReference *CreateReference(InteropCtx *ctx, EtsObject *etsObject, napi_value jsObject); 68 69 SharedReference *GetReference(void *data); 70 void RemoveReference(SharedReference *sharedRef); 71 72 bool CheckAlive(void *data); 73 friend class SharedReference; 74 friend class testing::SharedReferenceStorage1GTest; 75 }; 76 77 } // namespace panda::ets::interop::js::ets_proxy 78 79 #endif // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_ETS_PROXY_SHARED_REFERENCE_STORAGE_H 80