package edu.lsu.cct.MCSMPGC; public class Node { String value; int address; int rc[] = new int[2]; int which; int phantom; int phantom_self; References refs; Object lock = new Object(); int collapseptr = -1; public Node(int adrs, Memory m) { this.address = adrs; rc[0] = rc[1] = 0; which = 0; phantom = 0; phantom_self = 0; refs = new References(m); } public void free() { Main.memory.allocated.remove(this.address); this.address = -1; rc[0] = rc[1] = 0; which = 0; phantom = 0; phantom_self = 0; refs.free(); Main.memory.free.add(this); } public void anchored(Memory memory) { incSRC(); memory.anchors.add(this.address); } public synchronized void incSRC() { rc[which]++; } public boolean removeanchor() { decSRC(); if (getSRC() == 0) return true; else return false; } public void decSRC() { synchronized (lock) { rc[which]--; } } public void incPhantom_self() { synchronized (lock) { phantom_self++; } } public int getPhantom() { synchronized (lock) { return phantom; } } public void decPhantom() { synchronized (lock) { // if(phantom>0) phantom--; } } public void decPhantom_self() { synchronized (lock) { phantom_self--; } } public int getPhantomLinkCount() { int x = 0; for (int i = 0; i < refs.getCount(); i++) { Reference l = refs.links.get(i); if (l.isPhantomized()) x++; } return x; } public int giveSupport(boolean ps) { synchronized (lock) { if (ps == false) { if (rc[which] == 0) { phantom--; rc[which]++; return which; } else if (rc[which] > 0 && refs.getCount() == getPhantomLinkCount()) { phantom--; rc[which]++; return which; } else if (rc[which] > 0) { phantom--; rc[1- which ]++; return 1 - which; } } else { if (rc[which] == 0) { phantom_self--; rc[which]++; return which; } else if (rc[which] > 0 && refs.getCount() == getPhantomLinkCount()) { phantom_self--; rc[which]++; return which; } else if (rc[which] > 0) { phantom_self--; rc[1 - which]++; return 1 - which; } } return 1- which; } } public boolean isDeletable() { Node current = this; if (current.getSRC() == 0 && current.getWRC() == 0 && current.getPhantom() == 0 && current.getPhantom_self() == 0) { return true; } return false; } public int getPhantom_self() { synchronized (lock) { return phantom_self; } } public int getSRC() { synchronized (lock) { return rc[which]; } } public int getWRC() { synchronized (lock) { return rc[1 - which]; } } public void incWRC() { synchronized (lock) { rc[1 - which]++; } } public void decWRC() { synchronized (lock) { rc[1 - which]--; } } public void decPandincPS() { synchronized (lock) { phantom--; phantom_self++; } } public void toggleWhich() { synchronized (lock) { which = 1 - which; } } public void incPhantom() { synchronized (lock) { phantom++; } } public int getWhich() { synchronized (lock) { return which; } } public int acceptIncomingLink() { if (getSRC() > 0 && outRefCount() > 0) { incWRC(); return 1 - getWhich(); } else if (getSRC() > 0) { incSRC(); return getWhich(); } else { incSRC(); return getWhich(); } } public int outRefCount() { return refs.getCount(); } }