Package org.apache.directmemory.memory.buffer

Examples of org.apache.directmemory.memory.buffer.MemoryBuffer


                throw new BufferOverflowException();
            }
        }

        // Try to allocate the given size
        final MemoryBuffer byteBuffer = slab.allocate( size );

        // If allocation succeed, return the buffer
        if ( byteBuffer != null )
        {
            return byteBuffer;
        }

        // Otherwise we have the option to allow in a bigger slab.
        if ( !allowAllocationToBiggerSlab )
        {
            if ( returnNullWhenNoBufferAvailable )
            {
                return null;
            }
            else
            {
                throw new BufferOverflowException();
            }
        }
        else
        {
            // We can try to allocate to a bigger slab.
            // size + 1 here because getSlabThatMatchTheSize do a size -1 and thus will return the same slab
            final int biggerSize = slab.getSliceSize() + 1;
            final FixedSizeByteBufferAllocatorImpl biggerSlab = getSlabThatMatchTheSize( biggerSize );
            if ( biggerSlab == null )
            {
                // We were already trying to allocate in the biggest slab
                if ( returnNullWhenNoBufferAvailable )
                {
                    return null;
                }
                else
                {
                    throw new BufferOverflowException();
                }
            }

            final MemoryBuffer secondByteBuffer = biggerSlab.allocate( size );

            if ( secondByteBuffer == null )
            {
                if ( returnNullWhenNoBufferAvailable )
                {
View Full Code Here


                else
                {
                    throw new BufferOverflowException();
                }
            }
            final MemoryBuffer buffer = allocator.allocate( payload.length );

            if ( buffer == null )
            {
                continue;
            }

            p = instanciatePointer( buffer, allocator.getNumber(), expiresIn, NEVER_EXPIRES );

            buffer.writerIndex( 0 );
            buffer.writeBytes( payload );

            used.addAndGet( payload.length );

        }
        while ( p == null );
View Full Code Here

            return null;
        }

        pointer.hit();

        final MemoryBuffer buf = pointer.getMemoryBuffer();
        buf.readerIndex( 0 );

        final byte[] swp = new byte[(int) buf.readableBytes()];
        buf.readBytes( swp );
        return swp;
    }
View Full Code Here

                {
                    throw new BufferOverflowException();
                }
            }

            final MemoryBuffer buffer = allocator.allocate( size );

            if ( buffer == null )
            {
                continue;
            }
View Full Code Here

    @Override
    public byte[] retrieve( Pointer<V> pointer )
    {
        final byte[] swp = new byte[(int) pointer.getSize()];

        MemoryBuffer memoryBuffer = pointer.getMemoryBuffer();
        memoryBuffer.readerIndex( 0 );
        memoryBuffer.readBytes( swp );

        return swp;
    }
View Full Code Here

        final int size1 = 4 * SMALL_PAYLOAD_LENGTH;
        final byte[] allocatedPayload1 = MemoryTestUtils.generateRandomPayload( size1 );
        final Pointer<Object> allocatedPointer1 = mms.allocate( Object.class, allocatedPayload1.length, -1, -1 );
        Assert.assertNotNull( allocatedPointer1 );
        final MemoryBuffer buffer1 = allocatedPointer1.getMemoryBuffer();
        Assert.assertNotNull( buffer1 );
        Assert.assertEquals( 0, buffer1.writerIndex() );
        Assert.assertEquals( size1, buffer1.capacity() );
        Assert.assertEquals( size1, buffer1.capacity() );
        buffer1.writeBytes( allocatedPayload1 );
        Assert.assertEquals( new String( allocatedPayload1 ), new String( mms.retrieve( allocatedPointer1 ) ) );

        final int size2 = 2 * SMALL_PAYLOAD_LENGTH;
        final byte[] allocatedPayload2 = MemoryTestUtils.generateRandomPayload( size2 );
        final Pointer<Object> allocatedPointer2 = mms.allocate( Object.class, allocatedPayload2.length, -1, -1 );
        Assert.assertNotNull( allocatedPointer2 );
        final MemoryBuffer buffer2 = allocatedPointer2.getMemoryBuffer();
        Assert.assertNotNull( buffer2 );
        Assert.assertEquals( size2, buffer2.capacity() );
        buffer2.writeBytes( allocatedPayload2 );
        Assert.assertEquals( new String( allocatedPayload2 ), new String( mms.retrieve( allocatedPointer2 ) ) );

        // Ensure the new allocation has not overwritten other data
        Assert.assertEquals( new String( payload2 ), new String( mms.retrieve( pointer2 ) ) );
        Assert.assertEquals( new String( payload3 ), new String( mms.retrieve( pointer3 ) ) );
View Full Code Here

        slabs.add( new FixedSizeByteBufferAllocatorImpl( 3, 1024, 1024, 1 ) );
       
       
        Allocator allocator = new SlabByteBufferAllocator( 0, slabs, false );
       
        MemoryBuffer bf1 = allocator.allocate( 250 );
        Assert.assertEquals( 256, bf1.maxCapacity() );
        Assert.assertEquals( 250, bf1.capacity() );
       
        MemoryBuffer bf2 = allocator.allocate( 251 );
        Assert.assertEquals( 256, bf2.maxCapacity() );
        Assert.assertEquals( 251, bf2.capacity() );
       
        MemoryBuffer bf3 = allocator.allocate( 200 );
        Assert.assertEquals( 256, bf3.maxCapacity() );
        Assert.assertEquals( 200, bf3.capacity() );
       
        MemoryBuffer bf4 = allocator.allocate( 100 );
        Assert.assertEquals( 128, bf4.maxCapacity() );
        Assert.assertEquals( 100, bf4.capacity() );
       
        MemoryBuffer bf5 = allocator.allocate( 550 );
        Assert.assertEquals( 1024, bf5.maxCapacity() );
        Assert.assertEquals( 550, bf5.capacity() );
       
        MemoryBuffer bf6 = allocator.allocate( 800 );
        Assert.assertNull( bf6 );

        allocator.free( bf5 );
       
        MemoryBuffer bf7 = allocator.allocate( 800 );
        Assert.assertEquals( 1024, bf7.maxCapacity() );
        Assert.assertEquals( 800, bf7.capacity() );
    
        allocator.close();
    }
View Full Code Here

        throws IOException
    {

        Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 5000, 256, 1 );

        MemoryBuffer bf1 = allocator.allocate( 250 );
        Assert.assertEquals( 256, bf1.maxCapacity() );
        Assert.assertEquals( 250, bf1.capacity() );

        MemoryBuffer bf2 = allocator.allocate( 251 );
        Assert.assertEquals( 256, bf2.maxCapacity() );
        Assert.assertEquals( 251, bf2.capacity() );

        MemoryBuffer bf3 = allocator.allocate( 200 );
        Assert.assertEquals( 256, bf3.maxCapacity() );
        Assert.assertEquals( 200, bf3.capacity() );

        MemoryBuffer bf4 = allocator.allocate( 2000 );
        Assert.assertNull( bf4 );

        MemoryBuffer bf5 = allocator.allocate( 298 );
        Assert.assertNull( bf5 );

        MemoryBuffer bf6 = allocator.allocate( 128 );
        Assert.assertEquals( 256, bf6.maxCapacity() );
        Assert.assertEquals( 128, bf6.capacity() );

        allocator.close();
    }
View Full Code Here

        throws IOException
    {

        Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );

        MemoryBuffer bf1 = allocator.allocate( 250 );
        Assert.assertEquals( 256, bf1.maxCapacity() );
        Assert.assertEquals( 250, bf1.capacity() );

        MemoryBuffer bf2 = allocator.allocate( 251 );
        Assert.assertEquals( 256, bf2.maxCapacity() );
        Assert.assertEquals( 251, bf2.capacity() );

        MemoryBuffer bf3 = allocator.allocate( 252 );
        Assert.assertEquals( 256, bf3.maxCapacity() );
        Assert.assertEquals( 252, bf3.capacity() );

        MemoryBuffer bf4 = allocator.allocate( 500 );
        Assert.assertNull( bf4 );

        allocator.free( bf1 );
        allocator.free( bf2 );

        MemoryBuffer bf5 = allocator.allocate( 500 );
        Assert.assertNull( bf5 );

        MemoryBuffer bf6 = allocator.allocate( 249 );
        Assert.assertEquals( 256, bf6.maxCapacity() );
        Assert.assertEquals( 249, bf6.capacity() );

        MemoryBuffer bf7 = allocator.allocate( 248 );
        Assert.assertEquals( 256, bf7.maxCapacity() );
        Assert.assertEquals( 248, bf7.capacity() );

        allocator.close();
    }
View Full Code Here

        Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );

        for ( int i = 0; i < 1000; i++ )
        {
            MemoryBuffer bf1 = allocator.allocate( 250 );
            Assert.assertEquals( 256, bf1.maxCapacity() );
            Assert.assertEquals( 250, bf1.capacity() );

            allocator.free( bf1 );
        }

        MemoryBuffer bf2 = allocator.allocate( 1000 );
        Assert.assertNull( bf2 );

        for ( int i = 0; i < 3; i++ )
        {
            MemoryBuffer bf3 = allocator.allocate( 250 );
            Assert.assertEquals( 256, bf3.maxCapacity() );
            Assert.assertEquals( 250, bf3.capacity() );

        }

        MemoryBuffer bf4 = allocator.allocate( 238 );
        Assert.assertNull( bf4 );

        allocator.close();
    }
View Full Code Here

TOP

Related Classes of org.apache.directmemory.memory.buffer.MemoryBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.