Summary: Don't allocate large arrays on stack when copying native pointers, use heap based array. Today the code copies the native pointers on the stack, since it may be too big, lets make sure to use heap based allocating using std::vector. This array is afterwards converted into a reversed map from index to pointer, so it is heap based anyhow. Changelog: [Internal] Don't allocate large arrays on stack when copying native pointers, use heap based array Reviewed By: Andrey-Mishanin Differential Revision: D28747213 fbshipit-source-id: da69b4b2d0960fdade9f07f44654b30d6dacc43a
48 lines
1.3 KiB
C++
48 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "jni.h"
|
|
#include <yoga/YGValue.h>
|
|
#include <yoga/Yoga.h>
|
|
#include <map>
|
|
#include "common.h"
|
|
|
|
using namespace facebook::yoga::vanillajni;
|
|
using namespace std;
|
|
|
|
class PtrJNodeMapVanilla {
|
|
std::map<YGNodeRef, size_t> ptrsToIdxs_;
|
|
jobjectArray javaNodes_;
|
|
|
|
public:
|
|
PtrJNodeMapVanilla() : ptrsToIdxs_{}, javaNodes_{} {}
|
|
PtrJNodeMapVanilla(jlongArray javaNativePointers, jobjectArray javaNodes)
|
|
: javaNodes_{javaNodes} {
|
|
|
|
JNIEnv* env = getCurrentEnv();
|
|
size_t nativePointersSize = env->GetArrayLength(javaNativePointers);
|
|
std::vector<jlong> nativePointers(nativePointersSize);
|
|
env->GetLongArrayRegion(
|
|
javaNativePointers, 0, nativePointersSize, nativePointers.data());
|
|
|
|
for (size_t i = 0; i < nativePointersSize; ++i) {
|
|
ptrsToIdxs_[(YGNodeRef) nativePointers[i]] = i;
|
|
}
|
|
}
|
|
|
|
ScopedLocalRef<jobject> ref(YGNodeRef node) {
|
|
JNIEnv* env = getCurrentEnv();
|
|
auto idx = ptrsToIdxs_.find(node);
|
|
if (idx == ptrsToIdxs_.end()) {
|
|
return ScopedLocalRef<jobject>(env);
|
|
} else {
|
|
return make_local_ref(
|
|
env, env->GetObjectArrayElement(javaNodes_, idx->second));
|
|
}
|
|
}
|
|
};
|