Author: raffaeleguidi Date: Thu Dec 8 11:14:53 2011 New Revision: 1211830 URL: http://svn.apache.org/viewvc?rev=1211830&view=rev Log: Added a MemoryManager.allocate(<size>) that returns a Pointer. The pointer in this case contains a directBuffer property which contains a slice() of the original ByteBuffer with the limit() set to <size>. The method does not allow to express an expiry date (yet).
Fixes https://issues.apache.org/jira/browse/DIRECTMEMORY-37 Added: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTests.java incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTests.java Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java Thu Dec 8 11:14:53 2011 @@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory; public class Cache { - private static Logger logger = LoggerFactory.getLogger(Cache.class); + private static Logger logger = LoggerFactory.getLogger(Cache.class); private static MemoryManagerService memoryManager = MemoryManager.getMemoryManager(); private static CacheService cacheService = new CacheServiceImpl(memoryManager); Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java Thu Dec 8 11:14:53 2011 @@ -83,4 +83,8 @@ public class MemoryManager { public static MemoryManagerService getMemoryManager() { return memoryManager; } + + public static Pointer allocate(int size) { + return memoryManager.allocate(size ,-1, -1); //add a version with expiry + } } Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java Thu Dec 8 11:14:53 2011 @@ -48,4 +48,6 @@ public interface MemoryManagerService { public OffHeapMemoryBuffer getActiveBuffer(); + public Pointer allocate(int size, int expiresIn, int expires); + } Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java Thu Dec 8 11:14:53 2011 @@ -127,4 +127,19 @@ public class MemoryManagerServiceImpl im public void setActiveBuffer(OffHeapMemoryBuffer activeBuffer) { this.activeBuffer = activeBuffer; } + +@Override +public Pointer allocate(int size, int expiresIn, int expires) { + Pointer p = activeBuffer.allocate(size, expiresIn, expires); + if (p == null) { + if (activeBuffer.bufferNumber + 1 == buffers.size()) { + return null; + } else { + // try next buffer + activeBuffer = buffers.get(activeBuffer.bufferNumber + 1); + p = activeBuffer.allocate(size, expiresIn, expires); + } + } + return p; +} } Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java Thu Dec 8 11:14:53 2011 @@ -281,5 +281,36 @@ public class OffHeapMemoryBuffer { free(pointer); return store(payload); } + + public Pointer allocate(int size, int expiresIn, int expires) { + Pointer goodOne = firstMatch(size); + + if (goodOne == null ) { + allocationErrors++; + throw new NullPointerException("did not find a suitable buffer " + allocationErrors + " times since last cleanup"); + } + + Pointer fresh = slice(goodOne, size); + + + fresh.created = System.currentTimeMillis(); + if (expiresIn > 0) { + fresh.expiresIn = expiresIn; + fresh.expires = 0; + } else if (expires > 0) { + fresh.expiresIn = 0; + fresh.expires = expires; + } + + fresh.free = false; + used.addAndGet(size); + ByteBuffer buf = buffer.slice(); + buf.position(fresh.start); + buf.limit(size); + + fresh.directBuffer = buf; + pointers.add(fresh); + return fresh; + } } Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java Thu Dec 8 11:14:53 2011 @@ -1,5 +1,7 @@ package org.apache.directmemory.memory; +import java.nio.ByteBuffer; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -30,6 +32,7 @@ public class Pointer { public long lastHit; public int bufferNumber; public Class<? extends Object> clazz; + public ByteBuffer directBuffer = null; public byte[] content() { return null; Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTests.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTests.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTests.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTests.java Thu Dec 8 11:14:53 2011 @@ -30,15 +30,12 @@ import org.apache.directmemory.memory.Of import org.apache.directmemory.memory.Pointer; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.MethodRule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.carrotsearch.junitbenchmarks.AbstractBenchmark; import com.carrotsearch.junitbenchmarks.BenchmarkOptions; -import com.carrotsearch.junitbenchmarks.BenchmarkRule; import com.carrotsearch.junitbenchmarks.annotation.AxisRange; import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart; import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart; Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTests.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTests.java?rev=1211830&r1=1211829&r2=1211830&view=diff ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTests.java (original) +++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTests.java Thu Dec 8 11:14:53 2011 @@ -19,9 +19,7 @@ package org.apache.directmemory.memory.t * under the License. */ -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - +import static org.junit.Assert.*; import java.util.Map; import java.util.Random; Added: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java?rev=1211830&view=auto ============================================================================== --- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java (added) +++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java Thu Dec 8 11:14:53 2011 @@ -0,0 +1,71 @@ +package org.apache.directmemory.memory.test; + +/* + * 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. + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.nio.ByteBuffer; +import java.util.Random; + +import org.apache.directmemory.measures.Ram; +import org.apache.directmemory.memory.MemoryManager; +import org.apache.directmemory.memory.Pointer; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NIOTests { + + private static Logger logger = LoggerFactory.getLogger(NIOTests.class); + + @BeforeClass + public static void init() { + logger.info("init"); + MemoryManager.init(1, Ram.Mb(100)); + } + + @Test + public void NIOTest() { + Random rnd = new Random(); + int size = rnd.nextInt(10) * (int)MemoryManager.capacity() / 100; + logger.info("payload size=" + Ram.inKb(size)); + Pointer p = MemoryManager.allocate(size); + ByteBuffer b = p.directBuffer; + logger.info("allocated"); + assertNotNull(p); + assertNotNull(b); + + assertTrue(b.isDirect()); + assertEquals(0, b.position()); + assertEquals(size, b.limit()); + + //assertEquals(size,p.end); + assertEquals(size,p.end-p.start); + assertEquals(size, MemoryManager.getActiveBuffer().used()); + MemoryManager.free(p); + assertEquals(0, MemoryManager.getActiveBuffer().used()); + logger.info("end"); + } + + +}
