Update structure for partials and examples
This commit is contained in:
parent
b1b3d6a960
commit
531ddf0721
89 changed files with 79 additions and 79 deletions
32
modules/ROOT/examples/Java-JNI-Pointers.adoc
Normal file
32
modules/ROOT/examples/Java-JNI-Pointers.adoc
Normal file
|
@ -0,0 +1,32 @@
|
|||
|
||||
JNIEXPORT jint JNICALL Java_sum
|
||||
(JNIEnv *jEnv, jclass clazz, jbyteArray buffer, jint offset, jint length)
|
||||
{
|
||||
assert(sizeof(jint) == sizeof(unsigned));
|
||||
if (offset < 0 || length < 0) {
|
||||
(*jEnv)->ThrowNew(jEnv, arrayIndexOutOfBoundsExceptionClass,
|
||||
"negative offset/length");
|
||||
return 0;
|
||||
}
|
||||
unsigned uoffset = offset;
|
||||
unsigned ulength = length;
|
||||
// This cannot overflow because of the check above.
|
||||
unsigned totallength = uoffset + ulength;
|
||||
unsigned actuallength = (*jEnv)->GetArrayLength(jEnv, buffer);
|
||||
if (totallength > actuallength) {
|
||||
(*jEnv)->ThrowNew(jEnv, arrayIndexOutOfBoundsExceptionClass,
|
||||
"offset + length too large");
|
||||
return 0;
|
||||
}
|
||||
unsigned char *ptr = (*jEnv)->GetPrimitiveArrayCritical(jEnv, buffer, 0);
|
||||
if (ptr == NULL) {
|
||||
return 0;
|
||||
}
|
||||
unsigned long long sum = 0;
|
||||
for (unsigned char *p = ptr + uoffset, *end = p + ulength; p != end; ++p) {
|
||||
sum += *p;
|
||||
}
|
||||
(*jEnv)->ReleasePrimitiveArrayCritical(jEnv, buffer, ptr, 0);
|
||||
return sum;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue