/** Provide a view of this memory using the given offset as the base address. The * returned {@link Pointer} will have a size equal to that of the original * minus the offset. * @throws IndexOutOfBoundsException if the requested memory is outside * the allocated bounds. */ @Override public Pointer share(long offset) { return share(offset, size() - offset); }
/** Provide a view onto this structure with the given alignment. * @param byteBoundary Align memory to this number of bytes; should be a * power of two. * @throws IndexOutOfBoundsException if the requested alignment can * not be met. * @throws IllegalArgumentException if the requested alignment is not * a positive power of two. */ public Memory align(int byteBoundary) { if (byteBoundary <= 0) { throw new IllegalArgumentException("Byte boundary must be positive: " + byteBoundary); } for (int i=0;i < 32;i++) { if (byteBoundary == (1<<i)) { long mask = ~((long)byteBoundary - 1); if ((peer & mask) != peer) { long newPeer = (peer + byteBoundary - 1) & mask; long newSize = peer + size - newPeer; if (newSize <= 0) { throw new IllegalArgumentException("Insufficient memory to align to the requested boundary"); } return (Memory)share(newPeer - peer, newSize); } return this; } } throw new IllegalArgumentException("Byte boundary must be a power of two"); }
Memory data = ...; // mzH = *(MZHeader*) dPtr; MZHeader mzH = new MZHeader(data); mzH.read(); // or put this in the MZHeader(Pointer) ctor // peH = *(PE_Header*)&dPtr[mzH.offsetToPE]; PE_Header peH = new PE_Header(data.share(mzH.offsetToPE)); peH.read(); // or put this in the PE_Header(Pointer) ctor // peXH = *(PE_ExtHeader*)&dPtr[mzH.offsetToPE + sizeof(PE_Header)]; PE_ExtHeader peXH = new PE_ExtHeader(data.share(mzH.offsetToPE + peH.size())); peXH.read(); // or put this in the PE_ExtHeader(Pointer) actor
/** Provide a view onto this structure with the given alignment. * @param byteBoundary Align memory to this number of bytes; should be a * power of two. * @throws IndexOutOfBoundsException if the requested alignment can * not be met. * @throws IllegalArgumentException if the requested alignment is not * a positive power of two. */ public Pointer align(int byteBoundary) { if (byteBoundary <= 0) { throw new IllegalArgumentException("Byte boundary must be positive: " + byteBoundary); } long mask = ~((long)byteBoundary - 1); if ((peer & ~mask) != peer) { long newPeer = (peer + ~mask) & mask; return share(newPeer - peer, peer + size - newPeer); } return this; }
private Gmp() { int offset = 0; for (int i = 0; i < MAX_OPERANDS; ++i) { this.sharedOperands[i] = new mpz_t(sharedMem.share(offset, mpz_t.SIZE)); __gmpz_init(sharedOperands[i]); offset += mpz_t.SIZE; } this.countPtr = sharedMem.share(offset, Native.SIZE_T_SIZE); offset += Native.SIZE_T_SIZE; assert offset == SHARED_MEM_SIZE; }
/** Provide a view of this memory using the given offset as the base address. The * returned {@link Pointer} will have a size equal to that of the original * minus the offset. * @throws IndexOutOfBoundsException if the requested memory is outside * the allocated bounds. */ @Override public Pointer share(long offset) { return share(offset, size() - offset); }
/** Provide a view onto this structure from the given offset. The * returned {@link Pointer} will have the same size as the original, * reduced by the offset. * @throws IndexOutOfBoundsException if the requested memory is outside * the allocated bounds. */ public Pointer share(long offset) { return share(offset, getSize() - offset); }
private Gmp() { int offset = 0; for (int i = 0; i < MAX_OPERANDS; ++i) { this.sharedOperands[i] = new mpz_t(sharedMem.share(offset, mpz_t.SIZE)); __gmpz_init(sharedOperands[i]); offset += mpz_t.SIZE; } this.countPtr = sharedMem.share(offset, Native.SIZE_T_SIZE); offset += Native.SIZE_T_SIZE; assert offset == SHARED_MEM_SIZE; }
/** Provide a view onto this structure with the given alignment. * @param byteBoundary Align memory to this number of bytes; should be a * power of two. * @throws IndexOutOfBoundsException if the requested alignment can * not be met. * @throws IllegalArgumentException if the requested alignment is not * a positive power of two. */ public Memory align(int byteBoundary) { if (byteBoundary <= 0) { throw new IllegalArgumentException("Byte boundary must be positive: " + byteBoundary); } for (int i=0;i < 32;i++) { if (byteBoundary == (1<<i)) { long mask = ~((long)byteBoundary - 1); if ((peer & mask) != peer) { long newPeer = (peer + byteBoundary - 1) & mask; long newSize = peer + size - newPeer; if (newSize <= 0) { throw new IllegalArgumentException("Insufficient memory to align to the requested boundary"); } return (Memory)share(newPeer - peer, newSize); } return this; } } throw new IllegalArgumentException("Byte boundary must be a power of two"); }
public void setValue(String value) { if(value == null) { value = ""; } try { byte[] encodedValue = value.getBytes("UTF-16LE"); // 4 bytes for the length prefix, length for the encoded data, // 2 bytes for the two NULL terminators Memory mem = new Memory(4 + encodedValue.length + 2); mem.clear(); mem.setInt(0, encodedValue.length); mem.write(4, encodedValue, 0, encodedValue.length); this.setPointer(mem.share(4)); } catch (UnsupportedEncodingException ex) { throw new RuntimeException("UTF-16LE charset is not supported", ex); } }