Skip to content

Commit

Permalink
Add a maximum size of cosntraint serialization buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-bell committed Jan 5, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 14be332 commit 2291d0c
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -10,12 +10,13 @@ public final class ByteArrayList implements Externalizable {
private static final long serialVersionUID = 7110944498336814100L;
private int size = 0;
private byte[] elements;
private final long maxSize;

/**
* Constructs a new list with an initial capacity of 10.
*/
public ByteArrayList() {
this(10);
this(10, -1);
}

/**
@@ -24,7 +25,8 @@ public ByteArrayList() {
* @param capacity the initial capacity of this list.
* @throws IllegalArgumentException if capacity is less than 0
*/
public ByteArrayList(int capacity) {
public ByteArrayList(int capacity, long maxSize) {
this.maxSize = maxSize;
if (capacity < 0) {
throw new IllegalArgumentException();
}
@@ -39,6 +41,7 @@ public ByteArrayList(int capacity) {
*/
public ByteArrayList(ByteArrayList list) {
this.size = list.size;
this.maxSize = -1;
this.elements = new byte[list.elements.length];
System.arraycopy(list.elements, 0, elements, 0, list.elements.length);
}
@@ -76,6 +79,9 @@ private void grow(int minCapacity) {
if (newCapacity - (Integer.MAX_VALUE - 8) > 0) {
newCapacity = Integer.MAX_VALUE - 8;
}
if(maxSize > 0 && newCapacity > maxSize){
throw new OutOfMemoryError("Unable to grow buffer. Maximum size is " + maxSize + " but needed " + newCapacity);
}
byte[] temp = elements;
elements = new byte[newCapacity];
System.arraycopy(temp, 0, elements, 0, temp.length);
Original file line number Diff line number Diff line change
@@ -10,9 +10,14 @@
import java.util.LinkedList;

public class ConstraintSerializer {
private final ByteArrayList buff = new ByteArrayList(100 * 1024);
private final ByteArrayList buff;
private HashSet<Integer> backReferences = new HashSet<>();

public ConstraintSerializer(){
long maxRAM = Runtime.getRuntime().maxMemory();
buff = new ByteArrayList(100 * 1024, maxRAM/2);
}

public void write(Expression expr){
write(expr, buff);
}

0 comments on commit 2291d0c

Please sign in to comment.