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