Author: mturk
Date: Mon Oct 19 12:47:40 2009
New Revision: 826654
URL: http://svn.apache.org/viewvc?rev=826654&view=rev
Log:
Add memalign API
Modified:
commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h
commons/sandbox/runtime/trunk/src/main/native/os/unix/memalign.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/memalign.c
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h?rev=826654&r1=826653&r2=826654&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h Mon Oct
19 12:47:40 2009
@@ -87,6 +87,18 @@
void *mem);
/**
+ * Apache's "replacement" for the memalign() function that throws
+ * thejava.lang.OutOfMemoryError in case of failure.
+ * @param env Current JNI environment.
+ * @param size Allocation size
+ * @param file Source file where the function was called
+ * @param line Source file line where the function was called
+ * @return Pointer to allocated memory.
+ */
+ACR_DECLARE(void*) ACR_Memalign(JNIEnv *env, const char *file, int line,
+ size_t size);
+
+/**
* Create Small Block Heap
* @param env Current JNI environment.
* @param buff Optional memory buffer to use. It has be larger then
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/memalign.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/memalign.c?rev=826654&r1=826653&r2=826654&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/memalign.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/memalign.c Mon Oct 19
12:47:40 2009
@@ -37,34 +37,46 @@
}
}
-ACR_JNI_EXPORT_DECLARE(jobject, AlignedMemory, malloc0)(ACR_JNISTDARGS,
- jlong siz)
+ACR_DECLARE(void*) ACR_Memalign(JNIEnv *_E, const char *file, int line,
+ size_t size)
{
- jobject ptr = NULL;
- void *mem;
- acr_size_t ass = (acr_size_t)ACR_ALIGN(siz, acr_page_size);
+ void *mem = NULL;
+ acr_size_t ass = (acr_size_t)ACR_ALIGN(size, acr_page_size);
- UNREFERENCED_O;
- if (ass < (acr_size_t)siz) {
+ if (ass < (acr_size_t)size) {
/* Guard against faulty size value */
- ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ ACR_ThrowException(_E, file, line, ACR_EX_EINVAL, 0);
return NULL;
}
#if HAVE_POSIX_MEMALIGN
if (posix_memalign(&mem, acr_page_size, ass)) {
- ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENOMEM, ACR_GET_OS_ERROR());
+ ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, ACR_GET_OS_ERROR());
return NULL;
}
#else
mem = memalign(acr_page_size, ass);
if (!mem) {
- ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENOMEM, ACR_GET_OS_ERROR());
- return NULL;
+ ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, ACR_GET_OS_ERROR());
}
#endif
- ptr = ACR_NewBasicPointer(_E, mem, ass, aligned_pointer_cleanup);
- if (!ptr) {
- free(mem);
+ return mem;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, AlignedMemory, malloc0)(ACR_JNISTDARGS,
+ jlong siz)
+{
+ void *mem;
+ jobject ptr = NULL;
+ acr_size_t ass = (acr_size_t)ACR_ALIGN(siz, acr_page_size);
+
+ UNREFERENCED_O;
+
+ mem = ACR_Memalign(_E, THROW_FMARK, ass);
+ if (mem) {
+ ptr = ACR_NewBasicPointer(_E, mem, ass, aligned_pointer_cleanup);
+ if (!ptr) {
+ free(mem);
+ }
}
return ptr;
}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/memalign.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/memalign.c?rev=826654&r1=826653&r2=826654&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/memalign.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/memalign.c Mon Oct
19 12:47:40 2009
@@ -37,17 +37,35 @@
}
}
+ACR_DECLARE(void*) ACR_Memalign(JNIEnv *_E, const char *file, int line,
+ size_t size)
+{
+ void *mem;
+ acr_size_t ass = (acr_size_t)ACR_ALIGN(size, acr_page_size);
+
+ if (ass < (acr_size_t)size) {
+ /* Guard against faulty size value */
+ ACR_ThrowException(_E, file, line, ACR_EX_EINVAL, 0);
+ return NULL;
+ }
+ mem = VirtualAlloc(NULL, ass,
+ MEM_COMMIT | MEM_RESERVE,
+ PAGE_READWRITE);
+ if (!mem) {
+ ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, ACR_GET_OS_ERROR());
+ }
+ return mem;
+}
+
ACR_JNI_EXPORT_DECLARE(jobject, AlignedMemory, malloc0)(ACR_JNISTDARGS,
jlong siz)
{
- jobject ptr = NULL;
- void *mem;
+ void *mem;
+ jobject ptr = NULL;
acr_size_t ass = (acr_size_t)ACR_ALIGN(siz, acr_page_size);
UNREFERENCED_O;
- mem = VirtualAlloc(NULL, ass,
- MEM_COMMIT | MEM_RESERVE,
- PAGE_READWRITE);
+ mem = ACR_Memalign(_E, THROW_FMARK, ass);
if (mem) {
/* Create the Pointer class with default cleanup.
*/
@@ -56,7 +74,6 @@
VirtualFree(mem, 0, MEM_RELEASE);
}
}
- else
- ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENOMEM, ACR_GET_OS_ERROR());
return ptr;
}
+