package re3lib;
import java.util.Hashtable;

import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.program.model.listing.Function;
import ghidra.util.task.TaskMonitor;

public class DecompileCache {
  private static final int TIMEOUT = 10000;

  Hashtable<Function, DecompileResults> cache = new Hashtable<>();
  DecompInterface decomp;
  TaskMonitor monitor;

  public DecompileCache(DecompInterface decomp, TaskMonitor monitor) {
    this.decomp = decomp;
    this.monitor = monitor;
  }

  public DecompileResults get(Function function) {
    return cache.get(function);
  }

  public DecompileResults getOrInsert(Function function) {
    DecompileResults res = cache.get(function);
    if (res == null) {
      res = decomp.decompileFunction(function, TIMEOUT, monitor);
      cache.put(function, res);
    }
    return res;
  }
}