• 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 #include "runtime/include/coretypes/array.h"
17 
18 #include "runtime/arch/memory_helpers.h"
19 #include "runtime/include/class-inl.h"
20 #include "runtime/include/class_linker.h"
21 #include "runtime/include/coretypes/dyn_objects.h"
22 #include "runtime/include/runtime.h"
23 #include "runtime/include/panda_vm.h"
24 
25 namespace panda::coretypes {
26 
AllocateArray(panda::BaseClass * arrayClass,size_t elemSize,ArraySizeT length,panda::SpaceType spaceType,const PandaVM * vm=Thread::GetCurrent ()->GetVM ())27 static Array *AllocateArray(panda::BaseClass *arrayClass, size_t elemSize, ArraySizeT length,
28                             panda::SpaceType spaceType, const PandaVM *vm = Thread::GetCurrent()->GetVM())
29 {
30     size_t size = Array::ComputeSize(elemSize, length);
31     if (UNLIKELY(size == 0)) {
32         LOG(ERROR, RUNTIME) << "Illegal array size: element size: " << elemSize << " array length: " << length;
33         ThrowOutOfMemoryError("OOM when allocating array");
34         return nullptr;
35     }
36     if (LIKELY(spaceType == panda::SpaceType::SPACE_TYPE_OBJECT)) {
37         return static_cast<coretypes::Array *>(
38             vm->GetHeapManager()->AllocateObject(arrayClass, size, DEFAULT_ALIGNMENT, ManagedThread::GetCurrent()));
39     }
40     if (spaceType == panda::SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT) {
41         return static_cast<coretypes::Array *>(vm->GetHeapManager()->AllocateNonMovableObject(
42             arrayClass, size, DEFAULT_ALIGNMENT, ManagedThread::GetCurrent()));
43     }
44     UNREACHABLE();
45 }
46 
47 /* static */
Create(panda::Class * arrayClass,const uint8_t * data,ArraySizeT length,panda::SpaceType spaceType)48 Array *Array::Create(panda::Class *arrayClass, const uint8_t *data, ArraySizeT length, panda::SpaceType spaceType)
49 {
50     size_t elemSize = arrayClass->GetComponentSize();
51     auto *array = AllocateArray(arrayClass, elemSize, length, spaceType);
52     if (UNLIKELY(array == nullptr)) {
53         LOG(ERROR, RUNTIME) << "Failed to allocate array.";
54         return nullptr;
55     }
56     // Order is matters here: GC can read data before it copied if we set length first.
57     // length == 0 is guaranteed by AllocateArray
58     TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
59     array->SetLength(length);
60     std::copy_n(data, length * elemSize, reinterpret_cast<uint8_t *>(array->GetData()));
61     TSAN_ANNOTATE_IGNORE_WRITES_END();
62     // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
63     // legth before it's set
64     arch::FullMemoryBarrier();
65     return array;
66 }
67 
68 /* static */
Create(panda::Class * arrayClass,ArraySizeT length,panda::SpaceType spaceType)69 Array *Array::Create(panda::Class *arrayClass, ArraySizeT length, panda::SpaceType spaceType)
70 {
71     size_t elemSize = arrayClass->GetComponentSize();
72     auto *array = AllocateArray(arrayClass, elemSize, length, spaceType);
73     if (array == nullptr) {
74         return nullptr;
75     }
76     // No need to memset - it is done in allocator
77     TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
78     array->SetLength(length);
79     TSAN_ANNOTATE_IGNORE_WRITES_END();
80     // Without full memory barrier it is possible that architectures with weak memory order can try fetching array
81     // length before it's set
82     arch::FullMemoryBarrier();
83     return array;
84 }
85 
86 /* static */
Create(DynClass * dynarrayclass,ArraySizeT length,panda::SpaceType spaceType)87 Array *Array::Create(DynClass *dynarrayclass, ArraySizeT length, panda::SpaceType spaceType)
88 {
89     size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
90     HClass *arrayClass = dynarrayclass->GetHClass();
91     auto *array = AllocateArray(arrayClass, elemSize, length, spaceType);
92     if (array == nullptr) {
93         return nullptr;
94     }
95     // No need to memset - it is done in allocator
96     TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
97     array->SetLength(length);
98     TSAN_ANNOTATE_IGNORE_WRITES_END();
99     // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
100     // legth before it's set
101     arch::FullMemoryBarrier();
102     return array;
103 }
104 
105 /* static */
CreateTagged(const PandaVM * vm,panda::BaseClass * arrayClass,ArraySizeT length,panda::SpaceType spaceType,TaggedValue initValue)106 Array *Array::CreateTagged(const PandaVM *vm, panda::BaseClass *arrayClass, ArraySizeT length,
107                            panda::SpaceType spaceType, TaggedValue initValue)
108 {
109     size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
110     auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, vm);
111     if (array == nullptr) {
112         return nullptr;
113     }
114     // Order is matters here: GC can read data before it copied if we set length first.
115     // length == 0 is guaranteed by AllocateArray
116     for (ArraySizeT i = 0; i < length; i++) {
117         array->Set<TaggedType, false, true>(i, initValue.GetRawData());
118     }
119     TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
120     array->SetLength(length);
121     TSAN_ANNOTATE_IGNORE_WRITES_END();
122     // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
123     // legth before it's set
124     arch::FullMemoryBarrier();
125     return array;
126 }
127 
128 }  // namespace panda::coretypes
129