ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From voze...@apache.org
Subject [06/12] ignite git commit: IGNITE-1282: Removed memory allocator interface.
Date Mon, 16 Nov 2015 08:51:05 GMT
IGNITE-1282: Removed memory allocator interface.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/2a60909e
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/2a60909e
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/2a60909e

Branch: refs/heads/ignite-1917
Commit: 2a60909e750dffe3a31c63f6512bf3dccd7ef5e6
Parents: 094ab72
Author: vozerov-gridgain <vozerov@gridgain.com>
Authored: Mon Nov 16 09:14:47 2015 +0300
Committer: vozerov-gridgain <vozerov@gridgain.com>
Committed: Mon Nov 16 09:14:47 2015 +0300

----------------------------------------------------------------------
 .../internal/portable/BinaryWriterExImpl.java   |  92 ++++++-----
 .../PortableThreadLocalMemoryAllocator.java     | 162 -------------------
 .../streams/PortableHeapOutputStream.java       |  51 ++----
 .../streams/PortableMemoryAllocator.java        | 153 +++++++++++++-----
 .../streams/PortableSimpleMemoryAllocator.java  |  66 --------
 .../GridPortableMarshallerSelfTest.java         |  14 +-
 6 files changed, 178 insertions(+), 360 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/2a60909e/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java
index 27e7743..f4cb1e6 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java
@@ -19,12 +19,13 @@ package org.apache.ignite.internal.portable;
 
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.binary.BinaryIdMapper;
-import org.apache.ignite.internal.portable.streams.PortableHeapOutputStream;
-import org.apache.ignite.internal.portable.streams.PortableOutputStream;
-import org.apache.ignite.internal.util.typedef.internal.A;
 import org.apache.ignite.binary.BinaryObjectException;
 import org.apache.ignite.binary.BinaryRawWriter;
 import org.apache.ignite.binary.BinaryWriter;
+import org.apache.ignite.internal.portable.streams.PortableHeapOutputStream;
+import org.apache.ignite.internal.portable.streams.PortableMemoryAllocator;
+import org.apache.ignite.internal.portable.streams.PortableOutputStream;
+import org.apache.ignite.internal.util.typedef.internal.A;
 import org.jetbrains.annotations.Nullable;
 
 import java.io.IOException;
@@ -105,13 +106,6 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
     /** Maximum offset which fits in 2 bytes. */
     private static final int MAX_OFFSET_2 = 1 << 16;
 
-    /** Thread-local schema. */
-    private static final ThreadLocal<SchemaHolder> SCHEMA = new ThreadLocal<SchemaHolder>()
{
-        @Override protected SchemaHolder initialValue() {
-            return new SchemaHolder();
-        }
-    };
-
     /** */
     private final PortableContext ctx;
 
@@ -143,7 +137,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
     private SchemaHolder schema;
 
     /** Schema ID. */
-    private int schemaId;
+    private int schemaId = FNV1_OFFSET_BASIS;
 
     /** Amount of written fields. */
     private int fieldCnt;
@@ -151,11 +145,40 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
     /** ID mapper. */
     private BinaryIdMapper idMapper;
 
+    private static final ThreadLocal<TLSContext> TLS_CTX = new ThreadLocal<TLSContext>()
{
+        @Override protected TLSContext initialValue() {
+            return new TLSContext();
+        }
+    };
+
+    private static class TLSContext {
+        public PortableMemoryAllocator.Chunk chunk = PortableMemoryAllocator.INSTANCE.chunk();
+        public SchemaHolder schema = new SchemaHolder();
+    }
+
     /**
      * @param ctx Context.
      */
     BinaryWriterExImpl(PortableContext ctx) {
-        this(ctx, new PortableHeapOutputStream(INIT_CAP));
+        TLSContext tlsCtx = TLS_CTX.get();
+
+        this.ctx = ctx;
+        this.out = new PortableHeapOutputStream(INIT_CAP, tlsCtx.chunk);
+        this.handles = new IdentityHashMap<>();
+
+        start = out.position();
+        schema = tlsCtx.schema;
+    }
+
+    /**
+     * @param ctx Context.
+     * @param typeId Type ID.
+     */
+    public BinaryWriterExImpl(PortableContext ctx, int typeId, boolean metaEnabled) {
+        this(ctx);
+
+        this.typeId = typeId;
+        this.metaEnabled = metaEnabled;
     }
 
     /**
@@ -163,7 +186,13 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
      * @param out Output stream.
      */
     BinaryWriterExImpl(PortableContext ctx, PortableOutputStream out) {
-        this(ctx, out, new IdentityHashMap<Object, Integer>());
+        this.ctx = ctx;
+        this.out = out;
+        this.handles = new IdentityHashMap<>();
+
+        start = out.position();
+
+        schema = TLS_CTX.get().schema;
     }
 
      /**
@@ -171,26 +200,17 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
       * @param out Output stream.
       * @param handles Handles.
       */
-     private BinaryWriterExImpl(PortableContext ctx, PortableOutputStream out, Map<Object,
Integer> handles) {
+     private BinaryWriterExImpl(PortableContext ctx, PortableOutputStream out, SchemaHolder
schema,
+         Map<Object, Integer> handles) {
          this.ctx = ctx;
          this.out = out;
+         this.schema = schema;
          this.handles = handles;
 
          start = out.position();
      }
 
     /**
-     * @param ctx Context.
-     * @param typeId Type ID.
-     */
-    public BinaryWriterExImpl(PortableContext ctx, int typeId, boolean metaEnabled) {
-        this(ctx);
-
-        this.typeId = typeId;
-        this.metaEnabled = metaEnabled;
-    }
-
-    /**
      * Close the writer releasing resources if necessary.
      */
     @Override public void close() {
@@ -345,7 +365,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
      * @param userType User type flag.
      */
     public void postWrite(boolean userType) {
-        if (schema != null) {
+        if (fieldCnt != 0) {
             // Write schema ID.
             out.writeInt(start + SCHEMA_ID_POS, schemaId);
 
@@ -562,7 +582,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
         if (obj == null)
             doWriteByte(NULL);
         else {
-            BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, handles);
+            BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles);
 
             writer.marshal(obj);
         }
@@ -1468,7 +1488,8 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
         if (obj == null)
             doWriteByte(NULL);
         else {
-            BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, new IdentityHashMap<Object,
Integer>());
+            BinaryWriterExImpl writer =
+                new BinaryWriterExImpl(ctx, out, schema, new IdentityHashMap<Object, Integer>());
 
             writer.marshal(obj);
         }
@@ -1783,19 +1804,6 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
     public void writeFieldId(int fieldId) {
         int fieldOff = out.position() - start;
 
-        if (schema == null) {
-            schema = SCHEMA.get();
-
-            if (schema == null) {
-                schema = new SchemaHolder();
-
-                SCHEMA.set(schema);
-            }
-
-            // Initialize offset when the first field is written.
-            schemaId = FNV1_OFFSET_BASIS;
-        }
-
         // Advance schema hash.
         int schemaId0 = schemaId ^ (fieldId & 0xFF);
         schemaId0 = schemaId0 * FNV1_PRIME;
@@ -1839,7 +1847,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
      * @return New writer.
      */
     public BinaryWriterExImpl newWriter(int typeId) {
-        BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, handles);
+        BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles);
 
         res.typeId = typeId;
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/2a60909e/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableThreadLocalMemoryAllocator.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableThreadLocalMemoryAllocator.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableThreadLocalMemoryAllocator.java
deleted file mode 100644
index 8f5bfb2..0000000
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableThreadLocalMemoryAllocator.java
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
- * 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.ignite.internal.portable;
-
-import org.apache.ignite.internal.portable.streams.PortableMemoryAllocator;
-import org.apache.ignite.internal.util.GridUnsafe;
-import org.apache.ignite.internal.util.typedef.internal.U;
-import sun.misc.Unsafe;
-
-import static org.apache.ignite.IgniteSystemProperties.IGNITE_MARSHAL_BUFFERS_RECHECK;
-
-/**
- * Thread-local memory allocator.
- */
-public class PortableThreadLocalMemoryAllocator implements PortableMemoryAllocator {
-    /** Memory allocator instance. */
-    public static final PortableThreadLocalMemoryAllocator THREAD_LOCAL_ALLOC =
-        new PortableThreadLocalMemoryAllocator();
-
-    /** Holders. */
-    private static final ThreadLocal<ByteArrayHolder> holders = new ThreadLocal<>();
-
-    /** Unsafe instance. */
-    protected static final Unsafe UNSAFE = GridUnsafe.unsafe();
-
-    /** Array offset: byte. */
-    protected static final long BYTE_ARR_OFF = UNSAFE.arrayBaseOffset(byte[].class);
-
-    /**
-     * Ensures singleton.
-     */
-    private PortableThreadLocalMemoryAllocator() {
-        // No-op.
-    }
-
-    /** {@inheritDoc} */
-    @Override public byte[] allocate(int size) {
-        ByteArrayHolder holder = holders.get();
-
-        if (holder == null)
-            holders.set(holder = new ByteArrayHolder());
-
-        if (holder.acquired)
-            return new byte[size];
-
-        holder.acquired = true;
-
-        if (holder.data == null || size > holder.data.length)
-            holder.data = new byte[size];
-
-        return holder.data;
-    }
-
-    /** {@inheritDoc} */
-    @Override public byte[] reallocate(byte[] data, int size) {
-        ByteArrayHolder holder = holders.get();
-
-        assert holder != null;
-
-        byte[] newData = new byte[size];
-
-        if (holder.data == data)
-            holder.data = newData;
-
-        UNSAFE.copyMemory(data, BYTE_ARR_OFF, newData, BYTE_ARR_OFF, data.length);
-
-        return newData;
-    }
-
-    /** {@inheritDoc} */
-    @Override public void release(byte[] data, int maxMsgSize) {
-        ByteArrayHolder holder = holders.get();
-
-        assert holder != null;
-
-        if (holder.data != data)
-            return;
-
-        holder.maxMsgSize = maxMsgSize;
-        holder.acquired = false;
-
-        holder.shrink();
-    }
-
-    /** {@inheritDoc} */
-    @Override public long allocateDirect(int size) {
-        return 0;
-    }
-
-    /** {@inheritDoc} */
-    @Override public long reallocateDirect(long addr, int size) {
-        return 0;
-    }
-
-    /** {@inheritDoc} */
-    @Override public void releaseDirect(long addr) {
-        // No-op
-    }
-
-    /**
-     * Checks whether a thread-local array is acquired or not.
-     * The function is used by Unit tests.
-     *
-     * @return {@code true} if acquired {@code false} otherwise.
-     */
-    public boolean isThreadLocalArrayAcquired() {
-        ByteArrayHolder holder = holders.get();
-
-        return holder != null && holder.acquired;
-    }
-
-    /**
-     * Thread-local byte array holder.
-     */
-    private static class ByteArrayHolder {
-        /** */
-        private static final Long CHECK_FREQ = Long.getLong(IGNITE_MARSHAL_BUFFERS_RECHECK,
10000);
-
-        /** Data array */
-        private byte[] data;
-
-        /** Max message size detected between checks. */
-        private int maxMsgSize;
-
-        /** Last time array size is checked. */
-        private long lastCheck = U.currentTimeMillis();
-
-        /** Whether the holder is acquired or not. */
-        private boolean acquired;
-
-        /**
-         * Shrinks array size if needed.
-         */
-        private void shrink() {
-            long now = U.currentTimeMillis();
-
-            if (now - lastCheck >= CHECK_FREQ) {
-                int halfSize = data.length >> 1;
-
-                if (maxMsgSize < halfSize)
-                    data = new byte[halfSize];
-
-                lastCheck = now;
-            }
-        }
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/2a60909e/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableHeapOutputStream.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableHeapOutputStream.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableHeapOutputStream.java
index 208ad33..49c5744 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableHeapOutputStream.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableHeapOutputStream.java
@@ -17,73 +17,40 @@
 
 package org.apache.ignite.internal.portable.streams;
 
-import static org.apache.ignite.internal.portable.PortableThreadLocalMemoryAllocator.DFLT_ALLOC;
-import static org.apache.ignite.internal.portable.PortableThreadLocalMemoryAllocator.THREAD_LOCAL_ALLOC;
-
 /**
  * Portable heap output stream.
  */
 public final class PortableHeapOutputStream extends PortableAbstractOutputStream {
-    /** Default capacity. */
-    private static final int DFLT_CAP = 1024;
-
     /** Allocator. */
-    private final PortableMemoryAllocator alloc;
+    private final PortableMemoryAllocator.Chunk chunk;
 
     /** Data. */
     private byte[] data;
 
     /**
      * Constructor.
-     */
-    public PortableHeapOutputStream() {
-        this(DFLT_CAP, DFLT_ALLOC);
-    }
-
-    /**
-     * Constructor.
      *
      * @param cap Initial capacity.
      */
     public PortableHeapOutputStream(int cap) {
-        this(cap, THREAD_LOCAL_ALLOC);
-    }
-
-    /**
-     * Constructor.
-     *
-     * @param cap Initial capacity.
-     * @param alloc Allocator.
-     */
-    public PortableHeapOutputStream(int cap, PortableMemoryAllocator alloc) {
-        data = alloc.allocate(cap);
-
-        this.alloc = alloc;
+        this(cap, PortableMemoryAllocator.INSTANCE.chunk());
     }
 
     /**
      * Constructor.
      *
-     * @param data Data.
+     * @param cap Capacity.
+     * @param chunk Chunk.
      */
-    public PortableHeapOutputStream(byte[] data) {
-        this(data, DFLT_ALLOC);
-    }
+    public PortableHeapOutputStream(int cap, PortableMemoryAllocator.Chunk chunk) {
+        this.chunk = chunk;
 
-    /**
-     * Constructor.
-     *
-     * @param data Data.
-     * @param alloc Allocator.
-     */
-    public PortableHeapOutputStream(byte[] data, PortableMemoryAllocator alloc) {
-        this.data = data;
-        this.alloc = alloc;
+        data = chunk.allocate(cap);
     }
 
     /** {@inheritDoc} */
     @Override public void close() {
-        alloc.release(data, pos);
+        chunk.release(data, pos);
     }
 
     /** {@inheritDoc} */
@@ -91,7 +58,7 @@ public final class PortableHeapOutputStream extends PortableAbstractOutputStream
         if (cnt > data.length) {
             int newCap = capacity(data.length, cnt);
 
-            data = alloc.reallocate(data, newCap);
+            data = chunk.reallocate(data, newCap);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/2a60909e/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableMemoryAllocator.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableMemoryAllocator.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableMemoryAllocator.java
index 7ddb457..e4e0dbf 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableMemoryAllocator.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableMemoryAllocator.java
@@ -17,60 +17,131 @@
 
 package org.apache.ignite.internal.portable.streams;
 
+import org.apache.ignite.internal.util.GridUnsafe;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import sun.misc.Unsafe;
+
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_MARSHAL_BUFFERS_RECHECK;
+
 /**
- * Portable memory allocator.
+ * Thread-local memory allocator.
  */
-public interface PortableMemoryAllocator {
-    /** Default memory allocator. */
-    public static final PortableMemoryAllocator DFLT_ALLOC = new PortableSimpleMemoryAllocator();
+public final class PortableMemoryAllocator {
+    /** Memory allocator instance. */
+    public static final PortableMemoryAllocator INSTANCE = new PortableMemoryAllocator();
 
-    /**
-     * Allocate memory.
-     *
-     * @param size Size.
-     * @return Data.
-     */
-    public byte[] allocate(int size);
+    /** Holders. */
+    private static final ThreadLocal<Chunk> holders = new ThreadLocal<>();
 
-    /**
-     * Reallocates memory.
-     *
-     * @param data Current data chunk.
-     * @param size New size required.
-     *
-     * @return Data.
-     */
-    public byte[] reallocate(byte[] data, int size);
+    /** Unsafe instance. */
+    protected static final Unsafe UNSAFE = GridUnsafe.unsafe();
 
-    /**
-     * Release memory.
-     *
-     * @param data Data.
-     * @param maxMsgSize Max message size sent during the time the allocator is used.
-     */
-    public void release(byte[] data, int maxMsgSize);
+    /** Array offset: byte. */
+    protected static final long BYTE_ARR_OFF = UNSAFE.arrayBaseOffset(byte[].class);
 
     /**
-     * Allocate memory.
-     *
-     * @param size Size.
-     * @return Address.
+     * Ensures singleton.
      */
-    public long allocateDirect(int size);
+    private PortableMemoryAllocator() {
+        // No-op.
+    }
+
+    public Chunk chunk() {
+        Chunk holder = holders.get();
+
+        if (holder == null)
+            holders.set(holder = new Chunk());
+
+        return holder;
+    }
 
     /**
-     * Reallocate memory.
+     * Checks whether a thread-local array is acquired or not.
+     * The function is used by Unit tests.
      *
-     * @param addr Address.
-     * @param size Size.
-     * @return Address.
+     * @return {@code true} if acquired {@code false} otherwise.
      */
-    public long reallocateDirect(long addr, int size);
+    public boolean isAcquired() {
+        Chunk holder = holders.get();
+
+        return holder != null && holder.acquired;
+    }
 
     /**
-     * Release memory.
-     *
-     * @param addr Address.
+     * Thread-local byte array holder.
      */
-    public void releaseDirect(long addr);
+    public static class Chunk {
+        /** */
+        private static final Long CHECK_FREQ = Long.getLong(IGNITE_MARSHAL_BUFFERS_RECHECK,
10000);
+
+        /** Data array */
+        private byte[] data;
+
+        /** Max message size detected between checks. */
+        private int maxMsgSize;
+
+        /** Last time array size is checked. */
+        private long lastCheck = U.currentTimeMillis();
+
+        /** Whether the holder is acquired or not. */
+        private boolean acquired;
+
+        /**
+         * Allocate.
+         *
+         * @param size Desired size.
+         * @return Data.
+         */
+        public byte[] allocate(int size) {
+            if (acquired)
+                return new byte[size];
+
+            acquired = true;
+
+            if (data == null || size > data.length)
+                data = new byte[size];
+
+            return data;
+        }
+
+        /**
+         * Reallocate.
+         *
+         * @param data Old data.
+         * @param size Size.
+         * @return New data.
+         */
+        public byte[] reallocate(byte[] data, int size) {
+            byte[] newData = new byte[size];
+
+            if (this.data == data)
+                this.data = newData;
+
+            UNSAFE.copyMemory(data, BYTE_ARR_OFF, newData, BYTE_ARR_OFF, data.length);
+
+            return newData;
+        }
+
+        /**
+         * Shrinks array size if needed.
+         */
+        public void release(byte[] data, int maxMsgSize) {
+            if (this.data != data)
+                return;
+
+            this.maxMsgSize = maxMsgSize;
+            this.acquired = false;
+
+            long now = U.currentTimeMillis();
+
+            if (now - this.lastCheck >= CHECK_FREQ) {
+                int halfSize = data.length >> 1;
+
+                if (this.maxMsgSize < halfSize)
+                    this.data = new byte[halfSize];
+
+                this.lastCheck = now;
+            }
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/2a60909e/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableSimpleMemoryAllocator.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableSimpleMemoryAllocator.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableSimpleMemoryAllocator.java
deleted file mode 100644
index 54d7b38..0000000
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableSimpleMemoryAllocator.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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.ignite.internal.portable.streams;
-
-import org.apache.ignite.internal.util.GridUnsafe;
-import sun.misc.Unsafe;
-
-/**
- * Naive implementation of portable memory allocator.
- */
-public class PortableSimpleMemoryAllocator implements PortableMemoryAllocator {
-    /** Unsafe. */
-    private static final Unsafe UNSAFE = GridUnsafe.unsafe();
-
-    /** Array offset: byte. */
-    protected static final long BYTE_ARR_OFF = UNSAFE.arrayBaseOffset(byte[].class);
-
-    /** {@inheritDoc} */
-    @Override public byte[] allocate(int size) {
-        return new byte[size];
-    }
-
-    /** {@inheritDoc} */
-    @Override public byte[] reallocate(byte[] data, int size) {
-        byte[] newData = new byte[size];
-
-        UNSAFE.copyMemory(data, BYTE_ARR_OFF, newData, BYTE_ARR_OFF, data.length);
-
-        return newData;
-    }
-
-    /** {@inheritDoc} */
-    @Override public void release(byte[] data, int maxMsgSize) {
-        // No-op.
-    }
-
-    /** {@inheritDoc} */
-    @Override public long allocateDirect(int size) {
-        return UNSAFE.allocateMemory(size);
-    }
-
-    /** {@inheritDoc} */
-    @Override public long reallocateDirect(long addr, int size) {
-        return UNSAFE.reallocateMemory(addr, size);
-    }
-
-    /** {@inheritDoc} */
-    @Override public void releaseDirect(long addr) {
-        UNSAFE.freeMemory(addr);
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/2a60909e/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
index b5a24b1..c7a340b 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
@@ -68,7 +68,7 @@ import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.jsr166.ConcurrentHashMap8;
 import sun.misc.Unsafe;
 
-import static org.apache.ignite.internal.portable.PortableThreadLocalMemoryAllocator.THREAD_LOCAL_ALLOC;
+import static org.apache.ignite.internal.portable.streams.PortableMemoryAllocator.INSTANCE;
 import static org.junit.Assert.assertArrayEquals;
 
 /**
@@ -2075,20 +2075,20 @@ public class GridPortableMarshallerSelfTest extends GridCommonAbstractTest
{
      */
     public void testThreadLocalArrayReleased() throws IgniteCheckedException {
         // Checking the writer directly.
-        assertEquals(false, THREAD_LOCAL_ALLOC.isThreadLocalArrayAcquired());
+        assertEquals(false, INSTANCE.isAcquired());
 
         try (BinaryWriterExImpl writer = new BinaryWriterExImpl(initPortableContext(new PortableMarshaller())))
{
-            assertEquals(true, THREAD_LOCAL_ALLOC.isThreadLocalArrayAcquired());
+            assertEquals(true, INSTANCE.isAcquired());
 
             writer.writeString("Thread local test");
 
             writer.array();
 
-            assertEquals(true, THREAD_LOCAL_ALLOC.isThreadLocalArrayAcquired());
+            assertEquals(true, INSTANCE.isAcquired());
         }
 
         // Checking the portable marshaller.
-        assertEquals(false, THREAD_LOCAL_ALLOC.isThreadLocalArrayAcquired());
+        assertEquals(false, INSTANCE.isAcquired());
 
         PortableMarshaller marsh = new PortableMarshaller();
 
@@ -2096,7 +2096,7 @@ public class GridPortableMarshallerSelfTest extends GridCommonAbstractTest
{
 
         marsh.marshal(new SimpleObject());
 
-        assertEquals(false, THREAD_LOCAL_ALLOC.isThreadLocalArrayAcquired());
+        assertEquals(false, INSTANCE.isAcquired());
 
         // Checking the builder.
         BinaryObjectBuilder builder = new BinaryObjectBuilderImpl(initPortableContext(new
PortableMarshaller()),
@@ -2106,7 +2106,7 @@ public class GridPortableMarshallerSelfTest extends GridCommonAbstractTest
{
 
         BinaryObject portableObj = builder.build();
 
-        assertEquals(false, THREAD_LOCAL_ALLOC.isThreadLocalArrayAcquired());
+        assertEquals(false, INSTANCE.isAcquired());
     }
 
     /**


Mime
View raw message