MNEMONIC-752: Add common.c to memkind service
Signed-off-by: Xiaojin Jiao <xjiao@apache.org>
diff --git a/mnemonic-memory-services/mnemonic-memkind-pmem-service/src/main/native/common.c b/mnemonic-memory-services/mnemonic-memkind-pmem-service/src/main/native/common.c
new file mode 100644
index 0000000..048d1f0
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-memkind-pmem-service/src/main/native/common.c
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <common.h>
+
+/******************************************************************************
+ ** Generally-useful functions for JNI programming.
+ *****************************************************************************/
+
+/**
+ * Throws a RuntimeException, with either an explicit message or the message
+ * corresponding to the current system error value.
+ */
+inline void
+throw(JNIEnv* env, const char* msg) {
+ if (msg == NULL)
+ msg = strerror(errno);
+
+ jclass xklass = (*env)->FindClass(env, "java/lang/RuntimeException");
+ (*env)->ThrowNew(env, xklass, msg);
+}
+
+inline void*
+addr_from_java(jlong addr) {
+ // This assert fails in a variety of ways on 32-bit systems.
+ // It is impossible to predict whether native code that converts
+ // pointers to longs will sign-extend or zero-extend the addresses.
+ //assert(addr == (uintptr_t)addr, "must not be odd high bits");
+ return (void*) (uintptr_t) addr;
+}
+
+inline jlong
+addr_to_java(void* p) {
+ assert(p == (void*) (uintptr_t) p);
+ return (long) (uintptr_t) p;
+}
+
+inline void *
+vrealloc(VMPool *pool, void *p, size_t size, int initzero) {
+ void *ret = NULL;
+ void *nativebuf = NULL;
+ if (NULL == p) {
+ if (initzero) {
+ nativebuf = memkind_calloc(pool->kind, 1, sizeof(uint8_t) * size + PMBHSZ);
+ } else {
+ nativebuf = memkind_malloc(pool->kind, sizeof(uint8_t) * size + PMBHSZ);
+ }
+ } else {
+ nativebuf = memkind_realloc(pool->kind, p, sizeof(uint8_t) * size + PMBHSZ);
+ }
+ if (NULL != nativebuf) {
+ ((PMBHeader *) nativebuf)->size = size + PMBHSZ;
+ ret = nativebuf + PMBHSZ;
+ }
+ return ret;
+}
+
+inline void
+vfree(VMPool *pool, void *p) {
+ if (p != NULL) {
+ memkind_free(pool->kind, p);
+ }
+}
+
+inline size_t
+vsize(VMPool *pool, void *p) {
+ size_t ret = 0;
+ void* nativebuf;
+ if (p != NULL) {
+ nativebuf = p - PMBHSZ;
+ ret = ((PMBHeader *) nativebuf)->size - PMBHSZ;
+ }
+ return ret;
+}