Search in sources :

Example 6 with Memory

use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.

the class VanillaBytes method append8bitNBS_S.

@NotNull
private Bytes<Underlying> append8bitNBS_S(@NotNull String s) throws BufferOverflowException {
    int length = s.length();
    // can re-assign the byteStore if not large enough.
    long offset = writeOffsetPositionMoved(length);
    @Nullable NativeBytesStore bytesStore = (NativeBytesStore) this.bytesStore;
    final long address = bytesStore.address + bytesStore.translate(offset);
    @Nullable final Memory memory = bytesStore.memory;
    if (memory == null)
        throw new AssertionError(bytesStore.releasedHere);
    if (Jvm.isJava9Plus()) {
        final byte[] chars = StringUtils.extractBytes(s);
        int i;
        for (i = 0; i < length; i++) {
            memory.writeByte(address + i, chars[i]);
        }
    } else {
        final char[] chars = StringUtils.extractChars(s);
        int i;
        for (i = 0; i < length - 3; i += 4) {
            int c0 = chars[i] & 0xFF;
            int c1 = chars[i + 1] & 0xFF;
            int c2 = chars[i + 2] & 0xFF;
            int c3 = chars[i + 3] & 0xFF;
            memory.writeInt(address + i, c0 | (c1 << 8) | (c2 << 16) | (c3 << 24));
        }
        for (; i < length; i++) {
            int c0 = chars[i];
            memory.writeByte(address + i, (byte) c0);
        }
    }
    return this;
}
Also used : Memory(net.openhft.chronicle.core.Memory) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Memory

use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.

the class VanillaBytes method toString2.

private String toString2(@NotNull NativeBytesStore bytesStore) {
    int length = (int) Math.min(Integer.MAX_VALUE, readRemaining());
    @NotNull char[] chars = new char[length];
    @Nullable final Memory memory = bytesStore.memory;
    final long address = bytesStore.address + bytesStore.translate(readPosition());
    for (int i = 0; i < length; i++) chars[i] = (char) (memory.readByte(address + i) & 0xFF);
    return StringUtils.newString(chars);
}
Also used : Memory(net.openhft.chronicle.core.Memory) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with Memory

use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.

the class VanillaBytes method isEqual0.

@Java9
private static boolean isEqual0(@NotNull byte[] bytes, byte coder, @NotNull NativeBytesStore bs, long address) {
    @Nullable Memory memory = bs.memory;
    if (coder == 0) {
        int i = 0;
        for (; i < bytes.length; i++) {
            byte b = memory.readByte(address + i);
            char c = (char) (bytes[i] & 0xFF);
            if (b != c) {
                return false;
            }
        }
    } else {
        int i = 0;
        for (; i < bytes.length; i++) {
            byte b = memory.readByte(address + i);
            char c = (char) (((bytes[i + 1] & 0xFF) << 8) | (bytes[i] & 0xFF));
            if (b != c) {
                return false;
            }
        }
    }
    return true;
}
Also used : Memory(net.openhft.chronicle.core.Memory) Nullable(org.jetbrains.annotations.Nullable) Java9(net.openhft.chronicle.core.annotation.Java9)

Example 9 with Memory

use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.

the class VanillaBytes method byteCheckSum.

@Override
public int byteCheckSum() throws IORuntimeException {
    if (readLimit() >= Integer.MAX_VALUE || start() != 0 || !isDirectMemory())
        return super.byteCheckSum();
    byte b = 0;
    @Nullable NativeBytesStore bytesStore = (NativeBytesStore) bytesStore();
    @Nullable Memory memory = bytesStore.memory;
    assert memory != null;
    for (int i = (int) readPosition(), lim = (int) readLimit(); i < lim; i++) {
        b += memory.readByte(bytesStore.address + i);
    }
    return b & 0xFF;
}
Also used : Memory(net.openhft.chronicle.core.Memory) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with Memory

use of net.openhft.chronicle.core.Memory in project Chronicle-Queue by OpenHFT.

the class InternalBenchmarkMain method readMessage.

private static long readMessage(Bytes<?> bytes) {
    Jvm.safepoint();
    long start = bytes.readLong();
    long rp = bytes.readPosition();
    long rl = bytes.readLimit();
    long addr = bytes.addressForRead(rp);
    long addrEnd = bytes.addressForRead(rl);
    Memory memory = OS.memory();
    for (addr += 8; addr + 7 < addrEnd; addr += 8) memory.readLong(addr);
    Jvm.safepoint();
    return start;
}
Also used : Memory(net.openhft.chronicle.core.Memory)

Aggregations

Memory (net.openhft.chronicle.core.Memory)17 Nullable (org.jetbrains.annotations.Nullable)11 UnsafeMemory (net.openhft.chronicle.core.UnsafeMemory)9 NotNull (org.jetbrains.annotations.NotNull)6 BufferOverflowException (java.nio.BufferOverflowException)1 Java9 (net.openhft.chronicle.core.annotation.Java9)1 Unsafe (sun.misc.Unsafe)1