1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.procore.cluster;
\r
14 import java.util.Arrays;
\r
16 class AllocationException extends java.lang.RuntimeException {
\r
17 private static final long serialVersionUID = 9025410962959482008L;
\r
18 final Allocator allocator;
\r
21 AllocationException(Allocator allocator, int size) {
\r
22 super("Allocator out of memory with size = " + size);
\r
23 this.allocator = allocator;
\r
27 void increment(int inc) {
\r
31 public synchronized Throwable fillInStackTrace() {
\r
36 abstract class Allocator {
\r
37 abstract int getCapacity();
\r
39 abstract void resizeMemory(int capacity);
\r
41 abstract void backupResize(int capacity, int size, int copySize);
\r
43 abstract void backupFree();
\r
45 static final int MaxIncrement = 1000000;
\r
46 static final int MinIncrement = 100;
\r
47 private int size; // size of used array elements
\r
53 void setSize(int size) {
\r
54 assert (0 < size && size <= getCapacity());
\r
58 int newElement(int size) {
\r
59 int oldSize = this.size;
\r
60 int newSize = oldSize + size;
\r
61 if (newSize > getCapacity()) {
\r
62 throw new AllocationException(this, size);
\r
64 this.size = newSize;
\r
65 // System.out.println("newElement: old size=" + oldSize + ", new size="
\r
66 // + newSize + ", capacity=" + getCapacity());
\r
70 // ensures that there is space of esize elements
\r
71 // if esize = 0 then uses default increment
\r
72 void ensureSpace(int esize) {
\r
73 // System.out.println("Ensure space " + esize);
\r
74 int space = getCapacity() - this.size;
\r
75 int increment = (space > 0 && esize > 0) ? (esize - space)
\r
79 increment = Math.max(increment, MinIncrement);
\r
80 int defaultCapacity = Math.min(getCapacity() << 1, getCapacity()
\r
82 int newCapacity = Math.max(defaultCapacity, getCapacity() + increment);
\r
83 resizeMemory(newCapacity);
\r
87 class LongAllocator extends Allocator {
\r
88 LongAllocator releaseMemory() {
\r
94 private long[] backup;
\r
95 private long[] memory;
\r
96 static final int ClusterId = 0;
\r
97 static final int HeaderSize = 1;
\r
99 long[] getBackup() {
\r
103 long[] getMemory() {
\r
107 long getClusterId() {
\r
108 return memory[ClusterId];
\r
111 void setClusterId(long a) {
\r
112 memory[ClusterId] = a;
\r
115 LongAllocator(long[] longs, int size) {
\r
116 assert (null != longs);
\r
117 assert (longs.length >= size);
\r
118 assert (longs.length >= HeaderSize);
\r
119 this.backup = null;
\r
120 this.memory = longs;
\r
124 int getCapacity() {
\r
125 return memory.length;
\r
128 void resizeMemory(int capacity) {
\r
129 assert (capacity >= HeaderSize);
\r
130 assert (null == backup);
\r
131 // System.out.println("new longs(1) = " + capacity);
\r
134 t = new long[capacity];
\r
135 } catch (OutOfMemoryError e) {
\r
136 e.printStackTrace();
\r
139 // memory = Arrays.copyOf(memory, capacity);
\r
140 int copySize = Math.min(capacity, getSize());
\r
141 System.arraycopy(memory, 0, t, 0, copySize);
\r
146 void backupResize(int capacity, int size, int copySize) {
\r
147 assert (capacity >= HeaderSize);
\r
148 assert (capacity >= copySize);
\r
149 assert (capacity >= size);
\r
152 else if (size < HeaderSize)
\r
154 assert (size <= capacity);
\r
157 assert (copySize <= size);
\r
158 assert (copySize <= getSize());
\r
159 assert (null == backup);
\r
161 memory = new long[capacity];
\r
162 // System.out.println("new longs(2) = " + capacity);
\r
163 setSize(size); // uses memory.length
\r
164 System.arraycopy(backup, 0, memory, 0, copySize);
\r
167 void backupFree() {
\r
172 for (int i = 0; i < getSize(); ++i)
\r
173 System.out.println("longs[" + i + "]=" + memory[i]);
\r
174 System.out.println("longs capacity=" + memory.length);
\r
178 class IntAllocator extends Allocator {
\r
179 IntAllocator releaseMemory() {
\r
187 static final int ResourceHash = 0;
\r
188 static final int ObjectHash = 1;
\r
189 static final int HeaderSize = 2;
\r
191 int getResourceHash() {
\r
192 if (null == memory)
\r
194 return memory[ResourceHash];
\r
197 int getObjectHash() {
\r
198 if (null == memory)
\r
200 return memory[ObjectHash];
\r
203 void setResourceHash(int a) {
\r
204 memory[ResourceHash] = a;
\r
207 void setObjectHash(int a) {
\r
208 memory[ObjectHash] = a;
\r
211 IntAllocator(int[] ints, int size) {
\r
212 this.backup = null;
\r
213 this.memory = ints;
\r
217 int[] getBackup() {
\r
221 int[] getMemory() {
\r
225 int getCapacity() {
\r
226 if (null == memory)
\r
228 return memory.length;
\r
231 void resizeMemory(int capacity) {
\r
232 assert (capacity >= HeaderSize);
\r
233 assert (null == backup);
\r
234 memory = Arrays.copyOf(memory, capacity);
\r
237 void backupResize(int capacity, int size, int copySize) {
\r
238 assert (capacity >= HeaderSize);
\r
239 assert (capacity >= copySize);
\r
240 assert (capacity >= size);
\r
243 else if (size < HeaderSize)
\r
245 assert (size <= capacity);
\r
248 assert (copySize <= size);
\r
249 assert (copySize <= getSize());
\r
250 assert (null == backup);
\r
252 memory = new int[capacity];
\r
253 setSize(size); // uses memory.length
\r
254 System.arraycopy(backup, 0, memory, 0, copySize);
\r
257 void backupFree() {
\r
262 for (int i = 0; i < getSize(); ++i)
\r
263 System.out.println("ints[" + i + "]=" + memory[i]);
\r
264 System.out.println("ints capacity=" + memory.length);
\r
268 class ByteAllocator extends Allocator {
\r
269 ByteAllocator releaseMemory() {
\r
275 private byte[] backup;
\r
276 private byte[] memory;
\r
277 static final int Reserved = 0;
\r
278 static final int HeaderSize = 1;
\r
280 ByteAllocator(byte[] bytes, int size) {
\r
281 this.backup = null;
\r
282 this.memory = bytes;
\r
286 byte[] getBackup() {
\r
290 byte[] getMemory() {
\r
294 int getReserved() {
\r
295 return memory[Reserved];
\r
298 void setReserved(byte a) {
\r
299 memory[Reserved] = a;
\r
302 int getCapacity() {
\r
303 return memory.length;
\r
306 void resizeMemory(int capacity) {
\r
307 assert (capacity >= HeaderSize);
\r
308 assert (null == backup);
\r
309 memory = Arrays.copyOf(memory, capacity);
\r
312 void backupResize(int capacity, int size, int copySize) {
\r
313 assert (capacity >= HeaderSize);
\r
314 assert (capacity >= copySize);
\r
315 assert (capacity >= size);
\r
318 else if (size < HeaderSize)
\r
320 assert (size <= capacity);
\r
323 assert (copySize <= size);
\r
324 assert (copySize <= getSize());
\r
325 assert (null == backup);
\r
327 memory = new byte[capacity];
\r
328 setSize(size); // uses memory.length
\r
329 System.arraycopy(backup, 0, memory, 0, copySize);
\r
332 void backupFree() {
\r
337 for (int i = 0; i < getSize(); ++i) {
\r
338 short t = (short) (memory[i] & 0xFF);
\r
339 System.out.println("bytes[" + i + "]=" + t);
\r
341 System.out.println("bytes capacity=" + memory.length);
\r