Author: mturk Date: Tue Apr 7 18:07:54 2009 New Revision: 762884 URL: http://svn.apache.org/viewvc?rev=762884&view=rev Log: Add DirectByteBuffer low level functions
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java (with props) Modified: commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java?rev=762884&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java Tue Apr 7 18:07:54 2009 @@ -0,0 +1,72 @@ +/* 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. + */ + +package org.apache.commons.runtime; + +import java.nio.ByteBuffer; + +/** DirectByteBuffer. + * + * @author Mladen Turk + */ +public final class DirectByteBuffer { + + /** + * Allocate a new ByteBuffer from memory + * @param size The amount of memory to allocate + * @return The ByteBuffer with allocated memory + */ + public static native ByteBuffer malloc(int size); + + /** + * Allocate a new ByteBuffer from memory and set all of the memory to 0 + * @param num Number of elements. + * @param size Length in bytes of each element. + * @return The ByteBuffer with allocated memory + */ + public static native ByteBuffer calloc(int num, int size); + + /** + * Allocate a new ByteBuffer from already allocated memory. + * <br />Allocated memory must be provided from call to the + * Stdlib.alloc or Stdlib.calloc methods. + * @param mem The memory to use + * @param size The amount of memory to use + * @return The ByteBuffer with attached memory + */ + public static native ByteBuffer create(long mem, int size); + + /** + * Deallocates or frees a memory block used by ByteBuffer + * <br /><b>Warning :</b> Call this method only on ByteBuffers + * that were created by calling Buffer.alloc or Buffer.calloc. + * @param buf Previously allocated ByteBuffer to be freed. + */ + public static native void free(ByteBuffer buf); + + /** + * Returns the memory address of the ByteBuffer. + * @param buf Previously allocated ByteBuffer. + */ + public static native long address(ByteBuffer buf); + + /** + * Returns the allocated memory size of the ByteBuffer. + * @param buf Previously allocated ByteBuffer. + */ + public static native long size(ByteBuffer buf); + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c?rev=762884&r1=762883&r2=762884&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c Tue Apr 7 18:07:54 2009 @@ -56,7 +56,7 @@ jint num, jint size) { - size_t sz = (size_t)APR_ALIGN_DEFAULT((size * num)); + size_t sz = (size_t)ACR_ALIGN_DEFAULT((size * num)); void *mem; UNREFERENCED_O; @@ -110,17 +110,17 @@ void *mem; UNREFERENCED_O; - if (!mem) { - ACR_ThrowNullPointerException(_E, THROW_FMARK, - "null address argument"); - return; - } + if ((mem = (*_E)->GetDirectBufferAddress(_E, bb)) != NULL) { /* This can cause core dump if address was * allocated using different method. */ free(mem); } + else { + ACR_ThrowNullPointerException(_E, THROW_FMARK, + "GetDirectBufferAddress is null"); + } } ACR_JNI_EXPORT_DECLARE(jlong, DirectByteBuffer, address)(ACR_JNISTDARGS,