Make AbstractInt subclass of Buffer and add Text class.

This commit is contained in:
Andy Nguyen 2021-11-13 10:54:47 +01:00
parent 44ae595424
commit c80b753e03
8 changed files with 69 additions and 89 deletions

View File

@ -10,9 +10,10 @@ CLASSES = \
$(SRC)/com/bdjb/Exploit.java \
$(SRC)/com/bdjb/JIT.java \
$(SRC)/com/bdjb/Screen.java \
$(SRC)/com/bdjb/api/AbstractInt.java \
$(SRC)/com/bdjb/api/API.java \
$(SRC)/com/bdjb/api/Buffer.java \
$(SRC)/com/bdjb/api/Text.java \
$(SRC)/com/bdjb/api/AbstractInt.java \
$(SRC)/com/bdjb/api/Int8.java \
$(SRC)/com/bdjb/api/Int16.java \
$(SRC)/com/bdjb/api/Int32.java \

View File

@ -7,54 +7,28 @@
package com.bdjb.api;
abstract class AbstractInt {
protected static final API api;
abstract class AbstractInt extends Buffer {
private final int[] dimensions;
static {
try {
api = API.getInstance();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private final int elementSize;
protected final long address;
protected final int size;
protected final int[] dimensions;
protected AbstractInt(int[] dimensions) {
protected AbstractInt(int[] dimensions, int elementSize) {
super(size(dimensions, elementSize));
this.dimensions = dimensions;
this.size = size(dimensions);
this.address = api.malloc(size);
this.elementSize = elementSize;
}
protected AbstractInt() {
this(new int[] {1});
protected AbstractInt(int elementSize) {
this(new int[] {1}, elementSize);
}
protected abstract int elementSize();
public void finalize() {
api.free(address);
}
public long address() {
return address;
}
public int size() {
return size;
}
public int size(int[] dimensions) {
static int size(int[] dimensions, int elementSize) {
assert (dimensions.length > 0);
int size = 1;
for (int i = 0; i < dimensions.length; i++) {
size *= dimensions[i];
}
size *= elementSize();
size *= elementSize;
return size;
}
@ -66,14 +40,8 @@ abstract class AbstractInt {
offset += stride * indices[i];
stride *= dimensions[i];
}
offset *= elementSize();
checkOffset(offset);
offset *= elementSize;
checkOffset(offset, elementSize);
return offset;
}
private void checkOffset(int offset) {
if (offset < 0 || (offset + elementSize()) > size) {
throw new IndexOutOfBoundsException();
}
}
}

View File

@ -8,7 +8,7 @@
package com.bdjb.api;
public class Buffer {
private static final API api;
protected static final API api;
static {
try {
@ -18,9 +18,9 @@ public class Buffer {
}
}
private final long address;
protected final long address;
private final int size;
protected final int size;
public Buffer(int size) {
this.size = size;
@ -84,11 +84,16 @@ public class Buffer {
api.memcpy(address + offset, buffer.address(), buffer.size());
}
public void put(int offset, byte[] buffer) {
checkOffset(offset, buffer.length);
api.memcpy(address + offset, buffer, buffer.length);
}
public void fill(byte value) {
api.memset(address, value, size);
}
private void checkOffset(int offset, int length) {
protected void checkOffset(int offset, int length) {
if (offset < 0 || length < 0 || (offset + length) > size) {
throw new IndexOutOfBoundsException();
}

View File

@ -11,11 +11,11 @@ public final class Int16 extends AbstractInt {
public static final int SIZE = 2;
public Int16(int[] dimensions) {
super(dimensions);
super(dimensions, SIZE);
}
public Int16() {
super();
super(SIZE);
}
public Int16(short value) {
@ -23,23 +23,19 @@ public final class Int16 extends AbstractInt {
this.set(value);
}
protected int elementSize() {
return SIZE;
}
public short get() {
return api.read16(address);
return getShort(0x00);
}
public void set(short value) {
api.write16(address, value);
putShort(0x00, value);
}
public short get(int[] indices) {
return api.read16(address + offset(indices));
return getShort(offset(indices));
}
public void set(int[] indices, short value) {
api.write16(address + offset(indices), value);
putShort(offset(indices), value);
}
}

View File

@ -11,11 +11,11 @@ public final class Int32 extends AbstractInt {
public static final int SIZE = 4;
public Int32(int[] dimensions) {
super(dimensions);
super(dimensions, SIZE);
}
public Int32() {
super();
super(SIZE);
}
public Int32(int value) {
@ -23,23 +23,19 @@ public final class Int32 extends AbstractInt {
this.set(value);
}
protected int elementSize() {
return SIZE;
}
public int get() {
return api.read32(address);
return getInt(0x00);
}
public void set(int value) {
api.write32(address, value);
putInt(0x00, value);
}
public int get(int[] indices) {
return api.read32(address + offset(indices));
return getInt(offset(indices));
}
public void set(int[] indices, int value) {
api.write32(address + offset(indices), value);
putInt(offset(indices), value);
}
}

View File

@ -11,11 +11,11 @@ public final class Int64 extends AbstractInt {
public static final int SIZE = 8;
public Int64(int[] dimensions) {
super(dimensions);
super(dimensions, SIZE);
}
public Int64() {
super();
super(SIZE);
}
public Int64(long value) {
@ -23,23 +23,19 @@ public final class Int64 extends AbstractInt {
this.set(value);
}
protected int elementSize() {
return SIZE;
}
public long get() {
return api.read64(address);
return getLong(0x00);
}
public void set(long value) {
api.write64(address, value);
putLong(0x00, value);
}
public long get(int[] indices) {
return api.read64(address + offset(indices));
return getLong(offset(indices));
}
public void set(int[] indices, long value) {
api.write64(address + offset(indices), value);
putLong(offset(indices), value);
}
}

View File

@ -11,11 +11,11 @@ public final class Int8 extends AbstractInt {
public static final int SIZE = 1;
public Int8(int[] dimensions) {
super(dimensions);
super(dimensions, SIZE);
}
public Int8() {
super();
super(SIZE);
}
public Int8(byte value) {
@ -23,23 +23,19 @@ public final class Int8 extends AbstractInt {
this.set(value);
}
protected int elementSize() {
return SIZE;
}
public byte get() {
return api.read8(address);
return getByte(0x00);
}
public void set(byte value) {
api.write8(address, value);
putByte(0x00, value);
}
public byte get(int[] indices) {
return api.read8(address + offset(indices));
return getByte(offset(indices));
}
public void set(int[] indices, byte value) {
api.write8(address + offset(indices), value);
putByte(offset(indices), value);
}
}

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2021 Andy Nguyen
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package com.bdjb.api;
public class Text extends Buffer {
private String text;
public Text(String text) {
super(text.length() + 1);
this.text = text;
api.strcpy(address, text);
}
public String toString() {
return text;
}
}