1 /** 2 * Copyright (c) 2023-2024 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 ark::ets::interop::js { 26 class InteropCtx; 27 } // namespace ark::ets::interop::js 28 29 namespace ark::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 PANDA_PUBLIC_API 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 PANDA_PUBLIC_API 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 PANDA_PUBLIC_API SharedReference *GetReference(napi_env env, napi_value jsObject); 52 PANDA_PUBLIC_API 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 PANDA_PUBLIC_API inline SharedReference *CreateReference(InteropCtx *ctx, EtsObject *etsObject, 68 napi_value jsObject); 69 70 PANDA_PUBLIC_API SharedReference *GetReference(void *data); 71 PANDA_PUBLIC_API void RemoveReference(SharedReference *sharedRef); 72 73 PANDA_PUBLIC_API bool CheckAlive(void *data); 74 friend class SharedReference; 75 friend class testing::SharedReferenceStorage1GTest; 76 }; 77 78 } // namespace ark::ets::interop::js::ets_proxy 79 80 #endif // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_ETS_PROXY_SHARED_REFERENCE_STORAGE_H_ 81