forked from geodmms/xdgnjobs

unknown
2014-08-04 be3f6f9762dd15177477a7bcf6e11a2e27c70e57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.ximple.eofms.util;
 
//~--- JDK imports ------------------------------------------------------------
 
import java.nio.ByteOrder;
import java.security.AccessController;
import java.security.PrivilegedAction;
 
import sun.misc.VM;
 
/**
 * Bits
 * User: Ulysses
 * Date: 2007/6/17
 * Time: 01:16:39
 */
public class Bits {
    // -- Unsafe access --
    // private static final Unsafe unsafe = Unsafe.getUnsafe();
 
    // -- Processor and memory-system properties --
    private static ByteOrder byteOrder = null;
    private static int pageSize = -1;
    private static boolean unaligned;
    private static boolean unalignedKnown = false;
 
    // -- Direct memory management --
    // A user-settable upper limit on the maximum amount of allocatable
    // direct buffer memory. This value may be changed during VM
    // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>".
    private static volatile long maxMemory = VM.maxDirectMemory();
    private static volatile long reservedMemory = 0;
    private static boolean memoryLimitSet = false;
 
    private Bits() {
    }
 
    // -- Swapping --
    public static short swap(short x) {
        return (short) ((x << 8) | ((x >> 8) & 0xff));
    }
 
    public static char swap(char x) {
        return (char) ((x << 8) | ((x >> 8) & 0xff));
    }
 
    public static int swap(int x) {
        return (int) ((swap((short) x) << 16) | (swap((short) (x >> 16)) & 0xffff));
    }
 
    public static long swap(long x) {
        return (long) (((long) swap((int) (x)) << 32) | ((long) swap((int) (x >> 32)) & 0xffffffffL));
    }
 
    // -- get/put char --
    static private char makeChar(byte b1, byte b0) {
        return (char) ((b1 << 8) | (b0 & 0xff));
    }
 
    private static byte char1(char x) {
        return (byte) (x >> 8);
    }
 
    private static byte char0(char x) {
        return (byte) (x >> 0);
    }
 
    // --get/put short--
    public static short makeShort(byte b1, byte b0) {
        return (short) ((b1 << 8) | (b0 & 0xff));
    }
 
    private static byte short1(short x) {
        return (byte) (x >> 8);
    }
 
    public static byte short0(short x) {
        return (byte) (x >> 0);
    }
 
    // -- get/put int --
    public static int makeInt(byte b3, byte b2, byte b1, byte b0) {
        return (int) ((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b1 & 0xff) << 8) | ((b0 & 0xff) << 0)));
    }
 
    public static int makeInt(short hiword, short loword) {
        return ((hiword & 0xffff) << 16) + (loword & 0xffff);
    }
 
    public static short getHiShort(int qwValue) {
        return ((short) (qwValue >>> 16));
    }
 
    public static short getLoShort(int qwValue) {
        return ((short) (qwValue & 0xFFFF));
    }
 
    public static byte int3(int x) {
        return (byte) (x >> 24);
    }
 
    public static byte int2(int x) {
        return (byte) (x >> 16);
    }
 
    private static byte int1(int x) {
        return (byte) (x >> 8);
    }
 
    private static byte int0(int x) {
        return (byte) (x >> 0);
    }
 
    // -- get/put long --
    public static long makeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0) {
        return ((((long) b7 & 0xff) << 56) | (((long) b6 & 0xff) << 48) | (((long) b5 & 0xff) << 40) | (((long) b4 & 0xff) << 32)
            | (((long) b3 & 0xff) << 24) | (((long) b2 & 0xff) << 16) | (((long) b1 & 0xff) << 8)
            | (((long) b0 & 0xff) << 0));
    }
 
    public static long makeLong(int LoValue, int HiValue) {
        return (((long) HiValue & 0xFFFFFFFF) << 32) + (((long) LoValue) & 0xFFFFFFFF);
    }
 
    public static int getHiInt(long qwValue) {
        return ((int) (qwValue >>> 32));
    }
 
    public static int getLoInt(long qwValue) {
        return ((int) (qwValue & 0xFFFFFFFF));
    }
 
    private static byte long7(long x) {
        return (byte) (x >> 56);
    }
 
    private static byte long6(long x) {
        return (byte) (x >> 48);
    }
 
    private static byte long5(long x) {
        return (byte) (x >> 40);
    }
 
    private static byte long4(long x) {
        return (byte) (x >> 32);
    }
 
    private static byte long3(long x) {
        return (byte) (x >> 24);
    }
 
    private static byte long2(long x) {
        return (byte) (x >> 16);
    }
 
    private static byte long1(long x) {
        return (byte) (x >> 8);
    }
 
    private static byte long0(long x) {
        return (byte) (x >> 0);
    }
 
    // -- get/put float --
 
    // -- get/put double --
 
    /*
    private static byte _get(long a)
    {
        return unsafe.getByte(a);
    }
 
    private static void _put(long a, byte b)
    {
        unsafe.putByte(a, b);
    }
 
    static Unsafe unsafe()
    {
        return unsafe;
    }
 
    static ByteOrder byteOrder()
    {
        if (byteOrder != null)
        {
            return byteOrder;
        }
 
        long a = unsafe.allocateMemory(8);
 
        try
        {
            unsafe.putLong(a, 0x0102030405060708L);
 
            byte b = unsafe.getByte(a);
 
            switch (b)
            {
            case 0x01 :
                byteOrder = ByteOrder.BIG_ENDIAN;
 
                break;
 
            case 0x08 :
                byteOrder = ByteOrder.LITTLE_ENDIAN;
 
                break;
 
            default :
                throw new Error("Unknown byte order");
            }
        } finally
        {
            unsafe.freeMemory(a);
        }
 
        return byteOrder;
    }
    */
 
    static boolean unaligned() {
        if (unalignedKnown) {
            return unaligned;
        }
 
        PrivilegedAction pa = new sun.security.action.GetPropertyAction("os.arch");
        String arch = (String) AccessController.doPrivileged(pa);
 
        unaligned = arch.equals("i386") || arch.equals("x86");
        unalignedKnown = true;
 
        return unaligned;
    }
 
    // These methods should be called whenever direct memory is allocated or
    //  freed. They allow the user to control the amount of direct memory
    // which a process may access. All sizes are specified in bytes.
    static void reserveMemory(long size) {
        synchronized (Bits.class) {
            if (!memoryLimitSet && VM.isBooted()) {
                maxMemory = VM.maxDirectMemory();
                memoryLimitSet = true;
            }
 
            if (size <= maxMemory - reservedMemory) {
                reservedMemory += size;
 
                return;
            }
        }
 
        System.gc();
 
        try {
            Thread.sleep(100);
        } catch (InterruptedException x) {
            // Restore interrupt status
            Thread.currentThread().interrupt();
        }
 
        synchronized (Bits.class) {
            if (reservedMemory + size > maxMemory) {
                throw new OutOfMemoryError("Direct buffer memory");
            }
 
            reservedMemory += size;
        }
    }
 
    static synchronized void unreserveMemory(long size) {
        if (reservedMemory > 0) {
            reservedMemory -= size;
            assert (reservedMemory > -1);
        }
    }
}