| 1 |
/* |
| 2 |
* Copyright (C) 2009 Wayne Meissner |
| 3 |
* |
| 4 |
* This file is part of jffi. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
* |
| 18 |
* |
| 19 |
* Alternatively, you can redistribute it and/or modify it under |
| 20 |
* the terms of the GNU Lesser General Public License as published by |
| 21 |
* the Free Software Foundation, either version 3 of the License, or |
| 22 |
* (at your option) any later version. |
| 23 |
* |
| 24 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 26 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 27 |
* for more details. |
| 28 |
* |
| 29 |
* You should have received a copy of the GNU Lesser General Public License |
| 30 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <sys/param.h> |
| 34 |
#include <sys/types.h> |
| 35 |
|
| 36 |
#include <stdlib.h> |
| 37 |
#if defined(__sun) || defined(_AIX) |
| 38 |
# include <alloca.h> |
| 39 |
#endif |
| 40 |
#ifdef _WIN32 |
| 41 |
# include <malloc.h> |
| 42 |
#endif |
| 43 |
#include <ffi.h> |
| 44 |
#include <jni.h> |
| 45 |
#include "com_kenai_jffi_Foreign.h" |
| 46 |
#include "jffi.h" |
| 47 |
#include "Exception.h" |
| 48 |
|
| 49 |
#ifndef MAX |
| 50 |
# define MAX(x,y) ((x) > (y) ? (x) : (y)) |
| 51 |
#endif |
| 52 |
#define FFI_ALIGN(v, a) (((((size_t) (v))-1) | ((a)-1))+1) |
| 53 |
|
| 54 |
/* |
| 55 |
* Class: com_kenai_jffi_Foreign |
| 56 |
* Method: newStruct |
| 57 |
* Signature: ([J)J |
| 58 |
*/ |
| 59 |
JNIEXPORT jlong JNICALL |
| 60 |
Java_com_kenai_jffi_Foreign_newStruct(JNIEnv* env, jobject self, jlongArray typeArray, jboolean isUnion) |
| 61 |
{ |
| 62 |
ffi_type* s = NULL; |
| 63 |
int fieldCount; |
| 64 |
jlong* fieldTypes; |
| 65 |
int i; |
| 66 |
|
| 67 |
if (typeArray == NULL) { |
| 68 |
throwException(env, NullPointer, "types array cannot be null"); |
| 69 |
return 0L; |
| 70 |
} |
| 71 |
|
| 72 |
fieldCount = (*env)->GetArrayLength(env, typeArray); |
| 73 |
if (fieldCount < 1) { |
| 74 |
throwException(env, IllegalArgument, "No fields specified"); |
| 75 |
return 0L; |
| 76 |
} |
| 77 |
|
| 78 |
s = calloc(1, sizeof(*s)); |
| 79 |
if (s == NULL) { |
| 80 |
throwException(env, OutOfMemory, "failed to allocate memory"); |
| 81 |
return 0L; |
| 82 |
} |
| 83 |
|
| 84 |
// |
| 85 |
// Need to terminate the list of field types with a NULL, so allocate 1 extra |
| 86 |
// |
| 87 |
s->elements = calloc(fieldCount + 1, sizeof(ffi_type *)); |
| 88 |
if (s->elements == NULL) { |
| 89 |
throwException(env, OutOfMemory, "failed to allocate memory"); |
| 90 |
goto error; |
| 91 |
} |
| 92 |
|
| 93 |
// Copy out all the field descriptors |
| 94 |
fieldTypes = alloca(fieldCount * sizeof(jlong)); |
| 95 |
(*env)->GetLongArrayRegion(env, typeArray, 0, fieldCount, fieldTypes); |
| 96 |
|
| 97 |
s->type = FFI_TYPE_STRUCT; |
| 98 |
s->size = 0; |
| 99 |
s->alignment = 0; |
| 100 |
|
| 101 |
for (i = 0; i < fieldCount; ++i) { |
| 102 |
ffi_type* elem = (ffi_type *) j2p(fieldTypes[i]); |
| 103 |
|
| 104 |
if (elem == NULL) { |
| 105 |
throwException(env, IllegalArgument, "type for field %d is NULL", i); |
| 106 |
goto error; |
| 107 |
} |
| 108 |
if (elem->size == 0) { |
| 109 |
throwException(env, IllegalArgument, "type for field %d has size 0", i); |
| 110 |
goto error; |
| 111 |
} |
| 112 |
|
| 113 |
s->elements[i] = elem; |
| 114 |
if (!isUnion) { |
| 115 |
s->size = FFI_ALIGN(s->size, elem->alignment) + elem->size; |
| 116 |
} else { |
| 117 |
s->size = MAX(s->size, elem->size); |
| 118 |
} |
| 119 |
s->alignment = MAX(s->alignment, elem->alignment); |
| 120 |
} |
| 121 |
if (s->size == 0) { |
| 122 |
throwException(env, Runtime, "struct size is zero"); |
| 123 |
goto error; |
| 124 |
} |
| 125 |
|
| 126 |
// Include tail padding |
| 127 |
s->size = FFI_ALIGN(s->size, s->alignment); |
| 128 |
return p2j(s); |
| 129 |
|
| 130 |
error: |
| 131 |
if (s != NULL) { |
| 132 |
if (s->elements != NULL) { |
| 133 |
free(s->elements); |
| 134 |
} |
| 135 |
free(s); |
| 136 |
} |
| 137 |
|
| 138 |
return 0L; |
| 139 |
} |
| 140 |
|
| 141 |
JNIEXPORT jlong JNICALL |
| 142 |
Java_com_kenai_jffi_Foreign_newArray(JNIEnv* env, jobject self, jlong type, jint length) |
| 143 |
{ |
| 144 |
ffi_type* elem = (ffi_type *) j2p(type); |
| 145 |
ffi_type* s = NULL; |
| 146 |
int i; |
| 147 |
|
| 148 |
if (elem == NULL) { |
| 149 |
throwException(env, NullPointer, "element type cannot be null"); |
| 150 |
return 0L; |
| 151 |
} |
| 152 |
|
| 153 |
if (elem->size == 0) { |
| 154 |
throwException(env, IllegalArgument, "element type size 0"); |
| 155 |
return 0L; |
| 156 |
} |
| 157 |
|
| 158 |
if (length < 1) { |
| 159 |
throwException(env, IllegalArgument, "array length == 0"); |
| 160 |
return 0L; |
| 161 |
} |
| 162 |
|
| 163 |
s = calloc(1, sizeof(*s)); |
| 164 |
if (s == NULL) { |
| 165 |
throwException(env, OutOfMemory, "failed to allocate memory"); |
| 166 |
return 0L; |
| 167 |
} |
| 168 |
|
| 169 |
s->type = FFI_TYPE_STRUCT; |
| 170 |
s->alignment = elem->alignment; |
| 171 |
s->size = length * elem->size; |
| 172 |
|
| 173 |
// Need to terminate the list of field types with a NULL, so allocate 1 extra |
| 174 |
s->elements = calloc(length + 1, sizeof(ffi_type *)); |
| 175 |
if (s->elements == NULL) { |
| 176 |
throwException(env, OutOfMemory, "failed to allocate memory"); |
| 177 |
free(s); |
| 178 |
return 0L; |
| 179 |
} |
| 180 |
|
| 181 |
for (i = 0; i < length; ++i) { |
| 182 |
s->elements[i] = elem; |
| 183 |
} |
| 184 |
|
| 185 |
return p2j(s); |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
* Class: com_kenai_jffi_Foreign |
| 190 |
* Method: freeStruct |
| 191 |
* Signature: (J)V |
| 192 |
*/ |
| 193 |
JNIEXPORT void JNICALL |
| 194 |
Java_com_kenai_jffi_Foreign_freeAggregate(JNIEnv* env, jobject self, jlong handle) |
| 195 |
{ |
| 196 |
ffi_type* s = (ffi_type *) j2p(handle); |
| 197 |
|
| 198 |
if (s != NULL) { |
| 199 |
free(s->elements); |
| 200 |
free(s); |
| 201 |
} |
| 202 |
} |