~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/misc/Unsafe.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2006-2012 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
 
 
25
package sun.misc;
 
26
 
 
27
import cli.System.IntPtr;
 
28
import cli.System.Runtime.InteropServices.Marshal;
 
29
import cli.System.Security.Permissions.SecurityAction;
 
30
import cli.System.Security.Permissions.SecurityPermissionAttribute;
 
31
import ikvm.lang.Internal;
 
32
import java.lang.reflect.Field;
 
33
import java.lang.reflect.Modifier;
 
34
import java.lang.reflect.ReflectHelper;
 
35
import java.security.ProtectionDomain;
 
36
import java.util.ArrayList;
 
37
 
 
38
public final class Unsafe
 
39
{
 
40
    public static final int INVALID_FIELD_OFFSET = -1;
 
41
    public static final int ARRAY_BYTE_BASE_OFFSET = 0;
 
42
    // NOTE sun.corba.Bridge actually access this field directly (via reflection),
 
43
    // so the name must match the JDK name.
 
44
    private static final Unsafe theUnsafe = new Unsafe();
 
45
    private static final ArrayList<Field> fields = new ArrayList<Field>();
 
46
 
 
47
    private Unsafe() { }
 
48
 
 
49
    @ikvm.internal.HasCallerID
 
50
    public static Unsafe getUnsafe()
 
51
    {
 
52
        if(ikvm.internal.CallerID.getCallerID().getCallerClassLoader() != null)
 
53
        {
 
54
            throw new SecurityException("Unsafe");
 
55
        }
 
56
        return theUnsafe;
 
57
    }
 
58
 
 
59
    // this is the intrinsified version of objectFieldOffset(XXX.class.getDeclaredField("xxx"))
 
60
    public long objectFieldOffset(Class c, String field)
 
61
    {
 
62
        return fieldOffset(ReflectHelper.createFieldAndMakeAccessible(c, field));
 
63
    }
 
64
 
 
65
    // NOTE we have a really lame (and slow) implementation!
 
66
    public long objectFieldOffset(Field field)
 
67
    {
 
68
        if(Modifier.isStatic(field.getModifiers()))
 
69
        {
 
70
            throw new IllegalArgumentException();
 
71
        }
 
72
        return fieldOffset(field);
 
73
    }
 
74
 
 
75
    @Deprecated
 
76
    public int fieldOffset(Field original)
 
77
    {
 
78
        Field copy = ReflectHelper.copyFieldAndMakeAccessible(original);
 
79
        synchronized(fields)
 
80
        {
 
81
            int id = fields.size();
 
82
            fields.add(copy);
 
83
            return id;
 
84
        }
 
85
    }
 
86
 
 
87
    public int arrayBaseOffset(Class c)
 
88
    {
 
89
        // don't change this, the Unsafe intrinsics depend on this value
 
90
        return 0;
 
91
    }
 
92
 
 
93
    public int arrayIndexScale(Class c)
 
94
    {
 
95
        // don't change this, the Unsafe intrinsics depend on this value
 
96
        return 1;
 
97
    }
 
98
 
 
99
    private static Field getField(long offset)
 
100
    {
 
101
        synchronized(fields)
 
102
        {
 
103
            return fields.get((int)offset);
 
104
        }
 
105
    }
 
106
 
 
107
    public boolean compareAndSwapObject(Object obj, long offset, Object expect, Object update)
 
108
    {
 
109
        if(obj instanceof Object[])
 
110
        {
 
111
            Object[] arr = (Object[])obj;
 
112
            int index = (int)offset;
 
113
            synchronized(this)
 
114
            {
 
115
                if(arr[index] == expect)
 
116
                {
 
117
                    arr[index] = update;
 
118
                    return true;
 
119
                }
 
120
                return false;
 
121
            }
 
122
        }
 
123
        else
 
124
        {
 
125
            Field field = getField(offset);
 
126
            synchronized(field)
 
127
            {
 
128
                try
 
129
                {
 
130
                    if(field.get(obj) == expect)
 
131
                    {
 
132
                        field.set(obj, update);
 
133
                        return true;
 
134
                    }
 
135
                    return false;
 
136
                }
 
137
                catch(IllegalAccessException x)
 
138
                {
 
139
                    throw (InternalError)new InternalError().initCause(x);
 
140
                }
 
141
            }
 
142
        }
 
143
    }
 
144
 
 
145
    public void putObjectVolatile(Object obj, long offset, Object newValue)
 
146
    {
 
147
        if(obj instanceof Object[])
 
148
        {
 
149
            synchronized(this)
 
150
            {
 
151
                ((Object[])obj)[(int)offset] = newValue;
 
152
            }
 
153
        }
 
154
        else
 
155
        {
 
156
            Field field = getField(offset);
 
157
            synchronized(field)
 
158
            {
 
159
                try
 
160
                {
 
161
                    field.set(obj, newValue);
 
162
                }
 
163
                catch(IllegalAccessException x)
 
164
                {
 
165
                    throw (InternalError)new InternalError().initCause(x);
 
166
                }
 
167
            }
 
168
        }
 
169
    }
 
170
 
 
171
    public void putOrderedObject(Object obj, long offset, Object newValue)
 
172
    {
 
173
        putObjectVolatile(obj, offset, newValue);
 
174
    }
 
175
 
 
176
    public Object getObjectVolatile(Object obj, long offset)
 
177
    {
 
178
        if(obj instanceof Object[])
 
179
        {
 
180
            synchronized(this)
 
181
            {
 
182
                return ((Object[])obj)[(int)offset];
 
183
            }
 
184
        }
 
185
        else
 
186
        {
 
187
            Field field = getField(offset);
 
188
            synchronized(field)
 
189
            {
 
190
                try
 
191
                {
 
192
                    return field.get(obj);
 
193
                }
 
194
                catch(IllegalAccessException x)
 
195
                {
 
196
                    throw (InternalError)new InternalError().initCause(x);
 
197
                }
 
198
            }
 
199
        }
 
200
    }
 
201
 
 
202
    public boolean compareAndSwapInt(Object obj, long offset, int expect, int update)
 
203
    {
 
204
        if(obj instanceof int[])
 
205
        {
 
206
            int[] arr = (int[])obj;
 
207
            int index = (int)offset;
 
208
            synchronized(this)
 
209
            {
 
210
                if(arr[index] == expect)
 
211
                {
 
212
                    arr[index] = update;
 
213
                    return true;
 
214
                }
 
215
                return false;
 
216
            }
 
217
        }
 
218
        else
 
219
        {
 
220
            Field field = getField(offset);
 
221
            synchronized(field)
 
222
            {
 
223
                try
 
224
                {
 
225
                    if(field.getInt(obj) == expect)
 
226
                    {
 
227
                        field.setInt(obj, update);
 
228
                        return true;
 
229
                    }
 
230
                    return false;
 
231
                }
 
232
                catch(IllegalAccessException x)
 
233
                {
 
234
                    throw (InternalError)new InternalError().initCause(x);
 
235
                }
 
236
            }
 
237
        }
 
238
    }
 
239
 
 
240
    public void putIntVolatile(Object obj, long offset, int newValue)
 
241
    {
 
242
        if(obj instanceof int[])
 
243
        {
 
244
            synchronized(this)
 
245
            {
 
246
                ((int[])obj)[(int)offset] = newValue;
 
247
            }
 
248
        }
 
249
        else
 
250
        {
 
251
            Field field = getField(offset);
 
252
            synchronized(field)
 
253
            {
 
254
                try
 
255
                {
 
256
                    field.setInt(obj, newValue);
 
257
                }
 
258
                catch(IllegalAccessException x)
 
259
                {
 
260
                    throw (InternalError)new InternalError().initCause(x);
 
261
                }
 
262
            }
 
263
        }
 
264
    }
 
265
 
 
266
    public void putOrderedInt(Object obj, long offset, int newValue)
 
267
    {
 
268
        putIntVolatile(obj, offset, newValue);
 
269
    }
 
270
 
 
271
    public int getIntVolatile(Object obj, long offset)
 
272
    {
 
273
        if(obj instanceof int[])
 
274
        {
 
275
            synchronized(this)
 
276
            {
 
277
                return ((int[])obj)[(int)offset];
 
278
            }
 
279
        }
 
280
        else
 
281
        {
 
282
            Field field = getField(offset);
 
283
            synchronized(field)
 
284
            {
 
285
                try
 
286
                {
 
287
                    return field.getInt(obj);
 
288
                }
 
289
                catch(IllegalAccessException x)
 
290
                {
 
291
                    throw (InternalError)new InternalError().initCause(x);
 
292
                }
 
293
            }
 
294
        }
 
295
    }
 
296
 
 
297
    public boolean compareAndSwapLong(Object obj, long offset, long expect, long update)
 
298
    {
 
299
        if(obj instanceof long[])
 
300
        {
 
301
            long[] arr = (long[])obj;
 
302
            int index = (int)offset;
 
303
            synchronized(this)
 
304
            {
 
305
                if(arr[index] == expect)
 
306
                {
 
307
                    arr[index] = update;
 
308
                    return true;
 
309
                }
 
310
                return false;
 
311
            }
 
312
        }
 
313
        else
 
314
        {
 
315
            Field field = getField(offset);
 
316
            synchronized(field)
 
317
            {
 
318
                try
 
319
                {
 
320
                    if(field.getLong(obj) == expect)
 
321
                    {
 
322
                        field.setLong(obj, update);
 
323
                        return true;
 
324
                    }
 
325
                    return false;
 
326
                }
 
327
                catch(IllegalAccessException x)
 
328
                {
 
329
                    throw (InternalError)new InternalError().initCause(x);
 
330
                }
 
331
            }
 
332
        }
 
333
    }
 
334
 
 
335
    public void putLongVolatile(Object obj, long offset, long newValue)
 
336
    {
 
337
        if(obj instanceof long[])
 
338
        {
 
339
            synchronized(this)
 
340
            {
 
341
                ((long[])obj)[(int)offset] = newValue;
 
342
            }
 
343
        }
 
344
        else
 
345
        {
 
346
            Field field = getField(offset);
 
347
            synchronized(field)
 
348
            {
 
349
                try
 
350
                {
 
351
                    field.setLong(obj, newValue);
 
352
                }
 
353
                catch(IllegalAccessException x)
 
354
                {
 
355
                    throw (InternalError)new InternalError().initCause(x);
 
356
                }
 
357
            }
 
358
        }
 
359
    }
 
360
 
 
361
    public void putOrderedLong(Object obj, long offset, long newValue)
 
362
    {
 
363
        putLongVolatile(obj, offset, newValue);
 
364
    }
 
365
 
 
366
    public long getLongVolatile(Object obj, long offset)
 
367
    {
 
368
        if(obj instanceof long[])
 
369
        {
 
370
            synchronized(this)
 
371
            {
 
372
                return ((long[])obj)[(int)offset];
 
373
            }
 
374
        }
 
375
        else
 
376
        {
 
377
            Field field = getField(offset);
 
378
            synchronized(field)
 
379
            {
 
380
                try
 
381
                {
 
382
                    return field.getLong(obj);
 
383
                }
 
384
                catch(IllegalAccessException x)
 
385
                {
 
386
                    throw (InternalError)new InternalError().initCause(x);
 
387
                }
 
388
            }
 
389
        }
 
390
    }
 
391
 
 
392
    public void putBoolean(Object obj, long offset, boolean newValue)
 
393
    {
 
394
        if (obj instanceof boolean[])
 
395
        {
 
396
            ((boolean[])obj)[(int)offset] = newValue;
 
397
        }
 
398
        else
 
399
        {
 
400
            try
 
401
            {
 
402
                getField(offset).setBoolean(obj, newValue);
 
403
            }
 
404
            catch (IllegalAccessException x)
 
405
            {
 
406
                throw (InternalError)new InternalError().initCause(x);
 
407
            }
 
408
        }
 
409
    }
 
410
 
 
411
    public boolean getBoolean(Object obj, long offset)
 
412
    {
 
413
        if (obj instanceof boolean[])
 
414
        {
 
415
            return ((boolean[])obj)[(int)offset];
 
416
        }
 
417
        else
 
418
        {
 
419
            try
 
420
            {
 
421
                return getField(offset).getBoolean(obj);
 
422
            }
 
423
            catch (IllegalAccessException x)
 
424
            {
 
425
                throw (InternalError)new InternalError().initCause(x);
 
426
            }
 
427
        }
 
428
    }
 
429
 
 
430
    public void putByte(Object obj, long offset, byte newValue)
 
431
    {
 
432
        if (obj instanceof byte[])
 
433
        {
 
434
            ((byte[])obj)[(int)offset] = newValue;
 
435
        }
 
436
        else
 
437
        {
 
438
            try
 
439
            {
 
440
                getField(offset).setByte(obj, newValue);
 
441
            }
 
442
            catch (IllegalAccessException x)
 
443
            {
 
444
                throw (InternalError)new InternalError().initCause(x);
 
445
            }
 
446
        }
 
447
    }
 
448
 
 
449
    public byte getByte(Object obj, long offset)
 
450
    {
 
451
        if (obj instanceof byte[])
 
452
        {
 
453
            return ((byte[])obj)[(int)offset];
 
454
        }
 
455
        else
 
456
        {
 
457
            try
 
458
            {
 
459
                return getField(offset).getByte(obj);
 
460
            }
 
461
            catch (IllegalAccessException x)
 
462
            {
 
463
                throw (InternalError)new InternalError().initCause(x);
 
464
            }
 
465
        }
 
466
    }
 
467
 
 
468
    public void putChar(Object obj, long offset, char newValue)
 
469
    {
 
470
        if (obj instanceof char[])
 
471
        {
 
472
            ((char[])obj)[(int)offset] = newValue;
 
473
        }
 
474
        else
 
475
        {
 
476
            try
 
477
            {
 
478
                getField(offset).setChar(obj, newValue);
 
479
            }
 
480
            catch (IllegalAccessException x)
 
481
            {
 
482
                throw (InternalError)new InternalError().initCause(x);
 
483
            }
 
484
        }
 
485
    }
 
486
 
 
487
    public char getChar(Object obj, long offset)
 
488
    {
 
489
        if (obj instanceof char[])
 
490
        {
 
491
            return ((char[])obj)[(int)offset];
 
492
        }
 
493
        else
 
494
        {
 
495
            try
 
496
            {
 
497
                return getField(offset).getChar(obj);
 
498
            }
 
499
            catch (IllegalAccessException x)
 
500
            {
 
501
                throw (InternalError)new InternalError().initCause(x);
 
502
            }
 
503
        }
 
504
    }
 
505
 
 
506
    public void putShort(Object obj, long offset, short newValue)
 
507
    {
 
508
        if (obj instanceof short[])
 
509
        {
 
510
            ((short[])obj)[(int)offset] = newValue;
 
511
        }
 
512
        else
 
513
        {
 
514
            try
 
515
            {
 
516
                getField(offset).setShort(obj, newValue);
 
517
            }
 
518
            catch (IllegalAccessException x)
 
519
            {
 
520
                throw (InternalError)new InternalError().initCause(x);
 
521
            }
 
522
        }
 
523
    }
 
524
 
 
525
    public short getShort(Object obj, long offset)
 
526
    {
 
527
        if (obj instanceof short[])
 
528
        {
 
529
            return ((short[])obj)[(int)offset];
 
530
        }
 
531
        else
 
532
        {
 
533
            try
 
534
            {
 
535
                return getField(offset).getShort(obj);
 
536
            }
 
537
            catch (IllegalAccessException x)
 
538
            {
 
539
                throw (InternalError)new InternalError().initCause(x);
 
540
            }
 
541
        }
 
542
    }
 
543
 
 
544
    public void putInt(Object obj, long offset, int newValue)
 
545
    {
 
546
        if (obj instanceof int[])
 
547
        {
 
548
            ((int[])obj)[(int)offset] = newValue;
 
549
        }
 
550
        else
 
551
        {
 
552
            try
 
553
            {
 
554
                getField(offset).setInt(obj, newValue);
 
555
            }
 
556
            catch (IllegalAccessException x)
 
557
            {
 
558
                throw (InternalError)new InternalError().initCause(x);
 
559
            }
 
560
        }
 
561
    }
 
562
 
 
563
    public int getInt(Object obj, long offset)
 
564
    {
 
565
        if (obj instanceof int[])
 
566
        {
 
567
            return ((int[])obj)[(int)offset];
 
568
        }
 
569
        else
 
570
        {
 
571
            try
 
572
            {
 
573
                return getField(offset).getInt(obj);
 
574
            }
 
575
            catch (IllegalAccessException x)
 
576
            {
 
577
                throw (InternalError)new InternalError().initCause(x);
 
578
            }
 
579
        }
 
580
    }
 
581
 
 
582
    public void putFloat(Object obj, long offset, float newValue)
 
583
    {
 
584
        if (obj instanceof float[])
 
585
        {
 
586
            ((float[])obj)[(int)offset] = newValue;
 
587
        }
 
588
        else
 
589
        {
 
590
            try
 
591
            {
 
592
                getField(offset).setFloat(obj, newValue);
 
593
            }
 
594
            catch (IllegalAccessException x)
 
595
            {
 
596
                throw (InternalError)new InternalError().initCause(x);
 
597
            }
 
598
        }
 
599
    }
 
600
 
 
601
    public float getFloat(Object obj, long offset)
 
602
    {
 
603
        if (obj instanceof float[])
 
604
        {
 
605
            return ((float[])obj)[(int)offset];
 
606
        }
 
607
        else
 
608
        {
 
609
            try
 
610
            {
 
611
                return getField(offset).getFloat(obj);
 
612
            }
 
613
            catch (IllegalAccessException x)
 
614
            {
 
615
                throw (InternalError)new InternalError().initCause(x);
 
616
            }
 
617
        }
 
618
    }
 
619
 
 
620
    public void putLong(Object obj, long offset, long newValue)
 
621
    {
 
622
        if (obj instanceof long[])
 
623
        {
 
624
            ((long[])obj)[(int)offset] = newValue;
 
625
        }
 
626
        else
 
627
        {
 
628
            try
 
629
            {
 
630
                getField(offset).setLong(obj, newValue);
 
631
            }
 
632
            catch (IllegalAccessException x)
 
633
            {
 
634
                throw (InternalError)new InternalError().initCause(x);
 
635
            }
 
636
        }
 
637
    }
 
638
 
 
639
    public long getLong(Object obj, long offset)
 
640
    {
 
641
        if (obj instanceof long[])
 
642
        {
 
643
            return ((long[])obj)[(int)offset];
 
644
        }
 
645
        else
 
646
        {
 
647
            try
 
648
            {
 
649
                return getField(offset).getLong(obj);
 
650
            }
 
651
            catch (IllegalAccessException x)
 
652
            {
 
653
                throw (InternalError)new InternalError().initCause(x);
 
654
            }
 
655
        }
 
656
    }
 
657
 
 
658
    public void putDouble(Object obj, long offset, double newValue)
 
659
    {
 
660
        if (obj instanceof double[])
 
661
        {
 
662
            ((double[])obj)[(int)offset] = newValue;
 
663
        }
 
664
        else
 
665
        {
 
666
            try
 
667
            {
 
668
                getField(offset).setDouble(obj, newValue);
 
669
            }
 
670
            catch (IllegalAccessException x)
 
671
            {
 
672
                throw (InternalError)new InternalError().initCause(x);
 
673
            }
 
674
        }
 
675
    }
 
676
 
 
677
    public double getDouble(Object obj, long offset)
 
678
    {
 
679
        if (obj instanceof double[])
 
680
        {
 
681
            return ((double[])obj)[(int)offset];
 
682
        }
 
683
        else
 
684
        {
 
685
            try
 
686
            {
 
687
                return getField(offset).getDouble(obj);
 
688
            }
 
689
            catch (IllegalAccessException x)
 
690
            {
 
691
                throw (InternalError)new InternalError().initCause(x);
 
692
            }
 
693
        }
 
694
    }
 
695
 
 
696
    public void putObject(Object obj, long offset, Object newValue)
 
697
    {
 
698
        if (obj instanceof Object[])
 
699
        {
 
700
            ((Object[])obj)[(int)offset] = newValue;
 
701
        }
 
702
        else
 
703
        {
 
704
            try
 
705
            {
 
706
                getField(offset).set(obj, newValue);
 
707
            }
 
708
            catch (IllegalAccessException x)
 
709
            {
 
710
                throw (InternalError)new InternalError().initCause(x);
 
711
            }
 
712
        }
 
713
    }
 
714
 
 
715
    public Object getObject(Object obj, long offset)
 
716
    {
 
717
        if (obj instanceof Object[])
 
718
        {
 
719
            return ((Object[])obj)[(int)offset];
 
720
        }
 
721
        else
 
722
        {
 
723
            try
 
724
            {
 
725
                return getField(offset).get(obj);
 
726
            }
 
727
            catch (IllegalAccessException x)
 
728
            {
 
729
                throw (InternalError)new InternalError().initCause(x);
 
730
            }
 
731
        }
 
732
    }
 
733
 
 
734
    @Deprecated
 
735
    public int getInt(Object o, int offset)
 
736
    {
 
737
        return getInt(o, (long)offset);
 
738
    }
 
739
 
 
740
    @Deprecated
 
741
    public void putInt(Object o, int offset, int x)
 
742
    {
 
743
        putInt(o, (long)offset, x);
 
744
    }
 
745
 
 
746
    @Deprecated
 
747
    public Object getObject(Object o, int offset)
 
748
    {
 
749
        return getObject(o, (long)offset);
 
750
    }
 
751
 
 
752
    @Deprecated
 
753
    public void putObject(Object o, int offset, Object x)
 
754
    {
 
755
        putObject(o, (long)offset, x);
 
756
    }
 
757
 
 
758
    @Deprecated
 
759
    public boolean getBoolean(Object o, int offset)
 
760
    {
 
761
        return getBoolean(o, (long)offset);
 
762
    }
 
763
 
 
764
    @Deprecated
 
765
    public void putBoolean(Object o, int offset, boolean x)
 
766
    {
 
767
        putBoolean(o, (long)offset, x);
 
768
    }
 
769
 
 
770
    @Deprecated
 
771
    public byte getByte(Object o, int offset)
 
772
    {
 
773
        return getByte(o, (long)offset);
 
774
    }
 
775
 
 
776
    @Deprecated
 
777
    public void putByte(Object o, int offset, byte x)
 
778
    {
 
779
        putByte(o, (long)offset, x);
 
780
    }
 
781
 
 
782
    @Deprecated
 
783
    public short getShort(Object o, int offset)
 
784
    {
 
785
        return getShort(o, (long)offset);
 
786
    }
 
787
 
 
788
    @Deprecated
 
789
    public void putShort(Object o, int offset, short x)
 
790
    {
 
791
        putShort(o, (long)offset, x);
 
792
    }
 
793
 
 
794
    @Deprecated
 
795
    public char getChar(Object o, int offset)
 
796
    {
 
797
        return getChar(o, (long)offset);
 
798
    }
 
799
 
 
800
    @Deprecated
 
801
    public void putChar(Object o, int offset, char x)
 
802
    {
 
803
        putChar(o, (long)offset, x);
 
804
    }
 
805
 
 
806
    @Deprecated
 
807
    public long getLong(Object o, int offset)
 
808
    {
 
809
        return getLong(o, (long)offset);
 
810
    }
 
811
 
 
812
    @Deprecated
 
813
    public void putLong(Object o, int offset, long x)
 
814
    {
 
815
        putLong(o, (long)offset, x);
 
816
    }
 
817
 
 
818
    @Deprecated
 
819
    public float getFloat(Object o, int offset)
 
820
    {
 
821
        return getFloat(o, (long)offset);
 
822
    }
 
823
 
 
824
    @Deprecated
 
825
    public void putFloat(Object o, int offset, float x)
 
826
    {
 
827
        putFloat(o, (long)offset, x);
 
828
    }
 
829
 
 
830
    @Deprecated
 
831
    public double getDouble(Object o, int offset)
 
832
    {
 
833
        return getDouble(o, (long)offset);
 
834
    }
 
835
 
 
836
    @Deprecated
 
837
    public void putDouble(Object o, int offset, double x)
 
838
    {
 
839
        putDouble(o, (long)offset, x);
 
840
    }
 
841
 
 
842
    public native void throwException(Throwable t);
 
843
 
 
844
    public native void ensureClassInitialized(Class clazz);
 
845
 
 
846
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, SerializationFormatter = true)
 
847
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
848
    public native Object allocateInstance(Class clazz) throws InstantiationException;
 
849
 
 
850
    public int addressSize()
 
851
    {
 
852
        return IntPtr.get_Size();
 
853
    }
 
854
 
 
855
    public int pageSize()
 
856
    {
 
857
        return 4096;
 
858
    }
 
859
 
 
860
    // The really unsafe methods start here. They are all have a LinkDemand for unmanaged code.
 
861
 
 
862
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
863
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
864
    public long allocateMemory(long bytes)
 
865
    {
 
866
        try
 
867
        {
 
868
            if (false) throw new cli.System.OutOfMemoryException();
 
869
            return Marshal.AllocHGlobal(IntPtr.op_Explicit(bytes)).ToInt64();
 
870
        }
 
871
        catch (cli.System.OutOfMemoryException x)
 
872
        {
 
873
            throw new OutOfMemoryError(x.get_Message());
 
874
        }
 
875
    }
 
876
 
 
877
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
878
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
879
    public void freeMemory(long address)
 
880
    {
 
881
        Marshal.FreeHGlobal(IntPtr.op_Explicit(address));
 
882
    }
 
883
 
 
884
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
885
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
886
    public void setMemory(long address, long bytes, byte value)
 
887
    {
 
888
        while (bytes-- > 0)
 
889
        {
 
890
            putByte(address++, value);
 
891
        }
 
892
    }
 
893
 
 
894
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
895
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
896
    public void copyMemory(long srcAddress, long destAddress, long bytes)
 
897
    {
 
898
        while (bytes-- > 0)
 
899
        {
 
900
            putByte(destAddress++, getByte(srcAddress++));
 
901
        }
 
902
    }
 
903
    
 
904
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
905
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
906
    public void copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes)
 
907
    {
 
908
        if (srcBase == null)
 
909
        {
 
910
            if (destBase instanceof byte[])
 
911
            {
 
912
                cli.System.Runtime.InteropServices.Marshal.Copy(IntPtr.op_Explicit(srcOffset), (byte[])destBase, (int)destOffset, (int)bytes);
 
913
            }
 
914
            else if (destBase instanceof boolean[])
 
915
            {
 
916
                byte[] tmp = new byte[(int)bytes];
 
917
                copyMemory(srcBase, srcOffset, tmp, 0, bytes);
 
918
                copyMemory(tmp, 0, destBase, destOffset, bytes);
 
919
            }
 
920
            else if (destBase instanceof short[])
 
921
            {
 
922
                cli.System.Runtime.InteropServices.Marshal.Copy(IntPtr.op_Explicit(srcOffset), (short[])destBase, (int)(destOffset >> 1), (int)(bytes >> 1));
 
923
            }
 
924
            else if (destBase instanceof char[])
 
925
            {
 
926
                cli.System.Runtime.InteropServices.Marshal.Copy(IntPtr.op_Explicit(srcOffset), (char[])destBase, (int)(destOffset >> 1), (int)(bytes >> 1));
 
927
            }
 
928
            else if (destBase instanceof int[])
 
929
            {
 
930
                cli.System.Runtime.InteropServices.Marshal.Copy(IntPtr.op_Explicit(srcOffset), (int[])destBase, (int)(destOffset >> 2), (int)(bytes >> 2));
 
931
            }
 
932
            else if (destBase instanceof float[])
 
933
            {
 
934
                cli.System.Runtime.InteropServices.Marshal.Copy(IntPtr.op_Explicit(srcOffset), (float[])destBase, (int)(destOffset >> 2), (int)(bytes >> 2));
 
935
            }
 
936
            else if (destBase instanceof long[])
 
937
            {
 
938
                cli.System.Runtime.InteropServices.Marshal.Copy(IntPtr.op_Explicit(srcOffset), (long[])destBase, (int)(destOffset >> 3), (int)(bytes >> 3));
 
939
            }
 
940
            else if (destBase instanceof double[])
 
941
            {
 
942
                cli.System.Runtime.InteropServices.Marshal.Copy(IntPtr.op_Explicit(srcOffset), (double[])destBase, (int)(destOffset >> 3), (int)(bytes >> 3));
 
943
            }
 
944
            else if (destBase == null)
 
945
            {
 
946
                copyMemory(srcOffset, destOffset, bytes);
 
947
            }
 
948
            else
 
949
            {
 
950
                throw new IllegalArgumentException();
 
951
            }
 
952
        }
 
953
        else if (srcBase instanceof cli.System.Array && destBase instanceof cli.System.Array)
 
954
        {
 
955
            cli.System.Buffer.BlockCopy((cli.System.Array)srcBase, (int)srcOffset, (cli.System.Array)destBase, (int)destOffset, (int)bytes);
 
956
        }
 
957
        else
 
958
        {
 
959
            if (srcBase instanceof byte[])
 
960
            {
 
961
                cli.System.Runtime.InteropServices.Marshal.Copy((byte[])srcBase, (int)srcOffset, IntPtr.op_Explicit(destOffset), (int)bytes);
 
962
            }
 
963
            else if (srcBase instanceof boolean[])
 
964
            {
 
965
                byte[] tmp = new byte[(int)bytes];
 
966
                copyMemory(srcBase, srcOffset, tmp, 0, bytes);
 
967
                copyMemory(tmp, 0, destBase, destOffset, bytes);
 
968
            }
 
969
            else if (srcBase instanceof short[])
 
970
            {
 
971
                cli.System.Runtime.InteropServices.Marshal.Copy((short[])srcBase, (int)(srcOffset >> 1), IntPtr.op_Explicit(destOffset), (int)(bytes >> 1));
 
972
            }
 
973
            else if (srcBase instanceof char[])
 
974
            {
 
975
                cli.System.Runtime.InteropServices.Marshal.Copy((char[])srcBase, (int)(srcOffset >> 1), IntPtr.op_Explicit(destOffset), (int)(bytes >> 1));
 
976
            }
 
977
            else if (srcBase instanceof int[])
 
978
            {
 
979
                cli.System.Runtime.InteropServices.Marshal.Copy((int[])srcBase, (int)(srcOffset >> 2), IntPtr.op_Explicit(destOffset), (int)(bytes >> 2));
 
980
            }
 
981
            else if (srcBase instanceof float[])
 
982
            {
 
983
                cli.System.Runtime.InteropServices.Marshal.Copy((float[])srcBase, (int)(srcOffset >> 2), IntPtr.op_Explicit(destOffset), (int)(bytes >> 2));
 
984
            }
 
985
            else if (srcBase instanceof long[])
 
986
            {
 
987
                cli.System.Runtime.InteropServices.Marshal.Copy((long[])srcBase, (int)(srcOffset >> 3), IntPtr.op_Explicit(destOffset), (int)(bytes >> 3));
 
988
            }
 
989
            else if (srcBase instanceof double[])
 
990
            {
 
991
                cli.System.Runtime.InteropServices.Marshal.Copy((double[])srcBase, (int)(srcOffset >> 3), IntPtr.op_Explicit(destOffset), (int)(bytes >> 3));
 
992
            }
 
993
            else
 
994
            {
 
995
                throw new IllegalArgumentException();
 
996
            }
 
997
        }
 
998
    }
 
999
 
 
1000
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1001
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1002
    public byte getByte(long address)
 
1003
    {
 
1004
        return cli.System.Runtime.InteropServices.Marshal.ReadByte(IntPtr.op_Explicit(address));
 
1005
    }
 
1006
 
 
1007
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1008
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1009
    public void putByte(long address, byte x)
 
1010
    {
 
1011
        cli.System.Runtime.InteropServices.Marshal.WriteByte(IntPtr.op_Explicit(address), x);
 
1012
    }
 
1013
 
 
1014
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1015
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1016
    public short getShort(long address)
 
1017
    {
 
1018
        return cli.System.Runtime.InteropServices.Marshal.ReadInt16(IntPtr.op_Explicit(address));
 
1019
    }
 
1020
 
 
1021
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1022
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1023
    public void putShort(long address, short x)
 
1024
    {
 
1025
        cli.System.Runtime.InteropServices.Marshal.WriteInt16(IntPtr.op_Explicit(address), x);
 
1026
    }
 
1027
 
 
1028
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1029
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1030
    public char getChar(long address)
 
1031
    {
 
1032
        return (char)cli.System.Runtime.InteropServices.Marshal.ReadInt16(IntPtr.op_Explicit(address));
 
1033
    }
 
1034
 
 
1035
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1036
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1037
    public void putChar(long address, char x)
 
1038
    {
 
1039
        cli.System.Runtime.InteropServices.Marshal.WriteInt16(IntPtr.op_Explicit(address), (short)x);
 
1040
    }
 
1041
 
 
1042
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1043
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1044
    public int getInt(long address)
 
1045
    {
 
1046
        return cli.System.Runtime.InteropServices.Marshal.ReadInt32(IntPtr.op_Explicit(address));
 
1047
    }
 
1048
 
 
1049
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1050
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1051
    public void putInt(long address, int x)
 
1052
    {
 
1053
        cli.System.Runtime.InteropServices.Marshal.WriteInt32(IntPtr.op_Explicit(address), x);
 
1054
    }
 
1055
 
 
1056
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1057
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1058
    public long getLong(long address)
 
1059
    {
 
1060
        return cli.System.Runtime.InteropServices.Marshal.ReadInt64(IntPtr.op_Explicit(address));
 
1061
    }
 
1062
 
 
1063
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1064
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1065
    public void putLong(long address, long x)
 
1066
    {
 
1067
        cli.System.Runtime.InteropServices.Marshal.WriteInt64(IntPtr.op_Explicit(address), x);
 
1068
    }
 
1069
 
 
1070
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1071
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1072
    public long getAddress(long address)
 
1073
    {
 
1074
        return cli.System.Runtime.InteropServices.Marshal.ReadIntPtr(IntPtr.op_Explicit(address)).ToInt64();
 
1075
    }
 
1076
 
 
1077
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1078
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1079
    public void putAddress(long address, long x)
 
1080
    {
 
1081
        cli.System.Runtime.InteropServices.Marshal.WriteIntPtr(IntPtr.op_Explicit(address), IntPtr.op_Explicit(x));
 
1082
    }
 
1083
 
 
1084
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1085
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1086
    public float getFloat(long address)
 
1087
    {
 
1088
        return Float.intBitsToFloat(getInt(address));
 
1089
    }
 
1090
 
 
1091
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1092
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1093
    public void putFloat(long address, float x)
 
1094
    {
 
1095
        putInt(address, Float.floatToIntBits(x));
 
1096
    }
 
1097
 
 
1098
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1099
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1100
    public double getDouble(long address)
 
1101
    {
 
1102
        return Double.longBitsToDouble(getLong(address));
 
1103
    }
 
1104
 
 
1105
    @SecurityPermissionAttribute.Annotation(value = SecurityAction.__Enum.LinkDemand, UnmanagedCode = true)
 
1106
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
1107
    public void putDouble(long address, double x)
 
1108
    {
 
1109
        putLong(address, Double.doubleToLongBits(x));
 
1110
    }
 
1111
    
 
1112
    public int getLoadAverage(double[] loadavg, int nelems)
 
1113
    {
 
1114
        return -1;
 
1115
    }
 
1116
 
 
1117
    public void park(boolean isAbsolute, long time)
 
1118
    {
 
1119
        if (isAbsolute)
 
1120
        {
 
1121
            java.util.concurrent.locks.LockSupport.parkUntil(time);
 
1122
        }
 
1123
        else
 
1124
        {
 
1125
            java.util.concurrent.locks.LockSupport.parkNanos(time);
 
1126
        }
 
1127
    }
 
1128
 
 
1129
    public void unpark(Object thread)
 
1130
    {
 
1131
        java.util.concurrent.locks.LockSupport.unpark((Thread)thread);
 
1132
    }
 
1133
 
 
1134
    public Object staticFieldBase(Field f)
 
1135
    {
 
1136
        return null;
 
1137
    }
 
1138
 
 
1139
    public native Class defineClass(String name, byte[] buf, int offset, int length, ClassLoader cl, ProtectionDomain pd);
 
1140
 
 
1141
    public void monitorEnter(Object o)
 
1142
    {
 
1143
        cli.System.Threading.Monitor.Enter(o);
 
1144
    }
 
1145
 
 
1146
    public void monitorExit(Object o)
 
1147
    {
 
1148
        cli.System.Threading.Monitor.Exit(o);
 
1149
    }
 
1150
 
 
1151
    public boolean tryMonitorEnter(Object o)
 
1152
    {
 
1153
        return cli.System.Threading.Monitor.TryEnter(o);
 
1154
    }
 
1155
}