mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-09 17:28:58 +00:00
Fix some C/C++ warnings in the jni part (#7385)
This commit is contained in:
parent
ab373d6f03
commit
d68cedfa85
3 changed files with 47 additions and 23 deletions
|
|
@ -507,6 +507,7 @@ size_t copyJavaToTensor(JNIEnv *jniEnv, ONNXTensorElementDataType onnxType, uint
|
|||
|
||||
size_t copyPrimitiveArrayToJava(JNIEnv *jniEnv, ONNXTensorElementDataType onnxType, uint8_t* tensor, jarray output) {
|
||||
uint32_t outputLength = (*jniEnv)->GetArrayLength(jniEnv,output);
|
||||
if (outputLength == 0) return 0;
|
||||
size_t consumedSize = outputLength * onnxTypeSize(onnxType);
|
||||
switch (onnxType) {
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8: // maps to c type uint8_t
|
||||
|
|
@ -534,7 +535,11 @@ size_t copyPrimitiveArrayToJava(JNIEnv *jniEnv, ONNXTensorElementDataType onnxTy
|
|||
return consumedSize;
|
||||
}
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16: { // stored as a uint16_t
|
||||
float *floatArr = malloc(sizeof(float) * outputLength);
|
||||
jfloat *floatArr = malloc(sizeof(jfloat) * outputLength);
|
||||
if(floatArr == NULL) {
|
||||
throwOrtException(jniEnv, 1, "Not enough memory");
|
||||
return 0;
|
||||
}
|
||||
uint16_t *halfArr = (uint16_t *) tensor;
|
||||
for (uint32_t i = 0; i < outputLength; i++) {
|
||||
floatArr[i] = convertHalfToFloat(halfArr[i]);
|
||||
|
|
@ -647,6 +652,10 @@ void copyStringTensorToArray(JNIEnv *jniEnv, const OrtApi * api, OrtAllocator* a
|
|||
checkOrtStatus(jniEnv,api,api->AllocatorAlloc(allocator,curSize,(void**)&tempBuffer));
|
||||
bufferSize = curSize;
|
||||
}
|
||||
if (tempBuffer == NULL) {
|
||||
throwOrtException(jniEnv, 1, "Not enough memory");
|
||||
return;
|
||||
}
|
||||
memcpy(tempBuffer,characterBuffer+offsets[i],curSize);
|
||||
tempBuffer[curSize-1] = '\0';
|
||||
jobject tempString = (*jniEnv)->NewStringUTF(jniEnv,tempBuffer);
|
||||
|
|
@ -991,44 +1000,47 @@ jint throwOrtException(JNIEnv *jniEnv, int messageId, const char *message) {
|
|||
jint convertErrorCode(OrtErrorCode code) {
|
||||
switch (code) {
|
||||
case ORT_OK:
|
||||
return 0;
|
||||
return 0;
|
||||
case ORT_FAIL:
|
||||
return 1;
|
||||
return 1;
|
||||
case ORT_INVALID_ARGUMENT:
|
||||
return 2;
|
||||
return 2;
|
||||
case ORT_NO_SUCHFILE:
|
||||
return 3;
|
||||
return 3;
|
||||
case ORT_NO_MODEL:
|
||||
return 4;
|
||||
return 4;
|
||||
case ORT_ENGINE_ERROR:
|
||||
return 5;
|
||||
return 5;
|
||||
case ORT_RUNTIME_EXCEPTION:
|
||||
return 6;
|
||||
return 6;
|
||||
case ORT_INVALID_PROTOBUF:
|
||||
return 7;
|
||||
return 7;
|
||||
case ORT_MODEL_LOADED:
|
||||
return 8;
|
||||
return 8;
|
||||
case ORT_NOT_IMPLEMENTED:
|
||||
return 9;
|
||||
return 9;
|
||||
case ORT_INVALID_GRAPH:
|
||||
return 10;
|
||||
return 10;
|
||||
case ORT_EP_FAIL:
|
||||
return 11;
|
||||
return 11;
|
||||
default:
|
||||
return -1; // Unknown error code
|
||||
}
|
||||
}
|
||||
|
||||
void checkOrtStatus(JNIEnv *jniEnv, const OrtApi * api, OrtStatus * status) {
|
||||
if (status != NULL) {
|
||||
const char* message = api->GetErrorMessage(status);
|
||||
size_t len = strlen(message)+1;
|
||||
char* copy = malloc(sizeof(char)*len);
|
||||
memcpy(copy,message,len);
|
||||
int messageId = convertErrorCode(api->GetErrorCode(status));
|
||||
api->ReleaseStatus(status);
|
||||
throwOrtException(jniEnv,messageId,copy);
|
||||
if (status == NULL) return;
|
||||
const char* message = api->GetErrorMessage(status);
|
||||
size_t len = strlen(message)+1;
|
||||
char* copy = malloc(sizeof(char)*len);
|
||||
if (copy == NULL) {
|
||||
throwOrtException(jniEnv, 1, "Not enough memory");
|
||||
return;
|
||||
}
|
||||
memcpy(copy,message,len);
|
||||
int messageId = convertErrorCode(api->GetErrorCode(status));
|
||||
api->ReleaseStatus(status);
|
||||
throwOrtException(jniEnv,messageId,copy);
|
||||
}
|
||||
|
||||
jsize safecast_size_t_to_jsize(size_t v) {
|
||||
|
|
|
|||
|
|
@ -22,9 +22,13 @@ JNIEXPORT jlong JNICALL Java_ai_onnxruntime_OrtSession_createSession__JJLjava_la
|
|||
#ifdef _WIN32
|
||||
const jchar* cPath = (*jniEnv)->GetStringChars(jniEnv, modelPath, NULL);
|
||||
size_t stringLength = (*jniEnv)->GetStringLength(jniEnv, modelPath);
|
||||
wchar_t* newString = (wchar_t*)calloc(stringLength+1,sizeof(jchar));
|
||||
wchar_t* newString = (wchar_t*)calloc(stringLength + 1, sizeof(wchar_t));
|
||||
if(newString == NULL) {
|
||||
throwOrtException(jniEnv, 1, "Not enough memory");
|
||||
return 0;
|
||||
}
|
||||
wcsncpy_s(newString, stringLength+1, (const wchar_t*) cPath, stringLength);
|
||||
checkOrtStatus(jniEnv,api,api->CreateSession((OrtEnv*)envHandle, (const wchar_t*)newString, (OrtSessionOptions*)optsHandle, &session));
|
||||
checkOrtStatus(jniEnv,api,api->CreateSession((OrtEnv*)envHandle, newString, (OrtSessionOptions*)optsHandle, &session));
|
||||
free(newString);
|
||||
(*jniEnv)->ReleaseStringChars(jniEnv,modelPath,cPath);
|
||||
#else
|
||||
|
|
|
|||
|
|
@ -92,6 +92,10 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024SessionOptions_setOpt
|
|||
const jchar* path = (*jniEnv)->GetStringChars(jniEnv, pathString, NULL);
|
||||
size_t stringLength = (*jniEnv)->GetStringLength(jniEnv, pathString);
|
||||
wchar_t* newString = (wchar_t*)calloc(stringLength+1,sizeof(jchar));
|
||||
if (newString == NULL) {
|
||||
throwOrtException(jniEnv, 1, "Not enough memory");
|
||||
return;
|
||||
}
|
||||
wcsncpy_s(newString, stringLength+1, (const wchar_t*) path, stringLength);
|
||||
checkOrtStatus(jniEnv,(const OrtApi*)apiHandle,api->SetOptimizedModelFilePath((OrtSessionOptions*) handle, (const wchar_t*) newString));
|
||||
free(newString);
|
||||
|
|
@ -162,6 +166,10 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024SessionOptions_enable
|
|||
const jchar* path = (*jniEnv)->GetStringChars(jniEnv, pathString, NULL);
|
||||
size_t stringLength = (*jniEnv)->GetStringLength(jniEnv, pathString);
|
||||
wchar_t* newString = (wchar_t*)calloc(stringLength+1,sizeof(jchar));
|
||||
if (newString == NULL) {
|
||||
throwOrtException(jniEnv, 1, "Not enough memory");
|
||||
return;
|
||||
}
|
||||
wcsncpy_s(newString, stringLength+1, (const wchar_t*) path, stringLength);
|
||||
checkOrtStatus(jniEnv,(const OrtApi*)apiHandle,api->EnableProfiling(options, (const wchar_t*) newString));
|
||||
free(newString);
|
||||
|
|
|
|||
Loading…
Reference in a new issue