more attempts at gc

This commit is contained in:
Daniella 2022-08-01 20:01:44 +02:00
parent 84f9a143b0
commit a9fbb5263c

View file

@ -1490,6 +1490,7 @@ public class ISBPL {
} }
o.type = getType("array"); o.type = getType("array");
object = isbplArray; object = isbplArray;
isbplArray.usedBy(o);
} }
o.object = object; o.object = object;
return o; return o;
@ -2197,34 +2198,42 @@ interface ISBPLUsable {
default void usedBy(ISBPLUsable other) { default void usedBy(ISBPLUsable other) {
setUses(getUses() + 1); setUses(getUses() + 1);
other.addChild(this); other.addChild(this);
System.out.println("USE " + this + " used by " + other);
} }
default void unusedBy(ISBPLUsable other) { default void unusedBy(ISBPLUsable other) {
if(!other.isUseless()) if(!other.isUseless())
if(other.removeChild(this)) if(other.removeChild(this)) {
removeUse(); removeUse();
System.out.println("UNU " + this + " UNused by " + other);
}
} }
default void addUse() { default void addUse() {
setUses(getUses() + 1); synchronized(this) {
setUses(getUses() + 1);
}
} }
default void removeUse() { default void removeUse() {
if(isUseless()) { synchronized(this) {
return; if(isUseless()) {
} return;
setUses(getUses() - 1);
if(getUses() <= 0) {
// Now useless, delete.
if(ISBPL.debug) {
ISBPL.gErrorStream.println("Garbage collecting " + this);
} }
ISBPLFastList<ISBPLUsable> usables = getChildren(); setUses(getUses() - 1);
ISBPLUsable[] arr = usables.toArray(); if(getUses() <= 0) {
for(int i = 0; i < arr.length; i++) { // Now useless, delete.
arr[i].unusedBy(this); //if(ISBPL.debug) {
ISBPL.gErrorStream.println("Garbage collecting " + this);
//}
ISBPLFastList<ISBPLUsable> usables = getChildren();
ISBPLUsable[] arr = usables.toArray();
for(int i = 0; i < arr.length; i++) {
arr[i].unusedBy(this);
System.out.println(arr[i]);
}
useless();
uses.remove(this);
children.remove(this);
ISBPL.requestGC();
} }
useless();
uses.remove(this);
children.remove(this);
ISBPL.requestGC();
} }
} }
@ -2368,7 +2377,7 @@ class ISBPLArray implements ISBPLUsable {
} }
public String toString() { public String toString() {
StringBuilder s = new StringBuilder("["); StringBuilder s = new StringBuilder("![");
for(int i = 0; i < length; i++) { for(int i = 0; i < length; i++) {
s.append(backend[i]); s.append(backend[i]);
if(i != length - 1) { if(i != length - 1) {
@ -2419,9 +2428,17 @@ class ISBPLObject implements ISBPLUsable {
} }
public void useless() { public void useless() {
if(type.name.equals("array")) {
new RuntimeException("removed array debug print").printStackTrace();
System.out.println(this);
System.out.println(type.vars.get(this));
System.out.println(this.hashCode());
}
/*
type.vars.remove(this); type.vars.remove(this);
object = null; object = null;
type = null; type = null;
*/
} }
public boolean isTruthy() { public boolean isTruthy() {
@ -2965,11 +2982,11 @@ class ISBPLStack<T extends ISBPLUsable> extends Stack<T> implements ISBPLUsable
final ISBPLFastList<T> toBeMarked = new ISBPLFastList<>(3); final ISBPLFastList<T> toBeMarked = new ISBPLFastList<>(3);
@Override @Override
public T push(T t) { public synchronized T push(T t) {
if(t == null) if(t == null)
new IllegalArgumentException("item is null").printStackTrace(); new IllegalArgumentException("item is null").printStackTrace();
if(t.isUseless()) { if(t.isUseless()) {
throw new IllegalStateException("item was garbage-collected (" + t + ")"); throw new IllegalStateException("garbage collected item was pushed to stack (" + t + ")");
} }
if(t instanceof ISBPLObject && ((ISBPLObject) t).type == null) { if(t instanceof ISBPLObject && ((ISBPLObject) t).type == null) {
throw new IllegalStateException("item was garbage-collected (" + t + ") but was not marked"); throw new IllegalStateException("item was garbage-collected (" + t + ") but was not marked");
@ -2979,7 +2996,7 @@ class ISBPLStack<T extends ISBPLUsable> extends Stack<T> implements ISBPLUsable
} }
@Override @Override
public T peek() { public synchronized T peek() {
T t = super.peek(); T t = super.peek();
if(t.isUseless()) { if(t.isUseless()) {
throw new IllegalStateException("item was garbage-collected (" + t + ") WHILE ON STACK!"); throw new IllegalStateException("item was garbage-collected (" + t + ") WHILE ON STACK!");
@ -2988,7 +3005,7 @@ class ISBPLStack<T extends ISBPLUsable> extends Stack<T> implements ISBPLUsable
} }
@Override @Override
public T pop() { public synchronized T pop() {
T t = super.pop(); T t = super.pop();
toBeMarked.add(t); toBeMarked.add(t);
if(t.isUseless()) { if(t.isUseless()) {
@ -2997,11 +3014,11 @@ class ISBPLStack<T extends ISBPLUsable> extends Stack<T> implements ISBPLUsable
return t; return t;
} }
public void drop() { public synchronized void drop() {
super.pop().unusedBy(this); super.pop().unusedBy(this);
} }
public void update() { public synchronized void update() {
while(!toBeMarked.isEmpty()) { while(!toBeMarked.isEmpty()) {
toBeMarked.get(0).unusedBy(this); toBeMarked.get(0).unusedBy(this);
toBeMarked.remove(0); toBeMarked.remove(0);