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