Skip to content

Commit

Permalink
classlib: add specialized implementation of NIO buffers for native ba…
Browse files Browse the repository at this point in the history
…ckends
  • Loading branch information
konsoletyper committed Jan 30, 2025
1 parent 9a57a1f commit 9dff03a
Show file tree
Hide file tree
Showing 23 changed files with 1,517 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.teavm.classlib.java.nio;

public abstract class TBuffer {
TBuffer nextRef;
int position;
int limit;
int mark = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import java.nio.BufferUnderflowException;
import java.util.Objects;
import org.teavm.backend.c.runtime.Memory;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.java.lang.TComparable;
import org.teavm.interop.Address;
import org.teavm.jso.typedarrays.Int8Array;
import org.teavm.runtime.GC;

public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBuffer> {
TByteOrder order = TByteOrder.BIG_ENDIAN;
Expand All @@ -36,6 +39,19 @@ public static TByteBuffer allocateDirect(int capacity) {
result.limit = capacity;
return result;
}
if (PlatformDetector.isC()) {
var memory = Memory.malloc(capacity);
var result = new TByteBufferNativeImpl(null, 0, null, memory, capacity, false);
GC.registerDirectBuffer(Address.ofObject(result).toStructure());
result.limit = capacity;
return result;
}
if (PlatformDetector.isWebAssembly()) {
var array = new byte[capacity];
var result = new TByteBufferNativeImpl(array, 0, array, Address.ofData(array), array.length, false);
result.limit = capacity;
return result;
}
return new TByteBufferImpl(capacity, true);
}

Expand All @@ -49,6 +65,12 @@ public static TByteBuffer allocate(int capacity) {
result.limit = capacity;
return result;
}
if (PlatformDetector.isC() || PlatformDetector.isWebAssembly()) {
var array = new byte[capacity];
var result = new TByteBufferNativeImpl(array, 0, array, Address.ofData(array), array.length, false);
result.limit = capacity;
return result;
}
return new TByteBufferImpl(capacity, false);
}

Expand All @@ -61,6 +83,12 @@ public static TByteBuffer wrap(byte[] array, int offset, int length) {
result.limit = offset + length;
return result;
}
if (PlatformDetector.isC() || PlatformDetector.isWebAssembly()) {
var result = new TByteBufferNativeImpl(array, 0, array, Address.ofData(array), array.length, false);
result.position = offset;
result.limit = offset + length;
return result;
}
return new TByteBufferImpl(0, array.length, array, offset, offset + length, false, false);
}

Expand Down Expand Up @@ -254,9 +282,13 @@ public final TByteOrder order() {

public final TByteBuffer order(TByteOrder bo) {
order = bo;
onOrderChanged();
return this;
}

void onOrderChanged() {
}

public abstract char getChar();

public abstract TByteBuffer putChar(char value);
Expand Down
Loading

0 comments on commit 9dff03a

Please sign in to comment.