| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import java.lang.reflect.ReflectPermission; |
| 17 |
import java.security.Permission; |
| 18 |
|
| 19 |
/** |
| 20 |
* A slice factory for creating unsafe slices |
| 21 |
*/ |
| 22 |
public class UnsafeSliceFactory |
| 23 |
{ |
| 24 |
/** |
| 25 |
* The Permission object that is used to check whether a client has |
| 26 |
* sufficient privilege to defeat Java language access control checks. |
| 27 |
* This is the same permission used by {@link java.lang.reflect.AccessibleObject}. |
| 28 |
*/ |
| 29 |
private static final Permission ACCESS_PERMISSION = new ReflectPermission("suppressAccessChecks"); |
| 30 |
|
| 31 |
/** |
| 32 |
* Accessible only to the privileged code. |
| 33 |
*/ |
| 34 |
private static final UnsafeSliceFactory INSTANCE = new UnsafeSliceFactory(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Get a factory for creating "unsafe" slices that can reference |
| 38 |
* arbitrary memory addresses. If there is a security manager, its |
| 39 |
* {@code checkPermission} method is called with a |
| 40 |
* {@code ReflectPermission("suppressAccessChecks")} permission. |
| 41 |
* |
| 42 |
* @return an unsafe slice factory |
| 43 |
*/ |
| 44 |
@SuppressWarnings("JavaDoc") // IDEA-81310 |
| 45 |
public static UnsafeSliceFactory getInstance() |
| 46 |
{ |
| 47 |
// see setAccessible() in AccessibleObject |
| 48 |
SecurityManager sm = System.getSecurityManager(); |
| 49 |
if (sm != null) { |
| 50 |
sm.checkPermission(ACCESS_PERMISSION); |
| 51 |
} |
| 52 |
return INSTANCE; |
| 53 |
} |
| 54 |
|
| 55 |
private UnsafeSliceFactory() {} |
| 56 |
|
| 57 |
/** |
| 58 |
* Creates a slice for directly a raw memory address. This is |
| 59 |
* inherently unsafe as it may be used to access arbitrary memory. |
| 60 |
* |
| 61 |
* @param address the raw memory address base |
| 62 |
* @param size the size of the slice |
| 63 |
* @return the unsafe slice |
| 64 |
*/ |
| 65 |
public Slice newSlice(long address, int size) |
| 66 |
{ |
| 67 |
if (address <= 0) { |
| 68 |
throw new IllegalArgumentException("Invalid address: " + address); |
| 69 |
} |
| 70 |
if (size == 0) { |
| 71 |
return Slices.EMPTY_SLICE; |
| 72 |
} |
| 73 |
return new Slice(null, address, size, 0, null); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Creates a slice for directly a raw memory address. This is |
| 78 |
* inherently unsafe as it may be used to access arbitrary memory. |
| 79 |
* The slice will hold the specified object reference to prevent the |
| 80 |
* garbage collector from freeing it while it is in use by the slice. |
| 81 |
* |
| 82 |
* @param address the raw memory address base |
| 83 |
* @param size the size of the slice |
| 84 |
* @param reference the object reference |
| 85 |
* @return the unsafe slice |
| 86 |
*/ |
| 87 |
public Slice newSlice(long address, int size, Object reference) |
| 88 |
{ |
| 89 |
if (address <= 0) { |
| 90 |
throw new IllegalArgumentException("Invalid address: " + address); |
| 91 |
} |
| 92 |
if (reference == null) { |
| 93 |
throw new NullPointerException("Object reference is null"); |
| 94 |
} |
| 95 |
if (size == 0) { |
| 96 |
return Slices.EMPTY_SLICE; |
| 97 |
} |
| 98 |
return new Slice(null, address, size, size, reference); |
| 99 |
} |
| 100 |
} |