]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/Allocator.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / Allocator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.procore.cluster;
13
14 import java.util.Arrays;
15
16 class AllocationException extends java.lang.RuntimeException {
17         private static final long serialVersionUID = 9025410962959482008L;
18         final Allocator allocator;
19         int size;
20
21         AllocationException(Allocator allocator, int size) {
22                 super("Allocator out of memory with size = " + size);
23                 this.allocator = allocator;
24                 this.size = size;
25         }
26
27         void increment(int inc) {
28                 this.size += inc;
29         }
30         @Override
31         public synchronized Throwable fillInStackTrace() {
32             return this;
33         }
34 }
35
36 abstract class Allocator {
37         abstract int getCapacity();
38
39         abstract void resizeMemory(int capacity);
40
41         abstract void backupResize(int capacity, int size, int copySize);
42
43         abstract void backupFree();
44
45         static final int MaxIncrement = 1000000;
46         static final int MinIncrement = 100;
47         private int size; // size of used array elements
48
49         int getSize() {
50                 return size;
51         }
52
53         void setSize(int size) {
54                 assert (0 < size && size <= getCapacity());
55                 this.size = size;
56         }
57
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);
63                 }
64                 this.size = newSize;
65                 // System.out.println("newElement: old size=" + oldSize + ", new size="
66                 // + newSize + ", capacity=" + getCapacity());
67                 return oldSize;
68         }
69
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)
76                                 : MinIncrement;
77                 if (increment <= 0)
78                         return;
79                 increment = Math.max(increment, MinIncrement);
80                 int defaultCapacity = Math.min(getCapacity() << 1, getCapacity()
81                                 + MaxIncrement);
82                 int newCapacity = Math.max(defaultCapacity, getCapacity() + increment);
83                 resizeMemory(newCapacity);
84         }
85 }
86
87 class LongAllocator extends Allocator {
88         LongAllocator releaseMemory() {
89                 backup = null;
90                 memory = null;
91                 return null;
92         }
93
94         private long[] backup;
95         private long[] memory;
96         static final int ClusterId = 0;
97         static final int HeaderSize = 1;
98
99         long[] getBackup() {
100                 return backup;
101         }
102
103         long[] getMemory() {
104                 return memory;
105         }
106
107         long getClusterId() {
108                 return memory[ClusterId];
109         }
110
111         void setClusterId(long a) {
112                 memory[ClusterId] = a;
113         }
114
115         LongAllocator(long[] longs, int size) {
116                 assert (null != longs);
117                 assert (longs.length >= size);
118                 assert (longs.length >= HeaderSize);
119                 this.backup = null;
120                 this.memory = longs;
121                 setSize(size);
122         }
123
124         int getCapacity() {
125                 return memory.length;
126         }
127
128         void resizeMemory(int capacity) {
129                 assert (capacity >= HeaderSize);
130                 assert (null == backup);
131                 // System.out.println("new longs(1) = " + capacity);
132                 long[] t = null;
133                 try {
134                         t = new long[capacity];
135                 } catch (OutOfMemoryError e) {
136                         e.printStackTrace();
137                         throw e;
138                 }
139                 // memory = Arrays.copyOf(memory, capacity);
140                 int copySize = Math.min(capacity, getSize());
141                 System.arraycopy(memory, 0, t, 0, copySize);
142                 setSize(copySize);
143                 memory = t;
144         }
145
146         void backupResize(int capacity, int size, int copySize) {
147                 assert (capacity >= HeaderSize);
148                 assert (capacity >= copySize);
149                 assert (capacity >= size);
150                 if (size < 0)
151                         size = getSize();
152                 else if (size < HeaderSize)
153                         size = HeaderSize;
154                 assert (size <= capacity);
155                 if (copySize < 0)
156                         copySize = size;
157                 assert (copySize <= size);
158                 assert (copySize <= getSize());
159                 assert (null == backup);
160                 backup = memory;
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);
165         }
166
167         void backupFree() {
168                 backup = null;
169         }
170
171         void dump() {
172                 for (int i = 0; i < getSize(); ++i)
173                         System.out.println("longs[" + i + "]=" + memory[i]);
174                 System.out.println("longs capacity=" + memory.length);
175         }
176 }
177
178 class IntAllocator extends Allocator {
179         IntAllocator releaseMemory() {
180                 backup = null;
181                 memory = null;
182                 return null;
183         }
184
185         int[] backup;
186         int[] memory;
187         static final int ResourceHash = 0;
188         static final int ObjectHash = 1;
189         static final int HeaderSize = 2;
190
191         int getResourceHash() {
192                 if (null == memory)
193                         return 0;
194                 return memory[ResourceHash];
195         }
196
197         int getObjectHash() {
198                 if (null == memory)
199                         return 0;
200                 return memory[ObjectHash];
201         }
202
203         void setResourceHash(int a) {
204                 memory[ResourceHash] = a;
205         }
206
207         void setObjectHash(int a) {
208                 memory[ObjectHash] = a;
209         }
210
211         IntAllocator(int[] ints, int size) {
212                 this.backup = null;
213                 this.memory = ints;
214                 setSize(size);
215         }
216
217         int[] getBackup() {
218                 return backup;
219         }
220
221         int[] getMemory() {
222                 return memory;
223         }
224
225         int getCapacity() {
226                 if (null == memory)
227                         return 0;
228                 return memory.length;
229         }
230
231         void resizeMemory(int capacity) {
232                 assert (capacity >= HeaderSize);
233                 assert (null == backup);
234                 memory = Arrays.copyOf(memory, capacity);
235         }
236
237         void backupResize(int capacity, int size, int copySize) {
238                 assert (capacity >= HeaderSize);
239                 assert (capacity >= copySize);
240                 assert (capacity >= size);
241                 if (size < 0)
242                         size = getSize();
243                 else if (size < HeaderSize)
244                         size = HeaderSize;
245                 assert (size <= capacity);
246                 if (copySize < 0)
247                         copySize = size;
248                 assert (copySize <= size);
249                 assert (copySize <= getSize());
250                 assert (null == backup);
251                 backup = memory;
252                 memory = new int[capacity];
253                 setSize(size); // uses memory.length
254                 System.arraycopy(backup, 0, memory, 0, copySize);
255         }
256
257         void backupFree() {
258                 backup = null;
259         }
260
261         void dump() {
262                 for (int i = 0; i < getSize(); ++i)
263                         System.out.println("ints[" + i + "]=" + memory[i]);
264                 System.out.println("ints capacity=" + memory.length);
265         }
266 }
267
268 class ByteAllocator extends Allocator {
269         ByteAllocator releaseMemory() {
270                 backup = null;
271                 memory = null;
272                 return null;
273         }
274
275         private byte[] backup;
276         private byte[] memory;
277         static final int Reserved = 0;
278         static final int HeaderSize = 1;
279
280         ByteAllocator(byte[] bytes, int size) {
281                 this.backup = null;
282                 this.memory = bytes;
283                 setSize(size);
284         }
285
286         byte[] getBackup() {
287                 return backup;
288         }
289
290         byte[] getMemory() {
291                 return memory;
292         }
293
294         int getReserved() {
295                 return memory[Reserved];
296         }
297
298         void setReserved(byte a) {
299                 memory[Reserved] = a;
300         }
301
302         int getCapacity() {
303                 return memory.length;
304         }
305
306         void resizeMemory(int capacity) {
307                 assert (capacity >= HeaderSize);
308                 assert (null == backup);
309                 memory = Arrays.copyOf(memory, capacity);
310         }
311
312         void backupResize(int capacity, int size, int copySize) {
313                 assert (capacity >= HeaderSize);
314                 assert (capacity >= copySize);
315                 assert (capacity >= size);
316                 if (size < 0)
317                         size = getSize();
318                 else if (size < HeaderSize)
319                         size = HeaderSize;
320                 assert (size <= capacity);
321                 if (copySize < 0)
322                         copySize = size;
323                 assert (copySize <= size);
324                 assert (copySize <= getSize());
325                 assert (null == backup);
326                 backup = memory;
327                 memory = new byte[capacity];
328                 setSize(size); // uses memory.length
329                 System.arraycopy(backup, 0, memory, 0, copySize);
330         }
331
332         void backupFree() {
333                 backup = null;
334         }
335
336         void dump() {
337                 for (int i = 0; i < getSize(); ++i) {
338                         short t = (short) (memory[i] & 0xFF);
339                         System.out.println("bytes[" + i + "]=" + t);
340                 }
341                 System.out.println("bytes capacity=" + memory.length);
342         }
343 }