xref: /illumos-gate/usr/src/cmd/pools/poold/libjpool/jpool.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <errno.h>
28 #include <jni.h>
29 #include <pool.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include <sys/time.h>
34 
35 #include "jpool.h"
36 
37 struct pool_callback {
38 	jobject	pc_user;
39 	jobject	pc_handler;
40 	jobject	pc_elem;
41 	JNIEnv	*pc_env;
42 };
43 
44 static void throwException(JNIEnv *, const char *, const char *);
45 static void throw_pe(JNIEnv *);
46 static jobject makeUnsignedInt64(JNIEnv *, uint64_t);
47 static int pool_property_walker(pool_conf_t *, pool_elem_t *p, const char *,
48     pool_value_t *, void *);
49 static jobject copyArray(JNIEnv *, void **);
50 
51 /*
52  * Cached class, method, and field IDs.
53  */
54 static jclass ui64class;
55 static jmethodID ui64cons_mid;
56 
57 /*
58  * Throw an exception of the specified class with the specified message.
59  */
60 void
61 throwException(JNIEnv *env, const char *class, const char *msg)
62 {
63 	jclass clazz;
64 
65 	clazz = (*env)->FindClass(env, class);
66 
67 	(*env)->ThrowNew(env, clazz, msg);
68 }
69 
70 /*
71  * Throw a PoolsException.
72  */
73 void
74 throw_pe(JNIEnv *jenv)
75 {
76 	jclass clazz;
77 	jmethodID mid;
78 	jthrowable throwObj;
79 
80 	clazz = (*jenv)->FindClass(jenv,
81 	    "com/sun/solaris/service/pools/PoolsException");
82 	mid = (*jenv)->GetMethodID(jenv, clazz, "<init>", "()V");
83 	throwObj = (*jenv)->NewObject(jenv, clazz, mid);
84 	(*jenv)->Throw(jenv, throwObj);
85 }
86 
87 /*
88  * Return an instance of an UnsignedInt64 class which encapsulates the
89  * supplied value.
90  */
91 jobject
92 makeUnsignedInt64(JNIEnv *env, uint64_t value)
93 {
94 	jobject valueObj;
95 	jobject byteArray;
96 	jbyte *bytes;
97 	int i;
98 
99 	if (!(byteArray = (*env)->NewByteArray(env, 9)))
100 		return (NULL); /* OutOfMemoryError thrown */
101 	if (!(bytes = (*env)->GetByteArrayElements(env, byteArray, NULL)))
102 		return (NULL); /* OutOfMemoryError thrown */
103 
104 	/*
105 	 * Interpret the uint64_t as a 9-byte big-endian signed quantity
106 	 * suitable for constructing an UnsignedInt64 or BigInteger.
107 	 */
108 	for (i = 8; i >= 1; i--) {
109 		bytes[i] = value & 0xff;
110 		value >>= 8;
111 	}
112 	bytes[0] = 0;
113 	(*env)->ReleaseByteArrayElements(env, byteArray, bytes, 0);
114 
115 	if (!(valueObj = (*env)->NewObject(env, ui64class, ui64cons_mid,
116 	    byteArray)))
117 		return (NULL); /* exception thrown */
118 
119 	return (valueObj);
120 }
121 
122 /*
123  * Create an array list and then copy the native array into it
124  */
125 jobject
126 copyArray(JNIEnv *jenv, void **nativeArray)
127 {
128 	int i;
129 	jobject jresult = NULL;
130 
131 	if (nativeArray != NULL) {
132 		jclass ALclazz;
133 		jmethodID ALinit, ALadd;
134 		jclass Lclazz;
135 		jmethodID Linit;
136 
137 		ALclazz = (*jenv)->FindClass(jenv,
138 		    "java/util/ArrayList");
139 		ALinit = (*jenv)->GetMethodID(jenv,
140 		    ALclazz, "<init>", "()V");
141 		ALadd = (*jenv)->GetMethodID(jenv,
142 		    ALclazz, "add", "(Ljava/lang/Object;)Z");
143 		jresult = (*jenv)->NewObject(jenv, ALclazz, ALinit);
144 		Lclazz = (*jenv)->FindClass(jenv, "java/lang/Long");
145 		Linit = (*jenv)->GetMethodID(jenv,
146 		    Lclazz, "<init>", "(J)V");
147 		for (i = 0; nativeArray[i] != NULL; i++) {
148 			jobject L;
149 			/* Build longs and add them */
150 			L = (*jenv)->NewObject(jenv,
151 			    Lclazz, Linit, (jlong)(uintptr_t)nativeArray[i]);
152 			(*jenv)->CallBooleanMethod(jenv,
153 			    jresult, ALadd, L);
154 		}
155 		free(nativeArray);
156 	}
157 	return (jresult);
158 }
159 
160 /*
161  * pool_version(3pool) wrapper
162  */
163 /*ARGSUSED*/
164 JNIEXPORT jlong JNICALL
165 Java_com_sun_solaris_service_pools_PoolInternal_pool_1version(JNIEnv *jenv,
166     jclass jcls, jlong jver)
167 {
168 	return ((jlong)pool_version((uint_t)jver));
169 }
170 
171 /*
172  * native constant accessor
173  */
174 /*ARGSUSED*/
175 JNIEXPORT jint JNICALL
176 Java_com_sun_solaris_service_pools_PoolInternal_get_1POX_1NATIVE(JNIEnv *jenv,
177     jclass jcls)
178 {
179 	return ((jint)POX_NATIVE);
180 }
181 
182 /*
183  * native constant accessor
184  */
185 /*ARGSUSED*/
186 JNIEXPORT jint JNICALL
187 Java_com_sun_solaris_service_pools_PoolInternal_get_1POX_1TEXT(JNIEnv *jenv,
188     jclass jcls)
189 {
190 	return ((jint)POX_TEXT);
191 }
192 
193 /*
194  * native constant accessor
195  */
196 /*ARGSUSED*/
197 JNIEXPORT jint JNICALL
198 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1INVAL(JNIEnv *jenv,
199     jclass jcls)
200 {
201 	return ((jint)POC_INVAL);
202 }
203 
204 /*
205  * native constant accessor
206  */
207 /*ARGSUSED*/
208 JNIEXPORT jint JNICALL
209 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1UINT(JNIEnv *jenv,
210     jclass jcls)
211 {
212 	return ((jint)POC_UINT);
213 }
214 
215 /*
216  * native constant accessor
217  */
218 /*ARGSUSED*/
219 JNIEXPORT jint JNICALL
220 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1INT(JNIEnv *jenv,
221     jclass jcls)
222 {
223 	return ((jint)POC_INT);
224 }
225 
226 /*
227  * native constant accessor
228  */
229 /*ARGSUSED*/
230 JNIEXPORT jint JNICALL
231 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1DOUBLE(JNIEnv *jenv,
232     jclass jcls)
233 {
234 	return ((jint)POC_DOUBLE);
235 }
236 
237 /*
238  * native constant accessor
239  */
240 /*ARGSUSED*/
241 JNIEXPORT jint JNICALL
242 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1BOOL(JNIEnv *jenv,
243     jclass jcls)
244 {
245 	return ((jint)POC_BOOL);
246 }
247 
248 /*
249  * native constant accessor
250  */
251 /*ARGSUSED*/
252 JNIEXPORT jint JNICALL
253 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1STRING(JNIEnv *jenv,
254     jclass jcls)
255 {
256 	return ((jint)POC_STRING);
257 }
258 
259 /*
260  * native constant accessor
261  */
262 /*ARGSUSED*/
263 JNIEXPORT jint JNICALL
264 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1NONE(JNIEnv *jenv,
265     jclass jcls)
266 {
267 	return ((jint)POV_NONE);
268 }
269 
270 /*
271  * native constant accessor
272  */
273 /*ARGSUSED*/
274 JNIEXPORT jint JNICALL
275 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1LOOSE(JNIEnv *jenv,
276     jclass jcls)
277 {
278 	return ((jint)POV_LOOSE);
279 }
280 
281 /*
282  * native constant accessor
283  */
284 /*ARGSUSED*/
285 JNIEXPORT jint JNICALL
286 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1STRICT(JNIEnv *jenv,
287     jclass jcls)
288 {
289 	return ((jint)POV_STRICT);
290 }
291 
292 /*
293  * native constant accessor
294  */
295 /*ARGSUSED*/
296 JNIEXPORT jint JNICALL
297 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1RUNTIME(JNIEnv *jenv,
298     jclass jcls)
299 {
300 	return ((jint)POV_RUNTIME);
301 }
302 
303 /*
304  * native constant accessor
305  */
306 /*ARGSUSED*/
307 JNIEXPORT jint JNICALL
308 Java_com_sun_solaris_service_pools_PoolInternal_get_1POF_1INVALID(JNIEnv *jenv,
309     jclass jcls)
310 {
311 	return ((jint)POF_INVALID);
312 }
313 
314 /*
315  * native constant accessor
316  */
317 /*ARGSUSED*/
318 JNIEXPORT jint JNICALL
319 Java_com_sun_solaris_service_pools_PoolInternal_get_1POF_1VALID(JNIEnv *jenv,
320     jclass jcls)
321 {
322 	return ((jint)POF_VALID);
323 }
324 
325 /*
326  * native constant accessor
327  */
328 /*ARGSUSED*/
329 JNIEXPORT jint JNICALL
330 Java_com_sun_solaris_service_pools_PoolInternal_get_1POF_1DESTROY(JNIEnv *jenv,
331     jclass jcls)
332 {
333 	return ((jint)POF_DESTROY);
334 }
335 
336 /*
337  * pool_error(3pool) wrapper
338  */
339 /*ARGSUSED*/
340 JNIEXPORT jint JNICALL
341 Java_com_sun_solaris_service_pools_PoolInternal_pool_1error(JNIEnv *jenv,
342     jclass jcls)
343 {
344 	return ((jint)pool_error());
345 }
346 
347 /*
348  * pool_strerror(3pool) wrapper
349  */
350 /*ARGSUSED*/
351 JNIEXPORT jstring JNICALL
352 Java_com_sun_solaris_service_pools_PoolInternal_pool_1strerror(JNIEnv *jenv,
353     jclass jcls, jint jperr)
354 {
355 	jstring jresult = NULL;
356 	char *result;
357 
358 	result = (char *)pool_strerror((int)jperr);
359 
360 	if (result)
361 		jresult = (*jenv)->NewStringUTF(jenv, result);
362 	return (jresult);
363 }
364 
365 /*
366  * strerror(3c) wrapper
367  */
368 /*ARGSUSED*/
369 JNIEXPORT jstring JNICALL
370 Java_com_sun_solaris_service_pools_PoolInternal_pool_1strerror_1sys(JNIEnv *
371     jenv, jclass jcls)
372 {
373 	jstring jresult = NULL;
374 	char *result;
375 
376 	result = (char *)strerror(errno);
377 
378 	if (result)
379 		jresult = (*jenv)->NewStringUTF(jenv, result);
380 	return (jresult);
381 }
382 
383 /*
384  * errno(3c) accessor
385  */
386 /*ARGSUSED*/
387 JNIEXPORT jint JNICALL
388 Java_com_sun_solaris_service_pools_PoolsException_getErrno(JNIEnv *jenv,
389     jclass jcls)
390 {
391 	return ((jint)errno);
392 }
393 
394 /*
395  * pool_resource_type_list(3pool) wrapper
396  */
397 /*ARGSUSED*/
398 JNIEXPORT jint JNICALL
399 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1type_1list(
400     JNIEnv *jenv, jclass jcls, jlong jreslist, jlong jnumres)
401 {
402 	char **reslist = (char **)(uintptr_t)jreslist;
403 	uint_t *numres = (uint_t *)(uintptr_t)jnumres;
404 
405 	return ((jint)pool_resource_type_list((char const **)reslist, numres));
406 }
407 
408 /*
409  * pool_get_status(3pool) wrapper
410  */
411 /*ARGSUSED*/
412 JNIEXPORT jint JNICALL
413 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1status(JNIEnv *jenv,
414     jclass jcls)
415 {
416 	int status;
417 	int err;
418 
419 	err = pool_get_status(&status);
420 	if (err == -1)
421 		return ((jint)PO_FAIL);
422 	else
423 		return ((jint)status);
424 }
425 
426 /*
427  * pool_set_status(3pool) wrapper
428  */
429 /*ARGSUSED*/
430 JNIEXPORT jint JNICALL
431 Java_com_sun_solaris_service_pools_PoolInternal_pool_1set_1status(JNIEnv *jenv,
432     jclass jcls, jint jstate)
433 {
434 	return ((jint)pool_set_status((int)jstate));
435 }
436 
437 /*
438  * pool_conf_alloc(3pool) wrapper
439  */
440 /*ARGSUSED*/
441 JNIEXPORT jlong JNICALL
442 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1alloc(JNIEnv *jenv,
443     jclass jcls)
444 {
445 	return ((jlong)(uintptr_t)pool_conf_alloc());
446 }
447 
448 /*
449  * pool_conf_free(3pool) wrapper
450  */
451 /*ARGSUSED*/
452 JNIEXPORT void JNICALL
453 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1free(JNIEnv *jenv,
454     jclass jcls, jlong jconf)
455 {
456 	pool_conf_free((pool_conf_t *)(uintptr_t)jconf);
457 }
458 
459 /*
460  * pool_conf_status(3pool) wrapper
461  */
462 /*ARGSUSED*/
463 JNIEXPORT jint JNICALL
464 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1status(JNIEnv *jenv,
465     jclass jcls, jlong jconf)
466 {
467 	return ((jint)pool_conf_status((pool_conf_t *)(uintptr_t)jconf));
468 }
469 
470 /*
471  * pool_conf_close(3pool) wrapper
472  */
473 /*ARGSUSED*/
474 JNIEXPORT jint JNICALL
475 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1close(JNIEnv *jenv,
476     jclass jcls, jlong jconf)
477 {
478 	return ((jint)pool_conf_close((pool_conf_t *)(uintptr_t)jconf));
479 }
480 
481 /*
482  * pool_conf_remove(3pool) wrapper
483  */
484 /*ARGSUSED*/
485 JNIEXPORT jint JNICALL
486 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1remove(JNIEnv *jenv,
487     jclass jcls, jlong jconf)
488 {
489 	return ((jint)pool_conf_remove((pool_conf_t *)(uintptr_t)jconf));
490 }
491 
492 /*
493  * pool_conf_open(3pool) wrapper
494  */
495 /*ARGSUSED*/
496 JNIEXPORT jint JNICALL
497 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1open(JNIEnv *jenv,
498     jclass jcls, jlong jconf, jstring jlocation, jint jflags)
499 {
500 	const char *location;
501 	int result;
502 
503 	location = (jlocation) ? (*jenv)->GetStringUTFChars(jenv,
504 	    jlocation, 0) : NULL;
505 	result = (int)pool_conf_open((pool_conf_t *)(uintptr_t)jconf, location,
506 	    (int)jflags);
507 
508 	if (location)
509 		(*jenv)->ReleaseStringUTFChars(jenv, jlocation, location);
510 	return ((jint)result);
511 }
512 
513 /*
514  * pool_conf_rollback(3pool) wrapper
515  */
516 /*ARGSUSED*/
517 JNIEXPORT jint JNICALL
518 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1rollback(
519     JNIEnv *jenv, jclass jcls, jlong jconf)
520 {
521 	return ((jint)pool_conf_rollback((pool_conf_t *)(uintptr_t)jconf));
522 }
523 
524 /*
525  * pool_conf_commit(3pool) wrapper
526  */
527 /*ARGSUSED*/
528 JNIEXPORT jint JNICALL
529 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1commit(JNIEnv *jenv,
530     jclass jcls, jlong jconf, jint jactive)
531 {
532 	return ((jint)pool_conf_commit(
533 	    (pool_conf_t *)(uintptr_t)jconf, (int)jactive));
534 }
535 
536 /*
537  * pool_conf_export(3pool) wrapper
538  */
539 /*ARGSUSED*/
540 JNIEXPORT jint JNICALL
541 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1export(JNIEnv *jenv,
542     jclass jcls, jlong jconf, jstring jlocation, jint jformat)
543 {
544 	const char *location;
545 	int result;
546 
547 	location = (jlocation) ? (*jenv)->GetStringUTFChars(jenv,
548 	    jlocation, 0) : NULL;
549 	result = (int)pool_conf_export((pool_conf_t *)(uintptr_t)jconf,
550 	    location, (pool_export_format_t)jformat);
551 
552 	if (location)
553 		(*jenv)->ReleaseStringUTFChars(jenv, jlocation, location);
554 	return ((jint)result);
555 }
556 
557 /*
558  * pool_conf_validate(3pool) wrapper
559  */
560 /*ARGSUSED*/
561 JNIEXPORT jint JNICALL
562 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1validate(
563     JNIEnv *jenv, jclass jcls, jlong jconf, jint jlevel)
564 {
565 	return ((jint)pool_conf_validate((pool_conf_t *)(uintptr_t)jconf,
566 	    (pool_valid_level_t)jlevel));
567 }
568 
569 /*
570  * pool_conf_update(3pool) wrapper
571  */
572 /*ARGSUSED*/
573 JNIEXPORT jint JNICALL
574 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1update(JNIEnv *jenv,
575     jclass jcls, jlong jconf)
576 {
577 	int changed;
578 	int result;
579 
580 	result = pool_conf_update((pool_conf_t *)(uintptr_t)jconf, &changed);
581 
582 	if (result != PO_SUCCESS) {
583 		throw_pe(jenv);
584 	}
585 
586 	return ((jint)changed);
587 }
588 
589 /*
590  * pool_get_pool(3pool) wrapper
591  */
592 /*ARGSUSED*/
593 JNIEXPORT jlong JNICALL
594 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1pool(JNIEnv *jenv,
595     jclass jcls, jlong jconf, jstring jname)
596 {
597 	const char *name;
598 	pool_t *result;
599 
600 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
601 	    NULL;
602 	result = (pool_t *)pool_get_pool((pool_conf_t *)(uintptr_t)jconf, name);
603 
604 	if (name)
605 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
606 	return ((jlong)(uintptr_t)result);
607 }
608 
609 /*
610  * pool_query_pools(3pool) wrapper
611  */
612 /*ARGSUSED*/
613 JNIEXPORT jobject JNICALL
614 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1pools(JNIEnv *jenv,
615     jclass jcls, jlong jconf, jobject jprops)
616 {
617 	pool_value_t **props;
618 	pool_t **result;
619 	jclass Lclazz;
620 	jmethodID Lsize;
621 	jint size;
622 	uint_t nelem;
623 	int i;
624 
625 
626 	/*
627 	 * Initialize the target parameter for case when input is null
628 	 */
629 	props = NULL;
630 	if (jprops != NULL) {
631 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
632 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
633 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
634 
635 		if (size != 0) {
636 			jmethodID Lget;
637 
638 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
639 			    "(I)Ljava/lang/Object;");
640 			/*
641 			 * Allocate space for the props array
642 			 */
643 
644 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
645 			    == NULL) {
646 				throwException(jenv, "java/lang/Exception",
647 				    "Could not allocate props array");
648 				return (NULL);
649 			}
650 			/*
651 			 * Copy in the array
652 			 */
653 			for (i = 0; i < size; i++) {
654 				jobject aVal;
655 				jclass Vclazz;
656 				jfieldID Vthis;
657 				jlong this;
658 
659 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
660 				    Lget, (jint) i);
661 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
662 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
663 				    "_this", "J");
664 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
665 				props[i] = (pool_value_t *)(uintptr_t)this;
666 			}
667 		}
668 	}
669 	result = pool_query_pools((pool_conf_t *)(uintptr_t)jconf, &nelem,
670 	    props);
671 	free(props);
672 	return (copyArray(jenv, (void **)result));
673 }
674 
675 /*
676  * pool_get_resource(3pool) wrapper
677  */
678 /*ARGSUSED*/
679 JNIEXPORT jlong JNICALL
680 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1resource(
681     JNIEnv *jenv, jclass jcls, jlong jconf, jstring jtype, jstring jname)
682 {
683 	const char *type;
684 	const char *name;
685 	pool_resource_t *result;
686 
687 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) :
688 	    NULL;
689 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
690 	    NULL;
691 	result = pool_get_resource((pool_conf_t *)(uintptr_t)jconf, type, name);
692 
693 	if (type)
694 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
695 	if (name)
696 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
697 	return ((jlong)(uintptr_t)result);
698 }
699 
700 /*
701  * pool_query_resources(3pool) wrapper
702  */
703 /*ARGSUSED*/
704 JNIEXPORT jobject JNICALL
705 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1resources(
706     JNIEnv *jenv, jclass jcls, jlong jconf, jobject jprops)
707 {
708 	pool_value_t **props;
709 	pool_resource_t **result;
710 	jclass Lclazz;
711 	jmethodID Lsize;
712 	jint size;
713 	uint_t nelem;
714 	int i;
715 
716 	/*
717 	 * Initialize the target parameter for case when input is null
718 	 */
719 	props = NULL;
720 	if (jprops != NULL) {
721 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
722 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
723 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
724 
725 		if (size != 0) {
726 			jmethodID Lget;
727 
728 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
729 			    "(I)Ljava/lang/Object;");
730 			/*
731 			 * Allocate space for the props array
732 			 */
733 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
734 			    == NULL) {
735 				throwException(jenv, "java/lang/Exception",
736 				    "Could not allocate props array");
737 				return (NULL);
738 			}
739 			/*
740 			 * Copy in the array
741 			 */
742 			for (i = 0; i < size; i++) {
743 				jobject aVal;
744 				jclass Vclazz;
745 				jfieldID Vthis;
746 				jlong this;
747 
748 
749 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
750 				    Lget, (jint) i);
751 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
752 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
753 				    "_this", "J");
754 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
755 				props[i] = (pool_value_t *)(uintptr_t)this;
756 			}
757 		}
758 	}
759 	result = pool_query_resources((pool_conf_t *)(uintptr_t)jconf, &nelem,
760 	    props);
761 	free(props);
762 	return (copyArray(jenv, (void *)result));
763 }
764 
765 /*
766  * pool_query_components(3pool) wrapper
767  */
768 /*ARGSUSED*/
769 JNIEXPORT jobject JNICALL
770 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1components(
771     JNIEnv *jenv, jclass jcls, jlong jconf, jobject jprops)
772 {
773 	pool_value_t **props;
774 	pool_component_t **result;
775 	jclass Lclazz;
776 	jmethodID Lsize;
777 	jint size;
778 	uint_t nelem;
779 	int i;
780 
781 	/*
782 	 * Initialize the target parameter for case when input is null
783 	 */
784 	props = NULL;
785 	if (jprops != NULL) {
786 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
787 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
788 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
789 
790 		if (size != 0) {
791 			jmethodID Lget;
792 
793 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
794 			    "(I)Ljava/lang/Object;");
795 			/*
796 			 * Allocate space for the props array
797 			 */
798 
799 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
800 			    == NULL) {
801 				throwException(jenv, "java/lang/Exception",
802 				    "Could not allocate props array");
803 				return (NULL);
804 			}
805 			/*
806 			 * Copy in the array
807 			 */
808 			for (i = 0; i < size; i++) {
809 				jobject aVal;
810 				jclass Vclazz;
811 				jfieldID Vthis;
812 				jlong this;
813 
814 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
815 				    Lget, (jint) i);
816 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
817 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
818 				    "_this", "J");
819 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
820 				props[i] = (pool_value_t *)(uintptr_t)this;
821 			}
822 		}
823 	}
824 	result = pool_query_components((pool_conf_t *)(uintptr_t)jconf, &nelem,
825 	    props);
826 	free(props);
827 	return (copyArray(jenv, (void **)result));
828 }
829 
830 /*
831  * pool_conf_location(3pool) wrapper
832  */
833 /*ARGSUSED*/
834 JNIEXPORT jstring JNICALL
835 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1location(
836     JNIEnv *jenv, jclass jcls, jlong jconf)
837 {
838 	jstring jresult = NULL;
839 	const char *result;
840 
841 	result = pool_conf_location((pool_conf_t *)(uintptr_t)jconf);
842 
843 	if (result)
844 		jresult = (*jenv)->NewStringUTF(jenv, result);
845 	return (jresult);
846 }
847 
848 /*
849  * pool_conf_info(3pool) wrapper
850  */
851 /*ARGSUSED*/
852 JNIEXPORT jstring JNICALL
853 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1info(JNIEnv *jenv,
854     jclass jcls, jlong jconf, jint jflags)
855 {
856 	jstring jresult = NULL;
857 	const char *result;
858 
859 	result = pool_conf_info((pool_conf_t *)(uintptr_t)jconf, (int)jflags);
860 
861 	if (result)
862 		jresult = (*jenv)->NewStringUTF(jenv, result);
863 	free((void *)result);
864 	return (jresult);
865 }
866 
867 /*
868  * pool_resource_create(3pool) wrapper
869  */
870 /*ARGSUSED*/
871 JNIEXPORT jlong JNICALL
872 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1create(
873     JNIEnv *jenv, jclass jcls, jlong jconf, jstring jtype, jstring jname)
874 {
875 	const char *type;
876 	const char *name;
877 	pool_resource_t *result;
878 
879 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) :
880 	    NULL;
881 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
882 	    NULL;
883 	result =
884 	    pool_resource_create((pool_conf_t *)(uintptr_t)jconf, type, name);
885 
886 	if (type)
887 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
888 	if (name)
889 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
890 	return ((jlong)(uintptr_t)result);
891 }
892 
893 /*
894  * pool_resource_destroy(3pool) wrapper
895  */
896 /*ARGSUSED*/
897 JNIEXPORT jint JNICALL
898 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1destroy(
899     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource)
900 {
901 	return ((jint)pool_resource_destroy((pool_conf_t *)(uintptr_t)jconf,
902 	    (pool_resource_t *)(uintptr_t)jresource));
903 }
904 
905 /*
906  * pool_resource_transfer(3pool) wrapper
907  */
908 /*ARGSUSED*/
909 JNIEXPORT jint JNICALL
910 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1transfer(
911     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jsource, jlong jtarget,
912     jlong jsize)
913 {
914 	return (pool_resource_transfer((pool_conf_t *)(uintptr_t)jconf,
915 	    (pool_resource_t *)(uintptr_t)jsource,
916 	    (pool_resource_t *)(uintptr_t)jtarget,
917 	    (uint64_t)jsize));
918 }
919 
920 /*
921  * pool_resource_xtransfer(3pool) wrapper
922  */
923 /*ARGSUSED*/
924 JNIEXPORT jint JNICALL
925 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1xtransfer(
926     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jsource, jlong jtarget,
927     jobject jcomponents)
928 {
929 	pool_component_t **components;
930 	int result;
931 	jclass Lclazz;
932 	jmethodID Lsize;
933 	jint size;
934 
935 	/*
936 	 * Initialize the target parameter for case when input is null
937 	 */
938 	components = NULL;
939 	if (jcomponents != NULL) {
940 		Lclazz = (*jenv)->GetObjectClass(jenv, jcomponents);
941 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
942 		size = (*jenv)->CallIntMethod(jenv, jcomponents, Lsize);
943 
944 		if (size != 0) {
945 			jmethodID Lget;
946 			int i;
947 
948 			Lget = (*jenv)->GetMethodID(jenv,
949 			    Lclazz, "get", "(I)Ljava/lang/Object;");
950 			/* Allocate space for the components array */
951 
952 			if ((components = calloc(size + 1,
953 			    sizeof (pool_component_t *))) == NULL) {
954 				throwException(jenv, "java/lang/Exception",
955 				    "Could not allocate component array");
956 				return (0);
957 			}
958 			/*
959 			 * Copy in the array
960 			 */
961 			for (i = 0; i < size; i++) {
962 				jobject aVal;
963 				jclass Vclazz;
964 				jlong this;
965 				jmethodID Vthis;
966 
967 				aVal = (*jenv)->CallObjectMethod(jenv,
968 				    jcomponents, Lget, (jint) i);
969 				Vclazz = (*jenv)->GetObjectClass(jenv,
970 				    aVal);
971 				Vthis = (*jenv)->GetMethodID(jenv,
972 				    Vclazz, "getComponent", "()J");
973 				this = (*jenv)->CallLongMethod(jenv,
974 				    aVal, Vthis);
975 				components[i] =
976 				    (pool_component_t *)(uintptr_t)this;
977 			}
978 		}
979 	}
980 	result = (int)pool_resource_xtransfer((pool_conf_t *)(uintptr_t)jconf,
981 	    (pool_resource_t *)(uintptr_t)jsource,
982 	    (pool_resource_t *)(uintptr_t)jtarget,
983 	    components);
984 	free(components);
985 
986 	return ((jint)result);
987 }
988 
989 /*
990  * pool_query_resource_components(3pool) wrapper
991  */
992 /*ARGSUSED*/
993 JNIEXPORT jobject JNICALL
994 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1resource_\
995 1components(JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource,
996     jobject jprops)
997 {
998 	pool_value_t **props;
999 	pool_component_t **result;
1000 	jclass Lclazz;
1001 	jmethodID Lsize;
1002 	uint_t nelem;
1003 	jint size;
1004 
1005 	/*
1006 	 * Initialize the target parameter for case when input is null
1007 	 */
1008 	props = NULL;
1009 	if (jprops != NULL) {
1010 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
1011 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
1012 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
1013 
1014 		if (size != 0) {
1015 			jmethodID Lget;
1016 			int i;
1017 
1018 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
1019 			    "(I)Ljava/lang/Object;");
1020 			/*
1021 			 * Allocate space for the props array
1022 			 */
1023 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
1024 			    == NULL) {
1025 				throwException(jenv, "java/lang/Exception",
1026 				    "Could not allocate props array");
1027 				return (NULL);
1028 			}
1029 			/*
1030 			 * Copy in the array
1031 			 */
1032 			for (i = 0; i < size; i++) {
1033 				jobject aVal;
1034 				jclass Vclazz;
1035 				jfieldID Vthis;
1036 				jlong this;
1037 
1038 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
1039 				    Lget, (jint) i);
1040 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
1041 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
1042 				    "_this", "J");
1043 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
1044 				props[i] = (pool_value_t *)(uintptr_t)this;
1045 			}
1046 		}
1047 	}
1048 	result = pool_query_resource_components(
1049 	    (pool_conf_t *)(uintptr_t)jconf,
1050 	    (pool_resource_t *)(uintptr_t)jresource, &nelem, props);
1051 	free(props);
1052 	return (copyArray(jenv, (void **)result));
1053 }
1054 
1055 /*
1056  * pool_resource_info(3pool) wrapper
1057  */
1058 /*ARGSUSED*/
1059 JNIEXPORT jstring JNICALL
1060 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1info(
1061     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource, jint jflags)
1062 {
1063 	jstring jresult = NULL;
1064 	const char *result;
1065 
1066 	result = pool_resource_info((pool_conf_t *)(uintptr_t)jconf,
1067 	    (pool_resource_t *)(uintptr_t)jresource, (int)jflags);
1068 
1069 	if (result)
1070 		jresult = (*jenv)->NewStringUTF(jenv, result);
1071 	free((void *)result);
1072 	return (jresult);
1073 }
1074 
1075 /*
1076  * pool_create(3pool) wrapper
1077  */
1078 /*ARGSUSED*/
1079 JNIEXPORT jlong JNICALL
1080 Java_com_sun_solaris_service_pools_PoolInternal_pool_1create(JNIEnv *jenv,
1081     jclass jcls, jlong jconf, jstring jname)
1082 {
1083 	const char *name;
1084 	pool_t *result;
1085 
1086 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
1087 	    NULL;
1088 	result = pool_create((pool_conf_t *)(uintptr_t)jconf, name);
1089 
1090 	if (name)
1091 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1092 	return ((jlong)(uintptr_t)result);
1093 }
1094 
1095 /*
1096  * pool_destroy(3pool) wrapper
1097  */
1098 /*ARGSUSED*/
1099 JNIEXPORT jint JNICALL
1100 Java_com_sun_solaris_service_pools_PoolInternal_pool_1destroy(JNIEnv *jenv,
1101     jclass jcls, jlong jconf, jlong jpool)
1102 {
1103 	return ((jint)pool_destroy((pool_conf_t *)(uintptr_t)jconf,
1104 	    (pool_t *)(uintptr_t)jpool));
1105 }
1106 
1107 /*
1108  * pool_associate(3pool) wrapper
1109  */
1110 /*ARGSUSED*/
1111 JNIEXPORT jint JNICALL
1112 Java_com_sun_solaris_service_pools_PoolInternal_pool_1associate(JNIEnv *jenv,
1113     jclass jcls, jlong jconf, jlong jpool, jlong jresource)
1114 {
1115 	return ((jint)pool_associate((pool_conf_t *)(uintptr_t)jconf,
1116 	    (pool_t *)(uintptr_t)jpool,
1117 	    (pool_resource_t *)(uintptr_t)jresource));
1118 }
1119 
1120 /*
1121  * pool_dissociate(3pool) wrapper
1122  */
1123 /*ARGSUSED*/
1124 JNIEXPORT jint JNICALL
1125 Java_com_sun_solaris_service_pools_PoolInternal_pool_1dissociate(JNIEnv *jenv,
1126     jclass jcls, jlong jconf, jlong jpool, jlong jresource)
1127 {
1128 	return ((jint)pool_dissociate((pool_conf_t *)(uintptr_t)jconf,
1129 	    (pool_t *)(uintptr_t)jpool,
1130 	    (pool_resource_t *)(uintptr_t)jresource));
1131 }
1132 
1133 /*
1134  * pool_info(3pool) wrapper
1135  */
1136 /*ARGSUSED*/
1137 JNIEXPORT jstring JNICALL
1138 Java_com_sun_solaris_service_pools_PoolInternal_pool_1info(JNIEnv *jenv,
1139     jclass jcls, jlong jconf, jlong jpool, jint jflags)
1140 {
1141 	jstring jresult = NULL;
1142 	const char *result;
1143 
1144 	result = pool_info((pool_conf_t *)(uintptr_t)jconf,
1145 	    (pool_t *)(uintptr_t)jpool, (int)jflags);
1146 
1147 	if (result)
1148 		jresult = (*jenv)->NewStringUTF(jenv, result);
1149 	free((void *)result);
1150 	return (jresult);
1151 }
1152 
1153 /*
1154  * pool_query_pool_resources(3pool) wrapper
1155  */
1156 /*ARGSUSED*/
1157 JNIEXPORT jobject JNICALL
1158 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1pool_1resources(
1159     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jpool, jobject jprops)
1160 {
1161 	pool_value_t **props;
1162 	pool_resource_t **result;
1163 	jclass Lclazz;
1164 	jmethodID Lsize;
1165 	uint_t nelem;
1166 	jint size;
1167 
1168 	/*
1169 	 * Initialize the target parameter for case when input is null
1170 	 */
1171 	props = NULL;
1172 	if (jprops != NULL) {
1173 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
1174 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
1175 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
1176 
1177 		if (size != 0) {
1178 			jmethodID Lget;
1179 			int i;
1180 
1181 			Lget = (*jenv)->GetMethodID(jenv,
1182 			    Lclazz, "get", "(I)Ljava/lang/Object;");
1183 			/*
1184 			 * Allocate space for the props array
1185 			 */
1186 
1187 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
1188 			    == NULL) {
1189 				throwException(jenv, "java/lang/Exception",
1190 				    "Could not allocate props array");
1191 				return (NULL);
1192 			}
1193 			/*
1194 			 * Copy in the array
1195 			 */
1196 			for (i = 0; i < size; i++) {
1197 				jobject aVal;
1198 				jclass Vclazz;
1199 				jfieldID Vthis;
1200 				jlong this;
1201 
1202 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
1203 				    Lget, (jint) i);
1204 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
1205 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
1206 				    "_this", "J");
1207 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
1208 				props[i] = (pool_value_t *)(uintptr_t)this;
1209 			}
1210 		}
1211 	}
1212 	result = pool_query_pool_resources((pool_conf_t *)(uintptr_t)jconf,
1213 	    (pool_t *)(uintptr_t)jpool, &nelem, props);
1214 	free(props);
1215 	return (copyArray(jenv, (void **)result));
1216 }
1217 
1218 /*
1219  * pool_get_owning_resource(3pool) wrapper
1220  */
1221 /*ARGSUSED*/
1222 JNIEXPORT jlong JNICALL
1223 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1owning_1resource(
1224     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent)
1225 {
1226 	return ((jlong)(uintptr_t)pool_get_owning_resource(
1227 	    (pool_conf_t *)(uintptr_t)jconf,
1228 	    (pool_component_t *)(uintptr_t)jcomponent));
1229 }
1230 
1231 /*
1232  * pool_component_info(3pool) wrapper
1233  */
1234 /*ARGSUSED*/
1235 JNIEXPORT jstring JNICALL
1236 Java_com_sun_solaris_service_pools_PoolInternal_pool_1component_1info(
1237     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent, jint jflags)
1238 {
1239 	jstring jresult = NULL;
1240 	const char *result;
1241 
1242 	result = pool_component_info((pool_conf_t *)(uintptr_t)jconf,
1243 	    (pool_component_t *)(uintptr_t)jcomponent, (int)jflags);
1244 
1245 	if (result)
1246 		jresult = (*jenv)->NewStringUTF(jenv, result);
1247 	free((void *)result);
1248 	return (jresult);
1249 }
1250 
1251 /*
1252  * pool_get_property(3pool) wrapper
1253  */
1254 /*ARGSUSED*/
1255 JNIEXPORT jint JNICALL
1256 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1property(
1257     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jstring jname,
1258     jlong jproperty)
1259 {
1260 	const char *name;
1261 	int result;
1262 
1263 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
1264 	    NULL;
1265 	result = pool_get_property((pool_conf_t *)(uintptr_t)jconf,
1266 	    (pool_elem_t *)(uintptr_t)jelem, name,
1267 	    (pool_value_t *)(uintptr_t)jproperty);
1268 
1269 	if (name)
1270 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1271 	return ((jint)result);
1272 }
1273 
1274 /*
1275  * pool_put_property(3pool) wrapper
1276  */
1277 /*ARGSUSED*/
1278 JNIEXPORT jint JNICALL
1279 Java_com_sun_solaris_service_pools_PoolInternal_pool_1put_1property(
1280     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jstring jname,
1281     jlong jvalue)
1282 {
1283 	const char *name;
1284 	int result;
1285 
1286 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1287 	result = (int)pool_put_property((pool_conf_t *)(uintptr_t)jconf,
1288 	    (pool_elem_t *)(uintptr_t)jelem, name,
1289 	    (pool_value_t *)(uintptr_t)jvalue);
1290 
1291 	if (name)
1292 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1293 	return ((jint)result);
1294 }
1295 
1296 /*
1297  * pool_rm_property(3pool) wrapper
1298  */
1299 /*ARGSUSED*/
1300 JNIEXPORT jint JNICALL
1301 Java_com_sun_solaris_service_pools_PoolInternal_pool_1rm_1property(JNIEnv *jenv,
1302     jclass jcls, jlong jconf, jlong jelem, jstring jname)
1303 {
1304 	const char *name;
1305 	int result;
1306 
1307 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1308 	result = pool_rm_property((pool_conf_t *)(uintptr_t)jconf,
1309 	    (pool_elem_t *)(uintptr_t)jelem, name);
1310 
1311 	if (name)
1312 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1313 	return ((jint)result);
1314 }
1315 
1316 /*
1317  * pool_walk_properties(3pool) wrapper
1318  */
1319 /*ARGSUSED*/
1320 JNIEXPORT jint JNICALL
1321 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1properties(
1322     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jlong jarg,
1323     jlong jcallback)
1324 {
1325 	int result;
1326 
1327 	result = (int)pool_walk_properties((pool_conf_t *)(uintptr_t)jconf,
1328 	    (pool_elem_t *)(uintptr_t)jelem, (void *)(uintptr_t)jarg,
1329 	    (int (*)(pool_conf_t *, pool_elem_t *, char const *,
1330 	    pool_value_t *, void *))(uintptr_t)jcallback);
1331 
1332 	return ((jint)result);
1333 }
1334 
1335 /*
1336  * pool_conf_to_elem(3pool) wrapper
1337  */
1338 /*ARGSUSED*/
1339 JNIEXPORT jlong JNICALL
1340 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1to_1elem(
1341     JNIEnv *jenv, jclass jcls, jlong jconf)
1342 {
1343 	return ((jlong)(uintptr_t)pool_conf_to_elem(
1344 	    (pool_conf_t *)(uintptr_t)jconf));
1345 }
1346 
1347 /*
1348  * pool_to_elem(3pool) wrapper
1349  */
1350 /*ARGSUSED*/
1351 JNIEXPORT jlong JNICALL
1352 Java_com_sun_solaris_service_pools_PoolInternal_pool_1to_1elem(JNIEnv *jenv,
1353     jclass jcls, jlong jconf, jlong jpool)
1354 {
1355 	return ((jlong)(uintptr_t)pool_to_elem((pool_conf_t *)(uintptr_t)jconf,
1356 	    (pool_t *)(uintptr_t)jpool));
1357 }
1358 
1359 /*
1360  * pool_resource_to_elem(3pool) wrapper
1361  */
1362 /*ARGSUSED*/
1363 JNIEXPORT jlong JNICALL
1364 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1to_1elem(
1365     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource)
1366 {
1367 	return ((jlong)(uintptr_t)pool_resource_to_elem(
1368 	    (pool_conf_t *)(uintptr_t)jconf,
1369 	    (pool_resource_t *)(uintptr_t)jresource));
1370 }
1371 
1372 /*
1373  * pool_component_to_elem(3pool) wrapper
1374  */
1375 /*ARGSUSED*/
1376 JNIEXPORT jlong JNICALL
1377 Java_com_sun_solaris_service_pools_PoolInternal_pool_1component_1to_1elem(
1378     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent)
1379 {
1380 	return ((jlong)(uintptr_t)pool_component_to_elem(
1381 	    (pool_conf_t *)(uintptr_t)jconf,
1382 	    (pool_component_t *)(uintptr_t)jcomponent));
1383 }
1384 
1385 /*
1386  * pool_value_get_type(3pool) wrapper
1387  */
1388 /*ARGSUSED*/
1389 JNIEXPORT jint JNICALL
1390 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1get_1type(
1391     JNIEnv *jenv, jclass jcls, jlong jvalue)
1392 {
1393 	return ((jint)pool_value_get_type((pool_value_t *)(uintptr_t)jvalue));
1394 }
1395 
1396 /*
1397  * pool_value_set_uint64(3pool) wrapper
1398  */
1399 /*ARGSUSED*/
1400 JNIEXPORT void JNICALL
1401 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1uint64(
1402     JNIEnv *jenv, jclass jcls, jlong jvalue, jlong jui64)
1403 {
1404 	pool_value_set_uint64(
1405 	    (pool_value_t *)(uintptr_t)jvalue, (uint64_t)jui64);
1406 }
1407 
1408 /*
1409  * pool_value_set_int64(3pool) wrapper
1410  */
1411 /*ARGSUSED*/
1412 JNIEXPORT void JNICALL
1413 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1int64(
1414     JNIEnv *jenv, jclass jcls, jlong jvalue, jlong ji64)
1415 {
1416 	pool_value_set_int64((pool_value_t *)(uintptr_t)jvalue, (int64_t)ji64);
1417 }
1418 
1419 /*
1420  * pool_value_set_double(3pool) wrapper
1421  */
1422 /*ARGSUSED*/
1423 JNIEXPORT void JNICALL
1424 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1double(
1425     JNIEnv *jenv, jclass jcls, jlong jvalue, jdouble jd)
1426 {
1427 	pool_value_set_double((pool_value_t *)(uintptr_t)jvalue, (double)jd);
1428 }
1429 
1430 /*
1431  * pool_value_set_bool(3pool) wrapper
1432  */
1433 /*ARGSUSED*/
1434 JNIEXPORT void JNICALL
1435 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1bool(
1436     JNIEnv *jenv, jclass jcls, jlong jvalue, jshort jb)
1437 {
1438 	pool_value_set_bool((pool_value_t *)(uintptr_t)jvalue, (uchar_t)jb);
1439 }
1440 
1441 /*
1442  * pool_value_set_string(3pool) wrapper
1443  */
1444 /*ARGSUSED*/
1445 JNIEXPORT jint JNICALL
1446 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1string(
1447     JNIEnv * jenv, jclass jcls, jlong jvalue, jstring jstr)
1448 {
1449 	const char *str;
1450 	int result;
1451 
1452 	str = (jstr) ? (*jenv)->GetStringUTFChars(jenv, jstr, 0) : NULL;
1453 	result = pool_value_set_string((pool_value_t *)(uintptr_t)jvalue, str);
1454 
1455 	if (str)
1456 		(*jenv)->ReleaseStringUTFChars(jenv, jstr, str);
1457 	return ((jint)result);
1458 }
1459 
1460 /*
1461  * pool_value_get_name(3pool) wrapper
1462  */
1463 /*ARGSUSED*/
1464 JNIEXPORT jstring JNICALL
1465 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1get_1name(
1466     JNIEnv *jenv, jclass jcls, jlong jvalue)
1467 {
1468 	jstring jresult = NULL;
1469 	const char *result;
1470 
1471 	result = pool_value_get_name((pool_value_t *)(uintptr_t)jvalue);
1472 
1473 	if (result)
1474 		jresult = (*jenv)->NewStringUTF(jenv, result);
1475 	return (jresult);
1476 }
1477 
1478 /*
1479  * pool_value_set_name(3pool) wrapper
1480  */
1481 /*ARGSUSED*/
1482 JNIEXPORT jint JNICALL
1483 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1name(
1484     JNIEnv *jenv, jclass jcls, jlong jvalue, jstring jname)
1485 {
1486 	const char *name;
1487 	int result;
1488 
1489 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1490 	result = pool_value_set_name((pool_value_t *)(uintptr_t)jvalue, name);
1491 
1492 	if (name)
1493 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1494 	return ((jint)result);
1495 }
1496 
1497 /*
1498  * pool_value_alloc(3pool) wrapper
1499  */
1500 /*ARGSUSED*/
1501 JNIEXPORT jlong JNICALL
1502 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1alloc(JNIEnv *jenv,
1503     jclass jcls)
1504 {
1505 	return ((jlong)(uintptr_t)pool_value_alloc());
1506 }
1507 
1508 /*
1509  * pool_value_free(3pool) wrapper
1510  */
1511 /*ARGSUSED*/
1512 JNIEXPORT void JNICALL
1513 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1free(JNIEnv *jenv,
1514     jclass jcls, jlong jvalue)
1515 {
1516 	pool_value_free((pool_value_t *)(uintptr_t)jvalue);
1517 }
1518 
1519 /*
1520  * pool_static_location(3pool) wrapper
1521  */
1522 /*ARGSUSED*/
1523 JNIEXPORT jstring JNICALL
1524 Java_com_sun_solaris_service_pools_PoolInternal_pool_1static_1location(
1525     JNIEnv *jenv, jclass jcls)
1526 {
1527 	jstring jresult = NULL;
1528 	const char *result;
1529 
1530 	result = pool_static_location();
1531 
1532 	if (result)
1533 		jresult = (*jenv)->NewStringUTF(jenv, result);
1534 	return (jresult);
1535 }
1536 
1537 /*
1538  * pool_dynamic_location(3pool) wrapper
1539  */
1540 /*ARGSUSED*/
1541 JNIEXPORT jstring JNICALL
1542 Java_com_sun_solaris_service_pools_PoolInternal_pool_1dynamic_1location(JNIEnv *
1543     jenv, jclass jcls)
1544 {
1545 	jstring jresult = NULL;
1546 	const char *result;
1547 
1548 	result = pool_dynamic_location();
1549 
1550 	if (result)
1551 		jresult = (*jenv)->NewStringUTF(jenv, result);
1552 	return (jresult);
1553 }
1554 
1555 /*
1556  * pool_set_binding(3pool) wrapper
1557  */
1558 /*ARGSUSED*/
1559 JNIEXPORT jint JNICALL
1560 Java_com_sun_solaris_service_pools_PoolInternal_pool_1set_1binding(JNIEnv *jenv,
1561     jclass jcls, jstring jpool, jint jidtype, jint jpid)
1562 {
1563 	const char *pool;
1564 	int result;
1565 
1566 	pool = (jpool) ? (*jenv)->GetStringUTFChars(jenv, jpool, 0) : NULL;
1567 	result = (int)pool_set_binding(pool, (idtype_t)jidtype, (id_t)jpid);
1568 
1569 	if (pool)
1570 		(*jenv)->ReleaseStringUTFChars(jenv, jpool, pool);
1571 	return ((jint)result);
1572 }
1573 
1574 /*
1575  * pool_get_binding(3pool) wrapper
1576  */
1577 /*ARGSUSED*/
1578 JNIEXPORT jstring JNICALL
1579 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1binding(JNIEnv *jenv,
1580     jclass jcls, jint jpid)
1581 {
1582 	jstring jresult = NULL;
1583 	const char *result;
1584 
1585 	result = pool_get_binding((pid_t)jpid);
1586 
1587 	if (result)
1588 		jresult = (*jenv)->NewStringUTF(jenv, result);
1589 	free((void *)result);
1590 	return (jresult);
1591 }
1592 
1593 /*
1594  * pool_get_resource_binding(3pool) wrapper
1595  */
1596 /*ARGSUSED*/
1597 JNIEXPORT jstring JNICALL
1598 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1resource_1binding(
1599     JNIEnv *jenv, jclass jcls, jstring jtype, jint jpid)
1600 {
1601 	jstring jresult = NULL;
1602 	const char *type;
1603 	const char *result;
1604 
1605 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) : NULL;
1606 	result = pool_get_resource_binding(type, (pid_t)jpid);
1607 
1608 	if (result)
1609 		jresult = (*jenv)->NewStringUTF(jenv, result);
1610 	free((void *)result);
1611 	if (type)
1612 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
1613 	return (jresult);
1614 }
1615 
1616 /*
1617  * pool_walk_pools(3pool) wrapper
1618  */
1619 /*ARGSUSED*/
1620 JNIEXPORT jint JNICALL
1621 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1pools(JNIEnv *jenv,
1622     jclass jcls, jlong jconf, jlong jarg, jlong jcallback)
1623 {
1624 	int result;
1625 
1626 	result = pool_walk_pools((pool_conf_t *)(uintptr_t)jconf,
1627 	    (void *)(uintptr_t)jarg,
1628 	    (int (*)(pool_conf_t *, pool_t *, void *))(uintptr_t)jcallback);
1629 	return ((jint)result);
1630 }
1631 
1632 /*
1633  * pool_walk_resources(3pool) wrapper
1634  */
1635 /*ARGSUSED*/
1636 JNIEXPORT jint JNICALL
1637 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1resources(
1638     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jpool, jlong jarg,
1639     jlong jcallback)
1640 {
1641 	int result;
1642 
1643 	result = pool_walk_resources((pool_conf_t *)(uintptr_t)jconf,
1644 	    (pool_t *)(uintptr_t)jpool, (void *)(uintptr_t)jarg,
1645 	    (int (*)(pool_conf_t *, pool_resource_t *, void *))
1646 	    (uintptr_t)jcallback);
1647 	return ((jint)result);
1648 }
1649 
1650 /*
1651  * pool_walk_components(3pool) wrapper
1652  */
1653 /*ARGSUSED*/
1654 JNIEXPORT jint JNICALL
1655 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1components(
1656     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource, jlong jarg,
1657     jlong jcallback)
1658 {
1659 	int result;
1660 
1661 	result = pool_walk_components((pool_conf_t *)(uintptr_t)jconf,
1662 	    (pool_resource_t *)(uintptr_t)jresource, (void *)(uintptr_t)jarg,
1663 	    (int (*)(pool_conf_t *, pool_component_t *, void *))
1664 	    (uintptr_t)jcallback);
1665 	return ((jint)result);
1666 }
1667 
1668 /*ARGSUSED*/
1669 JNIEXPORT jint JNICALL
1670 Java_com_sun_solaris_service_pools_Element_walkProps(JNIEnv *env,
1671     jobject obj, jlong conf, jlong elem, jobject handler, jobject userobj)
1672 {
1673 	struct pool_callback pc;
1674 
1675 	pc.pc_user = userobj;
1676 	pc.pc_handler = handler;
1677 	pc.pc_elem = obj;
1678 	pc.pc_env = env;
1679 	return (pool_walk_properties((pool_conf_t *)*(void**)&conf,
1680 	    (pool_elem_t *)*(void**)&elem, (void *)&pc, pool_property_walker));
1681 }
1682 
1683 /*ARGSUSED*/
1684 static int
1685 pool_property_walker(pool_conf_t *conf, pool_elem_t *pe, const char *name,
1686     pool_value_t *pv, void *user)
1687 {
1688 	jclass clazz, vclazz;
1689 	jmethodID mgetwalk, mvcon;
1690 	struct pool_callback *pc = (struct pool_callback *)user;
1691 	jobject valueObj;
1692 	pool_value_t *pv_new;
1693 	uint64_t uval;
1694 	int64_t ival;
1695 	double dval;
1696 	uchar_t bval;
1697 	const char *sval;
1698 
1699 	/*
1700 	 * Since we intend to embed our value into a Java Value object
1701 	 * and then reclaim the value when the object is garbage
1702 	 * collected we must create a new pool value and then pass this
1703 	 * to the constructor.  We must not use the pool value which is
1704 	 * passed to us.
1705 	 */
1706 
1707 	if ((pv_new = pool_value_alloc()) == NULL)
1708 		return (PO_FAIL);
1709 	switch (pool_value_get_type(pv)) {
1710 	case POC_UINT:
1711 		(void) pool_value_get_uint64(pv, &uval);
1712 		(void) pool_value_set_uint64(pv_new, uval);
1713 		break;
1714 	case POC_INT:
1715 		(void) pool_value_get_int64(pv, &ival);
1716 		(void) pool_value_set_int64(pv_new, ival);
1717 		break;
1718 	case POC_DOUBLE:
1719 		(void) pool_value_get_double(pv, &dval);
1720 		(void) pool_value_set_double(pv_new, dval);
1721 		break;
1722 	case POC_BOOL:
1723 		(void) pool_value_get_bool(pv, &bval);
1724 		(void) pool_value_set_bool(pv_new, bval);
1725 		break;
1726 	case POC_STRING:
1727 		(void) pool_value_get_string(pv, &sval);
1728 		(void) pool_value_set_string(pv_new, sval);
1729 		break;
1730 	default:
1731 		pool_value_free(pv_new);
1732 		return (PO_FAIL);
1733 	}
1734 	if (pool_value_set_name(pv_new, name) != PO_SUCCESS ||
1735 	    (vclazz = (*pc->pc_env)->FindClass(pc->pc_env,
1736 	    "com/sun/solaris/service/pools/Value")) == NULL ||
1737 	    (mvcon = (*pc->pc_env)->GetMethodID(pc->pc_env, vclazz,
1738 	    "<init>", "(J)V")) == NULL ||
1739 	    (valueObj = (*pc->pc_env)->NewObject(pc->pc_env, vclazz, mvcon,
1740 	    pv_new)) == NULL ||
1741 	    (clazz = (*pc->pc_env)->GetObjectClass(pc->pc_env, pc->pc_handler))
1742 	    == NULL ||
1743 	    (mgetwalk = (*pc->pc_env)->GetMethodID(pc->pc_env,
1744 	    clazz, "walk",
1745 	    "(Lcom/sun/solaris/service/pools/Element;Lcom/sun/solaris/"
1746 	    "service/pools/Value;Ljava/lang/Object;)I")) == NULL)
1747 		return (PO_FAIL);
1748 	return ((*pc->pc_env)->CallIntMethod(pc->pc_env,
1749 	    pc->pc_handler, mgetwalk, pc->pc_elem, valueObj, pc->pc_user));
1750 }
1751 
1752 /*ARGSUSED*/
1753 JNIEXPORT jlong JNICALL
1754 Java_com_sun_solaris_service_pools_Value_getLongValue(JNIEnv *jenv,
1755     jclass class, jlong pointer)
1756 {
1757 	int64_t arg2;
1758 	int result;
1759 
1760 	result =
1761 	    pool_value_get_int64((pool_value_t *)(uintptr_t)pointer, &arg2);
1762 
1763 	if (result != PO_SUCCESS) { /* it could be a uint64 */
1764 		result =
1765 		    pool_value_get_uint64((pool_value_t *)(uintptr_t)pointer,
1766 		    (uint64_t *)&arg2);
1767 		if (result != PO_SUCCESS) {
1768 			throw_pe(jenv);
1769 		}
1770 		/*
1771 		 * Unfortunately, Java has no unsigned types, so we lose some
1772 		 * precision by forcing the top bit clear
1773 		 */
1774 		arg2 &= 0x7fffffffffffffffULL;
1775 	}
1776 	return ((jlong)arg2);
1777 }
1778 
1779 /*ARGSUSED*/
1780 JNIEXPORT jstring JNICALL
1781 Java_com_sun_solaris_service_pools_Value_getStringValue(JNIEnv *jenv,
1782     jclass class, jlong pointer)
1783 {
1784 	const char *arg2;
1785 	int result;
1786 
1787 	result =
1788 	    pool_value_get_string((pool_value_t *)(uintptr_t)pointer, &arg2);
1789 	if (result != PO_SUCCESS)
1790 		throw_pe(jenv);
1791 	return ((*jenv)->NewStringUTF(jenv, arg2));
1792 }
1793 
1794 /*ARGSUSED*/
1795 JNIEXPORT jboolean JNICALL
1796 Java_com_sun_solaris_service_pools_Value_getBoolValue(JNIEnv *jenv,
1797     jclass class, jlong pointer)
1798 {
1799 	uchar_t arg2;
1800 	int result;
1801 
1802 	result = pool_value_get_bool((pool_value_t *)(uintptr_t)pointer, &arg2);
1803 
1804 	if (result != PO_SUCCESS) {
1805 		throw_pe(jenv);
1806 	}
1807 	if (arg2 == PO_TRUE)
1808 		return (JNI_TRUE);
1809 	else
1810 		return (JNI_FALSE);
1811 }
1812 
1813 /*ARGSUSED*/
1814 JNIEXPORT jdouble JNICALL
1815 Java_com_sun_solaris_service_pools_Value_getDoubleValue(JNIEnv *jenv,
1816     jclass class, jlong pointer)
1817 {
1818 	double arg2;
1819 	int result;
1820 
1821 	result =
1822 	    pool_value_get_double((pool_value_t *)(uintptr_t)pointer, &arg2);
1823 
1824 	if (result != PO_SUCCESS) {
1825 		throw_pe(jenv);
1826 	}
1827 	return ((jdouble)arg2);
1828 }
1829 
1830 /*ARGSUSED*/
1831 JNIEXPORT jobject JNICALL
1832 Java_com_sun_solaris_service_pools_Value_getUnsignedInt64Value(JNIEnv *jenv,
1833     jclass class, jlong pointer)
1834 {
1835 	uint64_t arg2;
1836 	int result;
1837 
1838 	result =
1839 	    pool_value_get_uint64((pool_value_t *)(uintptr_t)pointer, &arg2);
1840 
1841 	if (result != PO_SUCCESS) {
1842 		throw_pe(jenv);
1843 	}
1844 	return (makeUnsignedInt64(jenv, arg2));
1845 }
1846 
1847 /*ARGSUSED*/
1848 JNIEXPORT jobject JNICALL
1849 Java_com_sun_solaris_service_pools_HRTime_timestamp(JNIEnv *env, jobject obj)
1850 {
1851 	return (makeUnsignedInt64(env, gethrtime()));
1852 }
1853 
1854 /*
1855  * Cache class, method, and field IDs.
1856  */
1857 /*ARGSUSED*/
1858 JNIEXPORT void JNICALL
1859 Java_com_sun_solaris_service_pools_PoolInternal_init(JNIEnv *env, jclass clazz)
1860 {
1861 	jclass ui64class_lref;
1862 
1863 	if (!(ui64class_lref = (*env)->FindClass(env,
1864 	    "com/sun/solaris/service/pools/UnsignedInt64")))
1865 		return; /* exception thrown */
1866 	if (!(ui64class = (*env)->NewGlobalRef(env, ui64class_lref)))
1867 		return; /* exception thrown */
1868 	ui64cons_mid = (*env)->GetMethodID(env, ui64class, "<init>", "([B)V");
1869 }
1870