1 /*
2 * Copyright (c) 2021-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 #ifndef PANDA_RUNTIME_MEM_TEST_UTILS_H
17 #define PANDA_RUNTIME_MEM_TEST_UTILS_H
18
19 #include "runtime/include/runtime.h"
20 #include "runtime/include/coretypes/array.h"
21 #include "runtime/include/coretypes/string.h"
22 #include "runtime/include/panda_vm.h"
23 #include "runtime/include/class_linker.h"
24 #include "runtime/include/class_root.h"
25
26 namespace panda::mem {
AllocateNullifiedPayloadString(size_t size)27 [[maybe_unused]] static ObjectHeader *AllocateNullifiedPayloadString(size_t size)
28 {
29 auto *vm = Runtime::GetCurrent()->GetPandaVM();
30 ASSERT(vm != nullptr);
31 auto *string_class = Runtime::GetCurrent()->GetClassLinker()->GetExtension(
32 vm->GetLanguageContext())->GetClassRoot(ClassRoot::STRING);
33 ASSERT(string_class != nullptr);
34 mem::HeapManager *heap_manager = vm->GetHeapManager();
35 ASSERT(heap_manager != nullptr);
36 return heap_manager->AllocateObject(string_class, size);
37 }
38
39 class ObjectAllocator {
40 public:
ObjectAllocator()41 ObjectAllocator() {}
~ObjectAllocator()42 ~ObjectAllocator() {}
43
AllocArray(size_t length,ClassRoot class_root,bool nonmovable)44 coretypes::Array *AllocArray(size_t length, ClassRoot class_root, bool nonmovable)
45 {
46 Runtime *runtime = Runtime::GetCurrent();
47 LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
48 SpaceType space_type = SpaceType::SPACE_TYPE_OBJECT;
49 auto *klass = runtime->GetClassLinker()->GetExtension(ctx)->GetClassRoot(class_root);
50 if (nonmovable) {
51 space_type = SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT;
52 }
53 return coretypes::Array::Create(klass, length, space_type);
54 }
55
AllocString(size_t length)56 coretypes::String *AllocString(size_t length)
57 {
58 Runtime *runtime = Runtime::GetCurrent();
59 LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
60 PandaVector<uint8_t> data;
61 data.resize(length);
62 return coretypes::String::CreateFromMUtf8(data.data(), length, length, true, ctx, runtime->GetPandaVM());
63 }
64
AllocObjectInYoung()65 ObjectHeader *AllocObjectInYoung()
66 {
67 Runtime *runtime = Runtime::GetCurrent();
68 LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
69 return coretypes::String::CreateEmptyString(ctx, runtime->GetPandaVM());
70 }
71 };
72 } // namespace panda::mem
73
74 #endif // PANDA_RUNTIME_MEM_TEST_UTILS_H
75