• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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_RUNTIME_MEM_TEST_UTILS_H
17 #define PANDA_RUNTIME_MEM_TEST_UTILS_H
18 
19 #include "libpandabase/test_utilities.h"
20 #include "runtime/include/runtime.h"
21 #include "runtime/include/coretypes/array.h"
22 #include "runtime/include/coretypes/string.h"
23 #include "runtime/include/panda_vm.h"
24 #include "runtime/include/class_linker.h"
25 #include "runtime/include/class_root.h"
26 
27 namespace ark::mem {
28 constexpr std::initializer_list<const char *> TESTED_GC = {"stw", "gen-gc", "g1-gc"};
29 
AllocateNullifiedPayloadString(size_t length)30 [[maybe_unused]] inline ObjectHeader *AllocateNullifiedPayloadString(size_t length)
31 {
32     auto *vm = Runtime::GetCurrent()->GetPandaVM();
33     ASSERT(vm != nullptr);
34     auto *stringClass = Runtime::GetCurrent()
35                             ->GetClassLinker()
36                             ->GetExtension(vm->GetLanguageContext())
37                             ->GetClassRoot(ClassRoot::STRING);
38     ASSERT(stringClass != nullptr);
39     mem::HeapManager *heapManager = vm->GetHeapManager();
40     ASSERT(heapManager != nullptr);
41     return heapManager->AllocateObject(stringClass, ark::coretypes::String::ComputeSizeUtf16(length));
42 }
43 
AllocNonMovableObject()44 inline ObjectHeader *AllocNonMovableObject()
45 {
46     uint16_t data = 0;
47     Runtime *runtime = Runtime::GetCurrent();
48     LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
49     return coretypes::String::CreateFromUtf16(&data, 0, ctx, runtime->GetPandaVM(), false);
50 }
51 
52 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions,-warnings-as-errors)
53 class ObjectAllocator {
54 public:
55     static coretypes::Array *AllocArray(size_t length, ClassRoot classRoot, bool nonmovable, bool pinned = false)
56     {
57         Runtime *runtime = Runtime::GetCurrent();
58         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
59         SpaceType spaceType = SpaceType::SPACE_TYPE_OBJECT;
60         auto *klass = runtime->GetClassLinker()->GetExtension(ctx)->GetClassRoot(classRoot);
61         if (nonmovable) {
62             spaceType = SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT;
63         }
64         return coretypes::Array::Create(klass, length, spaceType, pinned);
65     }
66 
67     static coretypes::String *AllocString(size_t length, bool pinned = false)
68     {
69         Runtime *runtime = Runtime::GetCurrent();
70         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
71         PandaVector<uint8_t> data;
72         data.resize(length);
73         return coretypes::String::CreateFromMUtf8(data.data(), length, length, true, ctx, runtime->GetPandaVM(), true,
74                                                   pinned);
75     }
76 
AllocObjectInYoung()77     static ObjectHeader *AllocObjectInYoung()
78     {
79         Runtime *runtime = Runtime::GetCurrent();
80         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
81         return coretypes::String::CreateEmptyString(ctx, runtime->GetPandaVM());
82     }
83 };
84 }  // namespace ark::mem
85 
86 #endif  // PANDA_RUNTIME_MEM_TEST_UTILS_H
87