@@ -50,23 +50,23 @@ struct server_response_reader;
5050// worker thread. Stored as the Java-side `ctx` (jlong) pointer.
5151// ---------------------------------------------------------------------------
5252struct jllama_context {
53- server_context server; // value member (pimpl inside)
54- std::thread worker;
55- bool vocab_only = false ;
53+ server_context server; // value member (pimpl inside)
54+ std::thread worker;
55+ bool vocab_only = false ;
5656 std::atomic<bool > worker_ready{false };
5757
5858 // Cached after load_model() — valid for the lifetime of this context.
59- const llama_vocab *vocab = nullptr ;
59+ const llama_vocab *vocab = nullptr ;
6060 // Non-null only in vocab-only mode (bypasses server_context entirely).
61- llama_model *vocab_only_model = nullptr ;
61+ llama_model *vocab_only_model = nullptr ;
6262
6363 // Saved copy of common_params used to load the model.
6464 // Required by server_task::params_from_json_cmpl which takes common_params&.
65- common_params params;
65+ common_params params;
6666
6767 // Per-streaming-task response readers, keyed by task id.
6868 // Guarded by readers_mutex.
69- std::mutex readers_mutex;
69+ std::mutex readers_mutex;
7070 std::map<int , std::unique_ptr<server_response_reader>> readers;
7171};
7272
@@ -80,9 +80,7 @@ inline void erase_reader(jllama_context *jctx, int id_task) {
8080
8181// Guard: throw and return false if the model was loaded without embedding
8282// support enabled. Used by every JNI entry point that produces embeddings.
83- [[nodiscard]] inline bool require_embedding_support (JNIEnv *env,
84- bool embedding_enabled,
85- jclass error_class) {
83+ [[nodiscard]] inline bool require_embedding_support (JNIEnv *env, bool embedding_enabled, jclass error_class) {
8684 if (embedding_enabled) {
8785 return true ;
8886 }
@@ -101,9 +99,7 @@ inline void erase_reader(jllama_context *jctx, int id_task) {
10199// already deleted (or never fully initialised), which is a valid no-op for
102100// a destructor-style call.
103101// ---------------------------------------------------------------------------
104- [[nodiscard]] inline jllama_context *get_jllama_context_impl (JNIEnv *env,
105- jobject obj,
106- jfieldID field_id) {
102+ [[nodiscard]] inline jllama_context *get_jllama_context_impl (JNIEnv *env, jobject obj, jfieldID field_id) {
107103 const jlong handle = env->GetLongField (obj, field_id);
108104 if (handle == 0 ) {
109105 return nullptr ;
@@ -117,10 +113,8 @@ inline void erase_reader(jllama_context *jctx, int id_task) {
117113// Checks that `data` contains the given key. Returns true if present.
118114// On missing key: throws "<field> is required" via JNI and returns false.
119115// ---------------------------------------------------------------------------
120- [[nodiscard]] inline bool require_json_field_impl (JNIEnv *env,
121- const nlohmann::json &data,
122- const char *field,
123- jclass error_class) {
116+ [[nodiscard]] inline bool require_json_field_impl (JNIEnv *env, const nlohmann::json &data, const char *field,
117+ jclass error_class) {
124118 if (data.contains (field)) {
125119 return true ;
126120 }
@@ -135,10 +129,9 @@ inline void erase_reader(jllama_context *jctx, int id_task) {
135129// Reads a Java int array into a std::vector<int32_t> and releases the JNI
136130// array elements with JNI_ABORT (read-only — no writeback needed).
137131// ---------------------------------------------------------------------------
138- [[nodiscard]] inline std::vector<int32_t > jint_array_to_tokens_impl (
139- JNIEnv *env, jintArray array) {
132+ [[nodiscard]] inline std::vector<int32_t > jint_array_to_tokens_impl (JNIEnv *env, jintArray array) {
140133 const jsize length = env->GetArrayLength (array);
141- jint *elements = env->GetIntArrayElements (array, nullptr );
134+ jint *elements = env->GetIntArrayElements (array, nullptr );
142135 std::vector<int32_t > tokens (elements, elements + length);
143136 env->ReleaseIntArrayElements (array, elements, JNI_ABORT );
144137 return tokens;
@@ -170,9 +163,7 @@ inline void erase_reader(jllama_context *jctx, int id_task) {
170163// construction to results_to_json (json_helpers.hpp) and serialisation to
171164// json_to_jstring_impl.
172165// ---------------------------------------------------------------------------
173- [[nodiscard]] inline jstring results_to_jstring_impl (
174- JNIEnv *env,
175- const std::vector<server_task_result_ptr> &results) {
166+ [[nodiscard]] inline jstring results_to_jstring_impl (JNIEnv *env, const std::vector<server_task_result_ptr> &results) {
176167 return json_to_jstring_impl (env, results_to_json (results));
177168}
178169
@@ -184,13 +175,9 @@ inline void erase_reader(jllama_context *jctx, int id_task) {
184175// On allocation failure: throws via JNI with oom_class and returns nullptr.
185176// ---------------------------------------------------------------------------
186177template <typename JArray, typename JElem, typename CppElem>
187- [[nodiscard]] inline JArray vec_to_jarray_impl (
188- JNIEnv *env,
189- const std::vector<CppElem> &values,
190- jclass oom_class,
191- const char *oom_msg,
192- JArray (JNIEnv_::*alloc)(jsize),
193- void (JNIEnv_::*copy)(JArray, jsize, jsize, const JElem *)) {
178+ [[nodiscard]] inline JArray vec_to_jarray_impl (JNIEnv *env, const std::vector<CppElem> &values, jclass oom_class,
179+ const char *oom_msg, JArray (JNIEnv_::*alloc)(jsize),
180+ void (JNIEnv_::*copy)(JArray, jsize, jsize, const JElem *)) {
194181 const jsize len = static_cast <jsize>(values.size ());
195182 JArray arr = (env->*alloc)(len);
196183 if (arr == nullptr ) {
@@ -202,21 +189,15 @@ template <typename JArray, typename JElem, typename CppElem>
202189}
203190
204191// Converts a float vector to a Java jfloatArray.
205- [[nodiscard]] inline jfloatArray embedding_to_jfloat_array_impl (
206- JNIEnv *env,
207- const std::vector<float > &values,
208- jclass oom_class) {
209- return vec_to_jarray_impl<jfloatArray, jfloat>(
210- env, values, oom_class, " could not allocate embedding" ,
211- &JNIEnv_::NewFloatArray, &JNIEnv_::SetFloatArrayRegion);
192+ [[nodiscard]] inline jfloatArray embedding_to_jfloat_array_impl (JNIEnv *env, const std::vector<float > &values,
193+ jclass oom_class) {
194+ return vec_to_jarray_impl<jfloatArray, jfloat>(env, values, oom_class, " could not allocate embedding" ,
195+ &JNIEnv_::NewFloatArray, &JNIEnv_::SetFloatArrayRegion);
212196}
213197
214198// Converts a token vector to a Java jintArray.
215- [[nodiscard]] inline jintArray tokens_to_jint_array_impl (
216- JNIEnv *env,
217- const std::vector<int32_t > &tokens,
218- jclass oom_class) {
219- return vec_to_jarray_impl<jintArray, jint>(
220- env, tokens, oom_class, " could not allocate token memory" ,
221- &JNIEnv_::NewIntArray, &JNIEnv_::SetIntArrayRegion);
199+ [[nodiscard]] inline jintArray tokens_to_jint_array_impl (JNIEnv *env, const std::vector<int32_t > &tokens,
200+ jclass oom_class) {
201+ return vec_to_jarray_impl<jintArray, jint>(env, tokens, oom_class, " could not allocate token memory" ,
202+ &JNIEnv_::NewIntArray, &JNIEnv_::SetIntArrayRegion);
222203}
0 commit comments