Allow passing addresses to Buffer and Int* primitives.

This commit is contained in:
Andy Nguyen 2021-11-13 14:29:58 +01:00
parent efad4e8928
commit 077b002273
6 changed files with 63 additions and 30 deletions

View File

@ -18,6 +18,16 @@ abstract class AbstractInt extends Buffer {
this.elementSize = elementSize; this.elementSize = elementSize;
} }
protected AbstractInt(long address, int[] dimensions, int elementSize) {
super(address, size(dimensions, elementSize));
this.dimensions = dimensions;
this.elementSize = elementSize;
}
protected AbstractInt(long address, int elementSize) {
this(address, new int[] {1}, elementSize);
}
protected AbstractInt(int elementSize) { protected AbstractInt(int elementSize) {
this(new int[] {1}, elementSize); this(new int[] {1}, elementSize);
} }

View File

@ -22,14 +22,25 @@ public class Buffer {
private final int size; private final int size;
private final boolean allocated;
public Buffer(int size) { public Buffer(int size) {
this.size = size;
this.address = api.malloc(size); this.address = api.malloc(size);
this.size = size;
this.allocated = true;
}
public Buffer(long address, int size) {
this.address = address;
this.size = size;
this.allocated = false;
} }
public void finalize() { public void finalize() {
if (allocated) {
api.free(address); api.free(address);
} }
}
public long address() { public long address() {
return address; return address;

View File

@ -10,17 +10,20 @@ package com.bdjb.api;
public final class Int16 extends AbstractInt { public final class Int16 extends AbstractInt {
public static final int SIZE = 2; public static final int SIZE = 2;
public Int16(int[] dimensions) {
super(dimensions, SIZE);
}
public Int16() { public Int16() {
super(SIZE); super(SIZE);
} }
public Int16(short value) { public Int16(long address) {
this(); super(address, SIZE);
this.set(value); }
public Int16(int[] dimensions) {
super(dimensions, SIZE);
}
public Int16(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public short get() { public short get() {

View File

@ -10,17 +10,20 @@ package com.bdjb.api;
public final class Int32 extends AbstractInt { public final class Int32 extends AbstractInt {
public static final int SIZE = 4; public static final int SIZE = 4;
public Int32(int[] dimensions) {
super(dimensions, SIZE);
}
public Int32() { public Int32() {
super(SIZE); super(SIZE);
} }
public Int32(int value) { public Int32(long address) {
this(); super(address, SIZE);
this.set(value); }
public Int32(int[] dimensions) {
super(dimensions, SIZE);
}
public Int32(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public int get() { public int get() {

View File

@ -10,17 +10,20 @@ package com.bdjb.api;
public final class Int64 extends AbstractInt { public final class Int64 extends AbstractInt {
public static final int SIZE = 8; public static final int SIZE = 8;
public Int64(int[] dimensions) {
super(dimensions, SIZE);
}
public Int64() { public Int64() {
super(SIZE); super(SIZE);
} }
public Int64(long value) { public Int64(long address) {
this(); super(address, SIZE);
this.set(value); }
public Int64(int[] dimensions) {
super(dimensions, SIZE);
}
public Int64(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public long get() { public long get() {

View File

@ -10,17 +10,20 @@ package com.bdjb.api;
public final class Int8 extends AbstractInt { public final class Int8 extends AbstractInt {
public static final int SIZE = 1; public static final int SIZE = 1;
public Int8(int[] dimensions) {
super(dimensions, SIZE);
}
public Int8() { public Int8() {
super(SIZE); super(SIZE);
} }
public Int8(byte value) { public Int8(long address) {
this(); super(address, SIZE);
this.set(value); }
public Int8(int[] dimensions) {
super(dimensions, SIZE);
}
public Int8(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public byte get() { public byte get() {