bni.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file bni.h
  18. * @date Mon Jul 2 16:54:58 2012
  19. *
  20. * @brief Beihai native interface.
  21. */
  22. #ifndef BNI_H
  23. #define BNI_H
  24. #include "bh_types.h"
  25. /* Primitive types */
  26. typedef uint8 jboolean;
  27. typedef int8 jbyte;
  28. typedef uint16 jchar;
  29. typedef int16 jshort;
  30. typedef int32 jint;
  31. typedef int64 jlong;
  32. typedef float jfloat;
  33. typedef double jdouble;
  34. typedef jint jsize;
  35. /* Predefined Java class types. */
  36. struct _jobject;
  37. typedef struct _jobject *jobject;
  38. struct _jclass;
  39. typedef struct _jclass *jclass;
  40. struct _jstring;
  41. typedef struct _jstring *jstring;
  42. /* Values of jboolean: */
  43. #define BNI_FALSE 0
  44. #define BNI_TRUE 1
  45. /**
  46. * Return the length of the array object.
  47. *
  48. * @param array Java array object
  49. *
  50. * @return the length of the Java array
  51. */
  52. #define bni_array_length(array) ((jsize)((uint32)(array)->__length >> 2))
  53. /**
  54. * Return the address of the first element of array object.
  55. *
  56. * @param array Java array object
  57. *
  58. * @return the address of the first element of array object
  59. */
  60. #define bni_array_elem(array) ((array)->__elem)
  61. /**
  62. * Find the Java class with given class name.
  63. *
  64. * @param name Java class name
  65. *
  66. * @return class object of the Java class if found, NULL otherwise
  67. *
  68. * @throws OutOfMemoryError if VM runs out of memory.
  69. */
  70. jclass
  71. bni_find_class(const char *name);
  72. /**
  73. * Throw an exception of given class with message.
  74. *
  75. * @param clazz class object of a subclass of java.lang.Throwable
  76. * @param msg message for the exception or NULL if no message
  77. *
  78. * @return 0 if succeeds, nonzero otherwise
  79. */
  80. jint
  81. bni_throw_new(jclass clazz, const char *msg);
  82. /**
  83. * Throw a NullPointerException.
  84. *
  85. * @throws NullPointerException
  86. */
  87. void
  88. bni_throw_npe(void);
  89. /**
  90. * Throw an ArrayIndexOutOfBoundsException
  91. *
  92. * @param index the index used to access the array
  93. *
  94. * @throws ArrayIndexOutOfBoundsException
  95. */
  96. void
  97. bni_throw_aioobe(int index);
  98. /**
  99. * Determine whether an exception is being thrown.
  100. *
  101. * @return exception object if exception is thrown, NULL otherwise
  102. */
  103. jobject
  104. bni_exception_occurred(void);
  105. /**
  106. * Print the current exception to error-reporting channel.
  107. */
  108. void
  109. bni_exception_describe(void);
  110. /**
  111. * Clear the currently thrown exception.
  112. */
  113. void
  114. bni_exception_clear(void);
  115. /**
  116. * Return the Unicode character number of a string.
  117. *
  118. * @param str Java string object
  119. *
  120. * @return the Unicode character number of the string
  121. */
  122. jsize
  123. bni_string_length(jstring str);
  124. /**
  125. * Return the length in bytes of the modified UTF-8 representation of
  126. * a string.
  127. *
  128. * @param str Java string object
  129. * @param start start offset in the string
  130. * @param len number of Unicode characters
  131. *
  132. * @return the UTF-8 length of the string
  133. *
  134. * @throws StringIndexOutOfBoundsException on index overflow.
  135. */
  136. jsize
  137. bni_string_utf_length(jstring str, jsize start, jsize len);
  138. /**
  139. * Copies len number of Unicode characters beginning at offset start
  140. * to the given buffer buf.
  141. *
  142. * @param str Java string object
  143. * @param start start offset in the string
  144. * @param len number of Unicode characters to copy
  145. * @param buf buffer for storing the result
  146. */
  147. void
  148. bni_string_region(jstring str, jsize start, jsize len, jchar *buf);
  149. /**
  150. * Translates len number of Unicode characters beginning at offset
  151. * start into modified UTF-8 encoding and place the result in the
  152. * given buffer buf.
  153. *
  154. * @param str Java string object
  155. * @param start start offset in the string
  156. * @param len number of Unicode characters to copy
  157. * @param buf buffer for storing the result
  158. *
  159. * @throws StringIndexOutOfBoundsException on index overflow.
  160. */
  161. void
  162. bni_string_utf_region(jstring str, jsize start, jsize len, char *buf);
  163. /**
  164. * Translate Unicode characters into modified UTF-8 encoding and return
  165. * the result.
  166. *
  167. * @param str Java string object
  168. *
  169. * @return the UTF-8 encoding string if succeeds, NULL otherwise
  170. */
  171. char *
  172. bni_string_get_utf_chars(jstring str);
  173. /**
  174. * Get the given Java object's class index.
  175. *
  176. * @param obj Java object
  177. *
  178. * @return -1 if obj is an array, class index of the object otherwise
  179. */
  180. jint
  181. bni_object_class_index(jobject obj);
  182. /**
  183. * Allocate memory from the current instance's private heap.
  184. *
  185. * @param size bytes to allocate
  186. *
  187. * @return pointer to the allocated memory
  188. *
  189. * @throws OutOfMemoryError if VM runs out of memory.
  190. */
  191. void*
  192. bni_malloc(unsigned size);
  193. /**
  194. * Allocate memory from the current instance's private heap and clear
  195. * to zero.
  196. *
  197. * @param size bytes to allocate
  198. *
  199. * @return pointer to the allocated memory
  200. *
  201. * @throws OutOfMemoryError if VM runs out of memory.
  202. */
  203. void*
  204. bni_calloc(unsigned size);
  205. /**
  206. * Free the memory allocated from the current instance's private heap.
  207. *
  208. * @param ptr pointer to the memory in current instance's private heap
  209. */
  210. void
  211. bni_free(void *ptr);
  212. #endif