]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/Allocator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / Allocator.java
diff --git a/bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/Allocator.java b/bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/Allocator.java
new file mode 100644 (file)
index 0000000..f5f0fe6
--- /dev/null
@@ -0,0 +1,343 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.procore.cluster;\r
+\r
+import java.util.Arrays;\r
+\r
+class AllocationException extends java.lang.RuntimeException {\r
+       private static final long serialVersionUID = 9025410962959482008L;\r
+       final Allocator allocator;\r
+       int size;\r
+\r
+       AllocationException(Allocator allocator, int size) {\r
+               super("Allocator out of memory with size = " + size);\r
+               this.allocator = allocator;\r
+               this.size = size;\r
+       }\r
+\r
+       void increment(int inc) {\r
+               this.size += inc;\r
+       }\r
+       @Override\r
+       public synchronized Throwable fillInStackTrace() {\r
+           return this;\r
+       }\r
+}\r
+\r
+abstract class Allocator {\r
+       abstract int getCapacity();\r
+\r
+       abstract void resizeMemory(int capacity);\r
+\r
+       abstract void backupResize(int capacity, int size, int copySize);\r
+\r
+       abstract void backupFree();\r
+\r
+       static final int MaxIncrement = 1000000;\r
+       static final int MinIncrement = 100;\r
+       private int size; // size of used array elements\r
+\r
+       int getSize() {\r
+               return size;\r
+       }\r
+\r
+       void setSize(int size) {\r
+               assert (0 < size && size <= getCapacity());\r
+               this.size = size;\r
+       }\r
+\r
+       int newElement(int size) {\r
+               int oldSize = this.size;\r
+               int newSize = oldSize + size;\r
+               if (newSize > getCapacity()) {\r
+                       throw new AllocationException(this, size);\r
+               }\r
+               this.size = newSize;\r
+               // System.out.println("newElement: old size=" + oldSize + ", new size="\r
+               // + newSize + ", capacity=" + getCapacity());\r
+               return oldSize;\r
+       }\r
+\r
+       // ensures that there is space of esize elements\r
+       // if esize = 0 then uses default increment\r
+       void ensureSpace(int esize) {\r
+               // System.out.println("Ensure space " + esize);\r
+               int space = getCapacity() - this.size;\r
+               int increment = (space > 0 && esize > 0) ? (esize - space)\r
+                               : MinIncrement;\r
+               if (increment <= 0)\r
+                       return;\r
+               increment = Math.max(increment, MinIncrement);\r
+               int defaultCapacity = Math.min(getCapacity() << 1, getCapacity()\r
+                               + MaxIncrement);\r
+               int newCapacity = Math.max(defaultCapacity, getCapacity() + increment);\r
+               resizeMemory(newCapacity);\r
+       }\r
+}\r
+\r
+class LongAllocator extends Allocator {\r
+       LongAllocator releaseMemory() {\r
+               backup = null;\r
+               memory = null;\r
+               return null;\r
+       }\r
+\r
+       private long[] backup;\r
+       private long[] memory;\r
+       static final int ClusterId = 0;\r
+       static final int HeaderSize = 1;\r
+\r
+       long[] getBackup() {\r
+               return backup;\r
+       }\r
+\r
+       long[] getMemory() {\r
+               return memory;\r
+       }\r
+\r
+       long getClusterId() {\r
+               return memory[ClusterId];\r
+       }\r
+\r
+       void setClusterId(long a) {\r
+               memory[ClusterId] = a;\r
+       }\r
+\r
+       LongAllocator(long[] longs, int size) {\r
+               assert (null != longs);\r
+               assert (longs.length >= size);\r
+               assert (longs.length >= HeaderSize);\r
+               this.backup = null;\r
+               this.memory = longs;\r
+               setSize(size);\r
+       }\r
+\r
+       int getCapacity() {\r
+               return memory.length;\r
+       }\r
+\r
+       void resizeMemory(int capacity) {\r
+               assert (capacity >= HeaderSize);\r
+               assert (null == backup);\r
+               // System.out.println("new longs(1) = " + capacity);\r
+               long[] t = null;\r
+               try {\r
+                       t = new long[capacity];\r
+               } catch (OutOfMemoryError e) {\r
+                       e.printStackTrace();\r
+                       throw e;\r
+               }\r
+               // memory = Arrays.copyOf(memory, capacity);\r
+               int copySize = Math.min(capacity, getSize());\r
+               System.arraycopy(memory, 0, t, 0, copySize);\r
+               setSize(copySize);\r
+               memory = t;\r
+       }\r
+\r
+       void backupResize(int capacity, int size, int copySize) {\r
+               assert (capacity >= HeaderSize);\r
+               assert (capacity >= copySize);\r
+               assert (capacity >= size);\r
+               if (size < 0)\r
+                       size = getSize();\r
+               else if (size < HeaderSize)\r
+                       size = HeaderSize;\r
+               assert (size <= capacity);\r
+               if (copySize < 0)\r
+                       copySize = size;\r
+               assert (copySize <= size);\r
+               assert (copySize <= getSize());\r
+               assert (null == backup);\r
+               backup = memory;\r
+               memory = new long[capacity];\r
+               // System.out.println("new longs(2) = " + capacity);\r
+               setSize(size); // uses memory.length\r
+               System.arraycopy(backup, 0, memory, 0, copySize);\r
+       }\r
+\r
+       void backupFree() {\r
+               backup = null;\r
+       }\r
+\r
+       void dump() {\r
+               for (int i = 0; i < getSize(); ++i)\r
+                       System.out.println("longs[" + i + "]=" + memory[i]);\r
+               System.out.println("longs capacity=" + memory.length);\r
+       }\r
+}\r
+\r
+class IntAllocator extends Allocator {\r
+       IntAllocator releaseMemory() {\r
+               backup = null;\r
+               memory = null;\r
+               return null;\r
+       }\r
+\r
+       int[] backup;\r
+       int[] memory;\r
+       static final int ResourceHash = 0;\r
+       static final int ObjectHash = 1;\r
+       static final int HeaderSize = 2;\r
+\r
+       int getResourceHash() {\r
+               if (null == memory)\r
+                       return 0;\r
+               return memory[ResourceHash];\r
+       }\r
+\r
+       int getObjectHash() {\r
+               if (null == memory)\r
+                       return 0;\r
+               return memory[ObjectHash];\r
+       }\r
+\r
+       void setResourceHash(int a) {\r
+               memory[ResourceHash] = a;\r
+       }\r
+\r
+       void setObjectHash(int a) {\r
+               memory[ObjectHash] = a;\r
+       }\r
+\r
+       IntAllocator(int[] ints, int size) {\r
+               this.backup = null;\r
+               this.memory = ints;\r
+               setSize(size);\r
+       }\r
+\r
+       int[] getBackup() {\r
+               return backup;\r
+       }\r
+\r
+       int[] getMemory() {\r
+               return memory;\r
+       }\r
+\r
+       int getCapacity() {\r
+               if (null == memory)\r
+                       return 0;\r
+               return memory.length;\r
+       }\r
+\r
+       void resizeMemory(int capacity) {\r
+               assert (capacity >= HeaderSize);\r
+               assert (null == backup);\r
+               memory = Arrays.copyOf(memory, capacity);\r
+       }\r
+\r
+       void backupResize(int capacity, int size, int copySize) {\r
+               assert (capacity >= HeaderSize);\r
+               assert (capacity >= copySize);\r
+               assert (capacity >= size);\r
+               if (size < 0)\r
+                       size = getSize();\r
+               else if (size < HeaderSize)\r
+                       size = HeaderSize;\r
+               assert (size <= capacity);\r
+               if (copySize < 0)\r
+                       copySize = size;\r
+               assert (copySize <= size);\r
+               assert (copySize <= getSize());\r
+               assert (null == backup);\r
+               backup = memory;\r
+               memory = new int[capacity];\r
+               setSize(size); // uses memory.length\r
+               System.arraycopy(backup, 0, memory, 0, copySize);\r
+       }\r
+\r
+       void backupFree() {\r
+               backup = null;\r
+       }\r
+\r
+       void dump() {\r
+               for (int i = 0; i < getSize(); ++i)\r
+                       System.out.println("ints[" + i + "]=" + memory[i]);\r
+               System.out.println("ints capacity=" + memory.length);\r
+       }\r
+}\r
+\r
+class ByteAllocator extends Allocator {\r
+       ByteAllocator releaseMemory() {\r
+               backup = null;\r
+               memory = null;\r
+               return null;\r
+       }\r
+\r
+       private byte[] backup;\r
+       private byte[] memory;\r
+       static final int Reserved = 0;\r
+       static final int HeaderSize = 1;\r
+\r
+       ByteAllocator(byte[] bytes, int size) {\r
+               this.backup = null;\r
+               this.memory = bytes;\r
+               setSize(size);\r
+       }\r
+\r
+       byte[] getBackup() {\r
+               return backup;\r
+       }\r
+\r
+       byte[] getMemory() {\r
+               return memory;\r
+       }\r
+\r
+       int getReserved() {\r
+               return memory[Reserved];\r
+       }\r
+\r
+       void setReserved(byte a) {\r
+               memory[Reserved] = a;\r
+       }\r
+\r
+       int getCapacity() {\r
+               return memory.length;\r
+       }\r
+\r
+       void resizeMemory(int capacity) {\r
+               assert (capacity >= HeaderSize);\r
+               assert (null == backup);\r
+               memory = Arrays.copyOf(memory, capacity);\r
+       }\r
+\r
+       void backupResize(int capacity, int size, int copySize) {\r
+               assert (capacity >= HeaderSize);\r
+               assert (capacity >= copySize);\r
+               assert (capacity >= size);\r
+               if (size < 0)\r
+                       size = getSize();\r
+               else if (size < HeaderSize)\r
+                       size = HeaderSize;\r
+               assert (size <= capacity);\r
+               if (copySize < 0)\r
+                       copySize = size;\r
+               assert (copySize <= size);\r
+               assert (copySize <= getSize());\r
+               assert (null == backup);\r
+               backup = memory;\r
+               memory = new byte[capacity];\r
+               setSize(size); // uses memory.length\r
+               System.arraycopy(backup, 0, memory, 0, copySize);\r
+       }\r
+\r
+       void backupFree() {\r
+               backup = null;\r
+       }\r
+\r
+       void dump() {\r
+               for (int i = 0; i < getSize(); ++i) {\r
+                       short t = (short) (memory[i] & 0xFF);\r
+                       System.out.println("bytes[" + i + "]=" + t);\r
+               }\r
+               System.out.println("bytes capacity=" + memory.length);\r
+       }\r
+}\r