Skip to content

Commit 3acc2af

Browse files
committed
Add a skeleton for ArenaRef
1 parent 3095517 commit 3acc2af

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

firestore/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ set(android_SRCS
138138
src/android/wrapper.h
139139
src/android/write_batch_android.cc
140140
src/android/write_batch_android.h
141+
src/jni/arena_ref.cc
142+
src/jni/arena_ref.h
141143
src/jni/array.h
142144
src/jni/array_list.cc
143145
src/jni/array_list.h

firestore/integration_test_internal/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ set(FIREBASE_INTEGRATION_TEST_PORTABLE_TEST_SRCS
121121

122122
# These sources contain the actual tests that run on Android only.
123123
set(FIREBASE_INTEGRATION_TEST_ANDROID_TEST_SRCS
124+
src/android/arena_ref_android_test.cc
124125
src/android/field_path_portable_test.cc
125126
src/android/firestore_integration_test_android_test.cc
126127
src/android/geo_point_android_test.cc
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
#include "firestore/src/jni/arena_ref.h"
18+
19+
#include "firestore_integration_test_android.h"
20+
21+
#include "gmock/gmock.h"
22+
#include "gtest/gtest.h"
23+
24+
namespace firebase {
25+
namespace firestore {
26+
namespace jni {
27+
namespace {
28+
29+
using ArenaRefTestAndroid = FirestoreAndroidIntegrationTest;
30+
31+
TEST_F(ArenaRefTestAndroid, DefaultConstructorCreatesReferenceToNull) {
32+
Env env;
33+
ArenaRef arena_ref;
34+
EXPECT_EQ(arena_ref.get(env).get(), nullptr);
35+
}
36+
37+
TEST_F(ArenaRefTestAndroid, ConstructFromEnvAndObject) {
38+
Env env;
39+
Local<String> string = env.NewStringUtf("hello world");
40+
41+
ArenaRef arena_ref(env, string);
42+
43+
Local<String> stringRef2 = string;
44+
string.clear();
45+
EXPECT_TRUE(arena_ref.get(env).Equals(env, stringRef2));
46+
}
47+
48+
} // namespace
49+
} // namespace jni
50+
} // namespace firestore
51+
} // namespace firebase

firestore/src/android/firestore_android.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "firestore/src/common/hard_assert_common.h"
6060
#include "firestore/src/common/make_unique.h"
6161
#include "firestore/src/include/firebase/firestore.h"
62+
#include "firestore/src/jni/arena_ref.h"
6263
#include "firestore/src/jni/array.h"
6364
#include "firestore/src/jni/array_list.h"
6465
#include "firestore/src/jni/boolean.h"
@@ -314,6 +315,10 @@ bool FirestoreInternal::Initialize(App* app) {
314315
jni::Long::Initialize(loader);
315316
jni::Map::Initialize(loader);
316317

318+
// Initialize ArenaRef _after_ the other JNI classes because it relies on
319+
// HashMap having already been initialized.
320+
jni::ArenaRef::Initialize(env);
321+
317322
InitializeFirestore(loader);
318323
InitializeFirestoreTasks(loader);
319324
InitializeUserCallbackExecutor(loader);

firestore/src/jni/arena_ref.cc

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
#include <atomic>
17+
18+
#include "firestore/src/jni/arena_ref.h"
19+
20+
#include "firestore/src/jni/hash_map.h"
21+
#include "firestore/src/jni/long.h"
22+
#include "firestore/src/jni/map.h"
23+
24+
namespace firebase {
25+
namespace firestore {
26+
namespace jni {
27+
28+
namespace {
29+
30+
HashMap* gArenaRefHashMap = nullptr;
31+
32+
int64_t GetNextArenaRefKey() {
33+
static std::atomic<int64_t> next_key(1);
34+
return next_key.fetch_add(1);
35+
}
36+
37+
}
38+
39+
ArenaRef::ArenaRef(Env& env, const Object& object) : key_(GetNextArenaRefKey()) {
40+
gArenaRefHashMap->Put(env, key_object(env), object);
41+
}
42+
43+
ArenaRef::~ArenaRef() {
44+
if (key_ != 0) {
45+
Env env;
46+
gArenaRefHashMap->Remove(env, key_object(env));
47+
}
48+
}
49+
50+
Local<Long> ArenaRef::key_object(Env& env) const {
51+
return Long::Create(env, key_);
52+
}
53+
54+
void ArenaRef::Initialize(Env& env) {
55+
if (gArenaRefHashMap) {
56+
return;
57+
}
58+
Global<HashMap> hash_map(HashMap::Create(env));
59+
jobject hash_map_jobject = hash_map.release();
60+
gArenaRefHashMap = new HashMap(hash_map_jobject);
61+
}
62+
63+
Local<Object> ArenaRef::get(Env& env) const {
64+
return gArenaRefHashMap->Get(env, key_object(env));
65+
}
66+
67+
} // namespace jni
68+
} // namespace firestore
69+
} // namespace firebase

firestore/src/jni/arena_ref.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
#include <cstdlib>
17+
18+
#include <jni.h>
19+
20+
#include "firestore/src/jni/env.h"
21+
#include "firestore/src/jni/object.h"
22+
#include "firestore/src/jni/ownership.h"
23+
24+
#ifndef FIREBASE_FIRESTORE_SRC_JNI_ARENA_REF_H_
25+
#define FIREBASE_FIRESTORE_SRC_JNI_ARENA_REF_H_
26+
27+
namespace firebase {
28+
namespace firestore {
29+
namespace jni {
30+
31+
class ArenaRef {
32+
public:
33+
ArenaRef() = default;
34+
ArenaRef(Env&, const Object&);
35+
~ArenaRef();
36+
37+
static void Initialize(Env&);
38+
39+
Local<Object> get(Env&) const;
40+
41+
private:
42+
Local<Long> key_object(Env&) const;
43+
44+
int64_t key_ = 0;
45+
};
46+
47+
} // namespace jni
48+
} // namespace firestore
49+
} // namespace firebase
50+
51+
#endif // FIREBASE_FIRESTORE_SRC_JNI_ARENA_REF_H_

0 commit comments

Comments
 (0)