blob: ce9fe6377707c88bcece9fc69dafd19e845806b3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 */
28
29#include "jni.h"
30#include "jni_util.h"
31#include "jlong.h"
32#include <string.h>
33
34/*
35 * WARNING:
36 *
37 * Do not replace instances of:
38 *
39 * if (length > MBYTE)
40 * size = MBYTE;
41 * else
42 * size = length;
43 *
44 * with
45 *
46 * size = (length > MBYTE ? MBYTE : length);
47 *
48 * This expression causes a c compiler assertion failure when compiling on
49 * 32-bit sparc.
50 */
51
52#define MBYTE 1048576
53
54#define GETCRITICAL(bytes, env, obj) { \
55 bytes = (*env)->GetPrimitiveArrayCritical(env, obj, NULL); \
56 if (bytes == NULL) \
57 JNU_ThrowInternalError(env, "Unable to get array"); \
58}
59
60#define RELEASECRITICAL(bytes, env, obj, mode) { \
61 (*env)->ReleasePrimitiveArrayCritical(env, obj, bytes, mode); \
62}
63
64#define SWAPSHORT(x) ((jshort)(((x) << 8) | (((x) >> 8) & 0xff)))
65#define SWAPINT(x) ((jint)((SWAPSHORT((jshort)(x)) << 16) | \
66 (SWAPSHORT((jshort)((x) >> 16)) & 0xffff)))
67#define SWAPLONG(x) ((jlong)(((jlong)SWAPINT((jint)(x)) << 32) | \
68 ((jlong)SWAPINT((jint)((x) >> 32)) & 0xffffffff)))
69
70JNIEXPORT void JNICALL
71Java_java_nio_Bits_copyFromByteArray(JNIEnv *env, jobject this, jobject src,
72 jlong srcPos, jlong dstAddr, jlong length)
73{
74 jbyte *bytes;
75 size_t size;
76
77 while (length > 0) {
78 size = (length > MBYTE ? MBYTE : length);
79
80 GETCRITICAL(bytes, env, src);
81 memcpy((void *)dstAddr, bytes + srcPos, size);
82 RELEASECRITICAL(bytes, env, src, JNI_ABORT);
83
84 length -= size;
85 dstAddr += size;
86 srcPos += size;
87 }
88}
89
90JNIEXPORT void JNICALL
91Java_java_nio_Bits_copyToByteArray(JNIEnv *env, jobject this, jlong srcAddr,
92 jobject dst, jlong dstPos, jlong length)
93{
94 jbyte *bytes;
95 size_t size;
96
97 while (length > 0) {
98 size = (length > MBYTE ? MBYTE : length);
99
100 GETCRITICAL(bytes, env, dst);
101 memcpy(bytes + dstPos, (void *)srcAddr, size);
102 RELEASECRITICAL(bytes, env, dst, 0);
103
104 length -= size;
105 srcAddr += size;
106 dstPos += size;
107 }
108}
109
110JNIEXPORT void JNICALL
111Java_java_nio_Bits_copyFromShortArray(JNIEnv *env, jobject this, jobject src,
112 jlong srcPos, jlong dstAddr, jlong length)
113{
114 jbyte *bytes;
115 size_t i, size;
116 jshort *srcShort, *dstShort, *endShort;
117 jshort tmpShort;
118
119 dstShort = (jshort *)dstAddr;
120
121 while (length > 0) {
122 /* do not change this if-else statement, see WARNING above */
123 if (length > MBYTE)
124 size = MBYTE;
125 else
126 size = length;
127
128 GETCRITICAL(bytes, env, src);
129
130 srcShort = (jshort *)(bytes + srcPos);
131 endShort = srcShort + (size / sizeof(jshort));
132 while (srcShort < endShort) {
133 tmpShort = *srcShort++;
134 *dstShort++ = SWAPSHORT(tmpShort);
135 }
136
137 RELEASECRITICAL(bytes, env, src, JNI_ABORT);
138
139 length -= size;
140 dstAddr += size;
141 srcPos += size;
142 }
143}
144
145JNIEXPORT void JNICALL
146Java_java_nio_Bits_copyToShortArray(JNIEnv *env, jobject this, jlong srcAddr,
147 jobject dst, jlong dstPos, jlong length)
148{
149 jbyte *bytes;
150 size_t i, size;
151 jshort *srcShort, *dstShort, *endShort;
152 jshort tmpShort;
153
154 srcShort = (jshort *)srcAddr;
155
156 while (length > 0) {
157 /* do not change this if-else statement, see WARNING above */
158 if (length > MBYTE)
159 size = MBYTE;
160 else
161 size = length;
162
163 GETCRITICAL(bytes, env, dst);
164
165 dstShort = (jshort *)(bytes + dstPos);
166 endShort = srcShort + (size / sizeof(jshort));
167 while (srcShort < endShort) {
168 tmpShort = *srcShort++;
169 *dstShort++ = SWAPSHORT(tmpShort);
170 }
171
172 RELEASECRITICAL(bytes, env, dst, 0);
173
174 length -= size;
175 srcAddr += size;
176 dstPos += size;
177 }
178}
179
180JNIEXPORT void JNICALL
181Java_java_nio_Bits_copyFromIntArray(JNIEnv *env, jobject this, jobject src,
182 jlong srcPos, jlong dstAddr, jlong length)
183{
184 jbyte *bytes;
185 size_t i, size;
186 jint *srcInt, *dstInt, *endInt;
187 jint tmpInt;
188
189 dstInt = (jint *)dstAddr;
190
191 while (length > 0) {
192 /* do not change this code, see WARNING above */
193 if (length > MBYTE)
194 size = MBYTE;
195 else
196 size = length;
197
198 GETCRITICAL(bytes, env, src);
199
200 srcInt = (jint *)(bytes + srcPos);
201 endInt = srcInt + (size / sizeof(jint));
202 while (srcInt < endInt) {
203 tmpInt = *srcInt++;
204 *dstInt++ = SWAPINT(tmpInt);
205 }
206
207 RELEASECRITICAL(bytes, env, src, JNI_ABORT);
208
209 length -= size;
210 dstAddr += size;
211 srcPos += size;
212 }
213}
214
215JNIEXPORT void JNICALL
216Java_java_nio_Bits_copyToIntArray(JNIEnv *env, jobject this, jlong srcAddr,
217 jobject dst, jlong dstPos, jlong length)
218{
219 jbyte *bytes;
220 size_t i, size;
221 jint *srcInt, *dstInt, *endInt;
222 jint tmpInt;
223
224 srcInt = (jint *)srcAddr;
225
226 while (length > 0) {
227 /* do not change this code, see WARNING above */
228 if (length > MBYTE)
229 size = MBYTE;
230 else
231 size = length;
232
233 GETCRITICAL(bytes, env, dst);
234
235 dstInt = (jint *)(bytes + dstPos);
236 endInt = srcInt + (size / sizeof(jint));
237 while (srcInt < endInt) {
238 tmpInt = *srcInt++;
239 *dstInt++ = SWAPINT(tmpInt);
240 }
241
242 RELEASECRITICAL(bytes, env, dst, 0);
243
244 length -= size;
245 srcAddr += size;
246 dstPos += size;
247 }
248}
249
250JNIEXPORT void JNICALL
251Java_java_nio_Bits_copyFromLongArray(JNIEnv *env, jobject this, jobject src,
252 jlong srcPos, jlong dstAddr, jlong length)
253{
254 jbyte *bytes;
255 size_t i, size;
256 jlong *srcLong, *dstLong, *endLong;
257 jlong tmpLong;
258
259 dstLong = (jlong *)dstAddr;
260
261 while (length > 0) {
262 /* do not change this code, see WARNING above */
263 if (length > MBYTE)
264 size = MBYTE;
265 else
266 size = length;
267
268 GETCRITICAL(bytes, env, src);
269
270 srcLong = (jlong *)(bytes + srcPos);
271 endLong = srcLong + (size / sizeof(jlong));
272 while (srcLong < endLong) {
273 tmpLong = *srcLong++;
274 *dstLong++ = SWAPLONG(tmpLong);
275 }
276
277 RELEASECRITICAL(bytes, env, src, JNI_ABORT);
278
279 length -= size;
280 dstAddr += size;
281 srcPos += size;
282 }
283}
284
285JNIEXPORT void JNICALL
286Java_java_nio_Bits_copyToLongArray(JNIEnv *env, jobject this, jlong srcAddr,
287 jobject dst, jlong dstPos, jlong length)
288{
289 jbyte *bytes;
290 size_t i, size;
291 jlong *srcLong, *dstLong, *endLong;
292 jlong tmpLong;
293
294 srcLong = (jlong *)srcAddr;
295
296 while (length > 0) {
297 /* do not change this code, see WARNING above */
298 if (length > MBYTE)
299 size = MBYTE;
300 else
301 size = length;
302
303 GETCRITICAL(bytes, env, dst);
304
305 dstLong = (jlong *)(bytes + dstPos);
306 endLong = srcLong + (size / sizeof(jlong));
307 while (srcLong < endLong) {
308 tmpLong = *srcLong++;
309 *dstLong++ = SWAPLONG(tmpLong);
310 }
311
312 RELEASECRITICAL(bytes, env, dst, 0);
313
314 length -= size;
315 srcAddr += size;
316 dstPos += size;
317 }
318}