• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()
32                              ->GetClassLinker()
33                              ->GetExtension(vm->GetLanguageContext())
34                              ->GetClassRoot(ClassRoot::STRING);
35     ASSERT(string_class != nullptr);
36     mem::HeapManager *heap_manager = vm->GetHeapManager();
37     ASSERT(heap_manager != nullptr);
38     return heap_manager->AllocateObject(string_class, size);
39 }
40 
41 class ObjectAllocator {
42 public:
ObjectAllocator()43     ObjectAllocator() {}
~ObjectAllocator()44     ~ObjectAllocator() {}
45 
AllocArray(size_t length,ClassRoot class_root,bool nonmovable)46     coretypes::Array *AllocArray(size_t length, ClassRoot class_root, bool nonmovable)
47     {
48         Runtime *runtime = Runtime::GetCurrent();
49         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
50         SpaceType space_type = SpaceType::SPACE_TYPE_OBJECT;
51         auto *klass = runtime->GetClassLinker()->GetExtension(ctx)->GetClassRoot(class_root);
52         if (nonmovable) {
53             space_type = SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT;
54         }
55         return coretypes::Array::Create(klass, length, space_type);
56     }
57 
AllocString(size_t length)58     coretypes::String *AllocString(size_t length)
59     {
60         Runtime *runtime = Runtime::GetCurrent();
61         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
62         PandaVector<uint8_t> data;
63         data.resize(length);
64         return coretypes::String::CreateFromMUtf8(data.data(), length, length, true, ctx, runtime->GetPandaVM());
65     }
66 
AllocObjectInYoung()67     ObjectHeader *AllocObjectInYoung()
68     {
69         Runtime *runtime = Runtime::GetCurrent();
70         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
71         return coretypes::String::CreateEmptyString(ctx, runtime->GetPandaVM());
72     }
73 };
74 }  // namespace panda::mem
75 
76 #endif  // PANDA_RUNTIME_MEM_TEST_UTILS_H
77