1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_NATIVE_STRING_ARRAY_UTILS_H_
18 #define ART_RUNTIME_NATIVE_STRING_ARRAY_UTILS_H_
19 
20 #include "base/macros.h"
21 #include "base/locks.h"
22 #include "class_root-inl.h"
23 #include "handle_scope-inl.h"
24 #include "mirror/object_array-alloc-inl.h"
25 #include "mirror/string.h"
26 
27 namespace art HIDDEN {
28 
29 namespace detail {
30 
GetStringCStr(const char * str)31 inline const char* GetStringCStr(const char* str) { return str; }
GetStringCStr(const std::string & str)32 inline const char* GetStringCStr(const std::string& str) { return str.c_str(); }
33 
34 }  // namespace detail
35 
36 template <typename Container>
CreateStringArray(Thread * self,size_t size,const Container & entries)37 ObjPtr<mirror::ObjectArray<mirror::String>> CreateStringArray(
38     Thread* self, size_t size, const Container& entries) REQUIRES_SHARED(Locks::mutator_lock_) {
39   StackHandleScope<1u> hs(self);
40   Handle<mirror::ObjectArray<mirror::String>> array = hs.NewHandle(
41       mirror::ObjectArray<mirror::String>::Alloc(
42           self, GetClassRoot<mirror::ObjectArray<mirror::String>>(), size));
43   if (array == nullptr) {
44     DCHECK(self->IsExceptionPending());
45     return nullptr;
46   }
47   // Note: If the container's iterator returns a `std::string` by value, the `auto&&`
48   // binds as a const reference and extends the lifetime of the temporary object.
49   size_t pos = 0u;
50   for (auto&& entry : entries) {
51     ObjPtr<mirror::String> oentry =
52         mirror::String::AllocFromModifiedUtf8(self, detail::GetStringCStr(entry));
53     if (oentry == nullptr) {
54       DCHECK(self->IsExceptionPending());
55       return nullptr;
56     }
57     // We're initializing a newly allocated array object, so we do not need to record that under
58     // a transaction. If the transaction is aborted, the whole object shall be unreachable.
59     DCHECK_LT(pos, size);
60     array->SetWithoutChecks</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
61         pos, oentry);
62     ++pos;
63   }
64   DCHECK_EQ(pos, size);
65   return array.Get();
66 }
67 
68 template <typename Container>
CreateStringArray(Thread * self,const Container & entries)69 ObjPtr<mirror::ObjectArray<mirror::String>> CreateStringArray(
70     Thread* self, const Container& entries) REQUIRES_SHARED(Locks::mutator_lock_) {
71   return CreateStringArray(self, entries.size(), entries);
72 }
73 
CreateStringArray(Thread * self,std::initializer_list<const char * > entries)74 inline ObjPtr<mirror::ObjectArray<mirror::String>> CreateStringArray(
75     Thread* self, std::initializer_list<const char*> entries)
76     REQUIRES_SHARED(Locks::mutator_lock_) {
77   return CreateStringArray<std::initializer_list<const char*>>(self, entries);
78 }
79 
80 }  // namespace art
81 
82 #endif  // ART_RUNTIME_NATIVE_STRING_ARRAY_UTILS_H_
83