|
| 1 | +/** |
| 2 | + * Copyright © 2026, Evolved Binary Ltd |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of the <organization> nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 20 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +#include <jni.h> |
| 28 | + |
| 29 | +#include <algorithm> |
| 30 | +#include <cstring> |
| 31 | +#include <stdexcept> |
| 32 | + |
| 33 | +#include "Iterator.h" |
| 34 | + |
| 35 | +#ifdef __cplusplus |
| 36 | +extern "C" { |
| 37 | +#endif |
| 38 | + |
| 39 | +/* |
| 40 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 41 | + * Method: createIterator |
| 42 | + * Signature: (IJ)J |
| 43 | + */ |
| 44 | +JNIEXPORT jlong JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_createIterator |
| 45 | + (JNIEnv *env, jobject obj, jint num_elements, jlong element_size) { |
| 46 | + Iterator* iterator = new Iterator(num_elements, static_cast<size_t>(element_size)); |
| 47 | + return reinterpret_cast<jlong>(iterator); |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 52 | + * Method: hasNext |
| 53 | + * Signature: (J)B |
| 54 | + */ |
| 55 | +JNIEXPORT jboolean JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_hasNext |
| 56 | + (JNIEnv *env, jobject obj, jlong handle) { |
| 57 | + Iterator* iterator = reinterpret_cast<Iterator*>(handle); |
| 58 | + return iterator->hasNext() ? JNI_TRUE : JNI_FALSE; |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 63 | + * Method: next |
| 64 | + * Signature: (J)[B |
| 65 | + */ |
| 66 | +JNIEXPORT jbyteArray JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_next |
| 67 | + (JNIEnv *env, jobject obj, jlong handle) { |
| 68 | + Iterator* iterator = reinterpret_cast<Iterator*>(handle); |
| 69 | + try { |
| 70 | + const std::vector<char>& data = iterator->next(); |
| 71 | + jbyteArray result = env->NewByteArray(static_cast<jsize>(data.size())); |
| 72 | + if (result == nullptr) { |
| 73 | + return nullptr; |
| 74 | + } |
| 75 | + env->SetByteArrayRegion(result, 0, static_cast<jsize>(data.size()), reinterpret_cast<const jbyte*>(data.data())); |
| 76 | + return result; |
| 77 | + } catch (const std::out_of_range& e) { |
| 78 | + jclass exClass = env->FindClass("java/util/NoSuchElementException"); |
| 79 | + if (exClass != nullptr) { |
| 80 | + env->ThrowNew(exClass, e.what()); |
| 81 | + } |
| 82 | + return nullptr; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 88 | + * Method: disposeInternal |
| 89 | + * Signature: (J)V |
| 90 | + */ |
| 91 | +JNIEXPORT void JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_disposeInternal |
| 92 | + (JNIEnv *env, jobject obj, jlong handle) { |
| 93 | + Iterator* iterator = reinterpret_cast<Iterator*>(handle); |
| 94 | + delete iterator; |
| 95 | +} |
| 96 | + |
| 97 | +extern "C" void* iterator_create(int num_elements, size_t element_size) { |
| 98 | + return new Iterator(num_elements, element_size); |
| 99 | +} |
| 100 | + |
| 101 | +extern "C" int iterator_has_next(void* handle) { |
| 102 | + auto* iterator = reinterpret_cast<Iterator*>(handle); |
| 103 | + return iterator->hasNext() ? 1 : 0; |
| 104 | +} |
| 105 | + |
| 106 | +extern "C" int iterator_next(void* handle, char* dest, int dest_len) { |
| 107 | + auto* iterator = reinterpret_cast<Iterator*>(handle); |
| 108 | + try { |
| 109 | + const auto& data = iterator->next(); |
| 110 | + const int size = std::min(static_cast<int>(data.size()), dest_len); |
| 111 | + std::memcpy(dest, data.data(), static_cast<size_t>(size)); |
| 112 | + return size; |
| 113 | + } catch (const std::out_of_range&) { |
| 114 | + return -1; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +extern "C" void iterator_dispose(void* handle) { |
| 119 | + auto* iterator = reinterpret_cast<Iterator*>(handle); |
| 120 | + delete iterator; |
| 121 | +} |
| 122 | + |
| 123 | +#ifdef __cplusplus |
| 124 | +} |
| 125 | +#endif |
0 commit comments