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