Ghidra is a software reverse engineering (SRE) framework
GP-6679: Changing Ghidra to use read/write locks
ghidragon50c0cef1bd24
diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTrace.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTrace.javaindex 1c9c6284af..ac062d3d81 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTrace.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTrace.java@@ -286,7 +286,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceAddressPropertyManager createAddressPropertyManager(DBTraceThreadManager threadManager) throws CancelledException, IOException {return createTraceManager("Address Property Manager",- (openMode, monitor) -> new DBTraceAddressPropertyManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceAddressPropertyManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager));}@@ -294,7 +294,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceBookmarkManager createBookmarkManager(DBTraceThreadManager threadManager)throws CancelledException, IOException {return createTraceManager("Bookmark Manager",- (openMode, monitor) -> new DBTraceBookmarkManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceBookmarkManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager));}@@ -302,7 +302,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceBreakpointManager createBreakpointManager(DBTraceObjectManager objectManager)throws CancelledException, IOException {return createTraceManager("Breakpoint Manager",- (openMode, monitor) -> new DBTraceBreakpointManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceBreakpointManager(dbh, openMode, lock, monitor,this, objectManager));}@@ -312,7 +312,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, TraceDBTraceOverlaySpaceAdapter overlayAdapter, DBTraceReferenceManager referenceManager)throws CancelledException, IOException {return createTraceManager("Code Manager",- (openMode, monitor) -> new DBTraceCodeManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceCodeManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager, platformManager, dataTypeManager, overlayAdapter,referenceManager));}@@ -321,7 +321,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceCommentAdapter createCommentAdapter(DBTraceThreadManager threadManager)throws CancelledException, IOException {return createTraceManager("Comment Adapter",- (openMode, monitor) -> new DBTraceCommentAdapter(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceCommentAdapter(dbh, openMode, lock, monitor,baseLanguage, this, threadManager));}@@ -329,7 +329,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceDataSettingsAdapter createDataSettingsAdapter(DBTraceThreadManager threadManager) throws CancelledException, IOException {return createTraceManager("Data Settings Adapter",- (openMode, monitor) -> new DBTraceDataSettingsAdapter(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceDataSettingsAdapter(dbh, openMode, lock, monitor,baseLanguage, this, threadManager));}@@ -337,7 +337,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceDataTypeManager createDataTypeManager(DBTracePlatformManager platformManager)throws CancelledException, IOException {return createTraceManager("Data Type Manager", (openMode,- monitor) -> new DBTraceDataTypeManager(dbh, openMode, rwLock, monitor, this,+ monitor) -> new DBTraceDataTypeManager(dbh, openMode, lock, monitor, this,platformManager.getHostPlatform()));}@@ -345,7 +345,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceEquateManager createEquateManager(DBTraceThreadManager threadManager)throws CancelledException, IOException {return createTraceManager("Equate Manager",- (openMode, monitor) -> new DBTraceEquateManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceEquateManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager));}@@ -353,7 +353,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTracePlatformManager createPlatformManager()throws CancelledException, IOException {return createTraceManager("Platform Manager",- (openMode, monitor) -> new DBTracePlatformManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTracePlatformManager(dbh, openMode, lock, monitor,baseCompilerSpec, this));}@@ -361,7 +361,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceMemoryManager createMemoryManager(DBTraceThreadManager threadManager,DBTraceOverlaySpaceAdapter overlayAdapter) throws IOException, CancelledException {return createTraceManager("Memory Manager",- (openMode, monitor) -> new DBTraceMemoryManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceMemoryManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager, overlayAdapter));}@@ -369,14 +369,14 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceModuleManager createModuleManager(DBTraceObjectManager objectManager)throws CancelledException, IOException {return createTraceManager("Module Manager",- (openMode, monitor) -> new DBTraceModuleManager(dbh, openMode, rwLock, monitor, this,+ (openMode, monitor) -> new DBTraceModuleManager(dbh, openMode, lock, monitor, this,objectManager));}@DependentServiceprotected DBTraceObjectManager createObjectManager() throws CancelledException, IOException {return createTraceManager("Object Manager",- (openMode, monitor) -> new DBTraceObjectManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceObjectManager(dbh, openMode, lock, monitor,baseLanguage, this));}@@ -384,14 +384,14 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceOverlaySpaceAdapter createOverlaySpaceAdapter()throws CancelledException, IOException {return createTraceManager("Overlay Space Adapter", (openMode,- monitor) -> new DBTraceOverlaySpaceAdapter(dbh, openMode, rwLock, monitor, this));+ monitor) -> new DBTraceOverlaySpaceAdapter(dbh, openMode, lock, monitor, this));}@DependentServiceprotected DBTraceReferenceManager createReferenceManager(DBTraceThreadManager threadManager,DBTraceOverlaySpaceAdapter overlayAdapter) throws CancelledException, IOException {return createTraceManager("Reference Manager",- (openMode, monitor) -> new DBTraceReferenceManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceReferenceManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager, overlayAdapter));}@@ -400,7 +400,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, TraceDBTraceThreadManager threadManager, DBTracePlatformManager platformManager)throws CancelledException, IOException {return createTraceManager("Context Manager",- (openMode, monitor) -> new DBTraceRegisterContextManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceRegisterContextManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager, platformManager));}@@ -408,7 +408,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceStackManager createStackManager(DBTraceThreadManager threadManager,DBTraceOverlaySpaceAdapter overlayAdapter) throws CancelledException, IOException {return createTraceManager("Stack Manager",- (openMode, monitor) -> new DBTraceStackManager(dbh, openMode, rwLock, monitor, this,+ (openMode, monitor) -> new DBTraceStackManager(dbh, openMode, lock, monitor, this,threadManager, overlayAdapter));}@@ -416,7 +416,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceStaticMappingManager createStaticMappingManager(DBTraceOverlaySpaceAdapter overlayAdapter) throws CancelledException, IOException {return createTraceManager("Static Mapping Manager",- (openMode, monitor) -> new DBTraceStaticMappingManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceStaticMappingManager(dbh, openMode, lock, monitor,this, overlayAdapter));}@@ -425,7 +425,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, TraceDBTraceDataTypeManager dataTypeManager, DBTraceOverlaySpaceAdapter overlayAdapter)throws CancelledException, IOException {return createTraceManager("Symbol Manager",- (openMode, monitor) -> new DBTraceSymbolManager(dbh, openMode, rwLock, monitor,+ (openMode, monitor) -> new DBTraceSymbolManager(dbh, openMode, lock, monitor,baseLanguage, this, threadManager, dataTypeManager, overlayAdapter));}@@ -433,7 +433,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceThreadManager createThreadManager(DBTraceObjectManager objectManager)throws IOException, CancelledException {return createTraceManager("Thread Manager",- (openMode, monitor) -> new DBTraceThreadManager(dbh, openMode, rwLock, monitor, this,+ (openMode, monitor) -> new DBTraceThreadManager(dbh, openMode, lock, monitor, this,objectManager));}@@ -441,7 +441,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Traceprotected DBTraceTimeManager createTimeManager(DBTraceThreadManager threadManager)throws IOException, CancelledException {return createTraceManager("Time Manager", (openMode, monitor) -> new DBTraceTimeManager(dbh,- openMode, rwLock, monitor, this, threadManager));+ openMode, lock, monitor, this, threadManager));}@Override@@ -650,12 +650,12 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace@Overridepublic LockHold lockRead() {- return LockHold.lock(rwLock.readLock());+ return LockHold.lock(lock.readLock());}@Overridepublic LockHold lockWrite() {- return LockHold.lock(rwLock.writeLock());+ return LockHold.lock(lock.writeLock());}public void sourceArchiveChanged(UniversalID sourceArchiveID) {@@ -755,7 +755,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace@Overrideprotected void clearCache(boolean all) {- try (LockHold hold = LockHold.lock(rwLock.writeLock())) {+ try (LockHold hold = LockHold.lock(lock.writeLock())) {for (DBTraceManager m : managers) {m.invalidateCache(all);}diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForContainingQueries.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForContainingQueries.javaindex 7f04ba2fdc..64656baeae 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForContainingQueries.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForContainingQueries.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -78,7 +78,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {protected abstract V doGetContaining(K key);- protected List<? extends T> getAllInRangeCacheContaining(K key) {+ protected synchronized List<? extends T> getAllInRangeCacheContaining(K key) {List<T> result = new ArrayList<>();for (Entry<TraceAddressSnapRange, T> ent : rangeCache) {TraceAddressSnapRange range = ent.getKey();@@ -93,7 +93,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {return result;}- protected T getFirstInRangeCacheContaining(K key) {+ protected synchronized T getFirstInRangeCacheContaining(K key) {for (Entry<TraceAddressSnapRange, T> ent : rangeCache) {TraceAddressSnapRange range = ent.getKey();if (!range.getLifespan().contains(key.snap)) {@@ -107,12 +107,12 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {return null;}- protected boolean isInCachedRange(long snap, Address address) {+ protected synchronized boolean isInCachedRange(long snap, Address address) {return rangeCacheRange != null && rangeCacheRange.getLifespan().contains(snap) &&rangeCacheRange.getRange().contains(address);}- protected void ensureInCachedRange(long snap, Address address) {+ protected synchronized void ensureInCachedRange(long snap, Address address) {if (isInCachedRange(snap, address)) {return;}@@ -120,15 +120,15 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {loadRangeCache(rangeCacheRange = computeNewCachedRange(snap, address));}- protected TraceAddressSnapRange computeNewCachedRange(long snap, Address address) {+ protected synchronized TraceAddressSnapRange computeNewCachedRange(long snap, Address address) {return ImmutableTraceAddressSnapRange.centered(address, snap, addressBreadth, snapBreadth);}- public V getContaining(K key) {+ public synchronized V getContaining(K key) {return pointCache.computeIfAbsent(key, this::doGetContaining);}- public void notifyNewEntry(Lifespan lifespan, Address address, T item) {+ public synchronized void notifyNewEntry(Lifespan lifespan, Address address, T item) {// TODO: Can this be smarter?pointCache.clear();if (rangeCacheRange != null &&@@ -139,7 +139,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {}}- public void notifyNewEntry(Lifespan lifespan, AddressRange range, T item) {+ public synchronized void notifyNewEntry(Lifespan lifespan, AddressRange range, T item) {// TODO: Can this be smarter?pointCache.clear();if (rangeCacheRange != null &&@@ -150,7 +150,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {}}- public void notifyNewEntries(Lifespan lifespan, AddressSetView addresses, T item) {+ public synchronized void notifyNewEntries(Lifespan lifespan, AddressSetView addresses, T item) {// TODO: Can this be smarter?pointCache.clear();if (rangeCacheRange != null &&@@ -164,17 +164,18 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {}}- public void notifyEntryRemoved(Lifespan lifespan, AddressRange range, T item) {+ public synchronized void notifyEntryRemoved(Lifespan lifespan, AddressRange range, T item) {// TODO: Can this be smarter?invalidate();}- public void notifyEntryShapeChanged(Lifespan lifespan, AddressRange range, T item) {+ public synchronized void notifyEntryShapeChanged(Lifespan lifespan, AddressRange range,+ T item) {// TODO: Can this be smarter?invalidate();}- public void invalidate() {+ public synchronized void invalidate() {pointCache.clear();rangeCache.clear();rangeCacheRange = null;diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForSequenceQueries.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForSequenceQueries.javaindex db006379cb..4521c21ee2 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForSequenceQueries.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/DBTraceCacheForSequenceQueries.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -41,7 +41,7 @@ public abstract class DBTraceCacheForSequenceQueries<T> {this.max = range.getMaxAddress();}- public T getFloor(Address address) {+ public synchronized T getFloor(Address address) {Entry<Address, T> floor = nav.floorEntry(address);if (floor != null) {return floor.getValue();@@ -60,7 +60,7 @@ public abstract class DBTraceCacheForSequenceQueries<T> {return ent.getValue();}- public T getCeiling(Address address) {+ public synchronized T getCeiling(Address address) {Entry<Address, T> ceiling = nav.ceilingEntry(address);if (ceiling != null) {return ceiling.getValue();@@ -79,19 +79,20 @@ public abstract class DBTraceCacheForSequenceQueries<T> {return ent.getValue();}- public void load(+ public synchronized void load(ArrayList<? extends Entry<? extends TraceAddressSnapRange, ? extends T>> entries) {for (Entry<? extends TraceAddressSnapRange, ? extends T> ent : entries) {nav.put(ent.getKey().getX1(), ent.getValue());}}- protected boolean contains(Address address) {+ protected synchronized boolean contains(Address address) {return min.hasSameAddressSpace(address) && min.compareTo(address) <= 0 &&max.compareTo(address) >= 0;}- protected void reInit(@SuppressWarnings("hiding") long snap, AddressRange range) {+ protected synchronized void reInit(@SuppressWarnings("hiding") long snap,+ AddressRange range) {this.snap = snap;this.min = range.getMinAddress();this.max = range.getMaxAddress();@@ -101,7 +102,7 @@ public abstract class DBTraceCacheForSequenceQueries<T> {protected final int maxRegions;protected final int addressBreadth;// TODO: Depending on the number of regions, LinkedList may perform better- protected final List<CachedRegion> cache = new ArrayList<>();+ protected final List<CachedRegion> cache = Collections.synchronizedList(new ArrayList<>());public DBTraceCacheForSequenceQueries(int maxRegions, int addressBreadth) {this.maxRegions = maxRegions;diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/AbstractBaseDBTraceDefinedUnitsView.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/AbstractBaseDBTraceDefinedUnitsView.javaindex d52470b533..ecc867d8f9 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/AbstractBaseDBTraceDefinedUnitsView.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/AbstractBaseDBTraceDefinedUnitsView.java@@ -65,13 +65,13 @@ public abstract class AbstractBaseDBTraceDefinedUnitsView<T extends AbstractDBTr}@Override- protected void loadRangeCache(TraceAddressSnapRange range) {+ protected synchronized void loadRangeCache(TraceAddressSnapRange range) {rangeCache.addAll(mapSpace.reduce(TraceAddressSnapRangeQuery.intersecting(range)).entries());}@Override- protected T doGetContaining(GetKey key) {+ protected synchronized T doGetContaining(GetKey key) {ensureInCachedRange(key.snap, key.addr);return getFirstInRangeCacheContaining(key);}@@ -87,14 +87,15 @@ public abstract class AbstractBaseDBTraceDefinedUnitsView<T extends AbstractDBTr}@Override- protected void loadCachedRegion(CachedRegion region) {+ protected synchronized void loadCachedRegion(CachedRegion region) {region.load(new ArrayList<>(mapSpace.reduce(TraceAddressSnapRangeQuery.intersecting(region.min, region.max,region.snap, region.snap)).entries()));}@Override- protected Entry<TraceAddressSnapRange, T> doFloorEntry(long snap, Address max) {+ protected synchronized Entry<TraceAddressSnapRange, T> doFloorEntry(long snap,+ Address max) {Address spaceMin = space.space.getMinAddress();return mapSpace.reduce(TraceAddressSnapRangeQuery.intersecting(spaceMin, max, snap, snap)@@ -103,7 +104,8 @@ public abstract class AbstractBaseDBTraceDefinedUnitsView<T extends AbstractDBTr}@Override- protected Entry<TraceAddressSnapRange, T> doCeilingEntry(long snap, Address min) {+ protected synchronized Entry<TraceAddressSnapRange, T> doCeilingEntry(long snap,+ Address min) {Address spaceMax = space.space.getMaxAddress();return mapSpace.reduce(TraceAddressSnapRangeQuery.intersecting(min, spaceMax, snap, snap))diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceInstruction.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceInstruction.javaindex 729d46bca2..5fc8a3655c 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceInstruction.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceInstruction.java@@ -366,7 +366,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructi@Overridepublic Address getFallThrough() {try (LockHold hold = LockHold.lock(space.lock.readLock())) {- checkIsValid();+ refreshIfNeeded();if (isFallThroughOverridden()) {DBTraceReferenceSpace refSpace = space.referenceManager.get(space.space, false);if (refSpace == null) {@@ -392,7 +392,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructi@Overridepublic Address getFallFrom() {try (LockHold hold = LockHold.lock(space.lock.readLock())) {- checkIsValid();+ refreshIfNeeded();// Go back one, considering alignmentDBTraceInstruction ins = this;int alignment = Math.min(1, getLanguage().getInstructionAlignment());@@ -515,7 +515,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructi@Overridepublic boolean hasFallthrough() {try (LockHold hold = LockHold.lock(space.lock.readLock())) {- checkIsValid();+ refreshIfNeeded();if (isFallThroughOverridden()) {return getFallThrough() != null; // dest stored as reference}@@ -646,7 +646,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructireturn getBytes();}try (LockHold hold = LockHold.lock(space.lock.readLock())) {- checkIsValid();+ refreshIfNeeded();int len = getPrototype().getLength();byte[] b = new byte[len];Address addr = getAddress();diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/ByteCache.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/ByteCache.javaindex 465efc1796..868984dd2f 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/ByteCache.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/ByteCache.java@@ -79,19 +79,19 @@ public abstract class ByteCache {return new Page();}- public boolean canCache(Address address, int len) {+ public synchronized boolean canCache(Address address, int len) {long cacheBufOff = address.getOffset() & ~OFFSET_MASK;return cacheBufOff + len < pageCount * SIZE;}- public byte read(Address address) throws MemoryAccessException {+ public synchronized byte read(Address address) throws MemoryAccessException {Address pageStart = address.getNewAddress(address.getOffset() & OFFSET_MASK);Page page = ensurePageCached(pageStart, 1);int cacheBufOff = (int) address.subtract(pageStart);return page.bytes[cacheBufOff];}- public int read(Address address, ByteBuffer buf) throws MemoryAccessException {+ public synchronized int read(Address address, ByteBuffer buf) throws MemoryAccessException {long startOff = address.getOffset();long startPage = startOff & OFFSET_MASK;int bufStart = buf.position();@@ -108,7 +108,7 @@ public abstract class ByteCache {return buf.position() - bufStart;}- protected int choosePage(Address address, int len) {+ private int choosePage(Address address, int len) {for (int i = 0; i < pageCount; i++) {Page page = pages[i];if (page.contains(address, len)) {@@ -135,21 +135,17 @@ public abstract class ByteCache {if (chosen == 0) {return pages[0];}- synchronized (pages) {- Page temp = pages[chosen];- pages[chosen] = pages[0];- pages[0] = temp;- return temp;- }+ Page temp = pages[chosen];+ pages[chosen] = pages[0];+ pages[0] = temp;+ return temp;}protected abstract int doLoad(Address address, ByteBuffer buf) throws MemoryAccessException;- public void invalidate(AddressRange range) {- synchronized (pages) {- for (Page p : pages) {- p.invalidate(range);- }+ public synchronized void invalidate(AddressRange range) {+ for (Page p : pages) {+ p.invalidate(range);}}}diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/DBTraceProgramViewBookmarkManager.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/DBTraceProgramViewBookmarkManager.javaindex a412380710..376ad28a3e 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/DBTraceProgramViewBookmarkManager.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/DBTraceProgramViewBookmarkManager.java@@ -54,11 +54,6 @@ public class DBTraceProgramViewBookmarkManager implements TraceProgramViewBookmareturn bookmarkManager.defineBookmarkType(type, icon, color, priority);}- @Override- public boolean isDefinedType(String type) {- return bookmarkManager.isDefinedType(type);- }-@Overridepublic BookmarkType[] getBookmarkTypes() {Collection<? extends TraceBookmarkType> types = bookmarkManager.getDefinedBookmarkTypes();diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbol.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbol.javaindex 2479a0a84e..6843b65957 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbol.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbol.java@@ -236,7 +236,7 @@ public abstract class AbstractDBTraceSymbol extends DBAnnotatedObject@Overridepublic String[] getPath() {try (LockHold hold = LockHold.lock(manager.lock.readLock())) {- checkIsValid();+ refreshIfNeeded();if (isGlobal()) {return new String[] { getName() };}diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbolSingleTypeWithLocationView.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbolSingleTypeWithLocationView.javaindex 1eb049fcba..122478d492 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbolSingleTypeWithLocationView.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/symbol/AbstractDBTraceSymbolSingleTypeWithLocationView.java@@ -89,7 +89,7 @@ public abstract class AbstractDBTraceSymbolSingleTypeWithLocationView<T extends}@Override- protected void loadRangeCache(TraceAddressSnapRange range) {+ protected synchronized void loadRangeCache(TraceAddressSnapRange range) {rangeCache.clear();DBTraceAddressSnapRangePropertyMapSpace<Long, DBTraceSymbolIDEntry> idSpace =manager.idMap.getForSpace(range.getRange().getAddressSpace(), false);@@ -116,7 +116,7 @@ public abstract class AbstractDBTraceSymbolSingleTypeWithLocationView<T extends}@Override- protected Collection<? extends T> doGetContaining(GetSymbolsKey key) {+ protected synchronized Collection<? extends T> doGetContaining(GetSymbolsKey key) {if (key.thread != null) {List<T> result =new ArrayList<>(getIntersecting(Lifespan.at(key.snap),diff --git a/Ghidra/Debug/Framework-TraceModeling/src/test/java/ghidra/trace/database/map/DBTraceAddressSnapRangePropertyMapSpaceTest.java b/Ghidra/Debug/Framework-TraceModeling/src/test/java/ghidra/trace/database/map/DBTraceAddressSnapRangePropertyMapSpaceTest.javaindex eb18ad6ab0..15ca75dceb 100644--- a/Ghidra/Debug/Framework-TraceModeling/src/test/java/ghidra/trace/database/map/DBTraceAddressSnapRangePropertyMapSpaceTest.java+++ b/Ghidra/Debug/Framework-TraceModeling/src/test/java/ghidra/trace/database/map/DBTraceAddressSnapRangePropertyMapSpaceTest.java@@ -15,7 +15,7 @@*/package ghidra.trace.database.map;-import static ghidra.lifecycle.Unfinished.TODO;+import static ghidra.lifecycle.Unfinished.*;import static org.junit.Assert.*;import java.io.File;@@ -115,7 +115,7 @@ public class DBTraceAddressSnapRangePropertyMapSpaceTest@Overrideprotected void clearCache(boolean all) {- try (LockHold hold = LockHold.lock(rwLock.writeLock())) {+ try (LockHold hold = LockHold.lock(lock.writeLock())) {// TODO: Should each space have an invalidateCache method?super.clearCache(all);try {diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/GhidraLockHold.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/GhidraLockHold.javadeleted file mode 100644index 4fb3005ae4..0000000000--- a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/GhidraLockHold.java+++ /dev/null@@ -1,35 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.util;--public class GhidraLockHold implements AutoCloseable {- public static GhidraLockHold lock(Lock lock) {- GhidraLockHold hold = new GhidraLockHold(lock);- hold.lock.acquire();- return hold;- }-- private Lock lock;-- protected GhidraLockHold(Lock lock) {- this.lock = lock;- }-- @Override- public void close() {- this.lock.release();- }-}diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBAnnotatedObject.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBAnnotatedObject.javaindex 9e6d629f87..66d1f204b3 100644--- a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBAnnotatedObject.java+++ b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBAnnotatedObject.java@@ -19,7 +19,7 @@ import java.io.IOException;import java.util.List;import db.DBRecord;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.util.LockHold;import ghidra.util.database.DBCachedObjectStoreFactory.DBFieldCodec;import ghidra.util.database.annot.DBAnnotatedObjectInfo;@@ -116,7 +116,7 @@ import ghidra.util.database.annot.DBAnnotatedObjectInfo;* Note that there is no way to specify the primary key. For object stores, the primary key is* always the object id, and its type is always {@code long}.*/-public class DBAnnotatedObject extends DatabaseObject {+public class DBAnnotatedObject extends DbObject {private final DBCachedObjectStore<?> store;private final DBCachedDomainObjectAdapter adapter;private final List<DBFieldCodec<?, ?, ?>> codecs; // The codecs, ordered by field@@ -131,7 +131,7 @@ public class DBAnnotatedObject extends DatabaseObject {*/@SuppressWarnings({ "unchecked", "rawtypes" })protected DBAnnotatedObject(DBCachedObjectStore<?> store, DBRecord record) {- super(store == null ? null : store.cache, record == null ? -1 : record.getKey());+ super(record == null ? -1 : record.getKey());this.store = store;this.record = record;if (store != null) {diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedDomainObjectAdapter.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedDomainObjectAdapter.javaindex c547e0aca8..f53c92d9a0 100644--- a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedDomainObjectAdapter.java+++ b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedDomainObjectAdapter.java@@ -15,14 +15,12 @@*/package ghidra.util.database;-import java.util.concurrent.TimeUnit;-import java.util.concurrent.locks.*;+import java.util.concurrent.locks.ReentrantReadWriteLock;import db.DBHandle;import ghidra.framework.data.DBDomainObjectSupport;import ghidra.framework.data.OpenMode;-import ghidra.util.Msg;-import ghidra.util.Swing;+import ghidra.util.*;import ghidra.util.task.TaskMonitor;/**@@ -85,89 +83,12 @@ public abstract class DBCachedDomainObjectAdapter extends DBDomainObjectSupport}}- /**- * Adapts a {@link ghidra.util.Lock} to the {@link Lock} interface- *- * <p>- * Not all operations are supported. In particular, no {@link #lockInterruptibly()},- * {@link #tryLock(long,TimeUnit)}, nor {@link #newCondition()}.- */- static class GhidraLockWrappingLock implements Lock {- private final ghidra.util.Lock ghidraLock;-- public GhidraLockWrappingLock(ghidra.util.Lock ghidraLock) {- this.ghidraLock = ghidraLock;- }-- @Override- public void lock() {- ghidraLock.acquire();- }-- @Override- public void lockInterruptibly() throws InterruptedException {- throw new UnsupportedOperationException();- }-- @Override- public boolean tryLock() {- synchronized (ghidraLock) { // Yes, sync on the lock's intrinsic lock- Thread lockOwner = ghidraLock.getOwner();- if (lockOwner == null || lockOwner == Thread.currentThread()) {- ghidraLock.acquire();- return true;- }- return false;- }- }-- @Override- public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {- throw new UnsupportedOperationException();- }-- @Override- public void unlock() {- ghidraLock.release();- }-- @Override- public Condition newCondition() {- throw new UnsupportedOperationException();- }- }-- /**- * Not a true read-write lock, but adapts a {@link ghidra.util.Lock} to the- * {@link ReadWriteLock} interface. The read lock and the write lock are just the same lock- */- static class GhidraLockWrappingRWLock implements ReadWriteLock {- private final GhidraLockWrappingLock oneLock;-- public GhidraLockWrappingRWLock(ghidra.util.Lock ghidraLock) {- this.oneLock = new GhidraLockWrappingLock(ghidraLock);- }-- @Override- public Lock readLock() {- return oneLock;- }-- @Override- public Lock writeLock() {- return oneLock;- }- }-- protected ReadWriteLock rwLock;-/*** @see DBDomainObjectSupport*/protected DBCachedDomainObjectAdapter(DBHandle dbh, OpenMode openMode, TaskMonitor monitor,String name, int timeInterval, int bufSize, Object consumer) {super(dbh, openMode, monitor, name, timeInterval, bufSize, consumer);- this.rwLock = new GhidraLockWrappingRWLock(lock);}/**@@ -175,7 +96,7 @@ public abstract class DBCachedDomainObjectAdapter extends DBDomainObjectSupport** @return the lock*/- public ReadWriteLock getReadWriteLock() {- return rwLock;+ public Lock getReadWriteLock() {+ return lock;}}diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStore.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStore.javaindex bd743878d3..3715c2b323 100644--- a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStore.java+++ b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStore.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -28,7 +28,7 @@ import org.apache.commons.lang3.tuple.ImmutablePair;import db.*;import db.util.ErrorHandler;-import ghidra.program.database.DBObjectCache;+import ghidra.program.database.DbCache;import ghidra.program.model.address.KeyRange;import ghidra.util.LockHold;import ghidra.util.database.DBCachedObjectStoreFactory.DBFieldCodec;@@ -41,7 +41,7 @@ import ghidra.util.database.annot.DBAnnotatedField;* <p>* Essentially, this provides object-based accessed to records in the table via DAOs. See* {@link DBAnnotatedObject} for further documentation including an example object definition. The- * store keeps a cache of objects using {@link DBObjectCache}. See+ * store keeps a cache of objects using {@link DbCache}. See* {@link DBCachedObjectStoreFactory} for documentation describing how to create a store, including* for the example object definition.*@@ -200,7 +200,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa}E get(long key) throws IOException {- T cached = cache.get(key);+ T cached = cache.getCachedInstance(key);if (cached != null) {return fromObject(cached);}@@ -580,7 +580,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa@Overrideboolean typedContains(Long u) throws IOException {- if (cache.get(u) != null) {+ if (cache.getCachedInstance(u) != null) {return true;}return table.hasRecord(u);@@ -619,11 +619,12 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHaif (record == null) {return null;}- T cached = cache.get(record);+ T cached = cache.getCachedInstance(record);if (cached != null) {return cached;}T found = factory.create(DBCachedObjectStore.this, record);+ cache.add(found);found.doRefresh(record);return found;}@@ -747,7 +748,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHafinal DBCachedDomainObjectAdapter adapter;final DBHandle dbh;- final DBObjectCache<T> cache;+ final DbCache<T> cache;private final Class<T> objectType;private final DBAnnotatedObjectFactory<T> factory;private final String tableName;@@ -783,8 +784,9 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHathis.table = table;this.tableName = table.getName();this.schema = table.getSchema();- this.cache = new DBObjectCache<>(1000); // TODO: Parameterize this?this.lock = adapter.getReadWriteLock();+ this.cache = new DbCache<T>(new DebuggerFactoryAdapter<>(this, table, factory),+ adapter.getReadWriteLock(), 1000);this.codecs = DBCachedObjectStoreFactory.getCodecs(objectType);this.asForwardMap = new DBCachedObjectStoreMap<>(this, adapter, lock, Direction.FORWARD);@@ -923,11 +925,12 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa}}- protected T doCreate(long key) throws IOException {+ private T doCreate(long key) throws IOException {DBRecord rec = schema.createRecord(key);table.putRecord(rec);T created = factory.create(this, rec);created.fresh(true);+ cache.add(created);created.doUpdateAll();return created;}@@ -1224,7 +1227,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa* @return true if cached*/boolean isCached(long key) {- return cache.get(key) != null;+ return cache.getCachedInstance(key) != null;}/**diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DebuggerFactoryAdapter.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DebuggerFactoryAdapter.javanew file mode 100644index 0000000000..b9a6b2d236--- /dev/null+++ b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DebuggerFactoryAdapter.java@@ -0,0 +1,66 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.util.database;++import java.io.IOException;++import db.DBRecord;+import db.Table;+import ghidra.program.database.DbCache;+import ghidra.program.database.DbFactory;++/**+ * Maps {@link DBAnnotatedObjectFactory} objects into {@link DbFactory} objects that is needed by+ * the {@link DbCache}.+ *+ * @param <T> The DBAnnotatedObject type+ */+public class DebuggerFactoryAdapter<T extends DBAnnotatedObject> implements DbFactory<T> {++ private DBCachedObjectStore<T> store;+ private Table table;+ private DBAnnotatedObjectFactory<T> factory;++ public DebuggerFactoryAdapter(DBCachedObjectStore<T> store, Table table,+ DBAnnotatedObjectFactory<T> factory) {+ this.store = store;+ this.table = table;+ this.factory = factory;+ }++ @Override+ public T instantiate(long key) {+ try {+ DBRecord record = table.getRecord(key);+ return instantiate(record);+ }+ catch (IOException e) {+ store.dbError(e);+ }+ return null;+ }++ @Override+ public T instantiate(DBRecord record) {+ if (record == null) {+ return null;+ }+ T t = factory.create(store, record);+ t.refresh(record);+ return t;+ }++}diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/spatial/AbstractConstraintsTree.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/spatial/AbstractConstraintsTree.javaindex ff182b7021..2d25325e1d 100644--- a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/spatial/AbstractConstraintsTree.java+++ b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/spatial/AbstractConstraintsTree.java@@ -874,27 +874,31 @@ public abstract class AbstractConstraintsTree<* {@link #checkDataIntegrity(DBTreeDataRecord)} instead of this method.*/public void checkIntegrity() {- // Before we visit, integrity check that cache. Visiting will affect cache.- for (Entry<Long, Collection<DR>> ent : cachedDataChildren.entrySet()) {- Set<DR> databasedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));- // NOTE: Bypass the cache by using the variant with a key parameter- databasedChildren.addAll(getDataChildrenOf(ent.getKey()));- Set<DR> cachedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));- cachedChildren.addAll(ent.getValue());- if (!databasedChildren.equals(cachedChildren)) {- throw new AssertionError("Cached children of node " + ent.getKey() +- " out of sync: cache=" + cachedChildren + " db=" + databasedChildren);+ synchronized (cachedDataChildren) {+ // Before we visit, integrity check that cache. Visiting will affect cache.+ for (Entry<Long, Collection<DR>> ent : cachedDataChildren.entrySet()) {+ Set<DR> databasedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));+ // NOTE: Bypass the cache by using the variant with a key parameter+ databasedChildren.addAll(getDataChildrenOf(ent.getKey()));+ Set<DR> cachedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));+ cachedChildren.addAll(ent.getValue());+ if (!databasedChildren.equals(cachedChildren)) {+ throw new AssertionError("Cached children of node " + ent.getKey() ++ " out of sync: cache=" + cachedChildren + " db=" + databasedChildren);+ }}}- for (Entry<Long, Collection<NR>> ent : cachedNodeChildren.entrySet()) {- Set<NR> databasedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));- // NOTE: Bypass the cache by using the variant with a key parameter- databasedChildren.addAll(getNodeChildrenOf(ent.getKey()));- Set<NR> cachedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));- cachedChildren.addAll(ent.getValue());- if (!databasedChildren.equals(cachedChildren)) {- throw new AssertionError("Cached children of node " + ent.getKey() +- " out of sync: cache=" + cachedChildren + " db=" + databasedChildren);+ synchronized (cachedNodeChildren) {+ for (Entry<Long, Collection<NR>> ent : cachedNodeChildren.entrySet()) {+ Set<NR> databasedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));+ // NOTE: Bypass the cache by using the variant with a key parameter+ databasedChildren.addAll(getNodeChildrenOf(ent.getKey()));+ Set<NR> cachedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));+ cachedChildren.addAll(ent.getValue());+ if (!databasedChildren.equals(cachedChildren)) {+ throw new AssertionError("Cached children of node " + ent.getKey() ++ " out of sync: cache=" + cachedChildren + " db=" + databasedChildren);+ }}}visit(null, new TreeRecordVisitor() {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/bookmark/BookmarkNavigator.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/bookmark/BookmarkNavigator.javaindex 33d123b09d..81f8ca98f1 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/bookmark/BookmarkNavigator.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/bookmark/BookmarkNavigator.java@@ -43,7 +43,6 @@ public class BookmarkNavigator {final static Icon WARNING_ICON = new GIcon("icon.plugin.bookmark.type.warning");final static Icon ERROR_ICON = new GIcon("icon.plugin.bookmark.type.error");final static Icon ANALYSIS_ICON = new GIcon("icon.plugin.bookmark.type.analysis");- final static Icon DEFAULT_ICON = new GIcon("icon.plugin.bookmark.type.default");final static int NOTE_PRIORITY = MarkerService.BOOKMARK_PRIORITY;final static int ERROR_PRIORITY = MarkerService.BOOKMARK_PRIORITY + BIG_CHANGE;@@ -81,14 +80,6 @@ public class BookmarkNavigator {}Icon icon = bmt.getIcon();- if (icon == null) {- if (bookmarkManager.isDefinedType(type)) {- // This implies the client defined a type, but did not pass a valid icon. In this- // case we will show a special icon.- icon = DEFAULT_ICON;- }- }-Color color = bmt.getMarkerColor();if (color == null) {color = DEFAULT_COLOR;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.javaindex aafe6c4d5c..0caa26d22c 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java@@ -21,7 +21,7 @@ import javax.help.UnsupportedOperationException;import docking.widgets.OptionDialog;import docking.widgets.fieldpanel.support.*;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.database.data.DataTypeUtilities;import ghidra.program.model.data.*;import ghidra.program.model.lang.InsufficientBytesException;@@ -1443,7 +1443,7 @@ public abstract class CompEditorModel<T extends Composite> extends CompositeEditelse {// Check for managed datatype changingDataType originalDt = originalDTM.getDataType(newPath);- if (!(originalDt instanceof DatabaseObject)) {+ if (!(originalDt instanceof DbObject)) {return;}DataType dt = viewDTM.findMyDataTypeFromOriginalID(originalDTM.getID(originalDt));@@ -1532,7 +1532,7 @@ public abstract class CompEditorModel<T extends Composite> extends CompositeEdit// undo transactions for the viewDTM. An editor save could generate quite a few with// potentially many types getting changed by one change.DataType changedDt = originalDTM.getDataType(path);- if (!(changedDt instanceof DatabaseObject)) {+ if (!(changedDt instanceof DbObject)) {return;}DataType viewDt =diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeViewerDataTypeManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeViewerDataTypeManager.javaindex 1d8b8d1a62..a8575c90ea 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeViewerDataTypeManager.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeViewerDataTypeManager.java@@ -22,7 +22,7 @@ import java.util.TreeSet;import javax.help.UnsupportedOperationException;import db.util.ErrorHandler;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.data.*;import ghidra.program.model.lang.ProgramArchitecture;import ghidra.util.Swing;@@ -231,7 +231,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAlreturn viewComposite;}DataType resolvedDt = super.resolve(dataType, handler);- if ((dataType instanceof DatabaseObject) && originalDTM.contains(dataType)) {+ if ((dataType instanceof DbObject) && originalDTM.contains(dataType)) {long originalId = originalDTM.getID(dataType);long myId = getID(resolvedDt);dataTypeIDMap.put(myId, originalId);@@ -247,14 +247,14 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAlthrow new IllegalArgumentException("datatype is not from this manager");}- if (existingViewDt instanceof DatabaseObject) {+ if (existingViewDt instanceof DbObject) {dataTypeIDMap.remove(getID(existingViewDt));}DataType newResolvedDt =super.replaceDataType(existingViewDt, replacementDt, updateCategoryPath);- if (newResolvedDt instanceof DatabaseObject &&+ if (newResolvedDt instanceof DbObject &&replacementDt.getDataTypeManager() == originalDTM) {long originalId = originalDTM.getID(replacementDt);long myId = getID(newResolvedDt);@@ -271,7 +271,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAlthrow new IllegalArgumentException("datatype is not from this manager");}- if (existingViewDt instanceof DatabaseObject) {+ if (existingViewDt instanceof DbObject) {dataTypeIDMap.remove(getID(existingViewDt));}@@ -293,7 +293,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAlIterator<DataType> allDataTypes = getAllDataTypes();while (allDataTypes.hasNext()) {DataType dt = allDataTypes.next();- if (dt == viewComposite || !(dt instanceof DatabaseObject)) {+ if (dt == viewComposite || !(dt instanceof DbObject)) {continue;}@@ -396,7 +396,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAllong id = orphanIds.removeFirst();if (!hasParent(id)) {DataType dt = getDataType(id);- if (dt instanceof DatabaseObject) {+ if (dt instanceof DbObject) {if (dt == viewComposite) {continue;@@ -485,7 +485,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAlif (existingViewDt.getDataTypeManager() != this) {throw new IllegalArgumentException("datatype is not from this manager");}- if (!(existingViewDt instanceof DatabaseObject)) {+ if (!(existingViewDt instanceof DbObject)) {return false;}return dataTypeIDMap.getOriginalIDFromViewID(getID(existingViewDt)) != null;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.javaindex d3358b7694..7d9585ca1e 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.java@@ -36,7 +36,7 @@ import ghidra.app.plugin.core.stackeditor.StackFrameDataType.StackComponentWrappimport ghidra.app.util.datatype.EmptyCompositeException;import ghidra.framework.plugintool.Plugin;import ghidra.framework.plugintool.PluginTool;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.data.*;import ghidra.program.model.listing.*;import ghidra.program.model.symbol.SourceType;@@ -1125,7 +1125,7 @@ public class StackEditorModel extends CompositeEditorModel<StackFrameDataType> {// Check for managed datatype changingDataType originalDt = originalDTM.getDataType(newPath);- if (!(originalDt instanceof DatabaseObject)) {+ if (!(originalDt instanceof DbObject)) {return;}DataType dt = viewDTM.findMyDataTypeFromOriginalID(originalDTM.getID(originalDt));@@ -1161,7 +1161,7 @@ public class StackEditorModel extends CompositeEditorModel<StackFrameDataType> {}DataType changedDt = originalDTM.getDataType(path);- if (!(changedDt instanceof DatabaseObject)) {+ if (!(changedDt instanceof DbObject)) {return;}DataType viewDt = viewDTM.findMyDataTypeFromOriginalID(originalDTM.getID(changedDt));@@ -1250,7 +1250,7 @@ public class StackEditorModel extends CompositeEditorModel<StackFrameDataType> {for (int i = comps.length - 1; i >= 0; i--) {DataTypeComponent component = comps[i];DataType compDt = component.getDataType();- if (compDt instanceof DatabaseObject) {+ if (compDt instanceof DbObject) {// NOTE: viewDTM only maps view-to-original IDs for DataTypeDBlong myId = viewDTM.getID(compDt);if (viewDTM.findOriginalDataTypeFromMyID(myId) == null) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.javaindex 6bf4530140..af1b6347e5 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.java@@ -28,7 +28,7 @@ import ghidra.app.util.DataTypeNamingUtil;import ghidra.app.util.bin.format.dwarf.attribs.DWARFNumericAttribute;import ghidra.app.util.bin.format.dwarf.expression.DWARFExpressionException;import ghidra.app.util.bin.format.golang.rtti.types.GoKind;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.database.data.DataTypeUtilities;import ghidra.program.model.data.*;import ghidra.program.model.data.Enum;@@ -247,7 +247,7 @@ public class DWARFDataTypeImporter {* offset -> ddt*/private void recordTempDataType(DWARFDataType ddt) {- if (ddt.dataType instanceof DatabaseObject) {+ if (ddt.dataType instanceof DbObject) {// don't store info about types that are already in the databasereturn;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/OperandFieldHelper.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/OperandFieldHelper.javaindex 932603aca3..ff6cdf0ff1 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/OperandFieldHelper.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/OperandFieldHelper.java@@ -455,7 +455,6 @@ abstract class OperandFieldHelper extends FieldFactory {if (separator != null) {results.add(separator);}-for (int opIndex = 0; opIndex < numOperands; opIndex++) {OpInfo opInfo = new OpInfo(inst, opIndex);addOperandElements(opInfo, results);diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.javaindex 8ddf7e2fc1..67cbdb803c 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java@@ -21,7 +21,7 @@ import javax.swing.*;import org.junit.Test;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.data.*;public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTest {@@ -312,7 +312,7 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTesprivate DataTypeComponent addFlexDataType(Structure struct, DataType dataType, String name,String comment) {ArrayDataType a = new ArrayDataType(dataType, 0, 1);- if (struct instanceof DatabaseObject) {+ if (struct instanceof DbObject) {DataTypeManager dtm = struct.getDataTypeManager();return dtm.withTransaction("Add Flex Array", () -> struct.add(a, name, comment));}diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/ObjectCacheTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/ObjectCacheTest.javaindex 2da7410d93..ac8a2a3798 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/ObjectCacheTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/ObjectCacheTest.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -17,31 +17,37 @@ package ghidra.program.database;import static org.junit.Assert.*;-import java.util.Arrays;-import java.util.ConcurrentModificationException;+import java.util.*;+import org.junit.Before;import org.junit.Test;+import db.*;import ghidra.program.model.address.KeyRange;import ghidra.test.AbstractGhidraHeadedIntegrationTest;+import ghidra.util.Lock;/***/public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {- private DBObjectCache<TestObj> cache = new DBObjectCache<TestObj>(100);-- /**- * Constructor for ObjectCacheTest.- * @param arg0- */- public ObjectCacheTest() {- super();+ private DbCache<TestObj> cache;+ private Lock lock = new Lock("test");+ private Schema schema;+ private Map<Long, DBRecord> database = new HashMap<>();++ @Before+ public void setUp() {+ Field[] fields = { new IntField() };+ String[] fieldNames = { "Value" };++ schema = new Schema(1, "id", fields, fieldNames);+ cache = new DbCache<TestObj>(new TestFactory(), lock, 100);}@Test- public void testSetInvalid() {- TestObj obj1 = getTestObj(1);- obj1.setInvalid();+ public void testDeleted() {+ TestObj obj1 = createTestObj(1, 1);+ deleteTestObj(obj1);try {obj1.setValue(10);fail("Should have thrown a concurrent modification excption here.");@@ -53,8 +59,10 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {@Testpublic void testDeleteFromCacheSetsDeletedOnObject() {- TestObj obj1 = getTestObj(1);+ TestObj obj1 = createTestObj(1, 1);cache.delete(1);+ assertFalse(obj1.isValid());+try {obj1.setValue(10);fail("Should have thrown a concurrent modification excption here.");@@ -66,11 +74,12 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {@Testpublic void testDeleteUndoProblem() {- TestObj obj1 = getTestObj(1);- obj1.setInvalid();- obj1.checkIsValid();+ TestObj obj1 = createTestObj(1, 1);+ deleteTestObj(obj1);+ assertFalse(obj1.refreshIfNeeded());+ assertTrue(obj1.isDeleted(lock));- TestObj obj2 = getTestObj(1);+ TestObj obj2 = createTestObj(1, 1);assertTrue(obj1 != obj2);obj1.getValue(); // previously, this would have caused obj2 to be removed@@ -84,7 +93,7 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {@Testpublic void testDeleteRange() {for (int i = 0; i < 10; i++) {- getTestObj(i);+ createTestObj(i, i);}assertEquals(10, cache.size());cache.delete(Arrays.asList(new KeyRange(4, 6)));@@ -94,43 +103,105 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {@Testpublic void testDeleteBigRange() {for (int i = 0; i < 10; i++) {- getTestObj(i);+ createTestObj(i, i);}assertEquals(10, cache.size());cache.delete(Arrays.asList(new KeyRange(2, 100)));assertEquals(2, cache.size());}- private TestObj getTestObj(int key) {- TestObj obj = cache.get(key);- if (obj == null) {- obj = new TestObj(cache, key);- }- return obj;+ @Test+ public void testRefresh() {+ TestObj obj1 = createTestObj(1, 1);+ obj1.setInvalid();+ assertFalse(obj1.isValid());+ assertEquals(1, obj1.getValue());+ assertTrue(obj1.isValid());}-}-class TestObj extends DatabaseObject {- int value = -1;+ @Test+ public void testInvalideCache() {+ TestObj obj1 = createTestObj(1, 1);+ TestObj obj2 = createTestObj(2, 2);+ TestObj obj3 = createTestObj(3, 3);++ assertTrue(obj1.isValid());+ assertTrue(obj1.isValid());+ assertTrue(obj1.isValid());++ cache.invalidate();- TestObj(DBObjectCache<TestObj> cache, long key) {- super(cache, key);+ assertFalse(obj1.isValid());+ assertFalse(obj1.isValid());+ assertFalse(obj1.isValid());++ assertTrue(obj1 == getTestObj(1));+ assertTrue(obj2 == getTestObj(2));+ assertTrue(obj3 == getTestObj(3));++ assertTrue(obj1.isValid());+ assertTrue(obj1.isValid());+ assertTrue(obj1.isValid());}- public void setValue(int value) {- checkDeleted();- this.value = value;+ private TestObj createTestObj(long key, int value) {+ DBRecord record = schema.createRecord(key);+ record.setIntValue(0, value);+ database.put(key, record);+ return cache.getCachedInstance(record);}- public int getValue() {- checkIsValid();- return value;+ private TestObj getTestObj(long key) {+ return cache.getCachedInstance(key);}- @Override- protected boolean refresh() {- // return false to simulate the record has been deleted- return false;+ private void deleteTestObj(TestObj obj) {+ database.remove(obj.getKey());+ obj.setInvalid();}+ class TestFactory implements DbFactory<TestObj> {++ @Override+ public TestObj instantiate(long key) {+ DBRecord record = database.get(key);+ return instantiate(record);+ }++ @Override+ public TestObj instantiate(DBRecord record) {+ return new TestObj(record);+ }++ }++ class TestObj extends DbObject {+ private DBRecord record;++ TestObj(DBRecord record) {+ super(record.getKey());+ this.record = record;+ }++ public void setValue(int value) {+ checkDeleted();+ record.setIntValue(0, value);+ }++ public int getValue() {+ refreshIfNeeded();+ return record.getIntValue(0);+ }++ @Override+ protected boolean refresh() {+ DBRecord refreshedRecord = database.get(record.getKey());+ if (refreshedRecord != null) {+ record = refreshedRecord;+ return true;+ }+ return false;+ }++ }}diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/data/PointerTypedefDataTypeTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/data/PointerTypedefDataTypeTest.javaindex b24c469385..86c10eca67 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/data/PointerTypedefDataTypeTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/data/PointerTypedefDataTypeTest.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -20,7 +20,7 @@ import static org.junit.Assert.*;import org.junit.*;import ghidra.docking.settings.Settings;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.database.ProgramBuilder;import ghidra.program.database.data.PointerTypedefInspector;import ghidra.program.model.address.AddressSpace;@@ -96,7 +96,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertEquals("char * " + formatAttributes("offset(0x8)"), dt.getName());DataType dbDt = dtm.resolve(dt, null);assertTrue(dbDt instanceof TypeDef);- assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt instanceof DbObject); // transforms to TypedefDBassertTrue(dt.isEquivalent(dbDt));assertEquals(dt.getName(), dbDt.getName());@@ -112,7 +112,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertEquals("char * " + formatAttributes("relative"), dt.getName());dbDt = dtm.resolve(dt, null);assertTrue(dbDt instanceof TypeDef);- assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt instanceof DbObject); // transforms to TypedefDBassertTrue(dt.isEquivalent(dbDt));assertEquals(dt.getName(), dbDt.getName());@@ -131,7 +131,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertEquals("char *16 " + formatAttributes("space(register)"), dt.getName());DataType dbDt = dtm.resolve(dt, null);assertTrue(dbDt instanceof TypeDef);- assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt instanceof DbObject); // transforms to TypedefDBassertTrue(dt.isEquivalent(dbDt));assertEquals(dt.getName(), dbDt.getName());@@ -147,7 +147,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertEquals("char *32 " + formatAttributes("space(register)"), dt.getName());dbDt = dtm.resolve(dt, null);assertTrue(dbDt instanceof TypeDef);- assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt instanceof DbObject); // transforms to TypedefDBassertTrue(dt.isEquivalent(dbDt));assertEquals(dt.getName(), dbDt.getName());@@ -169,7 +169,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertEquals("foo *16 " + formatAttributes("space(register)"), dt.getName());DataType dbDt = dtm.resolve(dt, null);assertTrue(dbDt instanceof TypeDef);- assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt instanceof DbObject); // transforms to TypedefDBassertTrue(dt.isEquivalent(dbDt));assertEquals(dt.getName(), dbDt.getName());@@ -244,7 +244,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertEquals(2, dt.getLength());assertEquals("foo *16 " + formatAttributes("space(register)"), dt.getName());TypeDef dbDt = (TypeDef) dtm.resolve(dt, null);- assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt instanceof DbObject); // transforms to TypedefDBassertTrue(dt.isEquivalent(dbDt));assertEquals(dt.getName(), dbDt.getName());@@ -252,7 +252,8 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertTrue(dbDt == dtm.resolve(dt, null)); // should resolve to same instancedt = (TypeDef) dt.copy(dtm);- PointerTypeSettingsDefinition.DEF.setType(dt.getDefaultSettings(), PointerType.IMAGE_BASE_RELATIVE);+ PointerTypeSettingsDefinition.DEF.setType(dt.getDefaultSettings(),+ PointerType.IMAGE_BASE_RELATIVE);TypeDef dbDt2 = (TypeDef) dtm.resolve(dt, null); // should resolve to new instanceassertTrue(dbDt != dbDt2);assertEquals("foo *16 " + formatAttributes("image-base-relative,space(register)"),@@ -276,7 +277,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertEquals(2, dt.getLength());assertEquals("foo *16 " + formatAttributes("space(register)"), dt.getName());TypeDef dbDt = (TypeDef) dtm.resolve(dt, null);- assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt instanceof DbObject); // transforms to TypedefDBassertTrue(dt.isEquivalent(dbDt));assertEquals(dt.getName(), dbDt.getName());@@ -290,7 +291,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationTassertTrue(dbDt != dbDt2);assertFalse(dbDt.isEquivalent(dbDt2));assertFalse(dbDt2.isEquivalent(dbDt));- assertTrue(dbDt2 instanceof DatabaseObject); // transforms to TypedefDB+ assertTrue(dbDt2 instanceof DbObject); // transforms to TypedefDBassertTrue(dt2.isEquivalent(dbDt2));assertEquals(dt2.getName(), dbDt2.getName());diff --git a/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.java b/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.javaindex df3ae3db89..ba8a85d85b 100644--- a/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.java+++ b/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.java@@ -37,6 +37,7 @@ import ghidra.program.model.util.CodeUnitInsertionException;import ghidra.program.model.util.PropertyMap;import ghidra.test.ToyProgramBuilder;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.SaveableColor;import ghidra.util.exception.NoValueException;import ghidra.util.task.TaskMonitor;@@ -992,8 +993,7 @@ public class CodeManagerTest extends AbstractGenericTest {ProgramDB pdb = (ProgramDB) program;Lock lock = pdb.getLock();- lock.acquire();- try {+ try (Closeable c = lock.write()) {Instruction instructionAt = pdb.getListing().getInstructionAt(addr(0x2000));assertEquals(FlowOverride.NONE, instructionAt.getFlowOverride());@@ -1007,9 +1007,6 @@ public class CodeManagerTest extends AbstractGenericTest {instructionAt = pdb.getListing().getInstructionAt(addr(0x2000));assertEquals(FlowOverride.CALL, instructionAt.getFlowOverride());}- finally {- lock.release();- }}@Test@@ -1020,9 +1017,7 @@ public class CodeManagerTest extends AbstractGenericTest {ProgramDB pdb = (ProgramDB) program;Lock lock = pdb.getLock();- lock.acquire();- try {-+ try (Closeable c = lock.write()) {Instruction instructionAt = pdb.getListing().getInstructionAt(addr(0x2000));assertEquals(FlowOverride.NONE, instructionAt.getFlowOverride());@@ -1031,9 +1026,6 @@ public class CodeManagerTest extends AbstractGenericTest {instructionAt = pdb.getListing().getInstructionAt(addr(0x2002));assertEquals(null, instructionAt);}- finally {- lock.release();- }}private Address addr(long l) {diff --git a/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/DefaultDataCacheTest.java b/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/DefaultDataCacheTest.javaindex 1ed130a622..046a2c45c1 100644--- a/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/DefaultDataCacheTest.java+++ b/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/DefaultDataCacheTest.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -79,12 +79,12 @@ public class DefaultDataCacheTest extends AbstractGenericTest {assertTrue(cu instanceof Data);DataDB data = (DataDB) cu;assertTrue(!data.isDefined());- assertTrue(!((Boolean) invokeInstanceMethod("isInvalid", data)));+ assertFalse((Boolean) invokeInstanceMethod("needsRefreshing", data));AddressSet restrictedSet = new AddressSet(addr(0x1000), addr(0x1003));Disassembler disassembler = Disassembler.getDisassembler(program, TaskMonitor.DUMMY, null);AddressSetView disAddrs = disassembler.disassemble(addr(0x1000), restrictedSet);assertTrue(!disAddrs.isEmpty());- assertTrue(!((Boolean) invokeInstanceMethod("checkIsValid", data)));+ assertFalse((Boolean) invokeInstanceMethod("needsRefreshing", data));assertNull(listing.getCodeUnitAt(addr(0x1001)));}diff --git a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionRecord.java b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionRecord.javaindex 9cb1d5289f..1a49f2c5d1 100644--- a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionRecord.java+++ b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionRecord.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -19,14 +19,13 @@ import static ghidra.feature.fid.db.FunctionsTable.*;import db.DBRecord;import ghidra.feature.fid.hash.FidHashQuad;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.util.NumericUtilities;/*** Represents a function record in the FID database.*/-public class FunctionRecord extends DatabaseObject implements FidHashQuad {+public class FunctionRecord extends DbObject implements FidHashQuad {public final static int HAS_TERMINATOR_FLAG = 1;public final static int AUTO_PASS_FLAG = 2;public final static int AUTO_FAIL_FLAG = 4;@@ -46,11 +45,10 @@ public class FunctionRecord extends DatabaseObject implements FidHashQuad {/*** Package private constructor, to be called from FunctionsTable exclusively.* @param fid database (for string references)- * @param cache FunctionRecord object cache* @param record record for this function*/- FunctionRecord(FidDB fid, DBObjectCache<FunctionRecord> cache, DBRecord record) {- super(cache, record.getKey());+ FunctionRecord(FidDB fid, DBRecord record) {+ super(record.getKey());this.record = record;this.fidDb = fid;}@@ -169,6 +167,7 @@ public class FunctionRecord extends DatabaseObject implements FidHashQuad {byte val = record.getByteValue(FLAGS_COL);return ((val & FORCE_RELATION_FLAG) != 0);}+/*** Returns the record id (primary key).* @return the record iddiff --git a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionsTable.java b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionsTable.javaindex f87550652e..be1a9f59d6 100644--- a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionsTable.java+++ b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/FunctionsTable.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -22,7 +22,9 @@ import java.util.regex.Pattern;import db.*;import ghidra.feature.fid.hash.FidHashQuad;-import ghidra.program.database.DBObjectCache;+import ghidra.program.database.DbFactory;+import ghidra.program.database.DbCache;+import ghidra.util.Lock;import ghidra.util.UniversalIdGenerator;/**@@ -62,20 +64,19 @@ public class FunctionsTable {Table table;FidDB fidDb;StringsTable stringsTable;- DBObjectCache<FunctionRecord> functionCache;+ DbCache<FunctionRecord> functionCache;/*** Creates or attaches a functions table.+ * @param fid the fid database* @param handle database handle- * @param stringsTable strings table (must be created first!)- * @param create whether to create or just attach* @throws IOException if create fails*/public FunctionsTable(FidDB fid, DBHandle handle) throws IOException {table = handle.getTable(FUNCTIONS_TABLE);this.fidDb = fid;this.stringsTable = fid.getStringsTable();- functionCache = new DBObjectCache<>(CACHE_SIZE);+ functionCache = new DbCache<>(new FunctionRecordFactory(), new Lock("Fid"), CACHE_SIZE);}public static void createTable(DBHandle handle) throws IOException {@@ -116,11 +117,7 @@ public class FunctionsTable {if (record.getLongValue(SPECIFIC_HASH_COL) != hash) {continue;}- FunctionRecord functionRecord = functionCache.get(record);- if (functionRecord == null) {- functionRecord = new FunctionRecord(fidDb, functionCache, record);- }- list.add(functionRecord);+ list.add(functionCache.getCachedInstance(record));}return list;}@@ -141,12 +138,10 @@ public class FunctionsTable {List<FunctionRecord> list = new ArrayList<>();while (iterator.hasNext()) {Field key = iterator.next();- FunctionRecord functionRecord = functionCache.get(key.getLongValue());- if (functionRecord == null) {- DBRecord record = table.getRecord(key);- functionRecord = new FunctionRecord(fidDb, functionCache, record);+ FunctionRecord functionRecord = functionCache.getCachedInstance(key.getLongValue());+ if (functionRecord != null) {+ list.add(functionRecord);}- list.add(functionRecord);}return list;}@@ -179,7 +174,8 @@ public class FunctionsTable {byte flags = (byte) (hasTerminator ? FunctionRecord.HAS_TERMINATOR_FLAG : 0);record.setByteValue(FLAGS_COL, flags);table.putRecord(record);- FunctionRecord functionRecord = new FunctionRecord(fidDb, functionCache, record);+ FunctionRecord functionRecord = new FunctionRecord(fidDb, record);+ functionCache.add(functionRecord);return functionRecord;}@@ -188,7 +184,7 @@ public class FunctionsTable {* @param functionID is the id of the function record to modify* @param flagMask is the bit to modify* @param value is true to set, false to clear- * @throws IOException+ * @throws IOException if error occurs while reading database*/void modifyFlags(long functionID, int flagMask, boolean value) throws IOException {DBRecord record = table.getRecord(functionID);@@ -217,30 +213,12 @@ public class FunctionsTable {*/public List<FunctionRecord> getFunctionRecordsByNameSubstring(String nameSearch)throws IOException {- DBFieldIterator iterator = table.indexKeyIterator(NAME_ID_COL);-- if (!iterator.hasNext()) {- return Collections.emptyList();- }List<FunctionRecord> list = new ArrayList<>();+ RecordIterator iterator = table.iterator();while (iterator.hasNext()) {- Field key = iterator.next();- FunctionRecord functionRecord = functionCache.get(key.getLongValue());- if (functionRecord == null) {- DBRecord record = table.getRecord(key);- long nameID = record.getLongValue(NAME_ID_COL);- StringRecord nameRecord = stringsTable.lookupString(nameID);- String name = nameRecord.getValue();- if (name.contains(nameSearch)) {- functionRecord = new FunctionRecord(fidDb, functionCache, record);- }- }- else {- if (!functionRecord.getName().contains(nameSearch)) {- functionRecord = null;- }- }- if (functionRecord != null) {+ DBRecord record = iterator.next();+ FunctionRecord functionRecord = functionCache.getCachedInstance(record);+ if (functionRecord.getName().contains(nameSearch)) {list.add(functionRecord);}}@@ -256,32 +234,14 @@ public class FunctionsTable {*/public List<FunctionRecord> getFunctionRecordsByNameRegex(String regex) throws IOException {Matcher matcher = Pattern.compile(regex).matcher("");- DBFieldIterator iterator = table.indexKeyIterator(NAME_ID_COL);- if (!iterator.hasNext()) {- return Collections.emptyList();- }List<FunctionRecord> list = new ArrayList<>();+ RecordIterator iterator = table.iterator();while (iterator.hasNext()) {- Field key = iterator.next();- FunctionRecord functionRecord = functionCache.get(key.getLongValue());- if (functionRecord == null) {- DBRecord record = table.getRecord(key);- long nameID = record.getLongValue(NAME_ID_COL);- StringRecord nameRecord = stringsTable.lookupString(nameID);- String name = nameRecord.getValue();- matcher.reset(name);- if (matcher.matches()) {- functionRecord = new FunctionRecord(fidDb, functionCache, record);- }- }- else {- matcher.reset(functionRecord.getName());- if (!matcher.matches()) {- functionRecord = null;- }- }- if (functionRecord != null) {+ DBRecord record = iterator.next();+ FunctionRecord functionRecord = functionCache.getCachedInstance(record);+ matcher.reset(functionRecord.getName());+ if (matcher.matches()) {list.add(functionRecord);}}@@ -295,14 +255,7 @@ public class FunctionsTable {* @throws IOException if database seek encounters an error*/public FunctionRecord getFunctionByID(long functionID) throws IOException {- FunctionRecord functionRecord = functionCache.get(functionID);- if (functionRecord == null) {- DBRecord record = table.getRecord(functionID);- if (record != null) {- functionRecord = new FunctionRecord(fidDb, functionCache, record);- }- }- return functionRecord;+ return functionCache.getCachedInstance(functionID);}/**@@ -323,11 +276,7 @@ public class FunctionsTable {StringRecord domainPathRecord = stringsTable.lookupString(domainPathID);String domainPath = domainPathRecord.getValue();if (domainPath.contains(domainPathSearch)) {- FunctionRecord functionRecord = functionCache.get(record);- if (functionRecord == null) {- functionRecord = new FunctionRecord(fidDb, functionCache, record);- }- list.add(functionRecord);+ list.add(functionCache.getCachedInstance(record));}}return list;@@ -356,20 +305,31 @@ public class FunctionsTable {List<FunctionRecord> list = new ArrayList<>();while (iterator.hasNext()) {Field key = iterator.next();- FunctionRecord functionRecord = functionCache.get(key.getLongValue());- if (functionRecord == null) {+ FunctionRecord functionRecord = functionCache.getCachedInstance(key.getLongValue());+ if (functionRecord.getLibraryID() == libraryKey) {+ list.add(functionRecord);+ }+ }+ return list;+ }++ class FunctionRecordFactory implements DbFactory<FunctionRecord> {++ @Override+ public FunctionRecord instantiate(long key) {+ try {DBRecord record = table.getRecord(key);- if (record.getLongValue(LIBRARY_ID_COL) == libraryKey) {- functionRecord = new FunctionRecord(fidDb, functionCache, record);- list.add(functionRecord);- }+ return record == null ? null : instantiate(record);}- else {- if (functionRecord.getLibraryID() == libraryKey) {- list.add(functionRecord);- }+ catch (IOException e) {+ throw new RuntimeException("serious delayed database access error", e);}}- return list;++ @Override+ public FunctionRecord instantiate(DBRecord record) {+ return new FunctionRecord(fidDb, record);+ }}+}diff --git a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringRecord.java b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringRecord.javaindex 6b5002fab3..e21a4ab302 100644--- a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringRecord.java+++ b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringRecord.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -15,13 +15,12 @@*/package ghidra.feature.fid.db;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;/*** A string record in the FID database.*/-public class StringRecord extends DatabaseObject {+public class StringRecord extends DbObject {/*** The value of the string.*/@@ -29,12 +28,11 @@ public class StringRecord extends DatabaseObject {/*** Constructor with the primary key and the string value.- * @param cache StringRecord object cache* @param key primary key* @param value the string value*/- public StringRecord(DBObjectCache<StringRecord> cache, long key, String value) {- super(cache, key);+ public StringRecord(long key, String value) {+ super(key);this.value = value;}diff --git a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringsTable.java b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringsTable.javaindex 31ae4e42d3..5761613e63 100644--- a/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringsTable.java+++ b/Ghidra/Features/FunctionID/src/main/java/ghidra/feature/fid/db/StringsTable.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -18,7 +18,9 @@ package ghidra.feature.fid.db;import java.io.IOException;import db.*;-import ghidra.program.database.DBObjectCache;+import ghidra.program.database.DbFactory;+import ghidra.program.database.DbCache;+import ghidra.util.Lock;import ghidra.util.UniversalIdGenerator;/**@@ -43,17 +45,16 @@ public class StringsTable {static int[] INDEXED_COLUMNS = new int[] { STRING_VALUE_COL };Table table;- DBObjectCache<StringRecord> stringCache;+ DbCache<StringRecord> stringCache;/*** Creates or attaches the string table.* @param handle the database handle- * @param create whether to create or attach* @throws IOException if the database has a problem*/public StringsTable(DBHandle handle) throws IOException {table = handle.getTable(STRINGS_TABLE);- stringCache = new DBObjectCache<>(CACHE_SIZE);+ stringCache = new DbCache<>(new StringRecordFactory(), new Lock("Fid"), CACHE_SIZE);}public static void createTable(DBHandle handle) throws IOException {@@ -99,20 +100,25 @@ public class StringsTable {* @return the string record, or null if no such key*/StringRecord lookupString(long stringID) {- StringRecord stringRecord = stringCache.get(stringID);- if (stringRecord == null) {- DBRecord record;+ return stringCache.getCachedInstance(stringID);+ }++ class StringRecordFactory implements DbFactory<StringRecord> {++ @Override+ public StringRecord instantiate(long key) {try {- record = table.getRecord(stringID);- if (record != null) {- stringRecord =- new StringRecord(stringCache, stringID, record.getString(STRING_VALUE_COL));- }+ DBRecord record = table.getRecord(key);+ return record == null ? null : instantiate(record);}catch (IOException e) {throw new RuntimeException("serious delayed database access error", e);}}- return stringRecord;++ @Override+ public StringRecord instantiate(DBRecord record) {+ return new StringRecord(record.getKey(), record.getString(STRING_VALUE_COL));+ }}}diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/AssociationDatabaseManager.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/AssociationDatabaseManager.javaindex ecbdc67f83..446efcca8d 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/AssociationDatabaseManager.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/AssociationDatabaseManager.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -25,9 +25,11 @@ import ghidra.feature.vt.api.impl.*;import ghidra.feature.vt.api.main.*;import ghidra.feature.vt.api.util.VTAssociationStatusException;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DBObjectCache;+import ghidra.program.database.DbFactory;+import ghidra.program.database.DbCache;import ghidra.program.model.address.Address;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskLauncher;import ghidra.util.task.TaskMonitor;@@ -39,9 +41,9 @@ public class AssociationDatabaseManager implements VTAssociationManager {private VTAssociationTableDBAdapter associationTableAdapter;private VTMatchMarkupItemTableDBAdapter markupItemTableAdapter;- private DBObjectCache<MarkupItemStorageDB> markupItemCache;+ private DbCache<MarkupItemStorageDB> markupItemCache;private List<AssociationHook> associationHooks = new ArrayList<>();- private DBObjectCache<VTAssociationDB> associationCache;+ private DbCache<VTAssociationDB> associationCache;private AcceptedStatusCache acceptedStatusCache = new AcceptedStatusCache();Lock lock;@@ -67,8 +69,8 @@ public class AssociationDatabaseManager implements VTAssociationManager {AssociationDatabaseManager(VTSessionDB session) {this.session = session;lock = session.getLock();- associationCache = new DBObjectCache<>(10);- markupItemCache = new DBObjectCache<>(10);+ associationCache = new DbCache<>(new AssociationFactory(), lock, 10);+ markupItemCache = new DbCache<>(new MarkupFactory(), lock, 10);}/**@@ -84,8 +86,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {Collection<MarkupItemStorageDB> items = new ArrayList<>();VTAssociationDB associationDB = (VTAssociationDB) association;- try {- lock.acquire();+ try (Closeable c = lock.read()) {int recordCount = markupItemTableAdapter.getRecordCount();if (recordCount == 0) {recordCount = 1; // to give the appearance of progress@@ -98,7 +99,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {while (records.hasNext()) {monitor.checkCancelled();DBRecord record = records.next();- items.add(getMarkupItemForRecord(record));+ items.add(markupItemCache.getCachedInstance(record));monitor.incrementProgress(1);}@@ -107,9 +108,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {catch (IOException e) {session.dbError(e);}- finally {- lock.release();- }return items;}@@ -148,20 +146,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {}}- private MarkupItemStorageDB getMarkupItemForRecord(DBRecord markupItemRecord) {- try {- lock.acquire();- MarkupItemStorageDB markupItem = markupItemCache.get(markupItemRecord);- if (markupItem == null) {- markupItem = new MarkupItemStorageDB(markupItemRecord, markupItemCache, this);- }- return markupItem;- }- finally {- lock.release();- }- }-Address getDestinationAddressFromLong(long longValue) {return session.getDestinationAddressFromLong(longValue);}@@ -188,7 +172,8 @@ public class AssociationDatabaseManager implements VTAssociationManager {try {DBRecord record = markupItemTableAdapter.createMarkupItemRecord(markupItem);- MarkupItemStorageDB appliedMarkupItem = getMarkupItemForRecord(record);+ MarkupItemStorageDB appliedMarkupItem = new MarkupItemStorageDB(record, this);+ markupItemCache.add(appliedMarkupItem);return appliedMarkupItem;}catch (IOException e) {@@ -213,18 +198,15 @@ public class AssociationDatabaseManager implements VTAssociationManager {boolean isBlocked = isBlocked(sourceAddress, destinationAddress);VTAssociationDB newAssociation = null;- try {- lock.acquire();+ try (Closeable c = lock.write()) {DBRecord record = associationTableAdapter.insertRecord(sourceLong, destinationLong,type, isBlocked ? BLOCKED : AVAILABLE, 0);- newAssociation = new VTAssociationDB(this, associationCache, record);+ newAssociation = new VTAssociationDB(this, record);+ associationCache.add(newAssociation);}catch (IOException e) {session.dbError(e);}- finally {- lock.release();- }session.setChanged(VTEvent.ASSOCIATION_ADDED, null, newAssociation);return newAssociation;}@@ -274,33 +256,28 @@ public class AssociationDatabaseManager implements VTAssociationManager {@Overridepublic List<VTAssociation> getAssociations() {List<VTAssociation> list = new ArrayList<>();- lock.acquire();- try {+ try (Closeable c = lock.read()) {RecordIterator iterator = associationTableAdapter.getRecords();for (; iterator.hasNext();) {DBRecord nextRecord = iterator.next();- list.add(getAssociationForRecord(nextRecord));+ list.add(associationCache.getCachedInstance(nextRecord));}}catch (IOException e) {session.dbError(e);}- finally {- lock.release();- }return list;}@Overridepublic VTAssociation getAssociation(Address sourceAddress, Address destinationAddress) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {long addressKey = session.getLongFromSourceAddress(sourceAddress);RecordIterator iterator =associationTableAdapter.getRecordsForSourceAddress(addressKey);while (iterator.hasNext()) {DBRecord record = iterator.next();- VTAssociationDB associationDB = getAssociationForRecord(record);+ VTAssociationDB associationDB = associationCache.getCachedInstance(record);if (associationDB.getDestinationAddress().equals(destinationAddress)) {return associationDB;}@@ -309,9 +286,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {catch (IOException e) {session.dbError(e);}- finally {- lock.release();- }return null;}@@ -323,7 +297,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {associationTableAdapter.getRecordsForSourceAddress(addressKey);while (iterator.hasNext()) {DBRecord record = iterator.next();- VTAssociationDB associationDB = getAssociationForRecord(record);+ VTAssociationDB associationDB = associationCache.getCachedInstance(record);Address dbDestinatonAddress = associationDB.getDestinationAddress();if (destinationAddress.equals(dbDestinatonAddress)) {return associationDB;@@ -337,99 +311,51 @@ public class AssociationDatabaseManager implements VTAssociationManager {return null;}- private VTAssociationDB getAssociationForRecord(DBRecord record) {- if (record == null) {- throw new AssertException("How can we have a null record?!!!");- }- try {- lock.acquire();- VTAssociationDB associationDB = associationCache.get(record);- if (associationDB == null) {- associationDB = new VTAssociationDB(this, associationCache, record);- }- return associationDB;- }- finally {- lock.release();- }- }-- VTAssociationDB getAssociation(long associationKey) {- try {- lock.acquire();- VTAssociationDB associationDB = associationCache.get(associationKey);- if (associationDB != null) {- return associationDB;- }- DBRecord record = associationTableAdapter.getRecord(associationKey);- if (record == null) {- return null;- }- return new VTAssociationDB(this, associationCache, record);- }- catch (IOException e) {- session.dbError(e);- }- finally {- lock.release();- }- return null;- }-public VTSessionDB getSession() {return session;}@Overridepublic Collection<VTAssociation> getRelatedAssociationsBySourceAddress(Address sourceAddress) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {long sourceId = session.getLongFromSourceAddress(sourceAddress);Set<DBRecord> relatedRecords =associationTableAdapter.getRelatedAssociationRecordsBySourceAddress(sourceId);List<VTAssociation> associations = new ArrayList<>();for (DBRecord record : relatedRecords) {- associations.add(getAssociationForRecord(record));+ associations.add(associationCache.getCachedInstance(record));}return associations;}catch (IOException e) {session.dbError(e);}- finally {- lock.release();- }return Collections.emptyList();}@Overridepublic Collection<VTAssociation> getRelatedAssociationsByDestinationAddress(Address destinationAddress) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {long destinationId = session.getLongFromDestinationAddress(destinationAddress);Set<DBRecord> relatedRecords = associationTableAdapter.getRelatedAssociationRecordsByDestinationAddress(destinationId);List<VTAssociation> associations = new ArrayList<>();for (DBRecord record : relatedRecords) {- associations.add(getAssociationForRecord(record));+ associations.add(associationCache.getCachedInstance(record));}return associations;}catch (IOException e) {session.dbError(e);}- finally {- lock.release();- }return Collections.emptyList();}@Overridepublic Collection<VTAssociation> getRelatedAssociationsBySourceAndDestinationAddress(Address sourceAddress, Address destinationAddress) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {long sourceId = session.getLongFromSourceAddress(sourceAddress);long destinationId = session.getLongFromDestinationAddress(destinationAddress);Set<DBRecord> relatedRecords =@@ -437,16 +363,13 @@ public class AssociationDatabaseManager implements VTAssociationManager {sourceId, destinationId);List<VTAssociation> associations = new ArrayList<>();for (DBRecord record : relatedRecords) {- associations.add(getAssociationForRecord(record));+ associations.add(associationCache.getCachedInstance(record));}return associations;}catch (IOException e) {session.dbError(e);}- finally {- lock.release();- }return Collections.emptyList();}@@ -564,7 +487,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {sourceId, destinationId);relatedRecords.remove(association.getRecord()); // don't change the given associationfor (DBRecord record : relatedRecords) {- relatedAssociaitons.add(getAssociationForRecord(record));+ relatedAssociaitons.add(associationCache.getCachedInstance(record));}}catch (IOException e) {@@ -581,7 +504,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {session.dbError(e);}- VTAssociationDB association = getAssociationForRecord(record);+ VTAssociationDB association = associationCache.getCachedInstance(record);Address sourceAddress = association.getSourceAddress();Address destinationAddress = association.getDestinationAddress();VTAssociationStatus status = association.getStatus();@@ -619,6 +542,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {void removeMarkupRecord(long key) {try {markupItemTableAdapter.removeMarkupItemRecord(key);+ markupItemCache.delete(key);}catch (IOException e) {session.dbError(e);@@ -661,60 +585,43 @@ public class AssociationDatabaseManager implements VTAssociationManager {private boolean disposed = false;void dispose() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {disposed = true;invalidate();}- finally {- lock.release();- }}void invalidate() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {invalid = true;acceptedSourceAssociations.clear();acceptedDestinationAssociations.clear();}- finally {- lock.release();- }}void remove(Address sourceAddress, Address destinationAddress) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (disposed || invalid) {return;}acceptedSourceAssociations.remove(sourceAddress);acceptedDestinationAssociations.remove(destinationAddress);}- finally {- lock.release();- }}void add(Address sourceAddress, Address destinationAddress) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (disposed || invalid) {return;}acceptedSourceAssociations.add(sourceAddress);acceptedDestinationAssociations.add(destinationAddress);}- finally {- lock.release();- }}boolean isBlocked(Address sourceAddress, Address destinationAddress) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (disposed) {return true;@@ -735,9 +642,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {return true;}}- finally {- lock.release();- }return false;}@@ -749,7 +653,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {.getRelatedAssociationRecordsBySourceAndDestinationAddress(sourceID,destinationID);for (DBRecord record : relatedRecords) {- VTAssociationDB associationDB = getAssociationForRecord(record);+ VTAssociationDB associationDB = associationCache.getCachedInstance(record);VTAssociationStatus status = associationDB.getStatus();if (status == ACCEPTED) {return true;@@ -785,7 +689,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {while (it.hasNext()) {monitor.increment();DBRecord record = it.next();- VTAssociationDB associationDB = getAssociationForRecord(record);+ VTAssociationDB associationDB = associationCache.getCachedInstance(record);VTAssociationStatus status = associationDB.getStatus();if (status == ACCEPTED) {Address sourceAddress = associationDB.getSourceAddress();@@ -804,4 +708,48 @@ public class AssociationDatabaseManager implements VTAssociationManager {}}}++ private class AssociationFactory implements DbFactory<VTAssociationDB> {++ @Override+ public VTAssociationDB instantiate(long key) {+ try {+ DBRecord record = associationTableAdapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ session.dbError(e);+ return null;+ }+ }++ @Override+ public VTAssociationDB instantiate(DBRecord rec) {+ return new VTAssociationDB(AssociationDatabaseManager.this, rec);+ }+ }++ private class MarkupFactory implements DbFactory<MarkupItemStorageDB> {++ @Override+ public MarkupItemStorageDB instantiate(long key) {+ try {+ DBRecord record = markupItemTableAdapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ session.dbError(e);+ return null;+ }+ }++ @Override+ public MarkupItemStorageDB instantiate(DBRecord rec) {+ return new MarkupItemStorageDB(rec, AssociationDatabaseManager.this);+ }+ }++ VTAssociation getAssociation(long associationKey) {+ return associationCache.getCachedInstance(associationKey);+ }}diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/MarkupItemStorageDB.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/MarkupItemStorageDB.javaindex f85df416e4..214ae596fb 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/MarkupItemStorageDB.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/MarkupItemStorageDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -25,22 +25,21 @@ import ghidra.feature.vt.api.main.VTMarkupItemStatus;import ghidra.feature.vt.api.markuptype.VTMarkupType;import ghidra.feature.vt.api.markuptype.VTMarkupTypeFactory;import ghidra.feature.vt.api.util.Stringable;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Program;+import ghidra.util.Lock.Closeable;-public class MarkupItemStorageDB extends DatabaseObject implements MarkupItemStorage {+public class MarkupItemStorageDB extends DbObject implements MarkupItemStorage {private final AssociationDatabaseManager associationManager;private final VTAssociation association;private final VTSessionDB session;private DBRecord record;- MarkupItemStorageDB(DBRecord record, DBObjectCache<MarkupItemStorageDB> cache,- AssociationDatabaseManager associationManager) {- super(cache, record.getKey());+ MarkupItemStorageDB(DBRecord record, AssociationDatabaseManager associationManager) {+ super(record.getKey());this.record = record;this.associationManager = associationManager;this.session = associationManager.getSession();@@ -60,47 +59,67 @@ public class MarkupItemStorageDB extends DatabaseObject implements MarkupItemSto@Overridepublic Address getSourceAddress() {- long addressLong = record.getLongValue(SOURCE_ADDRESS_COL.column());- Program program = session.getSourceProgram();- AddressMap addressMap = program.getAddressMap();- return addressMap.decodeAddress(addressLong);+ try (Closeable c = associationManager.lock.read()) {+ refreshIfNeeded();+ long addressLong = record.getLongValue(SOURCE_ADDRESS_COL.column());+ Program program = session.getSourceProgram();+ AddressMap addressMap = program.getAddressMap();+ return addressMap.decodeAddress(addressLong);+ }}@Overridepublic Address getDestinationAddress() {- long addressLong = record.getLongValue(DESTINATION_ADDRESS_COL.column());- Program program = session.getDestinationProgram();- AddressMap addressMap = program.getAddressMap();- return addressMap.decodeAddress(addressLong);+ try (Closeable c = associationManager.lock.read()) {+ refreshIfNeeded();+ long addressLong = record.getLongValue(DESTINATION_ADDRESS_COL.column());+ Program program = session.getDestinationProgram();+ AddressMap addressMap = program.getAddressMap();+ return addressMap.decodeAddress(addressLong);+ }}@Overridepublic String getDestinationAddressSource() {- return record.getString(ADDRESS_SOURCE_COL.column());+ try (Closeable c = associationManager.lock.read()) {+ refreshIfNeeded();+ return record.getString(ADDRESS_SOURCE_COL.column());+ }}@Overridepublic VTMarkupItemStatus getStatus() {- checkIsValid();- byte ordinal = record.getByteValue(STATUS_COL.column());- return VTMarkupItemStatus.values()[ordinal];+ try (Closeable c = associationManager.lock.read()) {+ refreshIfNeeded();+ byte ordinal = record.getByteValue(STATUS_COL.column());+ return VTMarkupItemStatus.values()[ordinal];+ }}@Overridepublic String getStatusDescription() {- return record.getString(STATUS_DESCRIPTION_COL.column());+ try (Closeable c = associationManager.lock.read()) {+ refreshIfNeeded();+ return record.getString(STATUS_DESCRIPTION_COL.column());+ }}@Overridepublic Stringable getSourceValue() {- String string = record.getString(SOURCE_VALUE_COL.column());- return Stringable.getStringable(string, session.getSourceProgram());+ try (Closeable c = associationManager.lock.read()) {+ refreshIfNeeded();+ String string = record.getString(SOURCE_VALUE_COL.column());+ return Stringable.getStringable(string, session.getSourceProgram());+ }}@Overridepublic Stringable getDestinationValue() {- String string = record.getString(ORIGINAL_DESTINATION_VALUE_COL.column());- return Stringable.getStringable(string, session.getDestinationProgram());+ try (Closeable c = associationManager.lock.read()) {+ refreshIfNeeded();+ String string = record.getString(ORIGINAL_DESTINATION_VALUE_COL.column());+ return Stringable.getStringable(string, session.getDestinationProgram());+ }}@Override@@ -131,16 +150,13 @@ public class MarkupItemStorageDB extends DatabaseObject implements MarkupItemSto@Overridepublic MarkupItemStorage reset() {- associationManager.lock.acquire();- try {+ try (Closeable c = associationManager.lock.write()) {+ checkDeleted();MarkupItemStorage storage = new MarkupItemStorageImpl(getAssociation(), getMarkupType(),getSourceAddress(), getDestinationAddress(), getDestinationAddressSource());associationManager.removeMarkupRecord(record.getKey());return storage;}- finally {- associationManager.lock.release();- }}@Overridediff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTAssociationDB.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTAssociationDB.javaindex a046e5795c..a70d230e2e 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTAssociationDB.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTAssociationDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -24,21 +24,25 @@ import ghidra.feature.vt.api.impl.MarkupItemManagerImpl;import ghidra.feature.vt.api.impl.VTEvent;import ghidra.feature.vt.api.main.*;import ghidra.feature.vt.api.util.VTAssociationStatusException;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.address.Address;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.task.TaskMonitor;-public class VTAssociationDB extends DatabaseObject implements VTAssociation {+public class VTAssociationDB extends DbObject implements VTAssociation {public DBRecord record;private MarkupItemManagerImpl markupManager;public final AssociationDatabaseManager associationDBM;- public VTAssociationDB(AssociationDatabaseManager associationManager,- DBObjectCache<VTAssociationDB> cache, DBRecord record) {- super(cache, record.getKey());+ /**+ * Constructor+ * @param associationManager the association database manager+ * @param record the record for the association+ */+ VTAssociationDB(AssociationDatabaseManager associationManager, DBRecord record) {+ super(record.getKey());this.associationDBM = associationManager;this.record = record;markupManager = new MarkupItemManagerImpl(this);@@ -93,41 +97,29 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {@Overridepublic Address getSourceAddress() {- associationDBM.lock.acquire();- try {- checkIsValid();+ try (Closeable c = associationDBM.lock.read()) {+ refreshIfNeeded();return associationDBM.getSourceAddressFromLong(record.getLongValue(SOURCE_ADDRESS_COL.column()));}- finally {- associationDBM.lock.release();- }}@Overridepublic Address getDestinationAddress() {- associationDBM.lock.acquire();- try {- checkIsValid();+ try (Closeable c = associationDBM.lock.read()) {+ refreshIfNeeded();return associationDBM.getDestinationAddressFromLong(record.getLongValue(DESTINATION_ADDRESS_COL.column()));}- finally {- associationDBM.lock.release();- }}@Overridepublic VTAssociationType getType() {- associationDBM.lock.acquire();- try {- checkIsValid();+ try (Closeable c = associationDBM.lock.read()) {+ refreshIfNeeded();byte associationTypeOrdinal = record.getByteValue(TYPE_COL.column());return VTAssociationType.values()[associationTypeOrdinal];}- finally {- associationDBM.lock.release();- }}public void markupItemStatusChanged(VTMarkupItem markupItem) {@@ -136,37 +128,25 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {@Overridepublic VTAssociationStatus getStatus() {- associationDBM.lock.acquire();- try {- checkIsValid();+ try (Closeable c = associationDBM.lock.read()) {+ refreshIfNeeded();return VTAssociationStatus.values()[record.getByteValue(STATUS_COL.column())];}- finally {- associationDBM.lock.release();- }}@Overridepublic VTAssociationMarkupStatus getMarkupStatus() {- associationDBM.lock.acquire();- try {- checkIsValid();+ try (Closeable c = associationDBM.lock.read()) {+ refreshIfNeeded();return new VTAssociationMarkupStatus(record.getByteValue(APPLIED_STATUS_COL.column()));}- finally {- associationDBM.lock.release();- }}DBRecord getRecord() {- associationDBM.lock.acquire();- try {- checkIsValid();+ try (Closeable c = associationDBM.lock.read()) {+ refreshIfNeeded();return record;}- finally {- associationDBM.lock.release();- }}@Override@@ -220,20 +200,15 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {@Overridepublic int getVoteCount() {- associationDBM.lock.acquire();- try {- checkIsValid();+ try (Closeable c = associationDBM.lock.read()) {+ refreshIfNeeded();return record.getIntValue(VOTE_COUNT_COL.column());}- finally {- associationDBM.lock.release();- }}@Overridepublic void setMarkupStatus(VTAssociationMarkupStatus status) {- associationDBM.lock.acquire();- try {+ try (Closeable c = associationDBM.lock.write()) {checkDeleted();VTAssociationMarkupStatus existingStatus = getMarkupStatus();if (status.equals(existingStatus)) {@@ -246,14 +221,10 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {.setObjectChanged(VTEvent.ASSOCIATION_MARKUP_STATUS_CHANGED, this,existingStatus, status);}- finally {- associationDBM.lock.release();- }}public void setStatus(VTAssociationStatus status) {- associationDBM.lock.acquire();- try {+ try (Closeable c = associationDBM.lock.write()) {checkDeleted();VTAssociationStatus existingStatus = getStatus();if (status == existingStatus) {@@ -266,15 +237,11 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {.setObjectChanged(VTEvent.ASSOCIATION_STATUS_CHANGED, this, existingStatus,status);}- finally {- associationDBM.lock.release();- }}@Overridepublic void setVoteCount(int voteCount) {- associationDBM.lock.acquire();- try {+ try (Closeable c = associationDBM.lock.write()) {checkDeleted();voteCount = Math.max(0, voteCount);record.setIntValue(VOTE_COUNT_COL.column(), voteCount);@@ -282,9 +249,6 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {associationDBM.getSession().setObjectChanged(VTEvent.VOTE_COUNT_CHANGED, this, null, null);}- finally {- associationDBM.lock.release();- }}@Overridediff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchDB.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchDB.javaindex 2e9aa74dc0..e5017c7958 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchDB.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -23,14 +23,14 @@ import db.DBRecord;import ghidra.feature.vt.api.impl.VTEvent;import ghidra.feature.vt.api.impl.VTProgramCorrelatorInfo;import ghidra.feature.vt.api.main.*;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.address.Address;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.SystemUtilities;import ghidra.util.exception.AssertException;-public class VTMatchDB extends DatabaseObject implements VTMatch {+public class VTMatchDB extends DbObject implements VTMatch {private DBRecord record;private final VTMatchSetDB matchSet;@@ -42,8 +42,8 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {private boolean doCalculateHash = true;private int hash;- public VTMatchDB(DBObjectCache<VTMatchDB> cache, DBRecord record, VTMatchSetDB matchSet) {- super(cache, record.getKey());+ public VTMatchDB(DBRecord record, VTMatchSetDB matchSet) {+ super(record.getKey());this.record = record;this.matchSet = matchSet;session = (VTSessionDB) matchSet.getSession();@@ -105,8 +105,7 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {@Overridepublic void setTag(VTMatchTag tag) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (record == null) {return;@@ -127,9 +126,6 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {updateRecord();session.setObjectChanged(VTEvent.MATCH_TAG_CHANGED, this, oldTag, newTagDB);}- finally {- lock.release();- }}private VTAssociation loadAssociation() {@@ -145,16 +141,12 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {@Overridepublic VTAssociation getAssociation() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (association == null) {association = loadAssociation();}}- finally {- lock.release();- }return association;}diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchSetDB.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchSetDB.javaindex 2cbd3d6236..45df05c5d8 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchSetDB.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchSetDB.java@@ -30,22 +30,22 @@ import ghidra.feature.vt.api.main.*;import ghidra.framework.data.OpenMode;import ghidra.framework.options.Options;import ghidra.framework.options.ToolOptions;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.*;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressSet;import ghidra.program.model.listing.Program;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.VersionException;import ghidra.util.task.TaskMonitor;import ghidra.util.xml.XmlUtilities;-public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {+public class VTMatchSetDB extends DbObject implements VTMatchSet {private final DBRecord matchSetRecord;- private DBObjectCache<VTMatchDB> matchCache;+ private DbCache<VTMatchDB> matchCache;private final VTSessionDB session;private VTMatchTableDBAdapter matchTableAdapter;@@ -73,13 +73,13 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {}private VTMatchSetDB(DBRecord record, VTSessionDB session, DBHandle dbHandle, Lock lock) {- super(null, record.getKey());// cache not supported+ super(record.getKey());// cache not supportedthis.matchSetRecord = record;this.session = session;this.dbHandle = dbHandle;this.lock = lock;- matchCache = new DBObjectCache<>(10);+ matchCache = new DbCache<>(new MatchFactory(), lock, 10);}private void createTableAdapters(long tableID) throws IOException {@@ -162,20 +162,17 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {VTAssociationDB associationDB = associationManager.getOrCreateAssociationDB(info.getSourceAddress(), info.getDestinationAddress(), info.getAssociationType());VTMatchTag tag = info.getTag();- VTMatch newMatch = null;- try {- lock.acquire();+ VTMatchDB newMatch = null;+ try (Closeable c = lock.write()) {VTMatchTagDB tagDB = session.getOrCreateMatchTagDB(tag);DBRecord matchRecord =matchTableAdapter.insertMatchRecord(info, this, associationDB, tagDB);- newMatch = getMatchForRecord(matchRecord);+ newMatch = new VTMatchDB(matchRecord, this);+ matchCache.add(newMatch);}catch (IOException e) {dbError(e);}- finally {- lock.release();- }if (newMatch != null) {session.setObjectChanged(VTEvent.MATCH_ADDED, newMatch, null, newMatch);}@@ -213,8 +210,8 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {VTAssociation association = match.getAssociation();Address sourceAddress = association.getSourceAddress();Address destinationAddress = association.getDestinationAddress();- try {- lock.acquire();+ try (Closeable c = lock.write()) {+ checkDeleted();long matchKey = matchDb.getKey();boolean deleted = matchTableAdapter.deleteRecord(matchKey);if (deleted) {@@ -231,9 +228,6 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {catch (IOException e) {dbError(e);}- finally {- lock.release();- }DeletedMatch deletedMatch = new DeletedMatch(sourceAddress, destinationAddress);session.setObjectChanged(VTEvent.MATCH_DELETED, match, deletedMatch, null);@@ -247,35 +241,31 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {@Overridepublic Collection<VTMatch> getMatches() {List<VTMatch> list = new LinkedList<>();- try {- lock.acquire();+ try (Closeable c = lock.read()) {RecordIterator iterator = matchTableAdapter.getRecords();while (iterator.hasNext()) {DBRecord nextRecord = iterator.next();- list.add(getMatchForRecord(nextRecord));+ list.add(matchCache.getCachedInstance(nextRecord));}}catch (IOException e) {dbError(e);}- finally {- lock.release();- }return list;}@Overridepublic Collection<VTMatch> getMatches(VTAssociation association) {- VTAssociationDB associationDB = (VTAssociationDB) association;List<VTMatch> list = new LinkedList<>();- if (associationDB == null) {- return list; // No association, so no matches.- }- try {+ try (Closeable c = lock.read()) {+ VTAssociationDB associationDB = (VTAssociationDB) association;+ if (associationDB == null) {+ return list; // No association, so no matches.+ }RecordIterator iterator = matchTableAdapter.getRecords(associationDB.getKey());while (iterator.hasNext()) {DBRecord nextRecord = iterator.next();- VTMatch match = getMatchForRecord(nextRecord);+ VTMatch match = matchCache.getCachedInstance(nextRecord);list.add(match);}}@@ -287,44 +277,23 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {@Overridepublic Collection<VTMatch> getMatches(Address sourceAddress, Address destinationAddress) {- AssociationDatabaseManager associationManager = session.getAssociationManagerDBM();- VTAssociationDB existingAssociationDB =- associationManager.getExistingAssociationDB(sourceAddress, destinationAddress);- if (existingAssociationDB == null) {- return Collections.emptyList();+ try (Closeable c = lock.read()) {+ AssociationDatabaseManager associationManager = session.getAssociationManagerDBM();+ VTAssociationDB existingAssociationDB =+ associationManager.getExistingAssociationDB(sourceAddress, destinationAddress);+ if (existingAssociationDB == null) {+ return Collections.emptyList();+ }+ return getMatches(existingAssociationDB);}- return getMatches(existingAssociationDB);}@Overrideprotected boolean refresh() {+ // MatchSets are not cached, so this method is not usedreturn true;}- private VTMatch getMatchForRecord(DBRecord matchRecord) {- try {- lock.acquire();- VTMatchDB match = matchCache.get(matchRecord);- if (match == null) {- match = new VTMatchDB(matchCache, matchRecord, this);- }- return match;- }- finally {- lock.release();- }- }-- DBRecord getMatchRecord(long matchRecordKey) {- try {- return matchTableAdapter.getMatchRecord(matchRecordKey);- }- catch (IOException e) {- dbError(e);- }- return null;- }-Program getDestinationProgram() {return session.getDestinationProgram();}@@ -345,9 +314,34 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {matchCache.invalidate();}+ DBRecord getMatchRecord(long matchKey) {+ try {+ return matchTableAdapter.getMatchRecord(matchKey);+ }+ catch (IOException e) {+ session.dbError(e);+ return null;+ }+ }+@Overridepublic String toString() {return "Match Set " + getID() + " - " + getMatchCount() + " matches [Correlator=" +getProgramCorrelatorInfo().getName() + "]";}++ private class MatchFactory implements DbFactory<VTMatchDB> {++ @Override+ public VTMatchDB instantiate(long matchKey) {+ DBRecord record = getMatchRecord(matchKey);+ return record == null ? null : instantiate(record);+ }++ @Override+ public VTMatchDB instantiate(DBRecord rec) {+ return new VTMatchDB(rec, VTMatchSetDB.this);+ }+ }+}diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchTagDB.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchTagDB.javaindex ef107ce293..e572947b83 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchTagDB.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTMatchTagDB.java@@ -15,25 +15,24 @@*/package ghidra.feature.vt.api.db;-import static ghidra.feature.vt.api.db.VTMatchTagDBAdapter.ColumnDescription.TAG_NAME_COL;+import static ghidra.feature.vt.api.db.VTMatchTagDBAdapter.ColumnDescription.*;import java.io.IOException;import db.DBRecord;import ghidra.feature.vt.api.main.VTMatchTag;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;/*** The database object for a user defined tag on a version tracking match.*/-public class VTMatchTagDB extends DatabaseObject implements VTMatchTag {+public class VTMatchTagDB extends DbObject implements VTMatchTag {private VTSessionDB sessionDB;private DBRecord record;- VTMatchTagDB(VTSessionDB sessionDB, DBObjectCache<VTMatchTagDB> cache, DBRecord record) {- super(cache, record.getKey());+ VTMatchTagDB(VTSessionDB sessionDB, DBRecord record) {+ super(record.getKey());this.sessionDB = sessionDB;this.record = record;}@@ -75,7 +74,7 @@ public class VTMatchTagDB extends DatabaseObject implements VTMatchTag {* null if the match tag has been deleted.*/DBRecord getRecord() {- return checkIsValid() ? record : null;+ return refreshIfNeeded() ? record : null;}@Overridediff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTSessionDB.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTSessionDB.javaindex 3e08dbd370..887c97dc1d 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTSessionDB.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/db/VTSessionDB.java@@ -32,12 +32,14 @@ import ghidra.framework.model.*;import ghidra.framework.model.TransactionInfo.Status;import ghidra.framework.options.Options;import ghidra.framework.store.LockException;-import ghidra.program.database.DBObjectCache;+import ghidra.program.database.DbCache;+import ghidra.program.database.DbFactory;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressSet;import ghidra.program.model.listing.Program;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskLauncher;import ghidra.util.task.TaskMonitor;@@ -88,7 +90,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {private VTMatchSetTableDBAdapter matchSetTableAdapter;private AssociationDatabaseManager associationManager;private VTMatchTagDBAdapter matchTagAdapter;- private DBObjectCache<VTMatchTagDB> tagCache = new DBObjectCache<>(10);+ private DbCache<VTMatchTagDB> tagCache;private Program sourceProgram;private Program destinationProgram;@@ -127,7 +129,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {public VTSessionDB(String name, Program sourceProgram, Program destinationProgram,Object consumer) throws IOException {super(new DBHandle(), UNUSED_DEFAULT_NAME, EVENT_NOTIFICATION_DELAY, consumer);-+ tagCache = new DbCache<>(new TagFactory(), lock, 10);propertyTable = dbh.getTable(PROPERTY_TABLE_NAME);int ID = startTransaction("Constructing New Version Tracking Match Set");@@ -435,16 +437,12 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {@Overrideprotected void clearCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.clearCache(all);associationManager.invalidateCache();tagCache.invalidate();reconcileCachedMatchSets();}- finally {- lock.release();- }}@Override@@ -512,14 +510,10 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {@Overridepublic VTMatchSet createMatchSet(VTProgramCorrelator correlator) {- try {- lock.acquire();+ try (Closeable c = lock.write()) {long id = matchSetTableAdapter.getNextMatchSetID();return createMatchSet(correlator, id);}- finally {- lock.release();- }}private VTMatchSet createMatchSet(VTProgramCorrelator correlator, long id) {@@ -588,7 +582,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {@Overridepublic List<VTMatchSet> getMatchSets() {- return new ArrayList<>(matchSetMap.values());+ return lock.withRead(() -> new ArrayList<>(matchSetMap.values()));}AddressSet getSourceAddressSet(DBRecord record) throws IOException {@@ -649,17 +643,13 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {@Overridepublic List<VTMatch> getMatches(VTAssociation association) {- try {- lock.acquire();+ try (Closeable c = lock.read()) {List<VTMatch> matches = new ArrayList<>();for (VTMatchSet matchSet : matchSetMap.values()) {matches.addAll(matchSet.getMatches(association));}return matches;}- finally {- lock.release();- }}/**@@ -679,30 +669,22 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {@Overridepublic VTMatchSet getManualMatchSet() {- try {- lock.acquire();+ try (Closeable c = lock.read()) {if (manualMatchSet == null) {manualMatchSet = findMatchSet(ManualMatchProgramCorrelator.class.getName());}return manualMatchSet;}- finally {- lock.release();- }}@Overridepublic VTMatchSet getImpliedMatchSet() {- try {- lock.acquire();+ try (Closeable c = lock.read()) {if (impliedMatchSet == null) {impliedMatchSet = findMatchSet(ImpliedMatchProgramCorrelator.class.getName());}return impliedMatchSet;}- finally {- lock.release();- }}private VTMatchSet findMatchSet(String correlatorClassName) {@@ -719,8 +701,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {@Overridepublic void deleteMatchTag(VTMatchTag tag) {String tagName = tag.getName();- try {- lock.acquire();+ try (Closeable c = lock.write()) {VTMatchTagDB tagDB = getMatchTagDB(tagName);if (tagDB == null) {return; // not sure if this can happen@@ -734,30 +715,24 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {catch (IOException e) {dbError(e);}- finally {- lock.release();- }setObjectChanged(VTEvent.TAG_REMOVED, this, tagName, null);}@Overridepublic VTMatchTagDB createMatchTag(String tagName) {VTMatchTagDB matchTag = null;- try {- lock.acquire();+ try (Closeable c = lock.write()) {matchTag = getMatchTagDB(tagName);if (matchTag != null) {return matchTag;}DBRecord record = matchTagAdapter.insertRecord(tagName);- matchTag = new VTMatchTagDB(this, tagCache, record);+ matchTag = new VTMatchTagDB(this, record);+ tagCache.add(matchTag);}catch (IOException e) {dbError(e);}- finally {- lock.release();- }setObjectChanged(VTEvent.TAG_ADDED, matchTag, null, matchTag);return matchTag;}@@ -773,64 +748,26 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {return null;}+ public VTMatchTag getMatchTag(long tagKey) {+ VTMatchTagDB tag = tagCache.getCachedInstance(tagKey);+ return tag != null ? tag : VTMatchTag.UNTAGGED;+ }+@Overridepublic Set<VTMatchTag> getMatchTags() {Set<VTMatchTag> tags = new HashSet<>();- try {- lock.acquire();+ try (Closeable c = lock.read()) {RecordIterator records = matchTagAdapter.getRecords();while (records.hasNext()) {DBRecord record = records.next();- tags.add(getMatchTagNew(record));+ tags.add(tagCache.getCachedInstance(record));}}catch (IOException e) {dbError(e);}- finally {- lock.release();- }- return tags;- }- private VTMatchTagDB getMatchTagNew(DBRecord record) {- if (record == null) {- throw new AssertException("How can we have a null record?!!!");- }-- try {- lock.acquire();- VTMatchTagDB matchTagDB = tagCache.get(record);- if (matchTagDB == null) {- matchTagDB = new VTMatchTagDB(this, tagCache, record);- }-- return matchTagDB;- }- finally {- lock.release();- }- }-- public VTMatchTag getMatchTag(long key) {- try {- lock.acquire();- VTMatchTagDB matchTagDB = tagCache.get(key);- if (matchTagDB != null) {- return matchTagDB;- }- DBRecord record = matchTagAdapter.getRecord(key);- if (record != null) {- return new VTMatchTagDB(this, tagCache, record);- }- }- catch (IOException e) {- dbError(e);- }- finally {- lock.release();- }- return VTMatchTag.UNTAGGED;+ return tags;}DBRecord getTagRecord(long key) throws IOException {@@ -875,4 +812,24 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {associationManager.dispose();super.close();}++ private class TagFactory implements DbFactory<VTMatchTagDB> {+ @Override+ public VTMatchTagDB instantiate(long key) {+ try {+ DBRecord record = matchTagAdapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public VTMatchTagDB instantiate(DBRecord record) {+ return new VTMatchTagDB(VTSessionDB.this, record);+ }+ }+}diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/impl/MarkupItemImpl.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/impl/MarkupItemImpl.javaindex 8029a681c8..5d0c5b822c 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/impl/MarkupItemImpl.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/impl/MarkupItemImpl.java@@ -27,7 +27,7 @@ import ghidra.feature.vt.api.markuptype.*;import ghidra.feature.vt.api.util.Stringable;import ghidra.feature.vt.api.util.VersionTrackingApplyException;import ghidra.framework.options.ToolOptions;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.address.Address;import ghidra.program.util.ProgramLocation;import ghidra.util.SystemUtilities;@@ -402,7 +402,7 @@ public class MarkupItemImpl implements VTMarkupItem {}public boolean isStoredInDB() {- return (markupItemStorage instanceof DatabaseObject);+ return (markupItemStorage instanceof DbObject);}@Overridediff --git a/Ghidra/Framework/Generic/src/main/java/generic/cache/WeakReferenceCache.java b/Ghidra/Framework/Generic/src/main/java/generic/cache/WeakReferenceCache.javanew file mode 100644index 0000000000..9ac964b32f--- /dev/null+++ b/Ghidra/Framework/Generic/src/main/java/generic/cache/WeakReferenceCache.java@@ -0,0 +1,193 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package generic.cache;++import java.lang.ref.ReferenceQueue;+import java.lang.ref.WeakReference;+import java.util.*;+import java.util.function.Consumer;+import java.util.function.Predicate;++import org.apache.commons.collections4.map.LRUMap;++/**+ * Generic cache implementation that removes items from the cache when they are no longer+ * referenced. It uses weak references for the values and a small hard cache+ * so that recent items don't get garbage collected immediately.+ * <P>+ * This class is thread safe. All public methods are synchronized.+ *+ * @param <K> The type of the object stored in this cache+ * @param <V> The type of the object stored in this cache+ */+public class WeakReferenceCache<K, V> {++ private Map<K, KeyedReference<K, V>> refsById;+ private ReferenceQueue<V> refQueue;+ private LRUMap<K, V> hardCache;++ /**+ * Constructs a new WeakReferenceCache with a given hard cache size. The hard cache size is+ * the minimum number of objects to keep in the cache. Typically, the cache will contain+ * more than this number, but the excess objects are subject to garbage collection.+ * @param hardCacheSize the minimum number of objects to keep in the cache.+ */+ public WeakReferenceCache(int hardCacheSize) {+ refsById = new HashMap<>();+ refQueue = new ReferenceQueue<>();++ hardCache = new LRUMap<>(hardCacheSize);+ }++ /**+ * Retrieves the database object with the given key from the cache.+ * @param key the key of the object to retrieve.+ * @return the cached object or null if the object with that key is not currently cached.+ */+ public synchronized V get(K key) {+ KeyedReference<K, V> ref = refsById.get(key);+ if (ref == null) {+ return null;+ }+ V v = ref.get();+ if (v == null) {+ refsById.remove(key);+ return null;+ }+ hardCache.put(key, v);+ return v;+ }++ /**+ * Returns the number of objects currently in the cache.+ * @return the number of objects currently in the cache.+ */+ public synchronized int size() {+ return refsById.size();+ }++ /**+ * Sets the number of objects to protect against garbage collection.+ * @param size the minimum number of objects to keep in the cache.+ */+ public synchronized void setHardCacheSize(int size) {+ hardCache.clear();+ hardCache = new LRUMap<>(size);+ }++ /**+ * Adds the given database object to the cache.+ * @param key the key for the cached object+ * @param data the object to add to the cache+ * @return the object that has been added to the cache+ */+ public synchronized V add(K key, V data) {+ processQueue();+ hardCache.put(key, data);+ KeyedReference<K, V> ref = new KeyedReference<>(key, data, refQueue);+ refsById.put(key, ref);+ return data;+ }++ /**+ * Returns an List of all the cached objects.+ * @return an List of all the cached objects.+ */+ public synchronized List<V> getCachedObjects() {+ List<V> list = new ArrayList<>();+ processQueue();+ for (KeyedReference<K, V> ref : refsById.values()) {+ V v = ref.get();+ if (v != null) {+ list.add(v);+ }+ }+ return list;+ }++ /**+ * Applies the given consumer to all values in the cache.+ * @param consumer the consumer to apply to all values in the cache+ */+ public synchronized void apply(Consumer<V> consumer) {+ processQueue();+ for (KeyedReference<K, V> ref : refsById.values()) {+ V v = ref.get();+ if (v != null) {+ consumer.accept(v);+ }+ }+ }++ /**+ * Removes the object with the given key from the cache.+ * @param key the key of the object to remove+ * @return the value that was removed from the cache+ */+ public synchronized V delete(K key) {+ processQueue();+ KeyedReference<K, V> ref = refsById.remove(key);+ V v = null;+ if (ref != null) {+ v = ref.get();+ ref.clear();+ }+ return v;+ }++ public synchronized void deleteIf(Predicate<V> predicate) {+ Iterator<KeyedReference<K, V>> iterator = refsById.values().iterator();+ while (iterator.hasNext()) {+ KeyedReference<K, V> ref = iterator.next();+ V v = ref.get();+ if (v == null || predicate.test(v)) {+ iterator.remove();+ }+ }+ }++ // we know the cast is safe--we put them in there+ @SuppressWarnings("unchecked")+ private void processQueue() {+ KeyedReference<K, V> ref;+ while ((ref = (KeyedReference<K, V>) refQueue.poll()) != null) {+ K key = ref.getKey();+ KeyedReference<K, V> oldValue = refsById.remove(key);++ if (oldValue != null && oldValue != ref) {+ // we have put another item in the cache with the same key. Further, we+ // removed the item, but the garbage collector had not put the item on the+ // reference queue until after we added a new reference to the cache.+ // We want to keep the last value that was added, as it has not been deleted.+ refsById.put(key, oldValue);+ }+ }+ }++ private static class KeyedReference<K, V> extends WeakReference<V> {+ private K key;++ KeyedReference(K key, V obj, ReferenceQueue<V> queue) {+ super(obj, queue);+ this.key = key;+ }++ K getKey() {+ return key;+ }+ }++}diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DomainObjectChangeSupport.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DomainObjectChangeSupport.javaindex d3f9be5680..c6abc9f988 100644--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DomainObjectChangeSupport.java+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DomainObjectChangeSupport.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -16,14 +16,16 @@package ghidra.framework.data;import java.util.*;-import java.util.concurrent.Callable;+import java.util.function.Supplier;import generic.timer.GhidraTimer;import generic.timer.GhidraTimerFactory;import ghidra.framework.model.*;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.datastruct.WeakDataStructureFactory;import ghidra.util.datastruct.WeakSet;+import utility.function.Callback;/*** A class to queue and send {@link DomainObjectChangeRecord} events.@@ -225,35 +227,14 @@ class DomainObjectChangeSupport {//=================================================================================================// Note: all clients of lockQueue() must not call external APIs that could use locking- private void withLock(Runnable r) {-- try {- writeLock.acquire();- r.run();- }- finally {- writeLock.release();- }+ private void withLock(Callback r) {+ writeLock.withWrite(r);}// Note: all clients of lockQueue() must not call external APIs that could use locking- private <T> T withLock(Callable<T> c) {-- try {- writeLock.acquire();- T result;- try {- result = c.call();- return result;- }- catch (Exception e) {- // sholudn't happen- Msg.error(this, "Exception while updating change records", e);- return null;- }- }- finally {- writeLock.release();+ private <T> T withLock(Supplier<T> s) {+ try (Closeable c = writeLock.write()) {+ return s.get();}}diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/util/Lock.java b/Ghidra/Framework/Project/src/main/java/ghidra/util/Lock.javaindex 8b799f4e09..6ed2a49f35 100644--- a/Ghidra/Framework/Project/src/main/java/ghidra/util/Lock.java+++ b/Ghidra/Framework/Project/src/main/java/ghidra/util/Lock.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -15,14 +15,41 @@*/package ghidra.util;+import java.util.concurrent.locks.ReentrantReadWriteLock;+import java.util.function.Supplier;++import utility.function.Callback;+/**- * Ghidra synchronization lock. This class allows creation of named locks for- * synchronizing modification of multiple tables in the Ghidra database.+ * Ghidra synchronization read/write lock that provides read and write methods for acquiring either+ * a shared read access or an exclusive write access.+ * <P>+ * It extends Java's ReentrantReadWriteLock, but+ * adds convenient methods to get either a read or write lock that returns an {@link AutoCloseable}+ * object that can be used in a try with resources block that will auto release the lock when+ * the try block is exited.+ * <P>+ * To use a lock for share read access the general pattern is something like:+ * <PRE>+ * int getSize() {+ * try (Closeable c = lock.read()) {+ * return record.getIntValue(SIZE);+ * }+ * }+ * </PRE>+ * <P>+ * Similarly, to get exclusive access for modification:+ * <PRE>+ * int getSize() {+ * try (Closeable c = lock.write()) {+ * return record.setIntValue(SIZE);+ * database.updateRecord(record);+ * }+ * }+ * </PRE>+ **/-public class Lock {- private Thread owner;- private int lockAquireCount = 0;- private int waiterCount = 0;+public class Lock extends ReentrantReadWriteLock {private String name;/**@@ -34,58 +61,34 @@ public class Lock {this.name = name;}+ @Override+ public String toString() {+ return name + " Lock";+ }+/**- * Acquire this synchronization lock. (i.e. begin synchronizing on this named- * lock.)+ * Acquires the read lock that can allow simultaneous access by all read threads. Will block+ * if any thread already has a write lock.+ * @return An AutoCloseable handle to the lock that be used in a try with resources block to+ * automatically release the lock when the block is exited.*/- public synchronized void acquire() {- Thread currThread = Thread.currentThread();+ public Closeable read() {+ super.readLock().lock();+ return () -> super.readLock().unlock();- while (true) {- if (owner == null) {- lockAquireCount = 1;- owner = currThread;- return;- }- else if (owner == currThread) {- lockAquireCount++;- return;- }- try {- waiterCount++;- wait();- }- catch (InterruptedException e) {- // exception from another threads notify(), ignore- // and try to get lock again- }- finally {- waiterCount--;- }- }}/**- * Releases this lock, since you are through with the code that needed- * synchronization.+ * Acquires the exclusive write lock that prevents any other thread, reader or writer, from+ * getting a lock while the write lock is held. Will block if any other thread has either+ * a read or write lock. VERY IMPORTANT, any thread that attempts to acquire the write lock+ * while already holding the read lock, will cause an immediate deadlock.+ * @return An AutoCloseable handle to the lock that be used in a try with resources block to+ * automatically release the lock when the block is exited.*/- public synchronized void release() {- Thread currThread = Thread.currentThread();-- if (lockAquireCount > 0 && (owner == currThread)) {- if (--lockAquireCount == 0) {- owner = null;- // This is purely to help sample profiling. If notify() is called the- // sampler can attribute time to the methods calling this erroneously. For some reason- // the visualvm sampler gets a sample more often when notify() is called.- if (waiterCount != 0) {- notify();- }- }- }- else {- throw new IllegalStateException("Attempted to release an unowned lock: " + name);- }+ public Closeable write() {+ super.writeLock().lock();+ return () -> super.writeLock().unlock();}/**@@ -93,7 +96,50 @@ public class Lock {** @return the thread that owns the lock or null.*/+ @Overridepublic Thread getOwner() {- return owner;+ return super.getOwner();+ }++ /**+ * A convenience method for acquiring a read lock, executing a supplier object,+ * then releasing the lock.+ * @param <T> the supplier return type+ * @param supplier the supplier to execute while holding a read lock.+ * @return the result from the supplier+ */+ public <T> T withRead(Supplier<T> supplier) {+ readLock().lock();+ try {+ return supplier.get();+ }+ finally {+ readLock().unlock();+ }+ }++ /**+ * A convenience method for acquiring a write lock, executing a callback object,+ * then releasing the lock.+ * @param callback the callback to execute while holding a write lock.+ */+ public void withWrite(Callback callback) {+ writeLock().lock();+ try {+ callback.call();+ }+ finally {+ writeLock().unlock();+ }+ }++ /**+ * Object to auto close an acquired read or write lock. Note that we can't just use the+ * java {@link java.io.Closeable} because it throws an exception on the close call that+ * we don't want.+ */+ public interface Closeable extends AutoCloseable {+ @Override+ public void close();}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DBObjectCache.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DBObjectCache.javadeleted file mode 100644index 899eeb962a..0000000000--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DBObjectCache.java+++ /dev/null@@ -1,337 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.program.database;--import java.lang.ref.ReferenceQueue;-import java.lang.ref.WeakReference;-import java.util.*;--import db.DBRecord;-import ghidra.program.model.address.KeyRange;--/**- * Generic cache implementation for objects that extend DatabaseObject. This is a reference based- * cache such that objects are only ever automatically removed from the cache when there are no- * references to that object. It also maintains a small "hard" cache so that recently accessed objects- * are not prematurely removed from the cache if there are no references to them.- *- * @param <T> The type of the object stored in this cache- */-public class DBObjectCache<T extends DatabaseObject> {-- private Map<Long, KeyedSoftReference<T>> map;- private ReferenceQueue<T> refQueue;- private LinkedList<T> hardCache;- private int hardCacheSize;- private volatile int invalidateCount;-- /**- * Constructs a new DBObjectCache with a given hard cache size. The hard cache size is- * the minimum number of objects to keep in the cache. Typically, the cache will contain- * more than this number, but the excess objects are subject to garbage collections- * @param hardCacheSize the minimum number of objects to keep in the cache.- */- public DBObjectCache(int hardCacheSize) {- this.hardCacheSize = hardCacheSize;- map = new HashMap<>();- refQueue = new ReferenceQueue<>();- hardCache = new LinkedList<>();- }-- /**- * Retrieves the database object with the given key from the cache.- * @param key the key of the object to retrieve.- * @return the cached object or null if the object with that key is not currently cached.- */- public synchronized T get(long key) {- KeyedSoftReference<T> ref = map.get(key);- if (ref != null) {- T obj = ref.get();- if (obj == null) {- map.remove(key);- }- else {- if (obj.checkIsValid()) {- addToHardCache(obj);- return obj;- }- map.remove(key);- }- }- return null;- }-- /**- * Retrieves the database object with the given record and associated key from the cache.- * This form should be used in conjunction with record iterators to avoid unnecessary- * record query during a possible object refresh. To benefit from the record the cached- * object must implement the {@link DatabaseObject#refresh(DBRecord)} method which by default- * ignores the record and simply calls {@link DatabaseObject#refresh()}.- * @param objectRecord the valid record corresponding to the object to be retrieved and possibly- * used to refresh the associated object if found in cache- * @return the cached object or null if the object with that key is not currently cached.- */- public synchronized T get(DBRecord objectRecord) {- long key = objectRecord.getKey();- KeyedSoftReference<T> ref = map.get(key);- if (ref != null) {- T obj = ref.get();- if (obj == null) {- map.remove(key);- }- else {- if (obj.checkIsValid(objectRecord)) {- addToHardCache(obj);- return obj;- }- map.remove(key);- }- }- return null;- }-- /**- * Returns the number of objects currently in the cache.- * @return the number of objects currently in the cache.- */- public int size() {- return map.size();- }-- /**- * Sets the number of objects to protect against garbage collection.- * @param size the minimum number of objects to keep in the cache.- */- public synchronized void setHardCacheSize(int size) {- while (hardCache.size() > size) {- hardCache.removeLast();- }- this.hardCacheSize = size;- }-- /**- * Adds the given database object to the cache.- * @param data the object to add to the cache.- */- void put(T data) {- processQueue();- long key = data.getKey();- addToHardCache(data);- KeyedSoftReference<T> ref = new KeyedSoftReference<>(key, data, refQueue);- map.put(key, ref);- }-- /**- * Returns an List of all the cached objects.- * @return an List of all the cached objects.- */- public synchronized List<T> getCachedObjects() {- ArrayList<T> list = new ArrayList<>();- processQueue();- for (KeyedSoftReference<T> ref : map.values()) {- T obj = ref.get();- if (obj != null) {- list.add(obj);- }- }- return list;- }-- /**- * Delete all objects from HashMap whose key is contained- * within the specified keyRanges.- * @param keyRanges key ranges to delete- */-//TODO: Discourage large cases by only allowing a single range to be specified- public synchronized void delete(List<KeyRange> keyRanges) {- hardCache.clear();- processQueue();- long rangesSize = getKeyRangesSize(keyRanges); // < 0 too many ranges- if (rangesSize < 0 || rangesSize > map.size()) {- deleteLargeKeyRanges(keyRanges);- }- else {- deleteSmallKeyRanges(keyRanges);- }- }-- /**- * Delete all objects from cache whose key is contained- * within the specified keyRanges. Iteration over all- * keys contained within keyRanges will be performed.- * @param keyRanges key ranges to delete- */- private void deleteSmallKeyRanges(List<KeyRange> keyRanges) {- for (KeyRange range : keyRanges) {- for (long key = range.minKey; key <= range.maxKey; key++) {- KeyedSoftReference<T> ref = map.remove(key);- if (ref != null) {- DatabaseObject obj = ref.get();- if (obj != null) {- obj.setDeleted();- ref.clear();- }- }- }- }- }-- /**- * Delete all objects from cache whose key is contained- * within the specified keyRanges. Iteration over all- * keys contained within map will be performed.- * @param keyRanges key ranges to delete- */- private void deleteLargeKeyRanges(List<KeyRange> keyRanges) {- map.values().removeIf(ref -> checkRef(ref, keyRanges));- }-- private boolean checkRef(KeyedSoftReference<T> ref, List<KeyRange> keyRanges) {- long key = ref.getKey();- if (keyRangesContain(keyRanges, key)) {- DatabaseObject obj = ref.get();- if (obj != null) {- obj.setDeleted();- ref.clear();- }- return true;- }- return false;-- }-- /**- * Return total number of keys covered by list of keyRanges.- * @param keyRanges key ranges to get the number of keys- * @return number of keys, or -1 if more than Long.MAX_VALUE keys- */- private long getKeyRangesSize(List<KeyRange> keyRanges) {- long size = 0;- for (KeyRange range : keyRanges) {- size += range.length();- if (size < 0) {- return -1;- }- }- return size;- }-- private boolean keyRangesContain(List<KeyRange> keyRanges, long key) {- for (KeyRange range : keyRanges) {- if (range.contains(key)) {- return true;- }- }- return false;- }-- /**- * Marks all the cached objects as invalid. Invalid objects will have to refresh themselves- * before they are allowed to be used. If an invalidated object cannot refresh itself, then- * the object is removed from the cache and discarded and the application can no longer use- * that instance of the object.- */- public synchronized void invalidate() {- hardCache.clear();- processQueue();- if (++invalidateCount <= 0) {- invalidateCount = 1;- for (KeyedSoftReference<T> ref : map.values()) {- DatabaseObject obj = ref.get();- if (obj != null) {- obj.setInvalid();- }- }- }- }-- /**- * Get the current invalidate counter value which corresponds to the number of time- * the entire cache has been invalidated.- * @return current invalidate counter value.- */- int getInvalidateCount() {- return invalidateCount;- }-- /**- * Removes the object with the given key from the cache.- * @param key the key of the object to remove.- */- public synchronized void delete(long key) {- processQueue();- KeyedSoftReference<T> ref = map.get(key);- if (ref != null) {- T obj = ref.get();- if (obj != null) {- obj.setDeleted();- ref.clear();- }- map.remove(key);- }- }-- private void addToHardCache(T obj) {- hardCache.addLast(obj);- if (hardCache.size() > hardCacheSize) {- hardCache.removeFirst();- }- }-- // we know the cast is safe--we put them in there- @SuppressWarnings("unchecked")- private void processQueue() {- KeyedSoftReference<T> ref;- while ((ref = (KeyedSoftReference<T>) refQueue.poll()) != null) {- long key = ref.getKey();- KeyedSoftReference<T> oldValue = map.remove(key);-- if (oldValue != null && oldValue != ref) {- // we have put another item in the cache with the same key. Further, we- // removed the item, but the garbage collector had not put the item on the- // reference queue until after we added a new reference to the cache.- // We want to keep the last value that was added, as it has not been deleted.- map.put(key, oldValue);- }- }- }-- private static class KeyedSoftReference<T> extends WeakReference<T> {- private long key;-- KeyedSoftReference(long key, T obj, ReferenceQueue<T> queue) {- super(obj, queue);- this.key = key;- }-- long getKey() {- return key;- }- }-- public synchronized void keyChanged(long oldKey, long newKey) {- processQueue();-- KeyedSoftReference<T> ref = map.remove(oldKey);- if (ref != null) {- map.put(newKey, ref);- T t = ref.get();- if (t != null) {- t.setInvalid();- }- }- }-}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DataTypeArchiveDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DataTypeArchiveDB.javaindex 79fc46cc49..69c86e69f3 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DataTypeArchiveDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DataTypeArchiveDB.java@@ -33,6 +33,7 @@ import ghidra.program.model.listing.Program;import ghidra.program.util.ProgramChangeRecord;import ghidra.program.util.ProgramEvent;import ghidra.util.InvalidNameException;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -496,14 +497,10 @@ public class DataTypeArchiveDB extends DomainObjectAdapterDB implements DataType@Overrideprotected void clearCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.clearCache(all);dataTypeManager.invalidateCache();}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DatabaseObject.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DatabaseObject.javadeleted file mode 100644index 9f959e950a..0000000000--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DatabaseObject.java+++ /dev/null@@ -1,231 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.program.database;--import java.util.ConcurrentModificationException;--import db.DBRecord;-import ghidra.util.Lock;--/**- * Base class for an cached object in the database. Database objects have keys. They are marked as- * invalid when a database cache is cleared and can be revived on a refresh as long as they haven't- * been deleted. Instantiating an object will cause it to be added immediately to the associated- * cache.- */-public abstract class DatabaseObject {-- protected long key;- private volatile boolean deleted;- @SuppressWarnings("rawtypes")- private final DBObjectCache cache;- private volatile int invalidateCount;- private boolean refreshing = false;-- /**- * Constructs a new DatabaseObject and adds it to the specified cache.- *- * @param cache to be used for this object or null if object will not be cached- * @param key database key to uniquely identify this object- */- @SuppressWarnings("unchecked")- protected DatabaseObject(@SuppressWarnings("rawtypes") DBObjectCache cache, long key) {- this.key = key;- this.cache = cache;- if (cache != null) {- cache.put(this);- this.invalidateCount = cache.getInvalidateCount();- }- }-- /**- * Get the database key for this object.- */- public long getKey() {- return key;- }-- /**- * Marks the object as deleted.- */- protected void setDeleted() {- deleted = true;- }-- /**- *- * Invalidate this object. This does not necessarily mean that this object can never be used- * again. If the object can refresh itself, it may still be useable.- */- public void setInvalid() {- invalidateCount = getCurrentValidationCount() - 1;- }-- private void setValid() {- invalidateCount = getCurrentValidationCount();- }-- private int getCurrentValidationCount() {- return cache != null ? cache.getInvalidateCount() : 0;- }-- protected void keyChanged(long newKey) {- long oldKey = key;- this.key = newKey;- if (cache != null) {- cache.keyChanged(oldKey, key);- }- }-- /**- * Returns true if object is currently invalid and must be validated prior to further use.- * A deleted object will be considered valid since it cannot be refreshed.- * An invalid object may result from a cache invalidation which corresponds to wide-spread- * record changes. A common situation where this can occur is an undo/redo operation- * against the underlying database. The methods {@link #checkIsValid()}, {@link #checkDeleted()},- * {@link #validate(Lock)} and {@link #isDeleted(Lock)} are methods which will force- * a re-validation if required.- *- * @return true if this object is invalid and must be re-validated, else false if object state- * is currently valid which may include a deleted state.- */- protected final boolean isInvalid() {- return !deleted && invalidateCount != getCurrentValidationCount();- }-- /**- * Checks if this object has been deleted, in which case any use of the object is not allowed.- * This method should be invoked before any modifications to the object are performed to- * ensure it still exists and is in a valid state.- *- * @throws ConcurrentModificationException if the object has been deleted from the database.- */- protected void checkDeleted() {- if (!checkIsValid()) {- throw new ConcurrentModificationException("Object has been deleted.");- }- }-- /**- * Check whether this object is still valid. If the object is invalid, the object will attempt- * to refresh itself. If the refresh fails, the object will be marked as deleted.- *- * @return true if the object is valid, else false if deleted- */- protected boolean checkIsValid() {- return checkIsValid(null);- }-- /**- * Check whether this object is still valid. If the object is invalid and not deleted, the- * object will attempt to refresh itself using the specified record. If the refresh fails,- * the object will be marked as deleted and removed from cache. If this object is already- * marked as deleted a refresh will not be perormed.- * <P>- * This method may invoke {@link #refresh(DBRecord)} to perform a refresh. It is important- * that such a refresh avoid recursing back into this method.- *- * @param record optional record which may be used to refresh invalid object- * @return true if the object is valid or false if it has been deleted.- */- protected boolean checkIsValid(DBRecord record) {- if (isInvalid()) {- if (refreshing) {- // NOTE: We need to correct such recursion cases which should be- // avoided since object is not in a valid state until refresh completed.- return !deleted;- }- refreshing = true;- try {- if (refresh(record)) {- // Object is valid- setValid();- return true;- }-- // Object has been deleted- if (cache != null) {- cache.delete(key);- }- setDeleted();- return false;- }- finally {- refreshing = false;- }- }- return !deleted;- }-- /**- * This method provides a cheap (lock free) way to test if an object is valid. If this object is- * invalid and not deleted, then the lock will be used to refresh as needed. A deleted object- * will not be refreshed.- *- * @param lock the lock that will be used if the object needs to be refreshed.- * @return true if object is valid or false if deleted- */- protected boolean validate(Lock lock) {- if (!isInvalid()) {- return !deleted;- }- lock.acquire();- try {- return checkIsValid();- }- finally {- lock.release();- }- }-- /**- * Returns true if this object has been deleted. Note: once an object has been deleted, it will- * never be "refreshed". For example, if an object is ever deleted and is resurrected via an- * "undo", you will have get a fresh instance of the object.- *- * @param lock object cache lock object- * @return true if this object has been deleted.- */- public boolean isDeleted(Lock lock) {- return deleted || !validate(lock);- }-- /**- * Tells the object to refresh its state from the database.- *- * @return true if the object was able to refresh itself. Return false if the object was- * deleted. Objects that extend this class must implement a refresh method. If an object- * can never refresh itself, then it should always return false.- */- protected abstract boolean refresh();-- /**- * Tells the object to refresh its state from the database using the specified record if not- * null. NOTE: The default implementation ignores the record and invokes refresh().- * Implementations of this method must take care if multiple database tables are used since the- * record supplied could correspond to another object. In some cases it may be best not to- * override this method or ignore the record provided.- *- * @param record valid record associated with object's key (optional, may be null to force- * record lookup or other refresh technique)- * @return true if the object was able to refresh itself. Return false if record is null and- * object was deleted. Objects that extend this class must implement a refresh method.- * If an object can never refresh itself, then it should always return false.- */- protected boolean refresh(DBRecord record) {- return refresh();- }-}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbCache.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbCache.javanew file mode 100644index 0000000000..88f5231f35--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbCache.java@@ -0,0 +1,388 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.database;++import java.util.Collections;+import java.util.List;++import db.DBRecord;+import generic.cache.WeakReferenceCache;+import ghidra.program.model.address.KeyRange;+import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;++/**+ * A DbObject cache that efficiently manages the use of the database lock and uses a+ * factory to create objects in the cache when they are not present.+ * <P>+ * This version of the cache is designed to be used without first acquiring a database lock. It will+ * first attempt to retrieve the object without acquiring the lock. If that fails, it will acquire+ * the lock, attempt to refresh the object if necessary or possibly create a new instance using+ * the factory and add it to the cache.+ *+ * @param <T> the type of the database object+ */+public class DbCache<T extends DbObject> {+ public static final DbCache<?> DUMMY = new DummyCache();+ public static final int INVALID_COUNT = -1;+ protected Lock lock;+ protected DbFactory<T> factory;+ protected WeakReferenceCache<Long, T> refCache;+ private volatile int modificationCount;++ /**+ * Constructor+ * @param factory the factory for creating new instances of database objects+ * @param lock the database lock.+ * @param hardCacheSize the size of the hard reference cache+ */+ public DbCache(DbFactory<T> factory, Lock lock, int hardCacheSize) {+ this.refCache = new WeakReferenceCache<>(hardCacheSize);+ this.factory = factory;+ this.lock = lock;+ }++ private DbCache() {+ // do nothing (For Dummy)+ }++ /**+ * Returns the number of objects currently in the cache. A database lock is not required.+ * @return the number of objects currently in the cache.+ */+ public int size() {+ return refCache.size();+ }++ /**+ * Adds the given database object to the cache.+ * @param dbObject the object to add to the cache.+ * @return the object that was cached+ */+ public T add(T dbObject) {+ dbObject.setCache(this);+ return refCache.add(dbObject.getKey(), dbObject);+ }++ /**+ * Retrieves the database object with the given key from the cache. This differs from the+ * super's get() method in that it does not require that the database lock be acquired prior+ * to calling this method and in fact performs better if the lock is not acquired. This version+ * will first look in the cache and if it finds one that is definitely valid (there is+ * a quick check that doesn't require the lock), it returns it immediately. Otherwise, the+ * cache doesn't have the object or the object needs refreshing. In this case, this method+ * will acquire the lock, attempt refresh the object if it exists, and if necessary call a+ * factory method to create a new instance and add it to the cache.+ *+ * @param key the key of the object to retrieve.+ * @return the cached object or null if the object with that key is not currently cached.+ */+ public T getCachedInstance(Long key) {+ T t = refCache.get(key);+ if (t != null && t.isValid()) {+ return t;+ }++ try (Closeable c = lock.read()) {+ if (t != null && t.refreshIfNeeded()) {+ return t;+ }+ // note that the instantiateAndCacheSafely is synchronized and it will double check the cache+ // to prevent multiple threads from getting here and then creating a dbObject.+ return instantiateAndCacheSafely(key);+ }+ }++ /**+ * Retrieves the object from the cache, but only if it already exists and is valid in the cache.+ * It will not attempt to refresh the object or call the factory method to create new instances.+ *+ * @param key the key of the object to retrieve.+ * @return the cached object or null if the object with that key is not currently cached and+ * valid.+ */+ public T getIfValid(Long key) {+ T t = refCache.get(key);+ if (t != null && t.isValid()) {+ return t;+ }+ return null;+ }++ /**+ * Retrieves the object with the given key from the cache with checking if it is valid or needs+ * to be refreshed. It will not use the factory to create new instances if they don't exist.+ * This method is used in very specialized situations where the caller has already done+ * checking and they know the status of the object and how to refresh it cheaply if needed.+ * @param key the key of the object to retrieve+ * @return the object directly from the cache if one exists in the cache.+ */+ public T getRaw(Long key) {+ return refCache.get(key);+ }++ /**+ * Retrieves the database object with the given record and associated key from the cache.+ * This form should be used in conjunction with record iterators to avoid unnecessary+ * record query during a possible object refresh. To benefit from the record the cached+ * object must implement the {@link DbObject#refresh(DBRecord)} method which by default+ * ignores the record and simply calls {@link DbObject#refresh()}.+ * <P>+ * This method is similar to the get() method in that it can be called without the database+ * lock. It will first check if the object is in the cache and definitely valid before+ * acquiring the lock and refreshing or creating the database object.+ * @param dbRecord the valid record corresponding to the object to be retrieved and possibly+ * used to refresh the associated object if found in cache+ * @return the cached object or null if the object with that key is not currently cached.+ */+ public T getCachedInstance(DBRecord dbRecord) {+ if (dbRecord == null) {+ return null;+ }++ Long key = dbRecord.getKey();+ T t = refCache.get(key);+ if (t != null && t.isValid()) {+ return t;+ }+ try (Closeable c = lock.read()) {+ if (t != null && t.refreshIfNeeded(dbRecord)) {+ return t;+ }+ // note that the instantiateAndCacheSafely is synchronized and it will double check the cache+ // to prevent multiple threads from getting here and then creating a dbObject.+ return instantiateAndCacheSafely(dbRecord);+ }+ }++ /**+ * Returns an List of all the cached objects. These objects have not been checked to see if they+ * are valid or not.+ * @return an List of all the cached objects.+ */+ public List<T> getCachedObjects() {+ return refCache.getCachedObjects();+ }++ /**+ * Delete all objects from HashMap whose key is contained+ * within the specified keyRanges.+ * @param keyRanges key ranges to delete+ */+ public void delete(List<KeyRange> keyRanges) {+ long rangesSize = getKeyRangesSize(keyRanges); // < 0 too many ranges+ if (rangesSize < 0 || rangesSize > refCache.size()) {+ deleteLargeKeyRanges(keyRanges);+ }+ else {+ deleteSmallKeyRanges(keyRanges);+ }+ }++ /**+ * Marks all the cached objects as invalid. Invalid objects will have to refresh themselves+ * before they are allowed to be used. If an invalidated object cannot refresh itself, then+ * the object is removed from the cache and discarded and the application can no longer use+ * that instance of the object.+ */+ public void invalidate() {+ // Note: the ++ operation here is not atomic, but that shouldn't be an issue. If more+ // than one thread attempts to increment the count at the same time, it is possible for+ // the count not to be incremented the full number of times, but it will be incremented+ // at least by 1, which is all that really matters.+ if (++modificationCount < 0) {+ // if it overflows, reset to 0 so it is never negative+ modificationCount = 0;+ }+ }++ /**+ * Removes the object with the given key from the cache. A database lock is not required.+ * @param key the key of the object to remove.+ */+ public void delete(long key) {+ T deleted = refCache.delete(key);+ if (deleted != null) {+ deleted.setDeleted();+ }+ }++ /**+ * Updates the cache for an object whose key has changed.+ * @param oldKey the old key+ * @param newKey the new key+ */+ public void keyChanged(long oldKey, long newKey) {+ synchronized (refCache) {+ T t = refCache.delete(oldKey);+ if (t != null) {+ t.setInvalid();+ refCache.add(newKey, t);+ }+ }+ }++ /**+ * Get the current cache modification counter value which corresponds to the number of times+ * the entire cache has been invalidated.+ * @return the current modification counter value+ */+ public int getModificationCount() {+ return modificationCount;+ }++ private synchronized T instantiateAndCacheSafely(Long key) {+ // check if another thread got here first+ T t = refCache.get(key);+ if (t != null && t.refreshIfNeeded()) {+ return t;+ }++ t = factory.instantiate(key);+ if (t != null) {+ add(t);+ }+ return t;+ }++ private synchronized T instantiateAndCacheSafely(DBRecord record) {+ // check if another thread got here first+ T t = refCache.get(record.getKey());+ if (t != null && t.refreshIfNeeded()) {+ return t;+ }+ t = factory.instantiate(record);+ add(t);+ return t;+ }++ /**+ * Delete all objects from cache whose key is contained+ * within the specified keyRanges. Iteration over all+ * keys contained within keyRanges will be performed.+ * @param keyRanges key ranges to delete+ */+ private void deleteSmallKeyRanges(List<KeyRange> keyRanges) {+ synchronized (refCache) {+ for (KeyRange range : keyRanges) {+ for (long key = range.minKey; key <= range.maxKey; key++) {+ T dbObject = refCache.delete(key);+ if (dbObject != null) {+ dbObject.setDeleted();+ }+ }+ }+ }+ }++ /**+ * Delete all objects from cache whose key is contained+ * within the specified keyRanges. Iteration over all+ * keys contained within map will be performed.+ * @param keyRanges key ranges to delete+ */+ private void deleteLargeKeyRanges(List<KeyRange> keyRanges) {+ refCache.deleteIf(v -> checkInRange(v, keyRanges));+ }++ private boolean checkInRange(T t, List<KeyRange> keyRanges) {+ long key = t.getKey();+ if (keyRangesContain(keyRanges, key)) {+ t.setDeleted();+ return true;+ }+ return false;+ }++ /**+ * Return total number of keys covered by list of keyRanges.+ * @param keyRanges key ranges to get the number of keys+ * @return number of keys, or -1 if more than Long.MAX_VALUE keys+ */+ private long getKeyRangesSize(List<KeyRange> keyRanges) {+ long size = 0;+ for (KeyRange range : keyRanges) {+ size += range.length();+ if (size < 0) {+ return -1;+ }+ }+ return size;+ }++ private boolean keyRangesContain(List<KeyRange> keyRanges, long key) {+ for (KeyRange range : keyRanges) {+ if (range.contains(key)) {+ return true;+ }+ }+ return false;+ }++ private static class DummyCache extends DbCache<DbObject> {++ @Override+ public int getModificationCount() {+ return 0;+ }++ @Override+ public DbObject getCachedInstance(Long key) {+ return null;+ }++ @Override+ public DbObject getIfValid(Long key) {+ return null;+ }++ @Override+ public DbObject getCachedInstance(DBRecord dbRecord) {+ return null;+ }++ @Override+ public int size() {+ return 0;+ }++ @Override+ public DbObject add(DbObject dbObject) {+ return dbObject;+ }++ @Override+ public void keyChanged(long oldKey, long newKey) {+ // do nothing+ }++ @Override+ public void delete(long key) {+ // do nothing+ }++ @Override+ public void delete(List<KeyRange> keyRanges) {+ // do nothing+ }++ @Override+ public List<DbObject> getCachedObjects() {+ return Collections.emptyList();+ }+ }++}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbFactory.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbFactory.javanew file mode 100644index 0000000000..7cc049818a--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbFactory.java@@ -0,0 +1,48 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.database;++import db.DBRecord;++/**+ * Interface for Factories that create {@link DbObject}s. Required by the {@link DbCache}+ * <P>+ * NOTE: This factory is used to instantiate dbObject instances that are already backed+ * by records in the database. In general, these methods should only be used by the+ * DbCache class to insure that the objects aren't already cached if they are created using+ * this factor, they are added to the cache in a thread safe way.+ *+ * @param <T> the type of database object+ */+public interface DbFactory<T extends DbObject> {++ /**+ * Creates a new DatabaseObject of type T for the given key. It is+ * expected that a record for this object already exists in the database. This method is+ * simply creating the unique instance that is associated with that record.+ * @param key the database key the database key+ * @return the newly created instance of type T or null if no record found+ */+ public T instantiate(long key);++ /**+ * Creates a new DatabaseObject of type T for the given record.+ * @param record the database record to create its associated unique instance of T+ * @return the newly created instance of type T+ */+ public T instantiate(DBRecord record);++}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbObject.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbObject.javanew file mode 100644index 0000000000..25ed8477f4--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/DbObject.java@@ -0,0 +1,286 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.database;++import java.util.ConcurrentModificationException;++import db.DBRecord;+import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;++/**+ * Base class for objects stored in the database.+ * <P>+ * The general contract for database objects is that there should only ever be one instance for+ * a specific database object at any given time. To facilitate this, instances of database objects+ * should be stored in a {@link DbCache} and the cache should be queried before creating+ * any new instances.+ * <P>+ * Database objects have keys that are used by the database record and also serves as the key for+ * cache lookup. They are marked as invalid when a database cache is invalidated and can be revived+ * on a refresh as long as they haven't been deleted.+ */+public abstract class DbObject {++ protected long key;+ private volatile boolean deleted;+ private DbCache<?> cache = DbCache.DUMMY;+ private volatile int lastValidModificationCount;+ private boolean refreshing = false;++ /**+ * Constructs a new DatabaseObject and adds it to the specified cache.+ *+ * @param key database key to uniquely identify this object+ */+ protected DbObject(long key) {+ this.key = key;+ }++ /**+ * This is a special method for setting the cache and should ONLY be used by the+ * {@link DbCache} when an object is added to the cache.+ * @param cache the cache this object was added to.+ */+ void setCache(DbCache<?> cache) {+ this.cache = cache;+ this.lastValidModificationCount = cache.getModificationCount();+ }++ /**+ * Get the database key for this object.+ * @return the database key for this object.+ */+ public long getKey() {+ return key;+ }++ /**+ * Marks the object as deleted.+ */+ protected void setDeleted() {+ deleted = true;+ }++ /**+ *+ * Invalidate this object. This does not necessarily mean that this object can never be used+ * again. If the object can refresh itself, it may still be usable.+ */+ public void setInvalid() {+ lastValidModificationCount = DbCache.INVALID_COUNT;+ }++ /**+ * Marks this object as valid. Note that this call does no checking on its own should be used+ * very carefully and only when the caller is absolutely sure that the object is valid. Used+ * in a special case for default symbols that have no corresponding record.+ */+ protected void setValid() {+ lastValidModificationCount = cache.getModificationCount();+ }++ /**+ * Method for updating the cache if a object record key changed. This is a very unusual+ * situation and should generally be avoided.+ * @param newKey the new key for the associated record for this object+ */+ protected void keyChanged(long newKey) {+ long oldKey = key;+ this.key = newKey;+ cache.keyChanged(oldKey, key);+ }++ /**+ * Returns true if the object needs to be refreshed prior to further use. An object needs+ * to be refreshed if the cache has been invalidated since the last time the object was+ * checked for validity. There is a special case if the object has been deleted. It certainly+ * doesn't need to be refreshed but clients that have the object already in hand still want to+ * be able to call its getter methods without encountering an error. The assumption is that+ * eventually the client will be kicked so it knows it needs to re-aquire its objects.+ * <P>+ * An object that has not been deleted may be marked as stale and in need of a refresh in one of+ * two ways. The cache was invalidated or the object was specifically marked as invalid via its+ * {@link #setInvalid()} method. A common situation where this can occur is an undo/redo+ * operation against the underlying database. The methods {@link #refreshIfNeeded()},+ * {@link #checkDeleted()}, {@link #validate(Lock)} and {@link #isDeleted(Lock)} are methods+ * which will force a re-validation if required.+ * <P>+ * This method is final as it is intended to be a "fast" check that doesn't require a lock+ * to see if the object is possibly in need of a refresh. We don't want any subclass to add+ * additional checks that might require a database lock. The idea is that if the cache+ * modification hasn't changed, then we know the object is valid. If the modification count+ * has changed then we may or may not be valid and stronger checks are needed which will require+ * a lock. Subclasses should override {@link #refreshIfNeeded()}, {@link #refresh()} to do+ * the more robust validation checking.+ *+ * @return true if this object is invalid and must be re-validated, else false if object state+ * is currently valid which may include a deleted state.+ */+ protected final boolean needsRefreshing() {+ if (deleted) {+ return false;+ }+ return lastValidModificationCount != cache.getModificationCount();+ }++ /**+ * Return true if the object is known to be valid at this time. Note that this is is subtly+ * different from being the inverse of {@link #needsRefreshing()}, specifically in the case+ * where the object has been deleted. For a deleted object, the {@link #isValid} method+ * will return false, but the {@link #needsRefreshing()} will also return false.+ * @return true if the object is known to be valid at this time.+ */+ public final boolean isValid() {+ if (deleted) {+ return false;+ }+ return lastValidModificationCount == cache.getModificationCount();+ }++ /**+ * Refreshes the object's state from the database if it is possibly stale. The check to see+ * if an object is stale is fast, so this generally is a quick call unless an undo or+ * redo has taken place and a true refresh is required. If a refresh is performed, the+ * object may be discovered to be deleted. This method should only be called while+ * holding a {@link Lock#readLock()}+ *+ * @return true if the object is valid, else false if deleted+ */+ protected boolean refreshIfNeeded() {+ return refreshIfNeeded(null);+ }++ /**+ * Refreshes the object's state if it is stale using the given record.+ * If the object has already been deleted, it will immediately return false. Otherwise, it+ * will check to see if it needs to be refreshed, and if so, refresh it. While refreshing the+ * object, it may be discovered that it has been deleted and marked as such.+ * @param record a known valid record the object can use to refresh itself, if a refresh is+ * needed. Null is permitted, in which case the object will have to do a database lookup to+ * retrieve a record if a refresh is needed.+ *+ * @return true if the object is valid, else false if deleted+ */+ synchronized boolean refreshIfNeeded(DBRecord record) {+ if (needsRefreshing()) {+ doRefresh(record);+ }+ return !deleted;+ }++ /**+ * Checks if this object has been deleted, in which case any use of the object is not allowed.+ * This method should be invoked before any modifications to the object are performed to+ * ensure it still exists and is in a valid state.+ *+ * @throws ConcurrentModificationException if the object has been deleted from the database.+ */+ protected void checkDeleted() {+ if (!refreshIfNeeded(null)) {+ throw new ConcurrentModificationException("Object has been deleted.");+ }+ }++ /**+ * Internal method for performing a refresh on a database object. This method may be called+ * recursively, which is can detect and short circuit.+ * @param record a known valid record the object can use to refresh itself or null. If null+ * the object will have to do its own database retrieval of its record.+ */+ private void doRefresh(DBRecord record) {+ if (refreshing) {+ // NOTE: We need to correct such recursion cases which should be+ // avoided since object is not in a valid state until refresh completed.+ return;+ }+ refreshing = true;+ try {+ if (refresh(record)) {+ // Object is valid+ setValid();+ }+ else {+ // if refresh failed, object has been deleted+ cache.delete(key);+ setDeleted();+ }+ }+ finally {+ refreshing = false;+ }+ }++ /**+ * This method provides a cheap (lock free) way to test if an object is valid. If this object is+ * invalid and not deleted, then the lock will be used to refresh as needed. A deleted object+ * will not be refreshed.+ *+ * @param lock the lock that will be used if the object needs to be refreshed.+ * @return true if object is valid or false if deleted+ */+ protected boolean validate(Lock lock) {+ if (isValid()) {+ return true;+ }+ if (deleted) {+ return false;+ }++ try (Closeable c = lock.read()) {+ return refreshIfNeeded();+ }+ }++ /**+ * Returns true if this object has been deleted. Note: once an object has been deleted, it will+ * never be "refreshed". For example, if an object is ever deleted and is resurrected via an+ * "undo", you will have get a fresh instance of the object.+ *+ * @param lock object cache lock object+ * @return true if this object has been deleted.+ */+ public boolean isDeleted(Lock lock) {+ return deleted || !validate(lock);+ }++ /**+ * Tells the object to refresh its state from the database.+ *+ * @return true if the object was able to refresh itself. Return false if the object was+ * deleted. Objects that extend this class must implement a refresh method. If an object+ * can never refresh itself, then it should always return false.+ */+ protected abstract boolean refresh();++ /**+ * Tells the object to refresh its state from the database using the specified record if not+ * null. NOTE: The default implementation ignores the record and invokes refresh().+ * Implementations of this method must take care if multiple database tables are used since the+ * record supplied could correspond to another object. In some cases it may be best not to+ * override this method or ignore the record provided.+ *+ * @param record valid record associated with object's key (optional, may be null to force+ * record lookup or other refresh technique)+ * @return true if the object was able to refresh itself. Return false if record is null and+ * object was deleted. Objects that extend this class must implement a refresh method.+ * If an object can never refresh itself, then it should always return false.+ */+ protected boolean refresh(DBRecord record) {+ return refresh();+ }++}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/IntRangeMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/IntRangeMapDB.javaindex f181c8d145..e2e3235a3e 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/IntRangeMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/IntRangeMapDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -24,6 +24,7 @@ import ghidra.program.database.util.AddressRangeMapDB;import ghidra.program.model.address.*;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.exception.DuplicateNameException;import ghidra.util.task.TaskMonitor;@@ -42,24 +43,19 @@ public class IntRangeMapDB implements IntRangeMap {public static IntRangeMapDB getPropertyMap(ProgramDB program, String mapName,ErrorHandler errHandler, AddressMap addrMap, Lock lock) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBHandle dbh = program.getDBHandle();String tableName = IntRangeMapDB.TABLE_PREFIX + mapName;if (dbh.getTable(tableName) != null) {return new IntRangeMapDB(program, mapName, program, addrMap, lock);}}- finally {- lock.release();- }return null;}public static IntRangeMapDB createPropertyMap(ProgramDB program, String mapName,ErrorHandler errHandler, AddressMap addrMap, Lock lock) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {DBHandle dbh = program.getDBHandle();String tableName = TABLE_PREFIX + mapName;if (dbh.getTable(tableName) != null) {@@ -69,9 +65,6 @@ public class IntRangeMapDB implements IntRangeMap {return new IntRangeMapDB(program, mapName, program, addrMap, lock);}- finally {- lock.release();- }}private IntRangeMapDB(ProgramDB program, String mapName, ErrorHandler errHandler,@@ -99,23 +92,16 @@ public class IntRangeMapDB implements IntRangeMap {@Overridepublic void setValue(Address start, Address end, int value) {checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {propertyMap.paintRange(start, end, new IntField(value));program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }-}@Overridepublic void setValue(AddressSetView addresses, int value) {checkDeleted();-- lock.acquire();- try {+ try (Closeable c = lock.write()) {AddressRangeIterator iter = addresses.getAddressRanges();while (iter.hasNext()) {AddressRange range = iter.next();@@ -123,42 +109,30 @@ public class IntRangeMapDB implements IntRangeMap {}program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }}@Overridepublic void clearAll() {checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {propertyMap.dispose();program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }}@Overridepublic void clearValue(Address startAddr, Address endAddr) {checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {propertyMap.clearRange(startAddr, endAddr);program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }}@Overridepublic void clearValue(AddressSetView addresses) {checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {AddressRangeIterator iter = addresses.getAddressRanges();while (iter.hasNext()) {AddressRange range = iter.next();@@ -166,49 +140,34 @@ public class IntRangeMapDB implements IntRangeMap {}program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }}@Overridepublic Integer getValue(Address address) {checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.read()) {Field value = propertyMap.getValue(address);if (value == null) {return null;}return ((IntField) value).getIntValue();}- finally {- lock.release();- }}@Overridepublic AddressSet getAddressSet() {checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.read()) {return propertyMap.getAddressSet();}- finally {- lock.release();- }}@Overridepublic AddressSet getAddressSet(int value) {checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.read()) {return propertyMap.getAddressSet(new IntField(value));}- finally {- lock.release();- }}/**@@ -216,13 +175,16 @@ public class IntRangeMapDB implements IntRangeMap {* @param fromAddr move from address* @param toAddr move to address* @param length number of address to move- * @param monitor- * @throws CancelledException+ * @param monitor the task monitor+ * @throws CancelledException if cancelled*/@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws CancelledException {- propertyMap.moveAddressRange(fromAddr, toAddr, length, monitor);+ checkDeleted();+ try (Closeable c = lock.write()) {+ propertyMap.moveAddressRange(fromAddr, toAddr, length, monitor);+ }}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramDB.javaindex 48af92780f..931b495ac5 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramDB.java@@ -59,6 +59,7 @@ import ghidra.program.model.util.AddressSetPropertyMap;import ghidra.program.model.util.PropertyMapManager;import ghidra.program.util.*;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -1160,8 +1161,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM@Overridepublic void setName(String newName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (name.equals(newName)) {return;}@@ -1172,9 +1172,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcatch (IOException e) {dbError(e);}- finally {- lock.release();- }}private void refreshName() throws IOException {@@ -1206,8 +1203,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM}ProgramOverlayAddressSpace ovSpace = null;- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (imageBaseOverride) {throw new IllegalStateException("Overlay spaces may not be created while an image-base override is active");@@ -1220,9 +1216,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcatch (IOException e) {dbError(e);}- finally {- lock.release();- }return ovSpace;}@@ -1230,8 +1223,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMpublic void renameOverlaySpace(String overlaySpaceName, String newName)throws NotFoundException, InvalidNameException, DuplicateNameException, LockException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkExclusiveAccess();AddressSpace space = addressFactory.getAddressSpace(overlaySpaceName);@@ -1253,16 +1245,12 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcatch (IOException e) {dbError(e);}- finally {- lock.release();- }}@Overridepublic boolean removeOverlaySpace(String overlaySpaceName)throws LockException, NotFoundException {- lock.acquire();try {checkExclusiveAccess();@@ -1286,9 +1274,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcatch (IOException e) {dbError(e);}- finally {- lock.release();- }return false;}@@ -1321,8 +1306,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMif (commit) {checkExclusiveAccess();}- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address currentImageBase = getImageBase();if (!(commit && imageBaseOverride) && base.equals(currentImageBase)) {return;@@ -1382,9 +1366,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM}invalidate();}- finally {- lock.release();- }//NOTE://this needs to be outside the lock...((TreeManager) managers[TREE_MGR]).imageBaseChanged(commit);@@ -1396,14 +1377,10 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMif (!imageBaseOverride) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {imageBaseOverride = false;invalidate();}- finally {- lock.release();- }//NOTE://this needs to be outside the lock...((TreeManager) managers[TREE_MGR]).imageBaseChanged(false);@@ -1856,8 +1833,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM@Overrideprotected void clearCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.clearCache(all);refreshName();overlaySpaceAdapter.updateOverlaySpaces(addressFactory);@@ -1872,9 +1848,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcatch (IOException e) {dbError(e);}- finally {- lock.release();- }}@Override@@ -1944,8 +1917,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMthrows RollbackException {// TODO: ensure that managers are notified with address ranges which correspond to a sequential set of address keys- lock.acquire();- try {+ try (Closeable c = lock.write()) {for (int i = NUM_MANAGERS - 1; i >= 0; i--) {managers[i].deleteAddressRange(startAddr, endAddr, monitor);}@@ -1967,9 +1939,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcatch (CancelledException e) {throw new RollbackException("Operation cancelled");}- finally {- lock.release();- }}/**@@ -1989,8 +1958,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM// TODO: WARNING! fromAddr range may no longer exist in memory map which could affect certain database iterators- lock.acquire();- try {+ try (Closeable c = lock.write()) {for (int i = NUM_MANAGERS - 1; i >= 0; i--) {managers[i].moveAddressRange(fromAddr, toAddr, length, monitor);}@@ -2010,9 +1978,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcatch (CancelledException e) {throw new RollbackException("Operation cancelled");}- finally {- lock.release();- }}@Override@@ -2057,8 +2022,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMcheckExclusiveAccess();- lock.acquire();- try {+ try (Closeable c = lock.write()) {setEventsEnabled(false);try {boolean redisassemblyRequired = true;@@ -2167,9 +2131,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM}fireEvent(new DomainObjectChangeRecord(ProgramEvent.LANGUAGE_CHANGED));}- finally {- lock.release();- }+}/*@@ -2281,23 +2243,18 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM@Overridepublic AddressSetPropertyMap createAddressSetPropertyMap(String mapName)throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {AddressSetPropertyMapDB map =AddressSetPropertyMapDB.createPropertyMap(this, mapName, this, addrMap, lock);addrSetPropertyMap.put(mapName, map);setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_ADDED, null, mapName);return map;}- finally {- lock.release();- }}@Overridepublic AddressSetPropertyMap getAddressSetPropertyMap(String mapName) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {AddressSetPropertyMapDB map = addrSetPropertyMap.get(mapName);if (map != null) {return map;@@ -2309,15 +2266,11 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM}return map;}- finally {- lock.release();- }}@Overridepublic void deleteAddressSetPropertyMap(String mapName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {AddressSetPropertyMapDB pm = addrSetPropertyMap.remove(mapName);if (pm == null) {pm = AddressSetPropertyMapDB.getPropertyMap(this, mapName, this, addrMap, lock);@@ -2327,29 +2280,21 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMsetChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_REMOVED, null, mapName);}}- finally {- lock.release();- }}@Overridepublic IntRangeMapDB createIntRangeMap(String mapName) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {IntRangeMapDB map = IntRangeMapDB.createPropertyMap(this, mapName, this, addrMap, lock);intRangePropertyMap.put(mapName, map);setChanged(ProgramEvent.INT_PROPERTY_MAP_ADDED, null, mapName);return map;}- finally {- lock.release();- }}@Overridepublic IntRangeMap getIntRangeMap(String mapName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {IntRangeMapDB rangeMap = intRangePropertyMap.get(mapName);if (rangeMap != null) {return rangeMap;@@ -2361,15 +2306,11 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM}return rangeMap;}- finally {- lock.release();- }}@Overridepublic void deleteIntRangeMap(String mapName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {IntRangeMapDB rangeMap = intRangePropertyMap.remove(mapName);if (rangeMap == null) {rangeMap = IntRangeMapDB.getPropertyMap(this, mapName, this, addrMap, lock);@@ -2380,10 +2321,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMsetChanged(ProgramEvent.INT_PROPERTY_MAP_REMOVED, null, mapName);}}- finally {- lock.release();- }-}@Override@@ -2518,15 +2455,11 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeMif (!(compilerSpec instanceof ProgramCompilerSpec)) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {((ProgramCompilerSpec) compilerSpec).installExtensions();getFunctionManager().invalidateCache(true);getDataTypeManager().invalidateCache();}- finally {- lock.release();- }}private void registerCompilerSpecOptions() {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDB.javaindex 61f779b54b..02bdcfd11a 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDB.java@@ -16,19 +16,19 @@package ghidra.program.database.bookmark;import db.DBRecord;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Bookmark;import ghidra.program.model.listing.BookmarkType;+import ghidra.util.Lock.Closeable;-public class BookmarkDB extends DatabaseObject implements Bookmark {+public class BookmarkDB extends DbObject implements Bookmark {private BookmarkDBManager mgr;private DBRecord record;- BookmarkDB(BookmarkDBManager mgr, DBObjectCache<BookmarkDB> cache, DBRecord record) {- super(cache, record.getKey());+ BookmarkDB(BookmarkDBManager mgr, DBRecord record) {+ super(record.getKey());this.mgr = mgr;this.record = record;}@@ -57,13 +57,10 @@ public class BookmarkDB extends DatabaseObject implements Bookmark {@Overridepublic Address getAddress() {- checkIsValid();+ validate(mgr.lock);return mgr.getAddress(record.getLongValue(BookmarkDBAdapter.ADDRESS_COL));}- /**- * Returns bookmark type or null if type has been removed.- */@Overridepublic BookmarkType getType() {return mgr.getBookmarkType((int) (key >> BookmarkDBAdapterV3.TYPE_ID_OFFSET));@@ -76,41 +73,51 @@ public class BookmarkDB extends DatabaseObject implements Bookmark {@Overridepublic String getCategory() {+ validate(mgr.lock);String category = record.getString(BookmarkDBAdapter.CATEGORY_COL);return category != null ? category : ""; // NOTE: Old data may have stored null}public void setComment(String comment) {- checkDeleted();- if (comment == null)- comment = "";-- if (!comment.equals(record.getString(BookmarkDBAdapter.COMMENT_COL))) {- record.setString(BookmarkDBAdapter.COMMENT_COL, comment);- mgr.bookmarkChanged(this);+ try (Closeable c = mgr.lock.write()) {+ checkDeleted();+ if (comment == null) {+ comment = "";+ }++ if (!comment.equals(record.getString(BookmarkDBAdapter.COMMENT_COL))) {+ record.setString(BookmarkDBAdapter.COMMENT_COL, comment);+ mgr.bookmarkChanged(this);+ }}}@Overridepublic String getComment() {+ validate(mgr.lock);String comment = record.getString(BookmarkDBAdapter.COMMENT_COL);return comment != null ? comment : ""; // NOTE: Old data may have stored null+}@Overridepublic void set(String category, String comment) {- checkDeleted();-- if (category == null)- category = "";- if (comment == null)- comment = "";- if (!comment.equals(record.getString(BookmarkDBAdapter.COMMENT_COL)) ||- !category.equals(record.getString(BookmarkDBAdapter.CATEGORY_COL))) {-- record.setString(BookmarkDBAdapter.CATEGORY_COL, category);- record.setString(BookmarkDBAdapter.COMMENT_COL, comment);- mgr.bookmarkChanged(this);+ try (Closeable c = mgr.lock.write()) {+ checkDeleted();++ if (category == null) {+ category = "";+ }+ if (comment == null) {+ comment = "";+ }+ if (!comment.equals(record.getString(BookmarkDBAdapter.COMMENT_COL)) ||+ !category.equals(record.getString(BookmarkDBAdapter.CATEGORY_COL))) {++ record.setString(BookmarkDBAdapter.CATEGORY_COL, category);+ record.setString(BookmarkDBAdapter.COMMENT_COL, comment);+ mgr.bookmarkChanged(this);+ }}}@@ -134,9 +141,10 @@ public class BookmarkDB extends DatabaseObject implements Bookmark {/*** Returns record associated with this bookmark or* null if bookmark has been deleted.+ * @return record associated with this bookmark*/DBRecord getRecord() {- return checkIsValid() ? record : null;+ return refreshIfNeeded() ? record : null;}/*@@ -147,9 +155,6 @@ public class BookmarkDB extends DatabaseObject implements Bookmark {return (int) key;}- /* (non-Javadoc)- * @see java.lang.Comparable#compareTo(java.lang.Object)- */@Overridepublic int compareTo(Bookmark otherBm) {int rc = getAddress().compareTo(otherBm.getAddress());@@ -167,4 +172,13 @@ public class BookmarkDB extends DatabaseObject implements Bookmark {return getComment().compareTo(otherBm.getComment());}+ public boolean isOwnedBy(BookmarkDBManager bookmarkDBManager) {+ return bookmarkDBManager == this.mgr;+ }++ @Override+ public void checkDeleted() {+ super.checkDeleted();+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDBManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDBManager.javaindex 248aa15594..a436c635dc 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDBManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkDBManager.java@@ -25,6 +25,7 @@ import org.apache.commons.lang3.StringUtils;import db.*;import db.util.ErrorHandler;+import generic.theme.GIcon;import generic.util.*;import ghidra.framework.data.OpenMode;import ghidra.program.database.*;@@ -36,25 +37,24 @@ import ghidra.program.model.address.*;import ghidra.program.model.listing.*;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;-import ghidra.util.datastruct.ObjectArray;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerDB {+ final static Icon DEFAULT_ICON = new GIcon("icon.plugin.bookmark.type.default");private ProgramDB program;private AddressMap addrMap;private BookmarkTypeDBAdapter bookmarkTypeAdapter;private BookmarkDBAdapter bookmarkAdapter;- private DBObjectCache<BookmarkDB> cache;+ private DbCache<BookmarkDB> cache;+ private BookmarkTypes bookmarkTypes;private boolean upgrade = false;- private Map<String, BookmarkType> typesByName = new TreeMap<>();- private Set<String> definedTypes = new HashSet<>();- private ObjectArray typesArray = new ObjectArray();- private Lock lock;+ protected Lock lock;/*** Constructs a new CodeManager for a program.@@ -75,7 +75,8 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerbookmarkTypeAdapter = BookmarkTypeDBAdapter.getAdapter(handle, openMode);int[] types = bookmarkTypeAdapter.getTypeIds();bookmarkAdapter = BookmarkDBAdapter.getAdapter(handle, openMode, types, addrMap, monitor);- cache = new DBObjectCache<>(100);+ cache = new DbCache<>(new BookmarkFactory(), lock, 100);+ bookmarkTypes = new BookmarkTypes();}@Override@@ -104,8 +105,7 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerBookmarkTypeDB type =new BookmarkTypeDB(typeId, rec.getString(BookmarkTypeDBAdapter.TYPE_NAME_COL));type.setHasBookmarks(bookmarkAdapter.hasTable(typeId));- typesByName.put(type.getTypeString(), type);- typesArray.put(typeId, type);+ bookmarkTypes.addBookmarkType(type);}}catch (IOException e) {@@ -119,9 +119,6 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager// Nothing to do}- /*- * @see ghidra.util.ErrorHandler#dbError(java.io.IOException)- */@Overridepublic void dbError(IOException e) {program.dbError(e);@@ -132,21 +129,15 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager*/@Overridepublic void invalidateCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {cache.invalidate();bookmarkAdapter.reloadTables();refreshBookmarkTypes();}- finally {- lock.release();- }}private void refreshBookmarkTypes() {- Iterator<BookmarkType> it = typesByName.values().iterator();- while (it.hasNext()) {- BookmarkTypeDB type = (BookmarkTypeDB) it.next();+ for (BookmarkTypeDB type : bookmarkTypes.getAllTypes()) {type.setHasBookmarks(bookmarkAdapter.hasTable(type.getTypeId()));}}@@ -157,7 +148,6 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager* @param bm bookmark*/void bookmarkChanged(BookmarkDB bm) {- lock.acquire();try {DBRecord rec = bm.getRecord();if (rec != null) {@@ -170,9 +160,6 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Managercatch (IOException e) {dbError(e);}- finally {- lock.release();- }}/*@@ -196,19 +183,26 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}}- /*- * Get or create bookmark type+ /**+ * Get or create a bookmark type with the given name. This method should only be called while+ * holding the program's write lock.+ * @param typeName the name of the type.+ * @param createInDatabase if true, create a record in the database for this new type.+ * Otherwise, create a bookmark type without a corresponding record.+ * @return a new or existing bookmark type with the given name.+ * @throws IOException if an I/O error occurs trying to write to the database*/- private BookmarkTypeDB getBookmarkType(String type, boolean create) throws IOException {- BookmarkTypeDB bmt = (BookmarkTypeDB) typesByName.get(type);+ private BookmarkTypeDB getBookmarkType(String typeName, boolean createInDatabase)+ throws IOException {+ BookmarkTypeDB bmt = bookmarkTypes.get(typeName);if (bmt == null) {- int typeId = findNextTypeId();- bmt = new BookmarkTypeDB(typeId, type);- typesByName.put(type, bmt);- typesArray.put(typeId, bmt);+ // bookmark types are immutable, so need to create a new one with the new type, but+ // we only want one thread at a time able to do this.+ int typeId = bookmarkTypes.getLowestUnusedId();+ bmt = new BookmarkTypeDB(typeId, typeName);+ bookmarkTypes.addBookmarkType(bmt);}- if (create && !bmt.hasBookmarks()) {-+ if (createInDatabase && !bmt.hasBookmarks()) {// Ensure that both type record and bookmarks table existsbookmarkTypeAdapter.addType(bmt.getTypeId(), bmt.getTypeString());bookmarkAdapter.addType(bmt.getTypeId());@@ -221,32 +215,16 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Managerreturn bmt;}- private int findNextTypeId() {- int n = typesArray.getLastNonEmptyIndex() + 2;- for (int i = 0; i < n; i++) {- if (typesArray.get(i) == null) {- return i;- }- }- return n;- }-/** Get existing bookmark type*/BookmarkTypeDB getBookmarkType(int typeID) {- return (BookmarkTypeDB) typesArray.get(typeID);- }-- @Override- public boolean isDefinedType(String type) {- return definedTypes.contains(type);+ return bookmarkTypes.getTypeById(typeID);}@Overridepublic BookmarkType defineType(String type, Icon icon, Color color, int priority) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {String validatedType = StringUtils.trim(type);if (StringUtils.isBlank(validatedType) || icon == null || color == null) {throw new IllegalArgumentException(@@ -259,30 +237,22 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Managerbmt.setIcon(icon);bmt.setMarkerColor(color);bmt.setMarkerPriority(priority);- definedTypes.add(type);}catch (IOException e) {dbError(e);}return bmt;}- finally {- lock.release();- }}@Overridepublic BookmarkType[] getBookmarkTypes() {- lock.acquire();- try {- Collection<BookmarkType> c = typesByName.values();- BookmarkTypeDB[] bmTypes = new BookmarkTypeDB[c.size()];- c.toArray(bmTypes);+ try (Closeable c = lock.read()) {+ Collection<BookmarkTypeDB> values = bookmarkTypes.getAllTypes();+ BookmarkType[] bmTypes = new BookmarkType[values.size()];+ values.toArray(bmTypes);return bmTypes;}- finally {- lock.release();- }}@Override@@ -291,14 +261,13 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}@Override- public BookmarkType getBookmarkType(String type) {- return typesByName.get(type);+ public BookmarkType getBookmarkType(String typeName) {+ return bookmarkTypes.get(typeName);}@Overridepublic Bookmark setBookmark(Address addr, String type, String category, String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {BookmarkTypeDB bmt = getBookmarkType(type, true);int typeId = bmt.getTypeId();BookmarkDB bm = (BookmarkDB) getBookmark(addr, type, category);@@ -308,8 +277,8 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Managerelse {DBRecord rec = bookmarkAdapter.createBookmark(typeId, category,addrMap.getKey(addr, true), comment);- bm = new BookmarkDB(this, cache, rec);-+ bm = new BookmarkDB(this, rec);+ cache.add(bm);// fire eventprogram.setObjChanged(ProgramEvent.BOOKMARK_ADDED, addr, bm, null, null);}@@ -319,25 +288,13 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerdbError(e);}- finally {- lock.release();- }return null;}- private BookmarkDB getBookmark(DBRecord bookmarkRecord) {- BookmarkDB bm = cache.get(bookmarkRecord);- if (bm == null) {- bm = new BookmarkDB(this, cache, bookmarkRecord);- }- return bm;- }-@Overridepublic Bookmark getBookmark(Address addr, String type, String category) {- lock.acquire();- try {- BookmarkTypeDB bmt = getBookmarkType(type, false);+ try (Closeable c = lock.read()) {+ BookmarkTypeDB bmt = bookmarkTypes.get(type);if (bmt != null && bmt.hasBookmarks() && category != null) {int typeId = bmt.getTypeId();RecordIterator iter =@@ -346,7 +303,7 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerDBRecord rec = iter.next();String cat = rec.getString(BookmarkDBAdapter.CATEGORY_COL);if (category.equals(cat)) {- return getBookmark(rec);+ return cache.getCachedInstance(rec);}}}@@ -354,18 +311,17 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Managercatch (IOException e) {dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic void removeBookmark(Bookmark bookmark) {- lock.acquire();- try {- if (bookmark instanceof BookmarkDB) {- BookmarkDB bm = (BookmarkDB) bookmark;+ try (Closeable c = lock.write()) {+ if (bookmark instanceof BookmarkDB bm) {+ bm.checkDeleted();+ if (!bm.isOwnedBy(this)) {+ throw new IllegalArgumentException("Bookmark is not from this program!");+ }BookmarkTypeDB type = (BookmarkTypeDB) bm.getType();int typeId = type.getTypeId();doRemoveBookmark(bm);@@ -374,14 +330,11 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}}}- finally {- lock.release();- }}private void doRemoveBookmark(BookmarkDB bm) {Address addr = bm.getAddress();- bm.setInvalid();+ cache.delete(bm.getKey());try {bookmarkAdapter.deleteRecord(bm.getId());// fire event@@ -394,13 +347,11 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}@Override- public void removeBookmarks(String type) {- lock.acquire();- try {-+ public void removeBookmarks(String typeName) {+ try (Closeable c = lock.write()) {try {- BookmarkTypeDB bmt = (BookmarkTypeDB) typesByName.get(type);- if (bmt.hasBookmarks()) {+ BookmarkTypeDB bmt = bookmarkTypes.get(typeName);+ if (bmt != null && bmt.hasBookmarks()) {int typeId = bmt.getTypeId();bookmarkAdapter.deleteType(typeId);bookmarkTypeAdapter.deleteRecord(typeId);@@ -412,16 +363,12 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerdbError(e);}}- finally {- lock.release();- }}@Overridepublic void removeBookmarks(String type, String category, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {BookmarkType bmt = getBookmarkType(type);if (bmt == null || !bmt.hasBookmarks()) {return;@@ -430,7 +377,7 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerbookmarkAdapter.getRecordsByTypeAndCategory(bmt.getTypeId(), category);while (iter.hasNext()) {DBRecord rec = iter.next();- BookmarkDB bm = getBookmark(rec);+ BookmarkDB bm = cache.getCachedInstance(rec);removeBookmark(bm);monitor.checkCancelled();}@@ -438,9 +385,6 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Managercatch (IOException e) {dbError(e);}- finally {- lock.release();- }}/**@@ -470,23 +414,17 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager@Overridepublic Bookmark[] getBookmarks(Address addr) {- lock.acquire();- try {- int n = typesArray.getLastNonEmptyIndex();+ try (Closeable c = lock.read()) {List<Bookmark> list = new ArrayList<>();- for (int i = 0; i <= n; i++) {- BookmarkTypeDB bmt = (BookmarkTypeDB) typesArray.get(i);- if (bmt != null && bmt.hasBookmarks()) {- getBookmarks(addr, i, list);+ for (BookmarkTypeDB type : bookmarkTypes.getAllTypes()) {+ if (type != null && type.hasBookmarks()) {+ getBookmarks(addr, type.getTypeId(), list);}}Bookmark[] bookmarks = new Bookmark[list.size()];list.toArray(bookmarks);return bookmarks;}- finally {- lock.release();- }}private void getBookmarks(Address addr, int typeId, List<Bookmark> list) {@@ -498,7 +436,7 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerbookmarkAdapter.getRecordsByTypeAtAddress(typeId, addrMap.getKey(addr, false));while (iter.hasNext()) {DBRecord rec = iter.next();- list.add(getBookmark(rec));+ list.add(cache.getCachedInstance(rec));}}catch (IOException e) {@@ -508,8 +446,7 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager@Overridepublic Bookmark[] getBookmarks(Address address, String type) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Bookmark[] bookmarks = null;List<Bookmark> list = new ArrayList<>();BookmarkType bmt = getBookmarkType(type);@@ -520,30 +457,22 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Managerlist.toArray(bookmarks);return bookmarks;}- finally {- lock.release();- }}@Overridepublic boolean hasBookmarks(String type) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {BookmarkType bmt = getBookmarkType(type);if (bmt == null) {return false;}return bmt.hasBookmarks();}- finally {- lock.release();- }}@Overridepublic String[] getCategories(String type) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {BookmarkType bmt = getBookmarkType(type);if (bmt == null || !bmt.hasBookmarks()) {return new String[0];@@ -556,15 +485,11 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}return null;}- finally {- lock.release();- }}@Overridepublic AddressSetView getBookmarkAddresses(String type) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {BookmarkType bmt = getBookmarkType(type);if (bmt == null || !bmt.hasBookmarks()) {return new AddressSet();@@ -577,9 +502,6 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}return null;}- finally {- lock.release();- }}private Iterator<Bookmark> getBookmarksIterator(BookmarkType bmt) {@@ -621,54 +543,25 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager@Overridepublic Bookmark getBookmark(long id) {- lock.acquire();- try {- BookmarkDB bm = cache.get(id);- if (bm == null) {- DBRecord record = bookmarkAdapter.getRecord(id);- if (record == null) {- return null;- }- bm = new BookmarkDB(this, cache, record);- }- return bm;- }- catch (ClosedException e) {- return null;- }- catch (IOException e) {- program.dbError(e);- }- finally {- lock.release();- }- return null;+ return cache.getCachedInstance(id);}@Overridepublic int getBookmarkCount() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return bookmarkAdapter.getBookmarkCount();}- finally {- lock.release();- }}@Overridepublic int getBookmarkCount(String type) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {BookmarkType bmt = getBookmarkType(type);if (bmt == null) {return 0;}return bookmarkAdapter.getBookmarkCount(bmt.getTypeId());}- finally {- lock.release();- }}@Override@@ -679,77 +572,57 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager@Overridepublic Iterator<Bookmark> getBookmarksIterator(Address startAddress, boolean forward) {-- List<PeekableIterator<Bookmark>> list = new ArrayList<>();- int n = typesArray.getLastNonEmptyIndex();- for (int i = 0; i <= n; i++) {- BookmarkTypeDB bmt = (BookmarkTypeDB) typesArray.get(i);- if (bmt != null && bmt.hasBookmarks()) {- Iterator<Bookmark> bookmarksIterator =- getBookmarksIterator(startAddress, bmt, forward);- list.add(new WrappingPeekableIterator<>(bookmarksIterator));+ try (Closeable c = lock.read()) {+ List<PeekableIterator<Bookmark>> list = new ArrayList<>();+ for (BookmarkTypeDB type : bookmarkTypes.getAllTypes()) {+ if (type != null && type.hasBookmarks()) {+ Iterator<Bookmark> bookmarksIterator =+ getBookmarksIterator(startAddress, type, forward);+ list.add(new WrappingPeekableIterator<>(bookmarksIterator));+ }}+ return new MultiIterator<>(list, forward);}-- return new MultiIterator<>(list, forward);}@Overridepublic Iterator<Bookmark> getBookmarksIterator() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return new TotalIterator();}- finally {- lock.release();- }}@Overridepublic void removeBookmarks(AddressSetView set, TaskMonitor monitor) throws CancelledException {- lock.acquire();- try {- Iterator<BookmarkType> it = typesByName.values().iterator();- while (it.hasNext()) {- BookmarkTypeDB bt = (BookmarkTypeDB) it.next();- if (bt.hasBookmarks()) {- removeBookmarks(set, bt, null, monitor);+ try (Closeable c = lock.write()) {+ for (BookmarkTypeDB type : bookmarkTypes.getAllTypes()) {+ if (type.hasBookmarks()) {+ removeBookmarks(set, type, null, monitor);}}}- finally {- lock.release();- }}@Overridepublic void removeBookmarks(AddressSetView set, String type, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {BookmarkTypeDB bmt = (BookmarkTypeDB) getBookmarkType(type);if (bmt != null && bmt.hasBookmarks()) {removeBookmarks(set, bmt, null, monitor);}}- finally {- lock.release();- }}@Overridepublic void removeBookmarks(AddressSetView set, String type, String category,TaskMonitor monitor) throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {BookmarkTypeDB bmt = (BookmarkTypeDB) getBookmarkType(type);if (bmt != null && bmt.hasBookmarks()) {removeBookmarks(set, bmt, category, monitor);}}- finally {- lock.release();- }}private void removeBookmarks(AddressSetView set, BookmarkTypeDB bmt, String category,@@ -787,14 +660,11 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {cache.invalidate();- Iterator<BookmarkType> it = typesByName.values().iterator();- while (it.hasNext()) {- BookmarkTypeDB bt = (BookmarkTypeDB) it.next();- int typeId = bt.getTypeId();- if (bt.hasBookmarks()) {+ for (BookmarkTypeDB type : bookmarkTypes.getAllTypes()) {+ if (type.hasBookmarks()) {+ int typeId = type.getTypeId();Table table = bookmarkAdapter.getTable(typeId);if (table == null) {continue;@@ -810,9 +680,6 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}}}- finally {- lock.release();- }}//==================================================================================================@@ -843,19 +710,15 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}private void findNext() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {while (nextBookmark == null && (forward ? it.hasNext() : it.hasPrevious())) {DBRecord record = forward ? it.next() : it.previous();- nextBookmark = getBookmark(record);+ nextBookmark = cache.getCachedInstance(record);}}catch (IOException ioe) {// do nothing; the nextBookmark will not be set and we will return false for hasNext()}- finally {- lock.release();- }}@Override@@ -880,9 +743,7 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, ManagerTotalIterator() {List<BookmarkTypeDB> list = new ArrayList<>();- int n = typesArray.getLastNonEmptyIndex();- for (int i = 0; i <= n; i++) {- BookmarkTypeDB bmt = (BookmarkTypeDB) typesArray.get(i);+ for (BookmarkTypeDB bmt : bookmarkTypes.getAllTypes()) {if (bmt != null && bmt.hasBookmarks()) {list.add(bmt);}@@ -916,4 +777,22 @@ public class BookmarkDBManager implements BookmarkManager, ErrorHandler, Manager}+ private class BookmarkFactory implements DbFactory<BookmarkDB> {+ @Override+ public BookmarkDB instantiate(long key) {+ try {+ DBRecord record = bookmarkAdapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public BookmarkDB instantiate(DBRecord record) {+ return new BookmarkDB(BookmarkDBManager.this, record);+ }+ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkTypeDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkTypeDB.javaindex d178fe0894..df1f24f8fa 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkTypeDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkTypeDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -83,12 +83,8 @@ public class BookmarkTypeDB implements BookmarkType {return priority;}- /*- * @see java.lang.Object#toString()- */@Overridepublic String toString() {return type;}-}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkTypes.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkTypes.javanew file mode 100644index 0000000000..53b469880a--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/bookmark/BookmarkTypes.java@@ -0,0 +1,75 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.database.bookmark;++import java.util.*;++/**+ * Class for managing bookmark type objects. This object is immutable so that multiple threads+ * can read from it. When a new type is added, a BookmarkTypes object is created within a+ * synchronized call+ */+class BookmarkTypes {+ private Map<String, BookmarkTypeDB> nameToTypeMap = new HashMap<>();+ private Map<Integer, BookmarkTypeDB> idToTypeMap = new HashMap<>();+ private List<BookmarkTypeDB> bookmarkList = new ArrayList<>();++ void addBookmarkType(BookmarkTypeDB bookmarkType) {+ bookmarkList.add(bookmarkType);+ nameToTypeMap.put(bookmarkType.getTypeString(), bookmarkType);+ idToTypeMap.put(bookmarkType.getTypeId(), bookmarkType);+ Collections.sort(bookmarkList, (t1, t2) -> Integer.compare(t1.getTypeId(), t2.getTypeId()));+ }++ /**+ * {@return all the bookmark types in a random order}+ */+ Collection<BookmarkTypeDB> getAllTypes() {+ return bookmarkList;+ }++ /**+ * Returns the bookmark type for the given type name or null if no bookmark type exists with+ * that name.+ * @param typeName the name of the bookmark type to get+ * @return the bookmark type for the given type name or null if it doesn't exist+ */+ BookmarkTypeDB get(String typeName) {+ return nameToTypeMap.get(typeName);+ }++ /**+ * Returns the bookmark type with the given id.+ * @param typeId the id to get the bookmark type for+ * @return the bookmark type with the given id+ */+ BookmarkTypeDB getTypeById(int typeId) {+ return idToTypeMap.get(typeId);+ }++ /**+ * {@return the lowest id that doesn't have a corresponding bookmark type}+ */+ int getLowestUnusedId() {+ for (int i = 0; i < bookmarkList.size(); i++) {+ if (bookmarkList.get(i).getTypeId() != i) {+ return i;+ }+ }+ return bookmarkList.size();+ }++}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.javaindex 1ac2dff24d..bd5673e49a 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.java@@ -40,6 +40,7 @@ import ghidra.program.model.util.*;import ghidra.program.util.CommentChangeRecord;import ghidra.program.util.ProgramEvent;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -57,7 +58,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {private ProgramDB program;private PrototypeManager protoMgr;- private DBObjectCache<CodeUnitDB> cache;+ private CodeUnitCache cache;private ProgramDataTypeManager dataManager;private EquateTable equateTable;private SymbolManager symbolTable;@@ -70,7 +71,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {private boolean creatingInstruction = false;private volatile boolean redisassemblyMode = false;- Lock lock;+ private Lock lock;final static int DATA_OP_INDEX = 0; // operand index for data, will always be zeroprivate static final int MAX_SEGMENT_LIMIT = 2;@@ -94,7 +95,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {this.lock = lock;initializeAdapters(openMode, monitor);- cache = new DBObjectCache<>(1000);+ cache = new CodeUnitCache(new CodeUnitFactory(), lock, 1000);protoMgr = new PrototypeManager(handle, addrMap, openMode, monitor);lengthMgr =new IntPropertyMapDB(dbHandle, openMode, this, null, addrMap, "Lengths", monitor);@@ -262,12 +263,12 @@ public class CodeManager implements ErrorHandler, ManagerDB {RecordIterator recIt = instAdapter.getRecords(firstInstrStart, true);if (recIt.hasNext()) {DBRecord rec = recIt.next();- inst = getInstructionDB(rec);+ inst = cache.getInstruction(rec);recIt.previous();}if (recIt.hasPrevious()) {DBRecord rec = recIt.previous();- Instruction prevInst = getInstructionDB(rec);+ Instruction prevInst = cache.getInstruction(rec);if (prevInst.getMaxAddress().compareTo(firstInstrStart) >= 0) {return prevInst;}@@ -277,12 +278,12 @@ public class CodeManager implements ErrorHandler, ManagerDB {recIt = dataAdapter.getRecords(firstInstrStart, true);if (recIt.hasNext()) {DBRecord rec = recIt.next();- data = getDataDB(rec);+ data = cache.getData(rec);recIt.previous();}if (recIt.hasPrevious()) {DBRecord rec = recIt.previous();- Data prevData = getDataDB(rec);+ Data prevData = cache.getData(rec);if (prevData.getMaxAddress().compareTo(firstInstrStart) >= 0) {return prevData;}@@ -406,9 +407,8 @@ public class CodeManager implements ErrorHandler, ManagerDB {*/public AddressSetView addInstructions(InstructionSet instructionSet, boolean overwrite) {AddressSet set = new AddressSet();- lock.acquire();- creatingInstruction = true;- try {+ try (Closeable c = lock.write()) {+ creatingInstruction = true;HashSet<Address> skipDelaySlots = new HashSet<>();@@ -591,7 +591,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {}finally {creatingInstruction = false;- lock.release();}return set;}@@ -617,9 +616,8 @@ public class CodeManager implements ErrorHandler, ManagerDB {MemBuffer memBuf, ProcessorContextView context, int length)throws CodeUnitInsertionException {- lock.acquire();- creatingInstruction = true;- try {+ try (Closeable c = lock.write()) {+ creatingInstruction = true;int forcedLengthOverride = InstructionDB.checkLengthOverride(length, prototype);if (length == 0) {length = prototype.getLength();@@ -645,7 +643,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {}finally {creatingInstruction = false;- lock.release();}return null;}@@ -677,7 +674,8 @@ public class CodeManager implements ErrorHandler, ManagerDB {cache.delete(addrMap.getKeyRanges(address, endAddr, false));// create new InstructionDB object and add to the cache (conflicts assumed to have been removed)- InstructionDB inst = new InstructionDB(this, cache, address, addr, prototype, flags);+ InstructionDB inst = new InstructionDB(this, address, addr, prototype, flags);+ cache.add(inst);addReferencesForInstruction(inst);return inst;@@ -755,9 +753,8 @@ public class CodeManager implements ErrorHandler, ManagerDB {*/private void deleteAddressRange(Address start, Address end, boolean keepComments,TaskMonitor monitor) throws CancelledException {- lock.acquire();boolean success = false;- try {+ try (Closeable c = lock.write()) {monitor.checkCancelled();instAdapter.deleteRecords(start, end);monitor.checkCancelled();@@ -782,7 +779,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {if (!success) {cache.invalidate();}- lock.release();}}@@ -801,27 +797,21 @@ public class CodeManager implements ErrorHandler, ManagerDB {@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address start = fromAddr;Address newStart = toAddr;Address newEnd = newStart.add(length - 1);monitor.setMessage("Moving code...");- try {- moveDefinedCodeUnits(start, newStart, length, monitor);- invalidateCache(true);- addMovedInstructionReferences(newStart, newEnd, monitor);- addMovedDataReferences(newStart, newEnd, monitor);- }- catch (IOException e) {- program.dbError(e);- }+ moveDefinedCodeUnits(start, newStart, length, monitor);+ invalidateCache(true);+ addMovedInstructionReferences(newStart, newEnd, monitor);+ addMovedDataReferences(newStart, newEnd, monitor);invalidateCache(true);}- finally {- lock.release();+ catch (IOException e) {+ program.dbError(e);}}@@ -834,58 +824,15 @@ public class CodeManager implements ErrorHandler, ManagerDB {*/public CodeUnit getCodeUnitAt(Address address) {long addr = addrMap.getKey(address, false);- // FIXME Trying to get Data to display for External.- if (address.isExternalAddress()) {- Symbol externalSymbol = program.getSymbolTable().getPrimarySymbol(address);- if (externalSymbol == null) {- return getUndefinedDataDB(address, addr);- }- ExternalLocation externalLocation =- program.getExternalManager().getExternalLocation(externalSymbol);- DataType dataType = externalLocation.getDataType();- if (dataType == null || dataType == DataType.DEFAULT) {- // For now dummy back an undefined.- return getUndefinedDataDB(address, addr);- }- // Dummy back a Data for the data type.- DataDB dataDB = new DataDB(this, null, addr, address, addr, dataType);- return dataDB;- }return getCodeUnitAt(addr);}CodeUnit getCodeUnitAt(long addr) {- if (addr == AddressMap.INVALID_ADDRESS_KEY) {- return null;- }- lock.acquire();- try {- CodeUnitDB cu = cache.get(addr);- if (cu != null) {- return cu;- }-- try {- InstructionDB inst = getInstructionDB(addr);- if (inst != null) {- return inst;- }-- DataDB data = getDataDB(addr);- if (data != null) {- return data;- }- return getUndefinedAt(addrMap.decodeAddress(addr), addr);- }- catch (IOException e) {- program.dbError(e);- }- return null;-- }- finally {- lock.release();+ CodeUnitDB cu = cache.getCachedInstance(addr);+ if (cu != null) {+ return cu;}+ return null;}/**@@ -897,8 +844,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {* exist*/public CodeUnit getCodeUnitAfter(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {CodeUnit cu = getCodeUnitContaining(addr);if (cu != null) {addr = cu.getMaxAddress();@@ -913,9 +859,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {return getCodeUnitAt(addr);}}- finally {- lock.release();- }return null;}@@ -956,17 +899,17 @@ public class CodeManager implements ErrorHandler, ManagerDB {return null;}if (dataRec == null) {- return getInstructionDB(instRec);+ return cache.getInstruction(instRec);}if (instRec == null) {- return getDataDB(dataRec);+ return cache.getData(dataRec);}Address dataAddr = addrMap.decodeAddress(dataRec.getKey());Address instAddr = addrMap.decodeAddress(instRec.getKey());if (dataAddr.compareTo(instAddr) > 0) {- return getDataDB(dataRec);+ return cache.getData(dataRec);}- return getInstructionDB(instRec);+ return cache.getInstruction(instRec);}/**@@ -978,8 +921,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {* exist*/public CodeUnit getCodeUnitBefore(Address address) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {AddressIterator it = program.getMemory().getAddresses(address, false);Address addr = null;if (it.hasNext()) {@@ -995,15 +937,12 @@ public class CodeManager implements ErrorHandler, ManagerDB {if (cu != null && cu.contains(addr)) {return cu;}- return getUndefinedDataDB(addr, addrMap.getKey(addr, false));+ return cache.getCachedInstance(addrMap.getKey(addr, false));+// return getUndefinedDataDB(addr, addrMap.getKey(addr, false));}catch (IOException e) {dbError(e);}- finally {- lock.release();- }-return null;}@@ -1019,71 +958,45 @@ public class CodeManager implements ErrorHandler, ManagerDB {* not exist.*/public CodeUnit getCodeUnitContaining(Address address) {- lock.acquire();- try {- CodeUnit cu = getCodeUnitAt(address);- if (cu != null) {- return cu;- }- try {- DBRecord dataRec = dataAdapter.getRecordBefore(address);- DBRecord instRec = instAdapter.getRecordBefore(address);-- CodeUnit cuFirst = null, cuSecond = null;+ long addr = addrMap.getKey(address, false);+ CodeUnit cu = cache.getCachedInstance(addr);+ if (cu != null) {+ return cu;+ }+ try (Closeable c = lock.read()) {+ DBRecord dataRec = dataAdapter.getRecordBefore(address);+ DBRecord instRec = instAdapter.getRecordBefore(address);- if (instRec != null) {- cuFirst = getInstructionDB(instRec);- }- if (dataRec != null) {- cuSecond = getDataDB(dataRec);- }- // if dataRec is > instrRec, swap order of checking- if (dataRec != null && instRec != null) {- Address dataAddr = addrMap.decodeAddress(dataRec.getKey());- Address instAddr = addrMap.decodeAddress(instRec.getKey());- if (dataAddr.compareTo(instAddr) > 0) {- CodeUnit tmp = cuFirst;- cuFirst = cuSecond;- cuSecond = tmp;- }- }- if (cuFirst != null && cuFirst.contains(address)) {- return cuFirst;- }- if (cuSecond != null && cuSecond.contains(address)) {- return cuSecond;- }+ CodeUnit cuFirst = null, cuSecond = null;- if (program.getMemory().contains(address)) {- return getUndefinedAt(address);- }- // FIXME Trying to get Data to display for External.- if (address.isExternalAddress()) {- long addr = addrMap.getKey(address, false);- Symbol externalSymbol = program.getSymbolTable().getPrimarySymbol(address);- if (externalSymbol == null) {- return getUndefinedDataDB(address, addr);- }- ExternalLocation externalLocation =- program.getExternalManager().getExternalLocation(externalSymbol);- DataType dataType = externalLocation.getDataType();- if (dataType == null || dataType == DataType.DEFAULT) {- // For now dummy back an undefined.- return getUndefinedDataDB(address, addr);- }- // Dummy back a Data for the data type.- DataDB dataDB = new DataDB(this, null, addr, address, addr, dataType);- return dataDB;+ if (instRec != null) {+ cuFirst = cache.getInstruction(instRec);+ }+ if (dataRec != null) {+ cuSecond = cache.getData(dataRec);+ }+ // if dataRec is > instrRec, swap order of checking+ if (dataRec != null && instRec != null) {+ Address dataAddr = addrMap.decodeAddress(dataRec.getKey());+ Address instAddr = addrMap.decodeAddress(instRec.getKey());+ if (dataAddr.compareTo(instAddr) > 0) {+ CodeUnit tmp = cuFirst;+ cuFirst = cuSecond;+ cuSecond = tmp;}}- catch (IOException e) {- dbError(e);+ if (cuFirst != null && cuFirst.contains(address)) {+ return cuFirst;+ }+ if (cuSecond != null && cuSecond.contains(address)) {+ return cuSecond;}- return null;+ return instantiateUndefinedOrExternalData(address, addr);}- finally {- lock.release();+ catch (IOException e) {+ dbError(e);+ return null;}}@@ -1316,25 +1229,18 @@ public class CodeManager implements ErrorHandler, ManagerDB {}InstructionDB getInstructionAt(long addr) {- if (addr == AddressMap.INVALID_ADDRESS_KEY) {- return null;+ CodeUnitDB cu = cache.getIfValid(addr);+ if (cu != null) {+ return (cu instanceof InstructionDB instruction) ? instruction : null;}- lock.acquire();- try {- CodeUnitDB cu = cache.get(addr);- if (cu == null) {- return getInstructionDB(addr);- }- else if (cu instanceof InstructionDB) {- return (InstructionDB) cu;- }++ try (Closeable c = lock.read()) {+ DBRecord rec = instAdapter.getRecord(addr);+ return cache.getInstruction(rec);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1350,28 +1256,21 @@ public class CodeManager implements ErrorHandler, ManagerDB {}Data getDefinedDataAt(long addr) {- if (addr == AddressMap.INVALID_ADDRESS_KEY) {+ CodeUnitDB cu = cache.getIfValid(addr);+ if (cu != null) {+ if ((cu instanceof Data data) && data.isDefined()) {+ return data;+ }return null;}- lock.acquire();- try {- CodeUnit cu = cache.get(addr);- if (cu == null) {- DBRecord rec = dataAdapter.getRecord(addr);- return getDataDB(rec);- }- else if (cu instanceof Data) {- if (((Data) cu).isDefined()) {- return (DataDB) cu;- }- }++ try (Closeable c = lock.read()) {+ DBRecord rec = dataAdapter.getRecord(addr);+ return cache.getData(rec);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1384,17 +1283,13 @@ public class CodeManager implements ErrorHandler, ManagerDB {* does not exist*/public Instruction getInstructionBefore(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBRecord rec = instAdapter.getRecordBefore(addr);- return getInstructionDB(rec);+ return cache.getInstruction(rec);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1406,17 +1301,13 @@ public class CodeManager implements ErrorHandler, ManagerDB {* does not exist*/public Instruction getInstructionAfter(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBRecord rec = instAdapter.getRecordAfter(addr);- return getInstructionDB(rec);+ return cache.getInstruction(rec);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1444,8 +1335,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {* instruction does not exist*/public Instruction getInstructionContaining(Address address, boolean usePrototypeLength) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Instruction instr = getInstructionAt(address);if (instr != null) {return instr;@@ -1466,9 +1356,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {}return null;}- finally {- lock.release();- }}/**@@ -1482,22 +1369,9 @@ public class CodeManager implements ErrorHandler, ManagerDB {}Data getDataAt(Address address, long addr) {- if (addr == AddressMap.INVALID_ADDRESS_KEY) {- return getUndefinedDataDB(address, addr);- }- lock.acquire();- try {- DataDB data = getDataDB(addr);- if (data != null) {- return data;- }- return getUndefinedAt(address, addr);- }- catch (IOException e) {- program.dbError(e);- }- finally {- lock.release();+ CodeUnit cu = cache.getCachedInstance(addr);+ if (cu instanceof DataDB data) {+ return data;}return null;}@@ -1571,17 +1445,13 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @return the defined data after the specified address, null if a defined data does not exist*/public Data getDefinedDataAfter(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBRecord rec = dataAdapter.getRecordAfter(addr);- return getDataDB(rec);+ return cache.getData(rec);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1594,17 +1464,13 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @return the defined data before the specified address, null if a defined data does not exist*/public Data getDefinedDataBefore(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBRecord rec = dataAdapter.getRecordBefore(addr);- return getDataDB(rec);+ return cache.getData(rec);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1619,8 +1485,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @return the defined data containing the address, null if a defined data does not exist*/public Data getDefinedDataContaining(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Data data = getDefinedDataAt(addr);if (data != null) {return data;@@ -1634,15 +1499,11 @@ public class CodeManager implements ErrorHandler, ManagerDB {}return null;}- finally {- lock.release();- }}public AddressSetView getUndefinedRanges(AddressSetView set, boolean initializedMemoryOnly,TaskMonitor monitor) throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.read()) {monitor = TaskMonitor.dummyIfNull(monitor);Memory mem = program.getMemory();@@ -1731,7 +1592,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {DBRecord nextDataRec = dataIter.next();nextDataAddr = addrMap.decodeAddress(nextDataRec.getKey());nextDataEndAddr = nextDataAddr;- DataDB data = getDataDB(nextDataRec);+ DataDB data = cache.getData(nextDataRec);int len = data.getLength();if (len > 1) {try {@@ -1786,9 +1647,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return null; // will not happen}@@ -1803,44 +1661,9 @@ public class CodeManager implements ErrorHandler, ManagerDB {}Data getUndefinedAt(Address address, long addr) {- if (addr != AddressMap.INVALID_ADDRESS_KEY) {- lock.acquire();- try {- Instruction inst = getInstructionContaining(address, false);- if (inst != null) {- return null;- }-- Data data = getDefinedDataContaining(address);- if (data != null) {- return null;- }-- if (program.getMemory().contains(address)) {- return getUndefinedDataDB(address, addr);- }- }- finally {- lock.release();- }- }-- // FIXME Trying to get Data to display for External.- if (address.isExternalAddress()) {- Symbol externalSymbol = program.getSymbolTable().getPrimarySymbol(address);- if (externalSymbol == null) {- return getUndefinedDataDB(address, addr);- }- ExternalLocation externalLocation =- program.getExternalManager().getExternalLocation(externalSymbol);- DataType dataType = externalLocation.getDataType();- if (dataType == null || dataType == DataType.DEFAULT) {- // For now dummy back an undefined.- return getUndefinedDataDB(address, addr);- }- // Dummy back a Data for the data type.- DataDB dataDB = new DataDB(this, null, addr, address, addr, dataType);- return dataDB;+ CodeUnitDB cu = cache.getCachedInstance(addr);+ if (cu instanceof Data data && !data.isDefined()) {+ return data;}return null;}@@ -1948,7 +1771,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {RecordIterator recIt = instAdapter.getRecords(startAddr, true);if (recIt.hasNext()) {DBRecord rec = recIt.next();- Instruction inst = getInstructionDB(rec);+ Instruction inst = cache.getInstruction(rec);if (inst.getMinAddress().compareTo(endAddr) <= 0) {throw new CodeUnitInsertionException("Conflicting instruction exists at address " +inst.getMinAddress() + " to " + inst.getMaxAddress());@@ -1957,7 +1780,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {}if (recIt.hasPrevious()) {DBRecord rec = recIt.previous();- Instruction inst = getInstructionDB(rec);+ Instruction inst = cache.getInstruction(rec);if (inst.getMaxAddress().compareTo(startAddr) >= 0) {throw new CodeUnitInsertionException("Conflicting instruction exists at address " +inst.getMinAddress() + " to " + inst.getMaxAddress());@@ -1967,7 +1790,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {recIt = dataAdapter.getRecords(startAddr, true);if (recIt.hasNext()) {DBRecord rec = recIt.next();- Data data = getDataDB(rec);+ Data data = cache.getData(rec);if (data.getMinAddress().compareTo(endAddr) <= 0) {throw new CodeUnitInsertionException("Conflicting data exists at address " +data.getMinAddress() + " to " + data.getMaxAddress());@@ -1976,7 +1799,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {}if (recIt.hasPrevious()) {DBRecord rec = recIt.previous();- Data data = getDataDB(rec);+ Data data = cache.getData(rec);if (data.getMaxAddress().compareTo(startAddr) >= 0) {throw new CodeUnitInsertionException("Conflicting data exists at address " +data.getMinAddress() + " to " + data.getMaxAddress());@@ -1996,10 +1819,8 @@ public class CodeManager implements ErrorHandler, ManagerDB {public Data createCodeUnit(Address addr, DataType dataType, int length)throws CodeUnitInsertionException {- lock.acquire();-DataDB data = null;- try {+ try (Closeable c = lock.write()) {if (dataType instanceof BitFieldDataType) {throw new CodeUnitInsertionException("Bitfields not supported for Data");@@ -2050,7 +1871,11 @@ public class CodeManager implements ErrorHandler, ManagerDB {checkValidAddressRange(addr, endAddr);if (dataType == DataType.DEFAULT) {- return getUndefinedDataDB(addr, addrMap.getKey(addr, false));+ long key = addrMap.getKey(addr, false);+ DataDB dataDb =+ new DataDB(CodeManager.this, key, addr, key, DefaultDataType.dataType);+ cache.add(dataDb);+ return dataDb;}DBRecord record = dataAdapter.createData(addr, dataManager.getResolvedID(dataType));@@ -2064,7 +1889,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {}cache.delete(addrMap.getKeyRanges(addr, endAddr, false));- data = getDataDB(record);+ data = cache.getData(record);baseDt = data.getBaseDataType();// fire event@@ -2079,9 +1904,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return data;}@@ -2092,14 +1914,10 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @param data the data object to be updated*/public void updateDataReferences(Data data) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {refManager.removeAllReferencesFrom(data.getMinAddress(), data.getMinAddress());addDataReferences(data, new ArrayList<Address>());}- finally {- lock.release();- }}/**@@ -2218,8 +2036,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {*/public void clearComments(Address start, Address end) {AddressRange.checkValidRange(start, end);- lock.acquire();- try {+ try (Closeable c = lock.write()) {try {addCommentHistoryRecords(start, end);}@@ -2239,9 +2056,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {program.dbError(e);}}- finally {- lock.release();- }}/**@@ -2322,8 +2136,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {public void clearCodeUnits(Address start, Address end, boolean clearContext,TaskMonitor monitor) throws CancelledException {AddressRange.checkValidRange(start, end);- lock.acquire();- try {+ try (Closeable c = lock.write()) {// Expand range to include any overlapping or delay-slotted instructionsCodeUnit cu = getCodeUnitContaining(start);if (cu != null) {@@ -2350,9 +2163,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {program.setChanged(ProgramEvent.CODE_REMOVED, start, end, cu, null);}- finally {- lock.release();- }}/**@@ -2464,7 +2274,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {public InstructionIterator getInstructions(Address address, boolean forward) {try {RecordIterator recIt = instAdapter.getRecords(address, forward);- return new InstructionRecordIterator(this, recIt, forward);+ return new InstructionRecordIterator(cache, recIt, forward);}catch (IOException e) {program.dbError(e);@@ -2484,7 +2294,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {public DataIterator getDefinedData(Address address, boolean forward) {try {RecordIterator recIt = dataAdapter.getRecords(address, forward);- return new DataRecordIterator(this, recIt, forward);+ return new DataRecordIterator(cache, recIt, forward);}catch (IOException e) {program.dbError(e);@@ -2504,7 +2314,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {public InstructionIterator getInstructions(AddressSetView set, boolean forward) {try {RecordIterator recIt = instAdapter.getRecords(set, forward);- return new InstructionRecordIterator(this, recIt, forward);+ return new InstructionRecordIterator(cache, recIt, forward);}catch (IOException e) {program.dbError(e);@@ -2550,7 +2360,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {*/public DataIterator getDefinedData(AddressSetView set, boolean forward) {try {- return new DataRecordIterator(this, dataAdapter.getRecords(set, forward), forward);+ return new DataRecordIterator(cache, dataAdapter.getRecords(set, forward), forward);}catch (IOException e) {program.dbError(e);@@ -2569,8 +2379,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {*/public void checkContextWrite(Address start, Address end) throws ContextChangeException {AddressRange.checkValidRange(start, end);- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (!contextLockingEnabled || creatingInstruction ||!program.getMemory().contains(start, end)) {return;@@ -2594,9 +2403,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {"Context register change conflicts with one or more instructions");}}- finally {- lock.release();- }}/**@@ -2667,8 +2473,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @throws CancelledException if cancelled*/public void clearData(Set<Long> dataTypeIDs, TaskMonitor monitor) throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {List<Address> toClear = new ArrayList<>();RecordIterator it = dataAdapter.getRecords();while (it.hasNext()) {@@ -2688,9 +2493,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {catch (IOException e) {dbError(e);}- finally {- lock.release();- }}Program getProgram() {@@ -2709,64 +2511,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {return propertyMapMgr;}- /**- * Get the InstructionDB object from the cache; if it is not in the cache, create a new DB- * object and add it.- * @param rec record for the instruction- * @return the instruction- */- InstructionDB getInstructionDB(DBRecord rec) {- lock.acquire();- try {- if (rec != null) {- InstructionDB inst = (InstructionDB) cache.get(rec);- if (inst != null) {- return inst;- }- long addr = rec.getKey();- Address address = addrMap.decodeAddress(addr);- int protoID = rec.getIntValue(InstDBAdapter.PROTO_ID_COL);- byte flags = rec.getByteValue(InstDBAdapter.FLAGS_COL);- InstructionPrototype proto = protoMgr.getPrototype(protoID);- inst = new InstructionDB(this, cache, address, addr, proto, flags);- return inst;- }- return null;- }- finally {- lock.release();- }- }-- /**- * Get the DataDB object from the cache; if it is not in the cache, create a new DB object and- * add it.- * @param rec data record- * @return the data- */- DataDB getDataDB(DBRecord rec) {- lock.acquire();- try {- if (rec != null) {- DataDB data = (DataDB) cache.get(rec);- if (data != null) {- return data;- }- long addr = rec.getKey();- Address address = addrMap.decodeAddress(addr);- long datatypeID = rec.getLongValue(DataDBAdapter.DATA_TYPE_ID_COL);- DataType dt = dataManager.getDataType(datatypeID);- data = new DataDB(this, cache, addr, address, addr, dt);- return data;- }- return null;-- }- finally {- lock.release();- }- }-DataDBAdapter getDataAdapter() {return dataAdapter;}@@ -2776,8 +2520,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {}Address getDefinedAddressAfter(Address address) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBRecord dataRec = null;DBRecord instRec = null;try {@@ -2804,9 +2547,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {}return instAddr;}- finally {- lock.release();- }}/**@@ -2815,8 +2555,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {private void moveDefinedCodeUnits(Address startAddr, Address newStartAddr, long length,TaskMonitor monitor) throws IOException, CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address endAddr = startAddr.add(length - 1);monitor.checkCancelled();@@ -2828,9 +2567,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {dataAdapter.moveAddressRange(startAddr, newStartAddr, length, monitor);commentAdapter.moveAddressRange(startAddr, newStartAddr, length, monitor);}- finally {- lock.release();- }}/**@@ -2842,7 +2578,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {RecordIterator iter = instAdapter.getRecords(start, end, true);while (iter.hasNext()) {monitor.checkCancelled();- InstructionDB inst = getInstructionDB(iter.next());+ InstructionDB inst = cache.getInstruction(iter.next());addReferencesForInstruction(inst);}}@@ -2857,7 +2593,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {while (iter.hasNext()) {monitor.checkCancelled();DBRecord rec = iter.next();- Data data = getDataDB(rec);+ Data data = cache.getData(rec);addDataReferences(data, new ArrayList<Address>());}}@@ -3065,21 +2801,12 @@ public class CodeManager implements ErrorHandler, ManagerDB {}int getLength(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return lengthMgr.getInt(addr);}catch (NoValueException e) {return -1;}- finally {- lock.release();- }- }-- private InstructionDB getInstructionDB(long addr) throws IOException {- DBRecord rec = instAdapter.getRecord(addr);- return getInstructionDB(rec);}protected DBRecord getInstructionRecord(long addr) {@@ -3111,74 +2838,33 @@ public class CodeManager implements ErrorHandler, ManagerDB {return null;}- private DataDB getDataDB(long addr) throws IOException {- return getDataDB(dataAdapter.getRecord(addr));- }-- private DataDB getUndefinedDataDB(Address address, long addr) {- if (addr == AddressMap.INVALID_ADDRESS_KEY) {-// TODO: for now we will assume that all keys within defined memory blocks are known.-// When a memory block is created, only its start address key is generated, if the-// block spans a 32-bit boundary, null may be returned for all addresses beyond that-// boundary. A recent fix was added to the memory map to ensure that we can-// handle blocks which are at least 32-bits in size by ensuring that the end address-// key is also generated.- return null;- }- lock.acquire();- try {- CodeUnit cu = cache.get(addr);- if (cu == null) {- if (address instanceof SegmentedAddress) {- address = normalize((SegmentedAddress) address, program.getMemory());- }- DataDB data =- new DataDB(this, cache, addr, address, addr, DefaultDataType.dataType);- return data;- }- else if (cu instanceof Data) {- if (!((Data) cu).isDefined()) {- return (DataDB) cu;- }- }- return null;- }- finally {- lock.release();- }- }-- private Address normalize(SegmentedAddress addr, Memory memory) {- if (memory == null) {- return addr;- }- MemoryBlock block = memory.getBlock(addr);- if (block == null) {- return addr;- }- SegmentedAddress start = (SegmentedAddress) block.getStart();- return addr.normalize(start.getSegment());-- }+// private Address normalize(SegmentedAddress addr, Memory memory) {+// if (memory == null) {+// return addr;+// }+// MemoryBlock block = memory.getBlock(addr);+// if (block == null) {+// return addr;+// }+// SegmentedAddress start = (SegmentedAddress) block.getStart();+// return addr.normalize(start.getSegment());+//+// }@Overridepublic void invalidateCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {cache.invalidate();lengthMgr.invalidate();protoMgr.clearCache();}- finally {- lock.release();- }}/*** Invalidates the cache for the code units.*/public void invalidateCodeUnitCache() {- cache.invalidate();+ lock.withWrite(() -> cache.invalidate());}/**@@ -3187,13 +2873,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @param end end address of change*/public void memoryChanged(Address addr, Address end) {- lock.acquire();- try {- cache.invalidate();- }- finally {- lock.release();- }+ lock.withWrite(() -> cache.invalidate());}/**@@ -3206,8 +2886,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {newFallThroughRef.getReferenceType() != RefType.FALL_THROUGH) {throw new IllegalArgumentException("invalid reftype");}- lock.acquire();- try {+ try (Closeable c = lock.write()) {InstructionDB instr = getInstructionAt(addrMap.getKey(fromAddr, false));if (instr == null) {// Do not allow fallthrough ref without instruction@@ -3218,9 +2897,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {}instr.fallThroughChanged(newFallThroughRef);}- finally {- lock.release();- }}void setFlags(long addr, byte flags) {@@ -3289,13 +2965,12 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @throws IllegalArgumentException if type is not one of the types of comments supported*/public void setComment(Address address, CommentType commentType, String comment) {- CodeUnit cu = getCodeUnitAt(address);- if (cu != null) {- cu.setComment(commentType, comment);- return;- }- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ CodeUnit cu = getCodeUnitAt(address);+ if (cu != null) {+ cu.setComment(commentType, comment);+ return;+ }long addr = addrMap.getKey(address, true);DBRecord commentRec = getCommentAdapter().getRecord(addr);@@ -3323,9 +2998,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {catch (IOException e) {dbError(e);}- finally {- lock.release();- }}void sendNotification(Address address, CommentType commentType, String oldValue,@@ -3366,9 +3038,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {* @return zero length array if no history exists*/public CommentHistory[] getCommentHistory(Address addr, CommentType commentType) {- lock.acquire();- try {-+ try (Closeable c = lock.read()) {// records are sorted by date ascendingList<DBRecord> allRecords = getHistoryRecords(addr, commentType);@@ -3403,9 +3073,6 @@ public class CodeManager implements ErrorHandler, ManagerDB {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return new CommentHistory[0];}@@ -3446,8 +3113,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {}public void replaceDataTypes(Map<Long, Long> dataTypeReplacementMap) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {RecordIterator it = dataAdapter.getRecords();while (it.hasNext()) {DBRecord rec = it.next();@@ -3460,13 +3126,11 @@ public class CodeManager implements ErrorHandler, ManagerDB {program.setChanged(ProgramEvent.CODE_REPLACED, addr, addr, null, null);}}+ cache.invalidate();}catch (IOException e) {- dbError(e);- }- finally {cache.invalidate();- lock.release();+ dbError(e);}}@@ -3620,4 +3284,107 @@ public class CodeManager implements ErrorHandler, ManagerDB {return protoMgr.getPrototype(protoID);}+ private DataDB instantiateUndefinedOrExternalData(Address address, long addr) {+ if (address.isExternalAddress()) {+ return instantiateDataForExternalAddress(address, addr);+ }++ // first we have to make sure no instruction or data contains the address+ Instruction inst = getInstructionContaining(address, false);+ if (inst != null) {+ return null;+ }++ Data data = getDefinedDataContaining(address);+ if (data != null) {+ return null;+ }++ // also need to make sure the address is in memory to return a Data object+ if (program.getMemory().contains(address)) {+ DataDB dataDb =+ new DataDB(CodeManager.this, addr, address, addr, DefaultDataType.dataType);+ cache.add(dataDb);+ return dataDb;+ }+ return null;+ }++ Data getDataDB(DBRecord record) {+ return cache.getData(record);+ }++ public Instruction getInstructionDB(DBRecord record) {+ return cache.getInstruction(record);+ }++ private DataDB instantiateDataForExternalAddress(Address address, long addr) {+ Symbol externalSymbol = program.getSymbolTable().getPrimarySymbol(address);+ if (externalSymbol == null || externalSymbol.getSymbolType() != SymbolType.LABEL) {+ return null;+ }++ ExternalLocation externalLocation =+ program.getExternalManager().getExternalLocation(externalSymbol);++ DataType dataType = externalLocation.getDataType();+ dataType = dataType == null ? DataType.DEFAULT : dataType;+ DataDB dataDb = new DataDB(CodeManager.this, addr, address, addr, DefaultDataType.dataType);+ cache.add(dataDb);+ return dataDb;+ }++ public class CodeUnitFactory implements DbFactory<CodeUnitDB> {+ @Override+ public CodeUnitDB instantiate(long key) {+ try {+ if (key == AddressMap.INVALID_ADDRESS_KEY) {+ return null;+ }+ DBRecord record = instAdapter.getRecord(key);+ if (record != null) {+ return instantiateInstruction(record);+ }+ record = dataAdapter.getRecord(key);+ if (record != null) {+ return instantiateData(record);+ }+ return instantiateUndefinedOrExternalData(addrMap.decodeAddress(key), key);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public CodeUnitDB instantiate(DBRecord dbRecord) {+ if (dbRecord.hasSameSchema(DataDBAdapter.DATA_SCHEMA)) {+ return instantiateData(dbRecord);+ }+ return instantiateInstruction(dbRecord);+ }++ private InstructionDB instantiateInstruction(DBRecord rec) {+ long addr = rec.getKey();+ Address address = addrMap.decodeAddress(addr);+ int protoID = rec.getIntValue(InstDBAdapter.PROTO_ID_COL);+ byte flags = rec.getByteValue(InstDBAdapter.FLAGS_COL);+ InstructionPrototype proto = protoMgr.getPrototype(protoID);+ return new InstructionDB(CodeManager.this, address, addr, proto, flags);+ }++ private DataDB instantiateData(DBRecord rec) {+ long addr = rec.getKey();+ Address address = addrMap.decodeAddress(addr);+ long datatypeID = rec.getLongValue(DataDBAdapter.DATA_TYPE_ID_COL);+ DataType dt = dataManager.getDataType(datatypeID);+ return new DataDB(CodeManager.this, addr, address, addr, dt);+ }+ }++ Lock getLock() {+ return lock;+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitCache.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitCache.javanew file mode 100644index 0000000000..b2c3128833--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitCache.java@@ -0,0 +1,59 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.database.code;++import db.DBRecord;+import ghidra.program.database.DbCache;+import ghidra.program.database.code.CodeManager.CodeUnitFactory;+import ghidra.util.Lock;++/**+ * Specialized version of {@link DbCache} for code units. Because the cache has to+ * deal with both instructions and data, it is more convenient to have methods to specifically+ * get instruction or data when the client knows which it expects.+ */+public class CodeUnitCache extends DbCache<CodeUnitDB> {++ public CodeUnitCache(CodeUnitFactory factory, Lock lock, int hardCacheSize) {+ super(factory, lock, hardCacheSize);+ }++ /**+ * Gets the Data object for the given record or null if the record is an instruction record.+ * @param dbRecord the data or instruction record+ * @return the DataDB for the record or null if the record is an instruction+ */+ public DataDB getData(DBRecord dbRecord) {+ CodeUnitDB cu = getCachedInstance(dbRecord);+ if (cu instanceof DataDB data) {+ return data;+ }+ return null;+ }++ /**+ * Gets the Instruction object for the given record or null if the record is a data record.+ * @param dbRecord the data or instruction record+ * @return the InstructionDB for the record or null if the record is a Data record+ */+ public InstructionDB getInstruction(DBRecord dbRecord) {+ CodeUnitDB cu = getCachedInstance(dbRecord);+ if (cu instanceof InstructionDB instruction) {+ return instruction;+ }+ return null;+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.javaindex aae9a3fa82..8df5ec67c0 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.java@@ -22,7 +22,8 @@ import java.util.*;import org.apache.commons.lang3.StringUtils;import db.DBRecord;-import ghidra.program.database.*;+import ghidra.program.database.DbObject;+import ghidra.program.database.ProgramDB;import ghidra.program.database.references.ReferenceDBManager;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressOutOfBoundsException;@@ -32,6 +33,7 @@ import ghidra.program.model.mem.*;import ghidra.program.model.symbol.*;import ghidra.program.model.util.*;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;/**@@ -42,7 +44,7 @@ import ghidra.util.exception.*;* The CodeUnit key should only be used for managing an object cache. The addr field should be used within* this class instead of the key field.*/-abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorContext {+abstract class CodeUnitDB extends DbObject implements CodeUnit, ProcessorContext {protected CodeManager codeMgr;protected Address address;@@ -61,20 +63,18 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC/*** Construct a new CodeUnitDB* @param codeMgr code manager that created this codeUnit.- * @param cache CodeUnitDB cache* @param cacheKey the cache key (dataComponent does not use the address)* @param address min address of this code unit* @param addr index for min address- * @param length the length of the codeunit.+ * @param length the length of the code unit.*/- public CodeUnitDB(CodeManager codeMgr, DBObjectCache<? extends CodeUnitDB> cache, long cacheKey,- Address address, long addr, int length) {- super(cache, cacheKey);+ CodeUnitDB(CodeManager codeMgr, long cacheKey, Address address, long addr, int length) {+ super(cacheKey);this.codeMgr = codeMgr;this.address = address;this.addr = addr;this.length = length;- this.lock = codeMgr.lock;+ this.lock = codeMgr.getLock();program = (ProgramDB) codeMgr.getProgram();refMgr = program.getReferenceManager();programContext = program.getProgramContext();@@ -194,9 +194,8 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorCif (commentType == null) {return null;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (!checkedComments) {readComments();}@@ -205,9 +204,6 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC}return commentRec.getString(commentType.ordinal());}- finally {- lock.release();- }}@Override@@ -423,8 +419,7 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC@Overridepublic void setComment(CommentType commentType, String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (!checkedComments) {@@ -463,9 +458,6 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorCcodeMgr.dbError(e);}}- finally {- lock.release();- }}@Override@@ -481,8 +473,7 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC@Overridepublic void setProperty(String name, int value) {PropertyMapManager upm = codeMgr.getPropertyMapManager();- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();IntPropertyMap pm = upm.getIntPropertyMap(name);@@ -497,17 +488,13 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC}pm.add(address, value);}- finally {- lock.release();- }}@SuppressWarnings("unchecked")@Overridepublic <T extends Saveable> void setProperty(String name, T value) {PropertyMapManager mgr = codeMgr.getPropertyMapManager();- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();ObjectPropertyMap<?> pm = mgr.getObjectPropertyMap(name);if (pm == null) {@@ -522,16 +509,12 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC// Will throw IllegalArgumentException if map is not applicable for T((ObjectPropertyMap<T>) pm).add(address, value);}- finally {- lock.release();- }}@Overridepublic void setProperty(String name, String value) {PropertyMapManager upm = codeMgr.getPropertyMapManager();- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();StringPropertyMap pm = upm.getStringPropertyMap(name);if (pm == null) {@@ -545,16 +528,12 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC}pm.add(address, value);}- finally {- lock.release();- }}@Overridepublic void setProperty(String name) {PropertyMapManager upm = codeMgr.getPropertyMapManager();- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();VoidPropertyMap pm = upm.getVoidPropertyMap(name);if (pm == null) {@@ -567,9 +546,6 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC}pm.add(address);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataComponent.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataComponent.javaindex 708f11308d..9e1ac28e42 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataComponent.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataComponent.java@@ -17,12 +17,12 @@ package ghidra.program.database.code;import db.DBRecord;import ghidra.docking.settings.Settings;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.address.Address;import ghidra.program.model.data.*;import ghidra.program.model.listing.CommentType;import ghidra.program.model.listing.Data;import ghidra.program.model.mem.MemoryAccessException;+import ghidra.util.Lock.Closeable;/*** <code>DataComponent</code> provides Data and CodeUnit access to Struct and Array components.@@ -44,15 +44,14 @@ class DataComponent extends DataDB {* NOTE: a zero-length component will be forced to have a length of 1-byte.* This can result in what would appear to be overlapping components with the same overset.* @param codeMgr the code manager.- * @param componentCache data component cache* @param address the address of the data component* @param addr the convert address long value* @param parent the DataDB object that contains this component.* @param component the DataTypeComponent for this DataComponent.*/- public DataComponent(CodeManager codeMgr, DBObjectCache<DataDB> componentCache, Address address,- long addr, DataDB parent, DataTypeComponent component) {- super(codeMgr, componentCache, component.getOrdinal(), address, addr,+ DataComponent(CodeManager codeMgr, Address address, long addr, DataDB parent,+ DataTypeComponent component) {+ super(codeMgr, component.getOrdinal(), address, addr,component.getDataType());this.indexInParent = component.getOrdinal();this.parent = parent;@@ -75,9 +74,9 @@ class DataComponent extends DataDB {* @param array the array containing this component.* @param ordinal the array index for this component.*/- DataComponent(CodeManager codeMgr, DBObjectCache<DataDB> componentCache, Address address,- long addr, DataDB parent, Array array, int ordinal) {- super(codeMgr, componentCache, ordinal, address, addr, array.getDataType());+ DataComponent(CodeManager codeMgr, Address address, long addr, DataDB parent, Array array,+ int ordinal) {+ super(codeMgr, ordinal, address, addr, array.getDataType());int elementLength = array.getElementLength();this.indexInParent = ordinal;this.parent = parent;@@ -235,42 +234,30 @@ class DataComponent extends DataDB {@Overridepublic int getBytes(byte[] b, int off) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return parent.getBytes(b, this.offset + off);}- finally {- lock.release();- }}@Overridepublic byte[] getBytes() throws MemoryAccessException {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();byte[] b = new byte[length];if (parent.getBytes(b, this.offset) != length) {throw new MemoryAccessException("Couldn't get all bytes for CodeUnit");}return b;}- finally {- lock.release();- }}@Overridepublic byte getByte(int n) throws MemoryAccessException {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return parent.getByte(this.offset + n);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.javaindex f0e9f78cf8..26683e6db2 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java@@ -20,7 +20,8 @@ import java.util.*;import db.DBRecord;import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsDefinition;-import ghidra.program.database.DBObjectCache;+import ghidra.program.database.DbCache;+import ghidra.program.database.DbFactory;import ghidra.program.database.data.ProgramDataTypeManager;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.*;@@ -31,6 +32,7 @@ import ghidra.program.model.mem.Memory;import ghidra.program.model.mem.MemoryBlock;import ghidra.program.model.scalar.Scalar;import ghidra.program.model.symbol.*;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;/**@@ -55,12 +57,23 @@ class DataDB extends CodeUnitDB implements Data {private static final int[] EMPTY_PATH = new int[0];- private DBObjectCache<DataDB> componentCache = null;// data components are keyed on index in parent (i.e., ordinal)-- DataDB(CodeManager codeMgr, DBObjectCache<? extends CodeUnitDB> codeUnitCache, long cacheKey,- Address address, long addr, DataType dataType) {-- super(codeMgr, codeUnitCache, cacheKey, address, addr,+ // data components are keyed on index in parent (i.e., ordinal)+ private DbCache<DataComponent> componentCache = null;++ /**+ * Constructs a new DataDB object+ * @param codeManager the code manager+ * @param cacheKey the cache key (normally this is the encoded address, but for data components+ * the objects are cached based on their ordinal (and the cache is a special cache inside the+ * data object and not the normal code manager cache)+ * @param address the address for the data+ * @param addr the encoded address for the data+ * @param dataType the datatype for the data+ */+ protected DataDB(CodeManager codeManager, long cacheKey, Address address, long addr,+ DataType dataType) {++ super(codeManager, cacheKey, address, addr,dataType == null ? 1 : dataType.getLength());if (dataType == null) {dataType = DataType.DEFAULT;@@ -108,6 +121,18 @@ class DataDB extends CodeUnitDB implements Data {Msg.error(this, "Data found but datatype missing at " + address);}}+ else if (address.isExternalAddress()) {+ Symbol externalSymbol = program.getSymbolTable().getPrimarySymbol(address);+ if (externalSymbol == null || externalSymbol.getSymbolType() != SymbolType.LABEL) {+ return true;+ }++ ExternalLocation externalLocation =+ program.getExternalManager().getExternalLocation(externalSymbol);++ dt = externalLocation.getDataType();+ dt = dt == null ? DataType.DEFAULT : dt;+ }else {dt = codeMgr.getDataType(addr);}@@ -208,55 +233,21 @@ class DataDB extends CodeUnitDB implements Data {removeOperandReference(CodeManager.DATA_OP_INDEX, refAddr);}+ private void createCacheIfNeeded() {+ if (componentCache == null) {+ componentCache =+ new DbCache<DataComponent>(new ComponentFactory(), lock, 1);+ }+ }+@Overridepublic Data getComponent(int index) {- lock.acquire();- try {-- checkIsValid();-+ try (Closeable c = lock.read()) {if (index < 0 || index >= getNumComponents()) {return null;}-- if (componentCache == null) {- componentCache = new DBObjectCache<>(1);- }- else {- Data data = componentCache.get(index);- if (data != null) {- return data;- }- }-- AddressMap addressMap = codeMgr.getAddressMap();-- if (baseDataType instanceof Array) {- Array array = (Array) baseDataType;- Address componentAddr = address.add(index * array.getElementLength());- return new DataComponent(codeMgr, componentCache, componentAddr,- addressMap.getKey(componentAddr, false), this, array, index);- }- if (baseDataType instanceof Composite) {- Composite composite = (Composite) baseDataType;- DataTypeComponent dtc = composite.getComponent(index);- Address componentAddr = address.add(dtc.getOffset());- return new DataComponent(codeMgr, componentCache, componentAddr,- addressMap.getKey(componentAddr, false), this, dtc);- }- if (baseDataType instanceof DynamicDataType) {- DynamicDataType ddt = (DynamicDataType) baseDataType;- DataTypeComponent dtc = ddt.getComponent(index, this);- Address componentAddr = address.add(dtc.getOffset());- return new DataComponent(codeMgr, componentCache, componentAddr,- addressMap.getKey(componentAddr, false), this, dtc);- }- Msg.error(this,- "Unsupported composite data type class: " + baseDataType.getClass().getName());- return null;- }- finally {- lock.release();+ createCacheIfNeeded();+ return componentCache.getCachedInstance((long) index);}}@@ -283,26 +274,18 @@ class DataDB extends CodeUnitDB implements Data {@Overridepublic String getDefaultValueRepresentation() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return dataType.getRepresentation(this, this, getLength());}- finally {- lock.release();- }}@Overridepublic String getMnemonicString() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return dataType.getMnemonic(this);}- finally {- lock.release();- }}@Override@@ -346,9 +329,8 @@ class DataDB extends CodeUnitDB implements Data {if (hasSetting != null && !hasSetting) {return mutabilityType == MutabilitySettingsDefinition.NORMAL;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();MutabilitySettingsDefinition def =getSettingsDefinition(MutabilitySettingsDefinition.class);if (def != null) {@@ -356,11 +338,8 @@ class DataDB extends CodeUnitDB implements Data {return def.getChoice(this) == mutabilityType;}hasMutabilitySetting = false;+ return false;}- finally {- lock.release();- }- return false;}@Override@@ -446,17 +425,13 @@ class DataDB extends CodeUnitDB implements Data {@Overridepublic Data getComponent(int[] componentPath) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (componentPath == null || componentPath.length <= level) {return this;}Data component = getComponent(componentPath[level]);return (component == null ? null : component.getComponent(componentPath));}- finally {- lock.release();- }}@Override@@ -488,9 +463,8 @@ class DataDB extends CodeUnitDB implements Data {@Overridepublic Data getComponentContaining(int offset) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (offset < 0 || offset > getLength()) {return null;}@@ -517,17 +491,12 @@ class DataDB extends CodeUnitDB implements Data {}return null;}- finally {- lock.release();- }-}@Overridepublic List<Data> getComponentsContaining(int offset) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (offset < 0 || offset >= getLength()) {return null;}@@ -572,10 +541,6 @@ class DataDB extends CodeUnitDB implements Data {}return Collections.emptyList();}- finally {- lock.release();- }-}@Override@@ -610,9 +575,8 @@ class DataDB extends CodeUnitDB implements Data {@Overridepublic int getNumComponents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (getLength() < dataType.getLength()) {return -1;}@@ -634,9 +598,6 @@ class DataDB extends CodeUnitDB implements Data {}return 0;}- finally {- lock.release();- }}@Override@@ -663,9 +624,8 @@ class DataDB extends CodeUnitDB implements Data {@Overridepublic Data getPrimitiveAt(int offset) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (offset < 0 || offset >= getLength()) {return null;}@@ -675,9 +635,6 @@ class DataDB extends CodeUnitDB implements Data {}return dc.getPrimitiveAt(offset - dc.getParentOffset());}- finally {- lock.release();- }}@Override@@ -692,14 +649,10 @@ class DataDB extends CodeUnitDB implements Data {@Overridepublic Object getValue() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return dataType.getValue(this, this, getLength());}- finally {- lock.release();- }}@Override@@ -795,4 +748,44 @@ class DataDB extends CodeUnitDB implements Data {return dataType.getDefaultSettings();}+ private class ComponentFactory implements DbFactory<DataComponent> {++ @Override+ public DataComponent instantiate(long ordinal) {+ AddressMap addressMap = codeMgr.getAddressMap();+ int index = (int) ordinal;+ if (baseDataType instanceof Array) {+ Array array = (Array) baseDataType;+ Address componentAddr = address.add(index * array.getElementLength());+ long dbKey = addressMap.getKey(componentAddr, false);+ return new DataComponent(codeMgr, componentAddr,+ dbKey, DataDB.this, array, index);+ }+ if (baseDataType instanceof Composite) {+ Composite composite = (Composite) baseDataType;+ DataTypeComponent dtc = composite.getComponent(index);+ Address componentAddr = address.add(dtc.getOffset());+ long dbKey = addressMap.getKey(componentAddr, false);+ return new DataComponent(codeMgr, componentAddr, dbKey, DataDB.this, dtc);+ }+ if (baseDataType instanceof DynamicDataType) {+ DynamicDataType ddt = (DynamicDataType) baseDataType;+ DataTypeComponent dtc = ddt.getComponent(index, DataDB.this);+ Address componentAddr = address.add(dtc.getOffset());+ long dbKey = addressMap.getKey(componentAddr, false);+ return new DataComponent(codeMgr, componentAddr, dbKey, DataDB.this, dtc);+ }+ Msg.error(this,+ "Unsupported composite data type class: " + baseDataType.getClass().getName());+ return null;+ }++ @Override+ public DataComponent instantiate(DBRecord record) {+ // DataComponents don't have records+ return null;+ }++ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataRecordIterator.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataRecordIterator.javaindex 8a2a393f3f..a3f3cd9b13 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataRecordIterator.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataRecordIterator.java@@ -15,31 +15,30 @@*/package ghidra.program.database.code;-import ghidra.program.model.listing.Data;-import ghidra.program.model.listing.DataIterator;-import java.io.IOException;import db.DBRecord;import db.RecordIterator;+import ghidra.program.model.listing.Data;+import ghidra.program.model.listing.DataIterator;/*** Converts a record iterator into a DataIterator.*/-public class DataRecordIterator implements DataIterator {- private CodeManager codeMgr;+class DataRecordIterator implements DataIterator {+ private CodeUnitCache cache;private RecordIterator it;private Data nextData;private boolean forward;/*** Constructs a new DataRecordIterator- * @param codeMgr the code manager+ * @param cache the code unit cache* @param it the record iterator* @param forward the direction of the iterator.*/- public DataRecordIterator(CodeManager codeMgr, RecordIterator it, boolean forward) {- this.codeMgr = codeMgr;+ DataRecordIterator(CodeUnitCache cache, RecordIterator it, boolean forward) {+ this.cache = cache;this.it = it;this.forward = forward;@@ -81,7 +80,7 @@ public class DataRecordIterator implements DataIterator {try {while (nextData == null && (forward ? it.hasNext() : it.hasPrevious())) {DBRecord record = forward ? it.next() : it.previous();- nextData = codeMgr.getDataDB(record);+ nextData = cache.getData(record);}}catch (IOException e) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionDB.javaindex 739678f250..1f9f7b4be5 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionDB.java@@ -18,7 +18,6 @@ package ghidra.program.database.code;import java.util.*;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressOverflowException;import ghidra.program.model.lang.*;@@ -30,6 +29,7 @@ import ghidra.program.model.scalar.Scalar;import ghidra.program.model.symbol.*;import ghidra.program.model.util.CodeUnitInsertionException;import ghidra.program.util.ProgramEvent;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.NoValueException;@@ -63,15 +63,14 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio/*** Construct a new InstructionDB.* @param codeMgr code manager- * @param cache code unit cache* @param address min address of this instruction* @param addr database key* @param proto instruction prototype* @param flags flow override flags*/- public InstructionDB(CodeManager codeMgr, DBObjectCache<? extends CodeUnitDB> cache,- Address address, long addr, InstructionPrototype proto, byte flags) {- super(codeMgr, cache, addr, address, addr, proto.getLength());+ InstructionDB(CodeManager codeMgr, Address address, long addr, InstructionPrototype proto,+ byte flags) {+ super(codeMgr, addr, address, addr, proto.getLength());this.proto = proto;this.flags = flags;flowOverride =@@ -158,13 +157,9 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioif (!proto.hasDelaySlots()) {return 0;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {return proto.getDelaySlotDepth(this);}- finally {- lock.release();- }}/**@@ -192,9 +187,8 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioif (!isLengthOverridden()) {return getBytes();}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();int len = proto.getLength();byte[] b = new byte[len];if (len != getMemory().getBytes(address, b)) {@@ -202,16 +196,12 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio}return b;}- finally {- lock.release();- }}@Overridepublic Address getFallFrom() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();// check if the instruction before this is in a delay slot// If it is then back up until hit an instruction that claims// to be a delay slot instruction that is not in a delay slot@@ -263,9 +253,6 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioreturn null;}- finally {- lock.release();- }}private Address getFallThroughReference() {@@ -337,14 +324,10 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio}// always reflects current flowOverride- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getOperandRefType(opIndex, this, new InstructionPcodeOverride(this));}- finally {- lock.release();- }}@Override@@ -354,9 +337,8 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio@Overridepublic String getDefaultOperandRepresentation(int opIndex) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();List<Object> opList = getDefaultOperandRepresentationList(opIndex);if (opList == null) {return "<UNSUPPORTED>";@@ -377,28 +359,20 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio}return strBuf.toString();}- finally {- lock.release();- }}@Overridepublic List<Object> getDefaultOperandRepresentationList(int opIndex) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getOpRepresentationList(opIndex, this);}- finally {- lock.release();- }}@Overridepublic int getOperandType(int opIndex) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();int optype = proto.getOpType(opIndex, this);Reference ref = getPrimaryReference(opIndex);@@ -414,9 +388,6 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio}return optype;}- finally {- lock.release();- }}@Override@@ -426,14 +397,10 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioreturn EMPTY_OBJ_ARRAY;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getOpObjects(opIndex, this);}- finally {- lock.release();- }}@Override@@ -454,38 +421,26 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioreturn null;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getRegister(opIndex, this);}- finally {- lock.release();- }}@Overridepublic Object[] getInputObjects() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getInputObjects(this);}- finally {- lock.release();- }}@Overridepublic Object[] getResultObjects() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getResultObjects(this);}- finally {- lock.release();- }}@Override@@ -500,9 +455,8 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioreturn null;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();Reference ref = refMgr.getPrimaryReferenceFrom(address, opIndex);if (ref != null) {return ref.getToAddress();@@ -516,16 +470,12 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioreturn null;}- finally {- lock.release();- }}@Overridepublic String toString() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();StringBuffer stringBuffer = new StringBuffer();stringBuffer.append(getMnemonicString());@@ -547,9 +497,6 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio}return stringBuffer.toString();}- finally {- lock.release();- }}@Override@@ -559,15 +506,11 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioif (curMnemonic != null) {return curMnemonic;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();mnemonicString = proto.getMnemonic(this);return mnemonicString;}- finally {- lock.release();- }}@Override@@ -582,15 +525,10 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioreturn null;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getScalar(opIndex, this);}- finally {- lock.release();- }-}/**@@ -616,8 +554,7 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioif (flow == null) {flow = FlowOverride.NONE;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (flow == flowOverride) {return;@@ -651,9 +588,6 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio}}}- finally {- lock.release();- }program.setChanged(ProgramEvent.FLOW_OVERRIDE_CHANGED, address, address, null, null);}@@ -677,30 +611,22 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio@Overridepublic PcodeOp[] getPcode(boolean includeOverrides) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (!includeOverrides) {return proto.getPcode(this, null);}return proto.getPcode(this, new InstructionPcodeOverride(this));}- finally {- lock.release();- }}@Overridepublic PcodeOp[] getPcode(int opIndex) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();// assumes operand pcode not affected by flow overridereturn proto.getPcode(this, opIndex);}- finally {- lock.release();- }}@Override@@ -768,8 +694,7 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructio@Overridepublic void clearFallThroughOverride() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (!isFallThroughOverridden()) {return;@@ -779,16 +704,12 @@ public class InstructionDB extends CodeUnitDB implements Instruction, InstructiosetFallthroughOverride(false);addLengthOverrideFallthroughRef(); // restore length-override fallthrough if needed}- finally {- lock.release();- }}@Overridepublic void setFallThrough(Address fallThroughAddr) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();Address defaultFallThrough = proto.getFallThrough(this);if (addrsEqual(fallThroughAddr, defaultFallThrough)) {@@ -806,24 +727,17 @@ public class InstructionDB extends CodeUnitDB implements Instruction, InstructioSourceType.USER_DEFINED, Reference.MNEMONIC);}}- finally {- lock.release();- }}@Overridepublic void setLengthOverride(int len) throws CodeUnitInsertionException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (doSetLengthOverride(len)) {program.setChanged(ProgramEvent.LENGTH_OVERRIDE_CHANGED, address, address, null,null);}}- finally {- lock.release();- }}/**@@ -941,14 +855,10 @@ public class InstructionDB extends CodeUnitDB implements Instruction, Instructioif (proto.getDelaySlotByteCount() <= 0) {return proto.getLength();}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return proto.getFallThroughOffset(this);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionRecordIterator.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionRecordIterator.javaindex a9b8ee809c..afbed23dd7 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionRecordIterator.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/InstructionRecordIterator.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -15,33 +15,32 @@*/package ghidra.program.database.code;-import ghidra.program.model.listing.Instruction;-import ghidra.program.model.listing.InstructionIterator;-import java.io.IOException;import java.util.Iterator;import db.DBRecord;import db.RecordIterator;+import ghidra.program.model.listing.Instruction;+import ghidra.program.model.listing.InstructionIterator;/*** Converts a record iterator into an instruction iterator.*/-public class InstructionRecordIterator implements InstructionIterator {- private CodeManager codeMgr;+class InstructionRecordIterator implements InstructionIterator {+ private CodeUnitCache cache;private RecordIterator it;private Instruction nextInstruction;private boolean forward;/*** Constructs a new InstructionRecordIterator- * @param codeMgr the code manager+ * @param cache the cache used to getCachedInstances for records* @param it the record iterator.* @param forward the direction of the iterator.*/- public InstructionRecordIterator(CodeManager codeMgr, RecordIterator it, boolean forward) {- this.codeMgr = codeMgr;+ InstructionRecordIterator(CodeUnitCache cache, RecordIterator it, boolean forward) {+ this.cache = cache;this.it = it;this.forward = forward;}@@ -74,7 +73,7 @@ public class InstructionRecordIterator implements InstructionIterator {try {while (nextInstruction == null && (forward ? it.hasNext() : it.hasPrevious())) {DBRecord record = forward ? it.next() : it.previous();- nextInstruction = codeMgr.getInstructionDB(record);+ nextInstruction = cache.getInstruction(record);}}catch (IOException e) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.javaindex ecd532fc2d..72606a288b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.java@@ -20,10 +20,10 @@ import java.io.IOException;import db.DBRecord;import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsDefinition;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.data.*;import ghidra.program.model.mem.MemBuffer;import ghidra.util.InvalidNameException;+import ghidra.util.Lock.Closeable;import ghidra.util.UniversalID;import ghidra.util.exception.DuplicateNameException;@@ -41,14 +41,12 @@ class ArrayDB extends DataTypeDB implements Array {/*** Constructor- * @param dataMgr- * @param cache- * @param adapter- * @param record+ * @param dataMgr the datatypes manager+ * @param adapter the arrays database adapter+ * @param record the record for the union*/- public ArrayDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- ArrayDBAdapter adapter, DBRecord record) {- super(dataMgr, cache, record);+ ArrayDB(DataTypeManagerDB dataMgr, ArrayDBAdapter adapter, DBRecord record) {+ super(dataMgr, record);this.adapter = adapter;}@@ -93,20 +91,16 @@ class ArrayDB extends DataTypeDB implements Array {@Overridepublic String getDisplayName() {String localDisplayName = displayName;- if (localDisplayName != null && !isInvalid()) {+ if (localDisplayName != null && !needsRefreshing()) {return localDisplayName;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (displayName == null) {displayName = DataTypeUtilities.getDisplayName(this, false);}return displayName;}- finally {- lock.release();- }}@Override@@ -140,15 +134,14 @@ class ArrayDB extends DataTypeDB implements Array {@Overridepublic String getDescription() {- checkIsValid();+ refreshIfNeeded();return "Array of " + getDataType().getDescription();}@Overridepublic DataType getDataType() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();long dataTypeID = record.getLongValue(ArrayDBAdapter.ARRAY_DT_ID_COL);DataType dt = dataMgr.getDataType(dataTypeID);if (dt == null) {@@ -156,9 +149,6 @@ class ArrayDB extends DataTypeDB implements Array {}return dt;}- finally {- lock.release();- }}@Override@@ -178,9 +168,8 @@ class ArrayDB extends DataTypeDB implements Array {@Overridepublic int getElementLength() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();DataType dt = getDataType();if (elementLength < 0) {if (dt instanceof Dynamic) {@@ -192,9 +181,6 @@ class ArrayDB extends DataTypeDB implements Array {}return elementLength;}- finally {- lock.release();- }}@Override@@ -269,11 +255,10 @@ class ArrayDB extends DataTypeDB implements Array {return;}DataTypeUtilities.checkValidReplacement(oldDt, newDt);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (oldDt == getDataType()) {- if (newDt == this) {+ if (newDt == this || newDt.getLength() < 0) {newDt = DataType.DEFAULT;}int oldElementLength = getElementLength();@@ -335,9 +320,6 @@ class ArrayDB extends DataTypeDB implements Array {}}}- finally {- lock.release();- }}@Override@@ -350,17 +332,13 @@ class ArrayDB extends DataTypeDB implements Array {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType() && dt.getLength() > 0) {elementLength = -1;notifySizeChanged(true);}}- finally {- lock.release();- }}@Override@@ -368,16 +346,12 @@ class ArrayDB extends DataTypeDB implements Array {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {notifyAlignmentChanged(true);}}- finally {- lock.release();- }}/**@@ -405,8 +379,7 @@ class ArrayDB extends DataTypeDB implements Array {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {elementLength = -1;@@ -414,9 +387,6 @@ class ArrayDB extends DataTypeDB implements Array {deleting = true;}}- finally {- lock.release();- }}@Override@@ -424,20 +394,16 @@ class ArrayDB extends DataTypeDB implements Array {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();+ String myOldName = getOldName();if (dt == getDataType()) {- String myOldName = getOldName();refreshName();if (!getName().equals(myOldName)) {notifyNameChanged(myOldName);}}}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.javaindex f85b3bd8f7..683107a829 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.java@@ -21,19 +21,19 @@ import java.util.concurrent.ConcurrentHashMap;import db.DBRecord;import db.Field;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.data.*;import ghidra.program.model.data.DataTypeConflictHandler.ConflictResult;import ghidra.util.InvalidNameException;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;/*** Database implementation for Category.*/-class CategoryDB extends DatabaseObject implements Category {+class CategoryDB extends DbObject implements Category {private DataTypeManagerDB mgr;private volatile CategoryDB parent;@@ -47,14 +47,12 @@ class CategoryDB extends DatabaseObject implements Category {/*** Category Constructor* @param dtMgr the data type manager- * @param cache CategoryDB object cache* @param id the id for this category* @param parent the parent for this category* @param name the name of this category*/- CategoryDB(DataTypeManagerDB dtMgr, DBObjectCache<CategoryDB> cache, long id, CategoryDB parent,- String name) {- super(cache, id);+ CategoryDB(DataTypeManagerDB dtMgr, long id, CategoryDB parent, String name) {+ super(id);this.mgr = dtMgr;this.name = name;this.parent = parent;@@ -74,24 +72,14 @@ class CategoryDB extends DatabaseObject implements Category {conflictMap = new ConflictMap(mgr.lock);}- /**- * Root Category Constructor- * @param dtMgr the data type manager- * @param cache CategoryDB object cache- */- CategoryDB(DataTypeManagerDB dtMgr, DBObjectCache<CategoryDB> cache) {- this(dtMgr, cache, DataTypeManagerDB.ROOT_CATEGORY_ID, null, "/");- }-@Overridepublic CategoryPath getCategoryPath() {CategoryPath localCategoryPath = categoryPath;- if (localCategoryPath != null && !isInvalid()) {+ if (localCategoryPath != null && !needsRefreshing()) {return localCategoryPath;}- mgr.lock.acquire();- try {- if (!checkIsValid() || isRoot()) {+ try (Closeable c = mgr.lock.read()) {+ if (!refreshIfNeeded() || isRoot()) {categoryPath = CategoryPath.ROOT;}if (categoryPath == null) {@@ -99,9 +87,6 @@ class CategoryDB extends DatabaseObject implements Category {}return categoryPath;}- finally {- mgr.lock.release();- }}@Override@@ -126,8 +111,8 @@ class CategoryDB extends DatabaseObject implements Category {if (rec == null) {return false;}- this.parent =- mgr.getCategoryDB(rec.getLongValue(CategoryDBAdapter.CATEGORY_PARENT_COL));+ long id = rec.getLongValue(CategoryDBAdapter.CATEGORY_PARENT_COL);+ this.parent = mgr.getCategoryDB(id);this.name = rec.getString(CategoryDBAdapter.CATEGORY_NAME_COL);}catch (IOException e) {@@ -154,8 +139,7 @@ class CategoryDB extends DatabaseObject implements Category {@Overridepublic void setName(String newName) throws InvalidNameException, DuplicateNameException {testName(newName);- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();CategoryPath oldPath = getCategoryPath();if (isRoot()) {@@ -179,9 +163,6 @@ class CategoryDB extends DatabaseObject implements Category {parent.catagoryRenamed(this, oldName);mgr.categoryRenamed(oldPath, this);}- finally {- mgr.lock.release();- }}private CategoryDB[] getCategories(long parentId) {@@ -242,8 +223,7 @@ class CategoryDB extends DatabaseObject implements Category {*/@Overridepublic DataType addDataType(DataType dt, DataTypeConflictHandler handler) {- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();if (!getCategoryPath().equals(dt.getCategoryPath())) {dt = dt.clone(mgr);@@ -257,9 +237,6 @@ class CategoryDB extends DatabaseObject implements Category {DataType resolvedDataType = mgr.resolve(dt, handler);return resolvedDataType;}- finally {- mgr.lock.release();- }}/**@@ -292,8 +269,7 @@ class CategoryDB extends DatabaseObject implements Category {@Overridepublic Category createCategory(String categoryName) throws InvalidNameException {testName(categoryName);- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();CategoryDB cat = mgr.createCategoryDB(this, categoryName);return cat;@@ -301,16 +277,12 @@ class CategoryDB extends DatabaseObject implements Category {catch (IOException e1) {mgr.dbError(e1);}- finally {- mgr.lock.release();- }return null;}@Overridepublic boolean removeCategory(String categoryName, TaskMonitor monitor) {- mgr.lock.acquire();- try {+ try (Closeable c1 = mgr.lock.write()) {checkDeleted();CategoryDB c = getCategory(categoryName);if (c == null) {@@ -345,29 +317,25 @@ class CategoryDB extends DatabaseObject implements Category {}return false;}- finally {- mgr.lock.release();- }}@Overridepublic boolean removeEmptyCategory(String categoryName, TaskMonitor monitor) {- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();- CategoryDB c = getCategory(categoryName);- if (c == null) {+ CategoryDB cat = getCategory(categoryName);+ if (cat == null) {return false;}- Category[] cats = c.getCategories();- DataType[] dts = c.getDataTypes();+ Category[] cats = cat.getCategories();+ DataType[] dts = cat.getDataTypes();if (cats.length != 0 || dts.length != 0) {return false;}try {- mgr.getCategoryDBAdapter().removeCategory(c.getKey());+ mgr.getCategoryDBAdapter().removeCategory(cat.getKey());subcategoryMap.remove(categoryName);- mgr.categoryRemoved(this, categoryName, c.getKey());+ mgr.categoryRemoved(this, categoryName, cat.getKey());return true;}catch (IOException e) {@@ -375,9 +343,6 @@ class CategoryDB extends DatabaseObject implements Category {}return false;}- finally {- mgr.lock.release();- }}/**@@ -392,8 +357,7 @@ class CategoryDB extends DatabaseObject implements Category {throw new IllegalArgumentException("Category is not a CategoryDB");}CategoryDB categoryDB = (CategoryDB) category;- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();if (getCategory(categoryDB.getName()) != null) {throw new DuplicateNameException(@@ -414,9 +378,6 @@ class CategoryDB extends DatabaseObject implements Category {subcategoryMap.put(categoryDB.getName(), categoryDB);mgr.categoryMoved(movedCategoryOriginalPath, categoryDB);}- finally {- mgr.lock.release();- }}/**@@ -427,8 +388,7 @@ class CategoryDB extends DatabaseObject implements Category {TaskMonitor monitor) {// TODO: source archive handling is not documentedboolean isInSameArchive = (mgr == category.getDataTypeManager());- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();Category cat = createCategory(category.getName());Category[] cats = category.getCategories();@@ -451,9 +411,6 @@ class CategoryDB extends DatabaseObject implements Category {catch (InvalidNameException e) {// can't happen--already had a valid name}- finally {- mgr.lock.release();- }return null;}@@ -513,8 +470,7 @@ class CategoryDB extends DatabaseObject implements Category {@Overridepublic void moveDataType(DataType movedDataType, DataTypeConflictHandler handler)throws DataTypeDependencyException {- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();CategoryPath path = getCategoryPath();if (handler == null) {@@ -550,9 +506,6 @@ class CategoryDB extends DatabaseObject implements Category {catch (DuplicateNameException e) {throw new AssertException(e); // can't happen?}- finally {- mgr.lock.release();- }}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CompositeDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CompositeDB.javaindex f1fcfa5111..70b15fb498 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CompositeDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CompositeDB.java@@ -23,9 +23,9 @@ import java.util.function.Consumer;import db.DBRecord;import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsImpl;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.data.*;import ghidra.program.model.mem.MemBuffer;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.UniversalID;import ghidra.util.exception.AssertException;@@ -42,16 +42,14 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {* Constructor for a composite data type (structure or union).** @param dataMgr the data type manager containing this data type.- * @param cache DataTypeDB object cache* @param compositeAdapter the database adapter for this data type.* @param componentAdapter the database adapter for the components of this data* type.* @param record the database record for this data type.*/- CompositeDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- CompositeDBAdapter compositeAdapter, ComponentDBAdapter componentAdapter,- DBRecord record) {- super(dataMgr, cache, record);+ protected CompositeDB(DataTypeManagerDB dataMgr, CompositeDBAdapter compositeAdapter,+ ComponentDBAdapter componentAdapter, DBRecord record) {+ super(dataMgr, record);this.compositeAdapter = compositeAdapter;this.componentAdapter = componentAdapter;initialize();@@ -113,8 +111,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {* @return updated component instance*/protected DataTypeComponentDB setFieldName(DataTypeComponentDB component, String name) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (component.getRecord() == null) {return component; // unable to change undefined component@@ -130,9 +127,6 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }return component; // unchanged}@@ -145,8 +139,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {* @return updated component instance*/protected DataTypeComponentDB setComment(DataTypeComponentDB component, String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (component.getRecord() == null) {return component; // unable to change undefined component@@ -162,9 +155,6 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }return component; // unchanged}@@ -316,8 +306,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overridepublic void setDescription(String desc) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (Objects.equals(desc, record.getString(CompositeDBAdapter.COMPOSITE_COMMENT_COL))) {return;@@ -329,22 +318,15 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic String getDescription() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();String s = record.getString(CompositeDBAdapter.COMPOSITE_COMMENT_COL);return s == null ? "" : s;}- finally {- lock.release();- }}@Override@@ -409,14 +391,10 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overridepublic boolean isPartOf(DataType dataTypeOfInterest) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return DataTypeUtilities.isSecondPartOfFirst(this, dataTypeOfInterest);}- finally {- lock.release();- }}protected DataType doCheckedResolve(DataType dt) throws DataTypeDependencyException {@@ -499,8 +477,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overridepublic void setLastChangeTime(long lastChangeTime) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doSetLastChangeTime(lastChangeTime);dataMgr.dataTypeChanged(this, false);@@ -508,15 +485,11 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void setLastChangeTimeInSourceArchive(long lastChangeTime) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(CompositeDBAdapter.COMPOSITE_SOURCE_SYNC_TIME_COL, lastChangeTime);compositeAdapter.updateRecord(record, false);@@ -525,9 +498,6 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -537,8 +507,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overridevoid setUniversalID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(CompositeDBAdapter.COMPOSITE_UNIVERSAL_DT_ID, id.getValue());compositeAdapter.updateRecord(record, false);@@ -547,9 +516,6 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@@ -561,8 +527,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overrideprotected void setSourceArchiveID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(CompositeDBAdapter.COMPOSITE_SOURCE_ARCHIVE_ID_COL, id.getValue());compositeAdapter.updateRecord(record, false);@@ -571,10 +536,6 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }-}protected final int getNonPackedAlignment() {@@ -602,25 +563,17 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overridepublic final int getAlignment() {- lock.acquire();- try {- return getComputedAlignment(checkIsValid() && dataMgr.isTransactionActive());- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ return getComputedAlignment(refreshIfNeeded() && dataMgr.isTransactionActive());}}@Overridepublic final void repack() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();repack(false, true);}- finally {- lock.release();- }}/**@@ -644,14 +597,10 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overridepublic int getStoredPackingValue() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getIntValue(CompositeDBAdapter.COMPOSITE_PACKING_COL);}- finally {- lock.release();- }}@Override@@ -689,8 +638,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {if (packingValue < NO_PACKING) {throw new IllegalArgumentException("invalid packing value: " + packingValue);}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();int oldPackingValue = getStoredPackingValue();if (packingValue == oldPackingValue) {@@ -709,9 +657,6 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -743,14 +688,10 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {@Overridepublic int getStoredMinimumAlignment() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getIntValue(CompositeDBAdapter.COMPOSITE_MIN_ALIGN_COL);}- finally {- lock.release();- }}@Override@@ -767,8 +708,7 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {throw new IllegalArgumentException("invalid minimum alignment value: " + minimumAlignment);}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (minimumAlignment == getStoredMinimumAlignment()) {return;@@ -782,9 +722,6 @@ abstract class CompositeDB extends DataTypeDB implements CompositeInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.javaindex 2381daeca4..165e72caaf 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.java@@ -22,20 +22,18 @@ import org.apache.commons.lang3.StringUtils;import db.DBRecord;import ghidra.docking.settings.*;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.data.*;import ghidra.program.model.mem.MemBuffer;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.NotYetImplementedException;/*** Base class for data types that are Database objects.- *- **/-abstract class DataTypeDB extends DatabaseObject implements DataType {+abstract class DataTypeDB extends DbObject implements DataType {protected DBRecord record;protected final DataTypeManagerDB dataMgr;@@ -50,9 +48,8 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {private volatile String name;private volatile Category category;- protected DataTypeDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- DBRecord record) {- super(cache, record.getKey());+ protected DataTypeDB(DataTypeManagerDB dataMgr, DBRecord record) {+ super(record.getKey());this.dataMgr = dataMgr;this.record = record;this.lock = dataMgr.lock;@@ -72,7 +69,7 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {* Clears the current name so that the next invocation of {@link #getName()} will* force its update via {@link #doGetName()}. It is important that {@link #doGetName()}* does not get invoked during a {@link #refresh()} to avoid problematic recursion during the- * refresh caused by recursive use of {@link #checkIsValid()} on the same object.+ * refresh caused by recursive use of {@link #refreshIfNeeded()} on the same object.* <P>* NOTE: This must only be invoked while in a locked-state*/@@ -162,20 +159,16 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {@Overridepublic final String getName() {String n = name;- if (n != null && !isInvalid()) {+ if (n != null && !needsRefreshing()) {return n;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (name == null) {name = doGetName();}return name;}- finally {- lock.release();- }}@Override@@ -200,12 +193,11 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {@Overridepublic Settings getDefaultSettings() {Settings localDefaultSettings = defaultSettings;- if (localDefaultSettings != null && !isInvalid()) {+ if (localDefaultSettings != null && !needsRefreshing()) {return localDefaultSettings;}- lock.acquire();- try {- if (checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (refreshIfNeeded()) {defaultSettings = doGetDefaultSettings();}else {@@ -213,9 +205,6 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {}return defaultSettings;}- finally {- lock.release();- }}/**@@ -302,12 +291,11 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {@Overridepublic CategoryPath getCategoryPath() {Category cat = category;- if (cat != null && !isInvalid()) {+ if (cat != null && !needsRefreshing()) {return cat.getCategoryPath();}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (category == null) {category = dataMgr.getCategory(doGetCategoryID());}@@ -316,9 +304,6 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {}return category.getCategoryPath();}- finally {- lock.release();- }}@Override@@ -328,8 +313,7 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {@Overridepublic void setName(String newName) throws InvalidNameException, DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (getName().equals(newName)) {return;@@ -341,9 +325,6 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {}doSetName(newName);}- finally {- lock.release();- }}private final void doSetName(String newName) throws InvalidNameException {@@ -365,8 +346,7 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {@Overridepublic void setCategoryPath(CategoryPath path) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (getCategoryPath().equals(path)) {return;@@ -378,9 +358,6 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {}doSetCategoryPath(path);}- finally {- lock.release();- }}private void doSetCategoryPath(CategoryPath path) {@@ -404,8 +381,7 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {@Overridepublic void setNameAndCategory(CategoryPath path, String name)throws InvalidNameException, DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();DataType dt = dataMgr.getDataType(path, name);if (dt != null) {@@ -432,9 +408,6 @@ abstract class DataTypeDB extends DatabaseObject implements DataType {doSetName(name);}}- finally {- lock.release();- }}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeManagerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeManagerDB.javaindex c440262c29..ccbacf5583 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeManagerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeManagerDB.java@@ -50,8 +50,8 @@ import ghidra.program.model.data.Enum;import ghidra.program.model.lang.*;import ghidra.program.model.listing.Function;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.classfinder.ClassTranslator;-import ghidra.util.datastruct.FixedSizeHashMap;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -137,13 +137,13 @@ abstract public class DataTypeManagerDB implements DataTypeManager {private DataTypeConflictHandler currentHandler;private CategoryDB root;- private DBObjectCache<DataTypeDB> dtCache;- private DBObjectCache<SourceArchiveDB> sourceArchiveDBCache;- private HashMap<Long, DataType> builtInMap = new HashMap<>();- private HashMap<DataType, Long> builtIn2IdMap = new HashMap<>();- private DBObjectCache<CategoryDB> catCache = new DBObjectCache<>(50);+ private DbCache<DataTypeDB> dtCache;+ private DbCache<SourceArchiveDB> sourceArchiveDBCache;+ private DbCache<CategoryDB> catCache;+ private Map<Long, DataType> builtInMap;+ private Map<DataType, Long> builtIn2IdMap;private SettingsCache<Long> settingsCache = new SettingsCache<>(200);- private List<DataType> sortedDataTypes;+ private List<DataType> lazySortedDataTypes;private Map<Long, Set<String>> enumValueMap;private Map<String, Set<String>> previouslyUsedSettingsValuesMap = new HashMap<>();@@ -154,7 +154,9 @@ abstract public class DataTypeManagerDB implements DataTypeManager {private int creatingDataType = 0;protected UniversalID universalID;- private Map<UniversalID, SourceArchive> sourceArchiveMap;+ // this map is lazy loaded and set to null when invalidated. Made volatile since checking+ // the existence of this variable is not synchronized.+ private volatile Map<UniversalID, SourceArchive> cachedSourceArchiveMap;private LinkedList<Long> idsToDelete = new LinkedList<>();private LinkedList<Pair<DataType, DataType>> typesToReplace = new LinkedList<>();private List<DataType> favoritesList = new ArrayList<>();@@ -403,11 +405,13 @@ abstract public class DataTypeManagerDB implements DataTypeManager {if (checkForSourceArchiveUpdatesNeeded(openMode, monitor)) {doSourceArchiveUpdates(monitor);}- dtCache = new DBObjectCache<>(10);- sourceArchiveDBCache = new DBObjectCache<>(10);- builtInMap = new HashMap<>();- builtIn2IdMap = new HashMap<>();- root = new CategoryDB(this, catCache);+ dtCache = new DbCache<>(new DtFactory(), lock, 10);+ sourceArchiveDBCache = new DbCache<>(new ArchiveFactory(), lock, 10);+ catCache = new DbCache<>(new CategoryFactory(), lock, 10);+ builtInMap = Collections.synchronizedMap(new HashMap<>());+ builtIn2IdMap = Collections.synchronizedMap(new HashMap<>());+ root = new CategoryDB(this, ROOT_CATEGORY_ID, null, "/");+ catCache.add(root);if (parentChildAdapter.needsInitializing()) {initializedParentChildTable();}@@ -571,7 +575,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {* able to instantiate all dataTypes to accomplish this.*/private void initializedParentChildTable() {- buildSortedDataTypeList();+ List<DataType> sortedDataTypes = getSortedDataTypes();for (DataType dt : sortedDataTypes) {if (dt instanceof Array) {((Array) dt).getDataType().addParent(dt);@@ -662,23 +666,18 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}boolean clearSetting(long dataTypeId, String name) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {settingsCache.remove(dataTypeId, name);return settingsAdapter.removeSettingsRecord(dataTypeId, name);}catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return false;}boolean clearAllSettings(long dataTypeId) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {boolean changed = false;Field[] keys = settingsAdapter.getSettingsKeys(dataTypeId);for (Field key : keys) {@@ -694,29 +693,21 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return false;}String[] getSettingsNames(long dataTypeId) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return settingsAdapter.getSettingsNames(dataTypeId);}catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return new String[0];}SettingDB getSetting(long dataTypeId, String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {SettingDB setting = settingsCache.get(dataTypeId, name);if (setting != null) {return setting;@@ -731,15 +722,11 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return null;}boolean updateSettingsRecord(long dataTypeId, String name, String strValue, long longValue) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {DBRecord rec =settingsAdapter.updateSettingsRecord(dataTypeId, name, strValue, longValue);if (rec != null) {@@ -759,9 +746,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return false;}@@ -785,8 +769,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {if (!settingsDefinition.supportsSuggestedValues()) {return Settings.EMPTY_STRING_ARRAY;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {Set<String> previouslyUsedSet = previouslyUsedSettingsValuesMap.computeIfAbsent(settingsDefinition.getStorageKey(), n -> generateSuggestions(settingsDefinition));// Last-minute additions are not cached since suggested values may change@@ -797,9 +780,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}return set.toArray(new String[set.size()]);}- finally {- lock.release();- }}/**@@ -983,14 +963,20 @@ abstract public class DataTypeManagerDB implements DataTypeManager {abstract protected String getPath();- private void buildSortedDataTypeList() {- if (sortedDataTypes != null) {- return;+ private List<DataType> getSortedDataTypes() {+ List<DataType> local = lazySortedDataTypes;+ if (local == null) {+ local = doBuildSortedDataTypesList();}+ return local;+ }++ private synchronized List<DataType> doBuildSortedDataTypesList() {List<DataType> list = new ArrayList<>();popuplateDataTypeList(list, root);Collections.sort(list, nameComparator);- sortedDataTypes = list;+ lazySortedDataTypes = list;+ return list;}private void buildEnumValueMap() {@@ -1003,31 +989,40 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}private void removeDataTypeFromSortedList(DataTypePath dataTypePath) {- if (sortedDataTypes == null) {+ // we assume we have the write lock here+ if (!lock.isWriteLockedByCurrentThread()) {+ throw new AssertException("Attempted to modify sorted datatypes without write lock");+ }+ if (lazySortedDataTypes == null) {return;}// find and remove exact match from sortedDataTypes listString name = dataTypePath.getDataTypeName();DataType compareDataType =new TypedefDataType(dataTypePath.getCategoryPath(), name, DataType.DEFAULT, this);- int index = Collections.binarySearch(sortedDataTypes, compareDataType, nameComparator);+ int index = Collections.binarySearch(lazySortedDataTypes, compareDataType, nameComparator);if (index >= 0) {- sortedDataTypes.remove(index);+ lazySortedDataTypes.remove(index);}}private void insertDataTypeIntoSortedList(DataType dataType) {- if (sortedDataTypes == null) {+ // we assume there is a write lock at this point+ // we assume we have the write lock here+ if (!lock.isWriteLockedByCurrentThread()) {+ throw new AssertException("Attempted to modify sorted datatypes without write lock");+ }+ if (lazySortedDataTypes == null) {return;}- int index = Collections.binarySearch(sortedDataTypes, dataType, nameComparator);+ int index = Collections.binarySearch(lazySortedDataTypes, dataType, nameComparator);if (index < 0) {index = -index - 1;- sortedDataTypes.add(index, dataType);+ lazySortedDataTypes.add(index, dataType);}else {// NOTE: ideally, this should never happen and may indicate presence of duplicate- sortedDataTypes.set(index, dataType);+ lazySortedDataTypes.set(index, dataType);}}@@ -1074,24 +1069,14 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic List<DataType> getFavorites() {- lock.acquire();- try {- return new ArrayList<>(favoritesList);- }- finally {- lock.release();- }+ return lock.withRead(() -> new ArrayList<>(favoritesList));}@Overridepublic boolean isFavorite(DataType dataType) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return favoritesList.contains(dataType);}- finally {- lock.release();- }}@Override@@ -1100,8 +1085,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {throw new IllegalArgumentException("Datatype does not belong to this datatype manager.");}- lock.acquire();- try {+ try (Closeable c = lock.write()) {boolean isInFavorites = favoritesList.contains(dataType);if (isInFavorites == isFavorite) {return; // no change@@ -1114,9 +1098,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}favoritesChanged(dataType, isFavorite);}- finally {- lock.release();- }}@Override@@ -1181,31 +1162,14 @@ abstract public class DataTypeManagerDB implements DataTypeManager {return parent.getCategory(path.getName());}- CategoryDB getCategoryDB(long id) throws IOException {- if (id == DataTypeManagerDB.ROOT_CATEGORY_ID) {- return root;- }- CategoryDB cat = catCache.get(id);- if (cat == null) {- DBRecord rec = categoryAdapter.getRecord(id);- if (rec != null) {- long parentID = rec.getLongValue(CategoryDBAdapter.CATEGORY_PARENT_COL);- CategoryDB parent = getCategoryDB(parentID);- String name = rec.getString(CategoryDBAdapter.CATEGORY_NAME_COL);- cat = new CategoryDB(this, catCache, id, parent, name);- }- }- return cat;- }-CategoryDB createCategoryDB(CategoryDB parent, String categoryName) throws IOException {CategoryDB c = parent.getCategory(categoryName);if (c != null) {return c;}DBRecord rec = categoryAdapter.createCategory(categoryName, parent.getKey());- String name = rec.getString(CategoryDBAdapter.CATEGORY_NAME_COL);- CategoryDB cat = new CategoryDB(this, catCache, rec.getKey(), parent, name);+ CategoryDB cat = new CategoryDB(this, rec.getKey(), parent, categoryName);+ catCache.add(cat);parent.categoryAdded(cat);// must be before the event notification belowcategoryCreated(cat);return cat;@@ -1218,17 +1182,11 @@ abstract public class DataTypeManagerDB implements DataTypeManager {*/@Overridepublic Category getCategory(long id) {- lock.acquire();- try {- return getCategoryDB(id);- }- catch (IOException e) {- dbError(e);- return null;- }- finally {- lock.release();+ if (id == DataTypeManagerDB.ROOT_CATEGORY_ID) {+ return root;}++ return catCache.getCachedInstance(id);}@Override@@ -1248,12 +1206,17 @@ abstract public class DataTypeManagerDB implements DataTypeManager {return resolveBitFieldDataType((BitFieldDataType) dataType, handler);}- lock.acquire();DataTypeConflictHandler originalHandler = null;- boolean isEquivalenceCacheOwner = activateEquivalenceCache();- boolean isResolveCacheOwner = activateResolveCache();+ boolean isResolveCacheOwner = false;+ boolean isEquivalenceCacheOwner = false;DataType resolvedDataType = null;++ // can't use the auto-closable here because most of the finally block need to happen+ // with the lock still in place. Auto-closables happen before the finally block in java+ lock.writeLock().lock();try {+ isEquivalenceCacheOwner = activateEquivalenceCache();+ isResolveCacheOwner = activateResolveCache();originalHandler = currentHandler;if (contains(dataType)) {@@ -1312,8 +1275,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {clearEquivalenceCache();}currentHandler = originalHandler;- lock.release();}+ lock.writeLock().unlock();}return resolvedDataType;}@@ -1791,13 +1754,17 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic void addDataTypes(Collection<DataType> dataTypes, DataTypeConflictHandler handler,TaskMonitor monitor) throws CancelledException {- lock.acquire();- boolean isEquivalenceCacheOwner = activateEquivalenceCache();- boolean isResolveCacheOwner = activateResolveCache();+ boolean isEquivalenceCacheOwner = false;+ boolean isResolveCacheOwner = false;// Note: extended hold time on lock may cause the GUI to become unresponsive. Consider// releasing lock between resolves, although this exposes risk of having active resolve// queue/cache without lock.+ // Note: we can't use auto-closeable lock here because we need to maintain the lock in+ // the finally clause (java auto-closeable's close gets called before the finally block)+ lock.writeLock().lock();try {+ isEquivalenceCacheOwner = activateEquivalenceCache();+ isResolveCacheOwner = activateResolveCache();monitor.setMessage("Adding datatypes...");monitor.setMaximum(dataTypes.size());monitor.setProgress(0);@@ -1818,9 +1785,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {if (isEquivalenceCacheOwner) {clearEquivalenceCache();}- lock.release();+ lock.writeLock().unlock();}-}@Override@@ -1828,14 +1794,13 @@ abstract public class DataTypeManagerDB implements DataTypeManager {if (sourceArchive == null) {return null;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {SourceArchive existingArchive = getSourceArchive(sourceArchive.getSourceArchiveID());if (existingArchive != null) {return existingArchive; // already have it}DBRecord record = sourceArchiveAdapter.createRecord(sourceArchive);- SourceArchive newSourceArchive = getSourceArchiveDB(record);+ SourceArchive newSourceArchive = sourceArchiveDBCache.getCachedInstance(record);invalidateSourceArchiveCache();sourceArchiveAdded(newSourceArchive.getSourceArchiveID());return newSourceArchive;@@ -1843,16 +1808,12 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic void removeSourceArchive(SourceArchive sourceArchive) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {UniversalID sourceArchiveID = sourceArchive.getSourceArchiveID();if (sourceArchiveID.equals(universalID) ||sourceArchiveID.equals(LOCAL_ARCHIVE_UNIVERSAL_ID)) {@@ -1867,9 +1828,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }}private void disassociateAllDataTypes(UniversalID sourceArchiveID) {@@ -1888,8 +1846,9 @@ abstract public class DataTypeManagerDB implements DataTypeManager {public DataType replaceDataType(DataType existingDt, DataType replacementDt,boolean updateCategoryPath)throws DataTypeDependencyException, IllegalArgumentException {- lock.acquire();- try {++ try (Closeable c = lock.write()) {+if (!contains(existingDt)) {throw new IllegalArgumentException("Datatype to replace is not contained in this datatype manager.");@@ -1946,9 +1905,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}return replacementDt;}- finally {- lock.release();- }}private void replace(DataType existingDt, DataType replacementDt)@@ -2033,7 +1989,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {// must be modified. Use of the sortedDataTypes list is the simplest way to do this.// A copy of the list must be used since it will changed if other types get removed// in the process.- buildSortedDataTypeList();+ List<DataType> sortedDataTypes = getSortedDataTypes();List<DataType> sortedDataTypesCopy = new ArrayList<>(sortedDataTypes);for (DataType dt : sortedDataTypesCopy) {dt.dataTypeReplaced(existingDt, newDt);@@ -2103,9 +2059,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}// ignore .conflict in both name and result matches- lock.acquire();- try {- buildSortedDataTypeList();+ try (Closeable c = lock.read()) {+ List<DataType> sortedDataTypes = getSortedDataTypes();// Use exemplar datatype in root category without .conflict to position at start// of possible matchesString baseName = DataTypeUtilities.getNameWithoutConflict(name);@@ -2129,9 +2084,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {++index;}}- finally {- lock.release();- }}@Override@@ -2148,9 +2100,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {monitor = TaskMonitor.dummyIfNull(monitor);Pattern regexp = UserSearchUtils.createSearchPattern(name, caseSensitive);- lock.acquire();- try {- buildSortedDataTypeList();+ try (Closeable c = lock.read()) {+ List<DataType> sortedDataTypes = getSortedDataTypes();for (DataType dt : sortedDataTypes) {if (monitor.isCancelled()) {return;@@ -2161,9 +2112,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}}}- finally {- lock.release();- }}@Override@@ -2287,7 +2235,14 @@ abstract public class DataTypeManagerDB implements DataTypeManager {if (dataTypeID == BAD_DATATYPE_ID) {return BadDataType.dataType;}- return getDataType(dataTypeID, null);+ int tableID = getTableID(dataTypeID);+ if (tableID == BUILT_IN) {+ return getBuiltInDataType(dataTypeID, null);+ }+ if (tableID == BITFIELD) {+ return BitFieldDBDataType.getBitFieldDataType(dataTypeID, this);+ }+ return dtCache.getCachedInstance(dataTypeID);}@Override@@ -2427,20 +2382,15 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic boolean remove(DataType dataType) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {return removeInternal(dataType);}- finally {- lock.release();- }}@Overridepublic void remove(List<DataType> dataTypes, TaskMonitor monitor) throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {// perform the delete in chunks so an excessively large list will still be cancellableint n = dataTypes.size();@@ -2457,9 +2407,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {chunkEnd = n < nextChunk ? n : nextChunk;}}- finally {- lock.release();- }}@Override@@ -2487,8 +2434,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic void disassociate(DataType dataType) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {UniversalID oldDtID = dataType.getUniversalID();SourceArchive sourceArchive = dataType.getSourceArchive();sourceArchive = resolveSourceArchive(sourceArchive);@@ -2517,9 +2463,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {dataTypeChanged(dataType, false);}- finally {- lock.release();- }}private Collection<DataType> filterOutNonSourceSettableDataTypes(@@ -2591,7 +2534,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {// must be modified. Use of the sortedDataTypes list is the simplest way to do this.// A copy of the list must be used since it will changed if other types get removed// in the process.- buildSortedDataTypeList();+ List<DataType> sortedDataTypes = getSortedDataTypes();List<DataType> sortedDataTypesCopy = new ArrayList<>(sortedDataTypes);for (DataType dt : sortedDataTypesCopy) {dt.dataTypeDeleted(dataType);@@ -2714,7 +2657,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {// otherwise, it probably belongs to this dataTypeManager, but it could a// leftover after an undo. So make sure it really is there.if (dataType instanceof DataTypeDB dtDb) {- return dtCache.get(dtDb.getKey()) == dataType && !dtDb.isDeleted();+ return dtCache.getCachedInstance(dtDb.getKey()) == dataType && !dtDb.isDeleted();}return builtIn2IdMap.containsKey(dataType);}@@ -2726,8 +2669,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic Category createCategory(CategoryPath path) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Category cat = getCategory(path);if (cat != null) {return cat;@@ -2745,9 +2687,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {// get exception herethrow new AssertException("Got invalid name exception here, but should be impossible.");}- finally {- lock.release();- }}@Override@@ -2782,9 +2721,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}List<DataType> getDataTypesInCategory(long categoryID) {- lock.acquire();ArrayList<DataType> list = new ArrayList<>();- try {+ try (Closeable c = lock.read()) {Field[] ids = builtinAdapter.getRecordIdsInCategory(categoryID);getDataTypes(ids, list);@@ -2810,9 +2748,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return list;}@@ -2823,9 +2758,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic int getDataTypeCount(boolean includePointersAndArrays) {- lock.acquire();- try {- buildSortedDataTypeList();+ try (Closeable c = lock.read()) {+ List<DataType> sortedDataTypes = getSortedDataTypes();int count = sortedDataTypes.size();if (includePointersAndArrays) {return count;@@ -2837,9 +2771,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}return count;}- finally {- lock.release();- }}private void getDataTypes(Field[] ids, ArrayList<DataType> list) {@@ -2856,43 +2787,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {return (int) (dataID >> DATA_TYPE_KIND_SHIFT);}- private DataType getDataType(long dataTypeID, DBRecord record) {- int tableId = getTableID(dataTypeID);- DataType dt = null;- switch (tableId) {- case BUILT_IN:- dt = getBuiltInDataType(dataTypeID, record);- break;- case COMPOSITE:- dt = getCompositeDataType(dataTypeID, record);- break;- case ARRAY:- dt = getArrayDataType(dataTypeID, record);- break;- case POINTER:- dt = getPointerDataType(dataTypeID, record);- break;- case TYPEDEF:- dt = getTypedefDataType(dataTypeID, record);- break;- case FUNCTION_DEF:- dt = getFunctionDefDataType(dataTypeID, record);- break;- case ENUM:- dt = getEnumDataType(dataTypeID, record);- break;- case BITFIELD:- dt = BitFieldDBDataType.getBitFieldDataType(dataTypeID, this);- break;- default:- return null;- }- return dt;- }-private DataType getBuiltInDataType(long dataTypeID, DBRecord record) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DataType dt = builtInMap.get(dataTypeID);if (dt != null) {return dt;@@ -2906,14 +2802,14 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}long catID = record.getLongValue(BuiltinDBAdapter.BUILT_IN_CAT_COL);- CategoryDB catDB = getCategoryDB(catID);+ CategoryDB catDB = catCache.getCachedInstance(catID);CategoryPath catPath = catDB.getCategoryPath();String classPath = record.getString(BuiltinDBAdapter.BUILT_IN_CLASSNAME_COL);String name = record.getString(BuiltinDBAdapter.BUILT_IN_NAME_COL);try {- Class<?> c;+ Class<?> clazz;try {- c = Class.forName(classPath);+ clazz = Class.forName(classPath);}catch (ClassNotFoundException | NoClassDefFoundError e) {// Check the classNameMap.@@ -2922,14 +2818,15 @@ abstract public class DataTypeManagerDB implements DataTypeManager {throw e;}try {- c = Class.forName(newClassPath);+ clazz = Class.forName(newClassPath);}catch (ClassNotFoundException e1) {throw e1;}}- BuiltInDataType bdt = (BuiltInDataType) c.getDeclaredConstructor().newInstance();+ BuiltInDataType bdt =+ (BuiltInDataType) clazz.getDeclaredConstructor().newInstance();bdt.setName(name);bdt.setCategoryPath(catPath);@@ -2968,9 +2865,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return null;}@@ -2986,155 +2880,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {return def != null && !(def instanceof TypeDefSettingsDefinition);}- private Enum getEnumDataType(long dataTypeID, DBRecord record) {- lock.acquire();- try {- EnumDB enu = (EnumDB) dtCache.get(dataTypeID);- if (enu == null) {- if (record == null) {- record = enumAdapter.getRecord(dataTypeID);- }- if (record != null) {- enu = new EnumDB(this, dtCache, enumAdapter, enumValueAdapter, record);- }- }- return enu;- }- catch (IOException e) {- errHandler.dbError(e);- }- finally {- lock.release();- }- return null;- }-- private Composite getCompositeDataType(long dataTypeID, DBRecord record) {- lock.acquire();- try {- CompositeDB comp = (CompositeDB) dtCache.get(dataTypeID);- if (comp == null) {- if (record == null) {- record = compositeAdapter.getRecord(dataTypeID);- }- if (record != null) {- if (record.getBooleanValue(CompositeDBAdapter.COMPOSITE_IS_UNION_COL)) {- comp =- new UnionDB(this, dtCache, compositeAdapter, componentAdapter, record);- }- else {- comp = new StructureDB(this, dtCache, compositeAdapter, componentAdapter,- record);- }- }- }- return comp;- }- catch (IOException e) {- errHandler.dbError(e);- }- finally {- lock.release();- }- return null;- }-- private TypeDef getTypedefDataType(long dataTypeID, DBRecord record) {- lock.acquire();- try {- TypedefDB typeDB = (TypedefDB) dtCache.get(dataTypeID);- if (typeDB == null) {- if (record == null) {- record = typedefAdapter.getRecord(dataTypeID);- }- if (record != null) {- typeDB = new TypedefDB(this, dtCache, typedefAdapter, record);- }- }- return typeDB;- }- catch (IOException e) {- errHandler.dbError(e);- }- finally {- lock.release();- }- return null;- }-- private Array getArrayDataType(long dataTypeID, DBRecord record) {- lock.acquire();- try {- ArrayDB arrayDB = (ArrayDB) dtCache.get(dataTypeID);- if (arrayDB == null) {- if (record == null) {- record = arrayAdapter.getRecord(dataTypeID);- }- if (record != null) {- arrayDB = new ArrayDB(this, dtCache, arrayAdapter, record);- }- }- return arrayDB;- }- catch (IOException e) {- errHandler.dbError(e);-- }- finally {- lock.release();- }- return null;- }-- private Pointer getPointerDataType(long dataTypeID, DBRecord record) {- lock.acquire();- try {- PointerDB ptrDB = (PointerDB) dtCache.get(dataTypeID);- if (ptrDB == null) {- if (record == null) {- record = pointerAdapter.getRecord(dataTypeID);- }- if (record != null) {- ptrDB = new PointerDB(this, dtCache, pointerAdapter, record);- }- }- return ptrDB;- }- catch (IOException e) {- errHandler.dbError(e);-- }- finally {- lock.release();- }- return null;- }-- private FunctionDefinition getFunctionDefDataType(long dataTypeID, DBRecord record) {- lock.acquire();- try {- FunctionDefinitionDB funDef = (FunctionDefinitionDB) dtCache.get(dataTypeID);- if (funDef == null) {- if (record == null) {- record = functionDefAdapter.getRecord(dataTypeID);- }- if (record != null) {- funDef = new FunctionDefinitionDB(this, dtCache, functionDefAdapter,- paramAdapter, record);- }- }- return funDef;- }- catch (IOException e) {- errHandler.dbError(e);-- }- finally {- lock.release();- }- return null;- }-private DataType createDataType(DataType dt, String name, SourceArchive sourceArchive,DataTypeConflictHandler handler) {@@ -3219,7 +2964,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {struct.getStoredMinimumAlignment());StructureDB structDB =- new StructureDB(this, dtCache, compositeAdapter, componentAdapter, record);+ new StructureDB(this, compositeAdapter, componentAdapter, record);+ dtCache.add(structDB);// Make sure category knows about structure before replace is performedcategory.dataTypeAdded(structDB);@@ -3263,7 +3009,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}DBRecord record = typedefAdapter.createRecord(getID(dataType), name, flags, cat.getID(),sourceArchiveIdValue, universalIdValue, typedef.getLastChangeTime());- TypedefDB typedefDB = new TypedefDB(this, dtCache, typedefAdapter, record);+ TypedefDB typedefDB = new TypedefDB(this, typedefAdapter, record);+ dtCache.add(typedefDB);// Copy TypeDef settings from originalDataTypeSettingsDB settings = (DataTypeSettingsDB) typedefDB.getDefaultSettings();@@ -3287,8 +3034,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {DBRecord record = compositeAdapter.createRecord(name, null, true, category.getID(), 0,-1, sourceArchiveIdValue, universalIdValue, union.getLastChangeTime(),union.getStoredPackingValue(), union.getStoredMinimumAlignment());- UnionDB unionDB =- new UnionDB(this, dtCache, compositeAdapter, componentAdapter, record);+ UnionDB unionDB = new UnionDB(this, compositeAdapter, componentAdapter, record);+ dtCache.add(unionDB);// Make sure category knows about union before replace is performedcategory.dataTypeAdded(unionDB);@@ -3323,7 +3070,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {enumValueAdapter.createRecord(enumID, enumName, enumm.getValue(enumName),enumm.getComment(enumName));}- EnumDB enumDB = new EnumDB(this, dtCache, enumAdapter, enumValueAdapter, record);+ EnumDB enumDB = new EnumDB(this, enumAdapter, enumValueAdapter, record);+ dtCache.add(enumDB);return enumDB;}@@ -3336,7 +3084,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {long dataTypeID = getResolvedID(dt);DBRecord record = pointerAdapter.createRecord(dataTypeID, cat.getID(), length);- PointerDB ptrDB = new PointerDB(this, dtCache, pointerAdapter, record);+ PointerDB ptrDB = new PointerDB(this, pointerAdapter, record);+ dtCache.add(ptrDB);if (dt != null) {dt.addParent(ptrDB);}@@ -3357,7 +3106,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {DBRecord record =arrayAdapter.createRecord(dataTypeID, numElements, elementLength, cat.getID());addParentChildRecord(record.getKey(), dataTypeID);- ArrayDB arrayDB = new ArrayDB(this, dtCache, arrayAdapter, record);+ ArrayDB arrayDB = new ArrayDB(this, arrayAdapter, record);+ dtCache.add(arrayDB);dt.addParent(arrayDB);return arrayDB;}@@ -3432,46 +3182,43 @@ abstract public class DataTypeManagerDB implements DataTypeManager {return getSourceArchive(getUniversalID());}- private synchronized SourceArchive getSourceArchiveFromCache(UniversalID sourceID) {- populateSourceArchiveCache();+ private SourceArchive getSourceArchiveFromCache(UniversalID sourceID) {+ Map<UniversalID, SourceArchive> sourceArchiveMap = getSourceArchiveMap();return sourceArchiveMap.get(sourceID);}- private synchronized void invalidateSourceArchiveCache() {- sourceArchiveMap = null;+ private void invalidateSourceArchiveCache() {+ cachedSourceArchiveMap = null;}- private synchronized Collection<SourceArchive> getSourceArchivesFromCache() {- populateSourceArchiveCache();+ private Collection<SourceArchive> getSourceArchivesFromCache() {+ Map<UniversalID, SourceArchive> sourceArchiveMap = getSourceArchiveMap();return new ArrayList<>(sourceArchiveMap.values());}- private synchronized void populateSourceArchiveCache() {- if (sourceArchiveMap != null) {- return;+ private Map<UniversalID, SourceArchive> getSourceArchiveMap() {+ Map<UniversalID, SourceArchive> map = cachedSourceArchiveMap;+ if (map == null) {+ map = buildSourceArchiveMap();+ cachedSourceArchiveMap = map;}+ return map;+ }++ private Map<UniversalID, SourceArchive> buildSourceArchiveMap() {Map<UniversalID, SourceArchive> archiveMap = new HashMap<>();archiveMap.put(BUILT_IN_ARCHIVE_UNIVERSAL_ID, BuiltInSourceArchive.INSTANCE);try {List<DBRecord> records = sourceArchiveAdapter.getRecords();for (DBRecord record : records) {- SourceArchive sourceArchive = getSourceArchiveDB(record);+ SourceArchive sourceArchive = sourceArchiveDBCache.getCachedInstance(record);archiveMap.put(sourceArchive.getSourceArchiveID(), sourceArchive);}-}catch (IOException e) {dbError(e);}- sourceArchiveMap = archiveMap;- }-- private SourceArchiveDB getSourceArchiveDB(DBRecord record) {- SourceArchiveDB archive = sourceArchiveDBCache.get(record.getKey());- if (archive == null) {- archive = new SourceArchiveDB(this, sourceArchiveDBCache, sourceArchiveAdapter, record);- }- return archive;+ return Collections.unmodifiableMap(archiveMap);}@Override@@ -3536,7 +3283,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {callingConventionId, sourceArchiveIdValue, universalIdValue,funDef.getLastChangeTime());FunctionDefinitionDB funDefDb =- new FunctionDefinitionDB(this, dtCache, functionDefAdapter, paramAdapter, record);+ new FunctionDefinitionDB(this, functionDefAdapter, paramAdapter, record);+ dtCache.add(funDefDb);// Make sure category knows about function definition before args/return resolvedcat.dataTypeAdded(funDefDb);@@ -3592,7 +3340,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {try {while (it.hasNext()) {DBRecord rec = it.next();- DataType dt = getDataType(rec.getKey(), rec);+ DataType dt = dtCache.getCachedInstance(rec);if (predicate.test(dt)) {nextDataType = (T) dt;return;@@ -3631,14 +3379,12 @@ abstract public class DataTypeManagerDB implements DataTypeManager {if (!(dt instanceof Array) && !(dt instanceof Pointer)) {for (Field arrayId : arrayAdapter.getRecordIdsInCategory(oldCatId)) {long id = arrayId.getLongValue();- DBRecord rec = arrayAdapter.getRecord(id);- ArrayDB array = (ArrayDB) getDataType(id, rec);+ ArrayDB array = (ArrayDB) dtCache.getCachedInstance(id);array.updatePath(dt);}for (Field ptrId : pointerAdapter.getRecordIdsInCategory(oldCatId)) {long id = ptrId.getLongValue();- DBRecord rec = pointerAdapter.getRecord(id);- PointerDB ptr = (PointerDB) getDataType(id, rec);+ PointerDB ptr = (PointerDB) dtCache.getCachedInstance(id);ptr.updatePath(dt);}}@@ -3646,8 +3392,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {// only affects those with auto-naming which must follow category changefor (Field ptrId : typedefAdapter.getRecordIdsInCategory(oldCatId)) {long id = ptrId.getLongValue();- DBRecord rec = typedefAdapter.getRecord(id);- TypedefDB td = (TypedefDB) getDataType(id, rec);+ TypedefDB td = (TypedefDB) dtCache.getCachedInstance(id);td.updatePath(dt);}}@@ -3660,26 +3405,18 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic Iterator<DataType> getAllDataTypes() {- lock.acquire();- try {- buildSortedDataTypeList();+ try (Closeable c = lock.read()) {+ List<DataType> sortedDataTypes = getSortedDataTypes();return new ArrayList<>(sortedDataTypes).iterator();}- finally {- lock.release();- }}@Overridepublic void getAllDataTypes(List<DataType> list) {- lock.acquire();- try {- buildSortedDataTypeList();+ try (Closeable c = lock.read()) {+ List<DataType> sortedDataTypes = getSortedDataTypes();list.addAll(sortedDataTypes);}- finally {- lock.release();- }}@Override@@ -3718,7 +3455,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}public void dispose() {- sortedDataTypes = null;+ lazySortedDataTypes = null;enumValueMap = null;}@@ -3731,8 +3468,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {* Invalidates the cache.*/public void invalidateCache() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {callingConventionAdapter.invalidateCache();knownCallingConventions = null;definedCallingConventions = null;@@ -3745,15 +3481,12 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catCache.invalidate();settingsCache.clear();settingsAdapter.invalidateNameCache();- sortedDataTypes = null;+ lazySortedDataTypes = null;enumValueMap = null;fireInvalidated();updateFavorites();idsToDataTypeMap.clear();}- finally {- lock.release();- }}private void updateFavorites() {@@ -3852,8 +3585,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}List<DataType> getParentDataTypes(long dataTypeId) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Set<Long> parentIds = parentChildAdapter.getParentIds(dataTypeId);// NOTE: Use of Set for containing datatypes is avoided due to the excessive// overhead of DataType.equals@@ -3873,9 +3605,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return null;}@@ -3888,8 +3617,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {*/@DeprecatedSet<DataType> getParentDataTypeSet(long dataTypeId) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Set<Long> parentIds = parentChildAdapter.getParentIds(dataTypeId);Set<DataType> dts = new HashSet<>();for (long id : parentIds) {@@ -3907,9 +3635,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return null;}@@ -3925,15 +3650,11 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}private void attemptRecordRemovalForParent(long parentKey) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (dbHandle.isTransactionActive()) {parentChildAdapter.removeAllRecordsForParent(parentKey);}}- finally {- lock.release();- }}@Override@@ -4043,8 +3764,13 @@ abstract public class DataTypeManagerDB implements DataTypeManager {protected void dataTypeNameChanged(DataType dt, String oldName) {CategoryDB category = (CategoryDB) getCategory(dt.getCategoryPath());category.dataTypeRenamed(dt, oldName);- if (sortedDataTypes != null) {- Collections.sort(sortedDataTypes, nameComparator);++ // we assume we have the write lock here+ if (!lock.isWriteLockedByCurrentThread()) {+ throw new AssertException("Attempted to modify sorted datatypes without write lock");+ }+ if (lazySortedDataTypes != null) {+ Collections.sort(lazySortedDataTypes, nameComparator);}updateLastChangeTime();setDirtyFlag(dt);@@ -4105,13 +3831,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic DataType getDataType(SourceArchive sourceArchive, UniversalID datatypeID) {UniversalID sourceID = sourceArchive == null ? null : sourceArchive.getSourceArchiveID();- lock.acquire();- try {- return idsToDataTypeMap.getDataType(sourceID, datatypeID);- }- finally {- lock.release();- }+ return lock.withRead(() -> idsToDataTypeMap.getDataType(sourceID, datatypeID));}@Override@@ -4133,9 +3853,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}private DataType findDataTypeForIDs(UniversalID sourceID, UniversalID datatypeID) {- lock.acquire();DBRecord record = null;- try {+ try (Closeable c = lock.read()) {record = typedefAdapter.getRecordWithIDs(sourceID, datatypeID);if (record == null) {record = compositeAdapter.getRecordWithIDs(sourceID, datatypeID);@@ -4150,11 +3869,8 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }if (record != null) {- return getDataType(record.getKey(), record);+ return dtCache.getCachedInstance(record);}return null;}@@ -4196,8 +3912,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {else if (id == UNKNOWN_CALLING_CONVENTION_ID) {return CompilerSpec.CALLING_CONVENTION_unknown;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {String callingConvention = callingConventionAdapter.getCallingConventionName(id);return callingConvention != null ? callingConvention: CompilerSpec.CALLING_CONVENTION_unknown;@@ -4205,9 +3920,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return null;}@@ -4236,8 +3948,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {return DEFAULT_CALLING_CONVENTION_ID;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {// If restrictive only permit a name which is knownif (restrictive &&GenericCallingConvention@@ -4253,9 +3964,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return UNKNOWN_CALLING_CONVENTION_ID;}@@ -4298,13 +4006,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic Collection<String> getDefinedCallingConventionNames() {- lock.acquire();- try {- return new ArrayList<>(getDefinedCallingConventionSet());- }- finally {- lock.release();- }+ return lock.withRead(() -> new ArrayList<>(getDefinedCallingConventionSet()));}private Set<String> getKnownCallingConventionSet() {@@ -4334,13 +4036,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {@Overridepublic Collection<String> getKnownCallingConventionNames() {- lock.acquire();- try {- return new ArrayList<>(getKnownCallingConventionSet());- }- finally {- lock.release();- }+ return lock.withRead(() -> new ArrayList<>(getKnownCallingConventionSet()));}@Override@@ -4401,7 +4097,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {if (!rec.getBooleanValue(CompositeDBAdapter.COMPOSITE_IS_UNION_COL)) {// StructureDB instantiation will perform an automatic flex-array// record migration if needed.- new StructureDB(this, dtCache, compositeAdapter, componentAdapter, rec);+ new StructureDB(this, compositeAdapter, componentAdapter, rec);}}@@ -4440,7 +4136,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}/**- * Fixup all composites and thier components which may be affected by a data organization+ * Fixup all composites and their components which may be affected by a data organization* change include primitive type size changes and alignment changes. It is highly recommended* that this program be open with exclusive access before invoking this method to avoid* excessive merge conflicts with other users.@@ -4449,16 +4145,12 @@ abstract public class DataTypeManagerDB implements DataTypeManager {* updated compiler specification*/public void fixupComposites(TaskMonitor monitor) throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {doCompositeFixup(monitor);}catch (IOException e) {dbError(e);}- finally {- lock.release();- }}private void doCompositeFixup(TaskMonitor monitor) throws CancelledException, IOException {@@ -4557,8 +4249,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {return false;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (dataType instanceof Pointer || dataType instanceof Array) {dataType = DataTypeUtilities.getBaseDataType(dataType);}@@ -4591,9 +4282,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {}return success;}- finally {- lock.release();- }}private record DedupedConflicts(int processCnt, int replaceCnt) {}@@ -4636,8 +4324,7 @@ abstract public class DataTypeManagerDB implements DataTypeManager {* @throws CancelledException if task is cancelled*/public void dedupeAllConflicts(TaskMonitor monitor) throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {List<DataType> conflictList = new ArrayList<>();int total = popuplateConflictList(conflictList, getRootCategory());monitor.initialize(total);@@ -4653,9 +4340,6 @@ abstract public class DataTypeManagerDB implements DataTypeManager {Msg.info(this, "Evaluated " + processed + " conflict types, replaced " + replacements +" base type conflicts");}- finally {- lock.release();- }}private int popuplateConflictList(List<DataType> conflictList, Category category) {@@ -4911,6 +4595,10 @@ abstract public class DataTypeManagerDB implements DataTypeManager {cache.setValue(key);}+ CategoryDB getCategoryDB(long id) {+ return catCache.getCachedInstance(id);+ }+/*** Cache the result of {@link DataTypeDB#isEquivalent(DataType)} for select* implementations (e.g., {@link StructureDB}, {@link UnionDB}, and@@ -4924,7 +4612,11 @@ abstract public class DataTypeManagerDB implements DataTypeManager {* @param isEquivalent true or false result from* {@link DataTypeDB#isEquivalent(DataType)}.*/- void putCachedEquivalence(DataTypeDB dataTypeDB, DataType dataType, boolean isEquivalent) {+ void putCachedEquivalence(DataTypeDB dataTypeDB, DataType dataType, Boolean isEquivalent) {+ if (isEquivalent == null) {+ Msg.error(this, "IsEquivalent = null");+ return;+ }EquivalenceCache cache = equivalenceCache.get();if (cache == null) {throw new IllegalStateException("equivalence cache not active - unexpected condition");@@ -5104,24 +4796,177 @@ abstract public class DataTypeManagerDB implements DataTypeManager {* @return total number of datatype records.*/int getDataTypeRecordCount() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return builtinAdapter.getRecordCount() + compositeAdapter.getRecordCount() +arrayAdapter.getRecordCount() + pointerAdapter.getRecordCount() +typedefAdapter.getRecordCount() + functionDefAdapter.getRecordCount() +enumAdapter.getRecordCount();}- finally {- lock.release();+ }++ private class CategoryFactory implements DbFactory<CategoryDB> {++ @Override+ public CategoryDB instantiate(long dataTypeId) {+ if (dataTypeId == DataTypeManagerDB.ROOT_CATEGORY_ID) {+ return root;+ }+ try {+ DBRecord record = categoryAdapter.getRecord(dataTypeId);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public CategoryDB instantiate(DBRecord rec) {+ long parentID = rec.getLongValue(CategoryDBAdapter.CATEGORY_PARENT_COL);+ CategoryDB parent = catCache.getCachedInstance(parentID);+ String name = rec.getString(CategoryDBAdapter.CATEGORY_NAME_COL);+ return new CategoryDB(DataTypeManagerDB.this, rec.getKey(), parent, name);}}-}+ private class ArchiveFactory implements DbFactory<SourceArchiveDB> {+ @Override+ public SourceArchiveDB instantiate(long key) {+ try {+ DBRecord record = sourceArchiveAdapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }-class CategoryCache extends FixedSizeHashMap<String, Category> {- private static final int CACHE_SIZE = 100;+ @Override+ public SourceArchiveDB instantiate(DBRecord record) {+ return new SourceArchiveDB(DataTypeManagerDB.this, sourceArchiveAdapter, record);+ }- CategoryCache() {- super(CACHE_SIZE, CACHE_SIZE);}++ private class DtFactory implements DbFactory<DataTypeDB> {++ @Override+ public DataTypeDB instantiate(long key) {+ return doInstantiate(key, null);+ }++ @Override+ public DataTypeDB instantiate(DBRecord record) {+ if (record == null) {+ return null;+ }+ return doInstantiate(record.getKey(), record);+ }++ private DataTypeDB doInstantiate(long dataTypeID, DBRecord rec) {+ try {+ int tableId = getTableID(dataTypeID);+ switch (tableId) {+ case COMPOSITE:+ return instantiateCompositeDataType(dataTypeID, rec);+ case ARRAY:+ return instantiateArrayDataType(dataTypeID, rec);+ case POINTER:+ return instantiatePointerDataType(dataTypeID, rec);+ case TYPEDEF:+ return instantiateTypedefDataType(dataTypeID, rec);+ case FUNCTION_DEF:+ return instantiateFunctionDefDataType(dataTypeID, rec);+ case ENUM:+ return instantiateEnumDataType(dataTypeID, rec);+ case BUILT_IN:+ case BITFIELD:+ // these types are not DB objects and are not cached+ default:+ return null;+ }+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ private CompositeDB instantiateCompositeDataType(long dataTypeID, DBRecord record)+ throws IOException {++ if (record == null) {+ record = compositeAdapter.getRecord(dataTypeID);+ }+ if (record == null) {+ return null;+ }+ if (record.getBooleanValue(CompositeDBAdapter.COMPOSITE_IS_UNION_COL)) {+ return new UnionDB(DataTypeManagerDB.this, compositeAdapter, componentAdapter,+ record);+ }+ return new StructureDB(DataTypeManagerDB.this, compositeAdapter, componentAdapter,+ record);+ }++ private ArrayDB instantiateArrayDataType(long dataTypeID, DBRecord record)+ throws IOException {+ if (record == null) {+ record = arrayAdapter.getRecord(dataTypeID);+ }+ if (record == null) {+ return null;+ }+ return new ArrayDB(DataTypeManagerDB.this, arrayAdapter, record);+ }++ private PointerDB instantiatePointerDataType(long dataTypeID, DBRecord record)+ throws IOException {+ if (record == null) {+ record = pointerAdapter.getRecord(dataTypeID);+ }+ if (record == null) {+ return null;+ }+ return new PointerDB(DataTypeManagerDB.this, pointerAdapter, record);+ }++ private TypedefDB instantiateTypedefDataType(long dataTypeID, DBRecord record)+ throws IOException {+ if (record == null) {+ record = typedefAdapter.getRecord(dataTypeID);+ }+ if (record == null) {+ return null;+ }+ return new TypedefDB(DataTypeManagerDB.this, typedefAdapter, record);+ }++ private FunctionDefinitionDB instantiateFunctionDefDataType(long dataTypeID,+ DBRecord record)+ throws IOException {+ if (record == null) {+ record = functionDefAdapter.getRecord(dataTypeID);+ }+ if (record == null) {+ return null;+ }+ return new FunctionDefinitionDB(DataTypeManagerDB.this, functionDefAdapter,+ paramAdapter, record);+ }++ private EnumDB instantiateEnumDataType(long dataTypeID, DBRecord record)+ throws IOException {+ if (record == null) {+ record = enumAdapter.getRecord(dataTypeID);+ }+ if (record == null) {+ return null;+ }+ return new EnumDB(DataTypeManagerDB.this, enumAdapter, enumValueAdapter, record);+ }+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/EnumDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/EnumDB.javaindex cac431deb7..73ad2f3bb3 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/EnumDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/EnumDB.java@@ -19,7 +19,8 @@ import static ghidra.program.database.data.EnumSignedState.*;import java.io.IOException;import java.math.BigInteger;-import java.util.*;+import java.util.List;+import java.util.NoSuchElementException;import org.apache.commons.lang3.StringUtils;@@ -27,13 +28,13 @@ import db.DBRecord;import db.Field;import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsDefinition;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.data.*;import ghidra.program.model.data.DataTypeConflictHandler.ConflictResult;import ghidra.program.model.data.Enum;import ghidra.program.model.mem.MemBuffer;import ghidra.program.model.mem.MemoryAccessException;import ghidra.program.model.scalar.Scalar;+import ghidra.util.Lock.Closeable;import ghidra.util.UniversalID;/**@@ -45,17 +46,19 @@ class EnumDB extends DataTypeDB implements Enum {private EnumDBAdapter adapter;private EnumValueDBAdapter valueAdapter;-- // Lazy fields whose initialization is triggered by nameMap=null (see initializeIfNeeded)- private Map<String, Long> nameMap; // lazy initialization, name to value- private SortedMap<Long, List<String>> valueMap; // lazy initialization, value to names- private Map<String, String> commentMap; // lazy initialization, name to comment+ private EnumValues lazyEnumValues;private List<BitGroup> bitGroups; // lazy initialization- private EnumSignedState signedState = null; // lazy initialization- EnumDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache, EnumDBAdapter adapter,+ /**+ * Constructor+ * @param dataMgr the datatypes manager+ * @param adapter the enum database adapter+ * @param valueAdapter the enum values database adapter+ * @param record the enum record+ */+ EnumDB(DataTypeManagerDB dataMgr, EnumDBAdapter adapter,EnumValueDBAdapter valueAdapter, DBRecord record) {- super(dataMgr, cache, record);+ super(dataMgr, record);this.adapter = adapter;this.valueAdapter = valueAdapter;}@@ -75,136 +78,45 @@ class EnumDB extends DataTypeDB implements Enum {return ENUM_SETTINGS_DEFINITIONS;}- private void initializeIfNeeded() {- if (nameMap != null) {- return;- }- try {- initialize();- }- catch (IOException e) {- dataMgr.dbError(e);- }- }-- private void initialize() throws IOException {- bitGroups = null;- nameMap = new HashMap<>();- valueMap = new TreeMap<>();- commentMap = new HashMap<>();-- Field[] ids = valueAdapter.getValueIdsInEnum(key);- for (Field id : ids) {- DBRecord rec = valueAdapter.getRecord(id.getLongValue());- String valueName = rec.getString(EnumValueDBAdapter.ENUMVAL_NAME_COL);- long value = rec.getLongValue(EnumValueDBAdapter.ENUMVAL_VALUE_COL);- String comment = rec.getString(EnumValueDBAdapter.ENUMVAL_COMMENT_COL);- addToCache(valueName, value, comment);- }- signedState = computeSignedness();- }-- private EnumSignedState computeSignedness() {- int length = record.getByteValue(EnumDBAdapter.ENUM_SIZE_COL);-- if (valueMap.isEmpty()) {- return NONE;- }-- long minValue = valueMap.firstKey();- long maxValue = valueMap.lastKey();-- if (maxValue > getMaxPossibleValue(length, true)) {- if (minValue < 0) {- return INVALID;+ private EnumValues getEnumValues() {+ EnumValues local = lazyEnumValues;+ if (local == null) {+ try {+ local = new EnumValues(this, valueAdapter);+ lazyEnumValues = local;}- return UNSIGNED;- }-- if (minValue < 0) {- return SIGNED;- }-- return NONE; // we have no negatives and no large unsigned values- }-- private void addToCache(String valueName, long value, String comment) {- nameMap.put(valueName, value);- List<String> list = valueMap.computeIfAbsent(value, v -> new ArrayList<>());- list.add(valueName);- if (!StringUtils.isBlank(comment)) {- commentMap.put(valueName, comment);- }- }-- private boolean removeFromCache(String valueName) {- Long value = nameMap.remove(valueName);- if (value == null) {- return false;- }- List<String> list = valueMap.get(value);- Iterator<String> iter = list.iterator();- while (iter.hasNext()) {- if (valueName.equals(iter.next())) {- iter.remove();- break;+ catch (IOException e) {+ dataMgr.dbError(e);}}- if (list.isEmpty()) {- valueMap.remove(value);- }- commentMap.remove(valueName);- return true;+ return local;+}@Overridepublic long getValue(String valueName) throws NoSuchElementException {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- Long value = nameMap.get(valueName);- if (value == null) {- throw new NoSuchElementException("No value for " + valueName);- }- return value;- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getValue(valueName);}}@Overridepublic String getName(long value) {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- List<String> list = valueMap.get(value);- if (list == null || list.isEmpty()) {- return null;- }- return list.get(0);- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getName(value);}}@Overridepublic String[] getNames(long value) {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- List<String> list = valueMap.get(value);- if (list == null || list.isEmpty()) {- return new String[0];- }- return list.toArray(new String[0]);- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getNames(value);}}@@ -215,65 +127,37 @@ class EnumDB extends DataTypeDB implements Enum {@Overridepublic String getComment(String valueName) {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- String comment = commentMap.get(valueName);- if (comment == null) {- comment = "";- }- return comment;- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getComment(valueName);}}@Overridepublic long[] getValues() {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- return valueMap.keySet().stream().mapToLong(Long::longValue).toArray();- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getValues();}}@Overridepublic String[] getNames() {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();-- // names are first sorted by int value, then sub-sorted by name value- List<String> names = new ArrayList<>();- Collection<List<String>> values = valueMap.values();- for (List<String> list : values) {- Collections.sort(list);- names.addAll(list);- }- return names.toArray(new String[0]);- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getNames();}}@Overridepublic int getCount() {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- return nameMap.size();- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.size();}}@@ -284,32 +168,27 @@ class EnumDB extends DataTypeDB implements Enum {@Overridepublic void add(String valueName, long value, String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();- initializeIfNeeded();- checkValue(value);- if (nameMap.containsKey(valueName)) {+ EnumValues enumValues = getEnumValues();+ if (enumValues.containsName(valueName)) {throw new IllegalArgumentException(valueName + " already exists in this enum");}+ checkValue(value);+ bitGroups = null;if (StringUtils.isBlank(comment)) {comment = null; // use null values in the db to save space}- bitGroups = null;valueAdapter.createRecord(key, valueName, value, comment);adapter.updateRecord(record, true);- addToCache(valueName, value, comment);- signedState = computeSignedness();+ enumValues.addValue(valueName, value, comment);dataMgr.dataTypeChanged(this, false);}catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}private void checkValue(long value) {@@ -329,15 +208,11 @@ class EnumDB extends DataTypeDB implements Enum {@Overridepublic void remove(String valueName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();- initializeIfNeeded();- if (!removeFromCache(valueName)) {- return;- }-+ lazyEnumValues = null;bitGroups = null;+Field[] ids = valueAdapter.getValueIdsInEnum(key);for (Field id : ids) {DBRecord rec = valueAdapter.getRecord(id.getLongValue());@@ -347,15 +222,11 @@ class EnumDB extends DataTypeDB implements Enum {}}adapter.updateRecord(record, true);- signedState = computeSignedness();dataMgr.dataTypeChanged(this, false);}catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -365,15 +236,11 @@ class EnumDB extends DataTypeDB implements Enum {}Enum enumm = (Enum) dataType;- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();bitGroups = null;- nameMap = new HashMap<>();- valueMap = new TreeMap<>();- commentMap = new HashMap<>();-+ lazyEnumValues = null;Field[] ids = valueAdapter.getValueIdsInEnum(key);for (Field id : ids) {valueAdapter.removeRecord(id.getLongValue());@@ -395,9 +262,7 @@ class EnumDB extends DataTypeDB implements Enum {}valueAdapter.createRecord(key, valueName, value, comment);adapter.updateRecord(record, true);- addToCache(valueName, value, comment);}- signedState = computeSignedness();if (oldLength != newLength) {notifySizeChanged(false);}@@ -408,9 +273,6 @@ class EnumDB extends DataTypeDB implements Enum {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -437,26 +299,18 @@ class EnumDB extends DataTypeDB implements Enum {@Overridepublic String getMnemonic(Settings settings) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return getDisplayName();}- finally {- lock.release();- }}@Overridepublic int getLength() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getByteValue(EnumDBAdapter.ENUM_SIZE_COL);}- finally {- lock.release();- }}@Override@@ -466,21 +320,16 @@ class EnumDB extends DataTypeDB implements Enum {@Overridepublic String getDescription() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();String s = record.getString(EnumDBAdapter.ENUM_COMMENT_COL);return s == null ? "" : s;}- finally {- lock.release();- }}@Overridepublic void setDescription(String description) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setString(EnumDBAdapter.ENUM_COMMENT_COL, description);adapter.updateRecord(record, true);@@ -489,16 +338,12 @@ class EnumDB extends DataTypeDB implements Enum {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic Object getValue(MemBuffer buf, Settings settings, int length) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();long value = 0;switch (getLength()) {case 1:@@ -519,9 +364,6 @@ class EnumDB extends DataTypeDB implements Enum {catch (MemoryAccessException e) {return null;}- finally {- lock.release();- }}@Override@@ -531,9 +373,8 @@ class EnumDB extends DataTypeDB implements Enum {@Overridepublic String getRepresentation(MemBuffer buf, Settings settings, int length) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();long value = 0;switch (getLength()) {@@ -555,14 +396,14 @@ class EnumDB extends DataTypeDB implements Enum {catch (MemoryAccessException e) {return "??";}- finally {- lock.release();- }}@Overridepublic String getRepresentation(BigInteger bigInt, Settings settings, int bitLength) {- return getRepresentation(bigInt.longValue());+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ return getRepresentation(bigInt.longValue());+ }}private String getRepresentation(long value) {@@ -664,60 +505,28 @@ class EnumDB extends DataTypeDB implements Enum {@Overridepublic long getMinPossibleValue() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();int length = record.getByteValue(EnumDBAdapter.ENUM_SIZE_COL);- return getMinPossibleValue(length, signedState != UNSIGNED);- }- finally {- lock.release();+ return getMinPossibleValue(length, enumValues.getSignedState() != UNSIGNED);}}@Overridepublic long getMaxPossibleValue() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();int length = record.getByteValue(EnumDBAdapter.ENUM_SIZE_COL);- return getMaxPossibleValue(length, signedState == SIGNED);- }- finally {- lock.release();- }- }-- private long getMaxPossibleValue(int bytes, boolean allowNegativeValues) {- if (bytes == 8) {- return Long.MAX_VALUE;- }- int bits = bytes * 8;- if (allowNegativeValues) {- bits -= 1; // take away 1 bit for the sign- }-- // the largest value that can be held in n bits in 2^n -1- return (1L << bits) - 1;- }-- private long getMinPossibleValue(int bytes, boolean allowNegativeValues) {- if (!allowNegativeValues) {- return 0;+ return getMaxPossibleValue(length, enumValues.getSignedState() == SIGNED);}- int bits = bytes * 8;-- // smallest value (largest negative) that can be stored in n bits is when the sign bit- // is on (and sign extended), and all less significant bits are 0- return -1L << (bits - 1);}@Overrideprotected boolean refresh() {try {- nameMap = null;- valueMap = null;- commentMap = null;+ lazyEnumValues = null;bitGroups = null;DBRecord rec = adapter.getRecord(key);if (rec != null) {@@ -780,8 +589,7 @@ class EnumDB extends DataTypeDB implements Enum {@Overrideprotected void setUniversalID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(EnumDBAdapter.ENUM_UNIVERSAL_DT_ID_COL, id.getValue());adapter.updateRecord(record, false);@@ -790,9 +598,6 @@ class EnumDB extends DataTypeDB implements Enum {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -802,8 +607,7 @@ class EnumDB extends DataTypeDB implements Enum {@Overrideprotected void setSourceArchiveID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(EnumDBAdapter.ENUM_SOURCE_ARCHIVE_ID_COL, id.getValue());adapter.updateRecord(record, false);@@ -812,15 +616,11 @@ class EnumDB extends DataTypeDB implements Enum {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void setLastChangeTime(long lastChangeTime) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(EnumDBAdapter.ENUM_LAST_CHANGE_TIME_COL, lastChangeTime);adapter.updateRecord(record, false);@@ -829,15 +629,11 @@ class EnumDB extends DataTypeDB implements Enum {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void setLastChangeTimeInSourceArchive(long lastChangeTimeInSourceArchive) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(EnumDBAdapter.ENUM_SOURCE_SYNC_TIME_COL,lastChangeTimeInSourceArchive);@@ -847,90 +643,50 @@ class EnumDB extends DataTypeDB implements Enum {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic boolean contains(String name) {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- return nameMap.containsKey(name);- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.containsName(name);}}@Overridepublic boolean contains(long value) {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- return valueMap.containsKey(value);- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.containsValue(value);}}@Overridepublic boolean isSigned() {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- return signedState == SIGNED;- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getSignedState() == SIGNED;}}@Overridepublic EnumSignedState getSignedState() {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- return signedState;- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getSignedState();}}@Overridepublic int getMinimumPossibleLength() {- lock.acquire();- try {- checkIsValid();- initializeIfNeeded();- if (valueMap.isEmpty()) {- return 1;- }- long minValue = valueMap.firstKey();- long maxValue = valueMap.lastKey();- boolean hasNegativeValues = minValue < 0;-- // check the min and max values in this enum to see if they fit in 1 byte enum, then- // 2 byte enum, then 4 byte enum. If the min and max values fit, then all other values- // will fit as well- for (int size = 1; size < 8; size *= 2) {- long minPossible = getMinPossibleValue(size, hasNegativeValues);- long maxPossible = getMaxPossibleValue(size, hasNegativeValues);- if (minValue >= minPossible && maxValue <= maxPossible) {- return size;- }- }- return 8;- }- finally {- lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ EnumValues enumValues = getEnumValues();+ return enumValues.getMinimumPossbileLength();}}@@ -951,4 +707,27 @@ class EnumDB extends DataTypeDB implements Enum {return buf.toString();}+ static long getMaxPossibleValue(int bytes, boolean allowNegativeValues) {+ if (bytes == 8) {+ return Long.MAX_VALUE;+ }+ int bits = bytes * 8;+ if (allowNegativeValues) {+ bits -= 1; // take away 1 bit for the sign+ }++ // the largest value that can be held in n bits in 2^n -1+ return (1L << bits) - 1;+ }++ static long getMinPossibleValue(int bytes, boolean allowNegativeValues) {+ if (!allowNegativeValues) {+ return 0;+ }+ int bits = bytes * 8;++ // smallest value (largest negative) that can be stored in n bits is when the sign bit+ // is on (and sign extended), and all less significant bits are 0+ return -1L << (bits - 1);+ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/EnumValues.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/EnumValues.javanew file mode 100644index 0000000000..1aa2fe3199--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/EnumValues.java@@ -0,0 +1,225 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.database.data;++import static ghidra.program.database.data.EnumSignedState.*;++import java.io.IOException;+import java.util.*;++import org.apache.commons.lang3.StringUtils;++import db.DBRecord;+import db.Field;++/**+ * Immutable class to hold the name/values for an {@link EnumDB} object. It is created lazily by an+ * EnumDB as needed. Any change to the name/values for an enum will cause this class to be nulled+ * out in it enum owner.+ */+class EnumValues {++ private Map<String, Long> nameMap;+ private SortedMap<Long, List<String>> valueMap;+ private Map<String, String> commentMap;+ private EnumSignedState signedState = null;+ private EnumDB enumm;++ EnumValues(EnumDB enumm, EnumValueDBAdapter valueAdapter) throws IOException {+ this.enumm = enumm;+ nameMap = new HashMap<>();+ valueMap = new TreeMap<>();+ commentMap = new HashMap<>();++ Field[] ids = valueAdapter.getValueIdsInEnum(enumm.getKey());+ for (Field id : ids) {+ DBRecord rec = valueAdapter.getRecord(id.getLongValue());+ String valueName = rec.getString(EnumValueDBAdapter.ENUMVAL_NAME_COL);+ long value = rec.getLongValue(EnumValueDBAdapter.ENUMVAL_VALUE_COL);+ String comment = rec.getString(EnumValueDBAdapter.ENUMVAL_COMMENT_COL);+ doAddValue(valueName, value, comment);+ }+ signedState = computeSignedness();+ }++ /**+ * Returns the value for a given name.+ * @param valueName the name to get a value for+ * @return the value for a given name+ * @throws NoSuchElementException if there is no value defined for the given name+ */+ long getValue(String valueName) {+ Long value = nameMap.get(valueName);+ if (value == null) {+ throw new NoSuchElementException("No value for " + valueName);+ }+ return value;+ }++ /**+ * Returns the name for a given value. If there more than one name for a value, the first+ * in alphabetical order is returned.+ * @param value the value for which to find a name+ * @return the first name for a given value, or null if there is not name for that value+ */+ String getName(long value) {+ List<String> list = valueMap.get(value);+ if (list == null || list.isEmpty()) {+ return null;+ }+ return list.get(0);+ }++ /**+ * Returns all the names for a given value.+ * @param value the value for which to find defined names+ * @return all the names for a given value+ */+ String[] getNames(long value) {+ List<String> list = valueMap.get(value);+ if (list == null || list.isEmpty()) {+ return new String[0];+ }+ return list.toArray(new String[0]);+ }++ /**+ * Returns the comment for a given value name.+ * @param valueName the name of the value to get a comment for+ * @return the comment for the given name or null if no comment set+ */+ String getComment(String valueName) {+ String comment = commentMap.get(valueName);+ if (comment == null) {+ comment = "";+ }+ return comment;+ }++ /**+ * {@return an array of all values defined in this enum}+ */+ long[] getValues() {+ return valueMap.keySet().stream().mapToLong(Long::longValue).toArray();+ }++ /**+ * {@return an array of all names defined in this enum.}+ */+ String[] getNames() {+ List<String> names = new ArrayList<>();+ Collection<List<String>> values = valueMap.values();+ for (List<String> list : values) {+ names.addAll(list);+ }+ return names.toArray(new String[0]);+ }++ /**+ * {@return the number of defined names in this enum}+ */+ int size() {+ return nameMap.size();+ }++ /**+ * Returns true if the given name has been defined in this enum.+ * @param valueName the name to check if it has been defined+ * @return true if the given name has been defined in this enum+ */+ boolean containsName(String valueName) {+ return nameMap.containsKey(valueName);+ }++ /**+ * Returns true if the given value has been defined in this enum.+ * @param value the value to check if it has been defined+ * @return true if the given value has been defined in this enum+ */+ boolean containsValue(long value) {+ return valueMap.containsKey(value);+ }++ /**+ * {@return the EnumSignedState for this enum}+ */+ EnumSignedState getSignedState() {+ return signedState;+ }++ /**+ * {@return the minimum size that enum can be and still represent all defined values}+ */+ int getMinimumPossbileLength() {+ if (valueMap.isEmpty()) {+ return 1;+ }+ long minValue = valueMap.firstKey();+ long maxValue = valueMap.lastKey();+ boolean hasNegativeValues = minValue < 0;++ // check the min and max values in this enum to see if they fit in 1 byte enum, then+ // 2 byte enum, then 4 byte enum. If the min and max values fit, then all other values+ // will fit as well+ for (int size = 1; size < 8; size *= 2) {+ long minPossible = EnumDB.getMinPossibleValue(size, hasNegativeValues);+ long maxPossible = EnumDB.getMaxPossibleValue(size, hasNegativeValues);+ if (minValue >= minPossible && maxValue <= maxPossible) {+ return size;+ }+ }+ return 8;+ }++ private EnumSignedState computeSignedness() {++ if (valueMap.isEmpty()) {+ return NONE;+ }++ long minValue = valueMap.firstKey();+ long maxValue = valueMap.lastKey();++ if (maxValue > EnumDB.getMaxPossibleValue(enumm.getLength(), true)) {+ if (minValue < 0) {+ return INVALID;+ }+ return UNSIGNED;+ }++ if (minValue < 0) {+ return SIGNED;+ }++ return NONE; // we have no negatives and no large unsigned values+ }++ void addValue(String valueName, long value, String comment) {+ doAddValue(valueName, value, comment);+ signedState = computeSignedness();+ }++ private void doAddValue(String valueName, long value, String comment) {+ nameMap.put(valueName, value);+ List<String> list = valueMap.computeIfAbsent(value, v -> new ArrayList<>());+ list.add(valueName);+ Collections.sort(list);+ if (!StringUtils.isBlank(comment)) {+ commentMap.put(valueName, comment);+ }+ }++}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/FunctionDefinitionDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/FunctionDefinitionDB.javaindex 4c33bd2a80..7aa490cf94 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/FunctionDefinitionDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/FunctionDefinitionDB.java@@ -22,7 +22,6 @@ import db.DBRecord;import db.Field;import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsImpl;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.data.*;import ghidra.program.model.data.DataTypeConflictHandler.ConflictResult;import ghidra.program.model.lang.*;@@ -30,6 +29,8 @@ import ghidra.program.model.listing.Function;import ghidra.program.model.listing.FunctionSignature;import ghidra.program.model.mem.MemBuffer;import ghidra.program.model.symbol.SourceType;+import ghidra.util.Lock.Closeable;+import ghidra.util.Msg;import ghidra.util.UniversalID;import ghidra.util.exception.AssertException;import ghidra.util.exception.InvalidInputException;@@ -40,10 +41,16 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {private FunctionParameterAdapter paramAdapter;private ArrayList<ParameterDefinitionDB> parameters;- FunctionDefinitionDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- FunctionDefinitionDBAdapter adapter, FunctionParameterAdapter paramAdapter,- DBRecord record) {- super(dataMgr, cache, record);+ /**+ * Constructor+ * @param dataMgr the datatypes manager+ * @param adapter the functions definition database adapter+ * @param paramAdapter the params database adapter+ * @param record the function definition record+ */+ FunctionDefinitionDB(DataTypeManagerDB dataMgr, FunctionDefinitionDBAdapter adapter,+ FunctionParameterAdapter paramAdapter, DBRecord record) {+ super(dataMgr, record);this.funDefAdapter = adapter;this.paramAdapter = paramAdapter;loadParameters();@@ -107,9 +114,8 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic String getPrototypeString(boolean includeCallingConvention) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();StringBuffer buf = new StringBuffer();if (includeCallingConvention && hasNoReturn()) {buf.append(NORETURN_DISPLAY_STRING);@@ -148,28 +154,20 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {return buf.toString();}- finally {- lock.release();- }}@Overridepublic ParameterDefinitionDB[] getArguments() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {ParameterDefinitionDB[] vars = new ParameterDefinitionDB[parameters.size()];return parameters.toArray(vars);}- finally {- lock.release();- }}@Overridepublic DataType getReturnType() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();long dtId = record.getLongValue(FunctionDefinitionDBAdapter.FUNCTION_DEF_RETURN_ID_COL);DataType dt = dataMgr.getDataType(dtId);if (dt == null) {@@ -177,9 +175,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {}return dt;}- finally {- lock.release();- }}@Override@@ -187,9 +182,9 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {if (!(dataType instanceof FunctionDefinition functionDefinition)) {throw new IllegalArgumentException();}- lock.acquire();- boolean isResolveCacheOwner = dataMgr.activateResolveCache();- try {+ boolean isResolveCacheOwner = false;+ try (Closeable c = lock.write()) {+ isResolveCacheOwner = dataMgr.activateResolveCache();checkDeleted();doReplaceWith(functionDefinition, true);}@@ -200,7 +195,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {if (isResolveCacheOwner) {dataMgr.processResolveQueue(true);}- lock.release();}}@@ -260,14 +254,10 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic String getComment() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getString(FunctionDefinitionDBAdapter.FUNCTION_DEF_COMMENT_COL);}- finally {- lock.release();- }}@Override@@ -324,17 +314,13 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic void setArguments(ParameterDefinition... args) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doSetArguments(args, false, true);}catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}private void doSetArguments(ParameterDefinition[] args, boolean checkedResolveReqd,@@ -363,14 +349,10 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic void setReturnType(DataType type) {type = ParameterDefinitionImpl.validateDataType(type, dataMgr, true);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doSetReturnType(type, false, true);}- finally {- lock.release();- }}private void doSetReturnType(DataType type, boolean checkedResolveReqd, boolean notify) {@@ -395,8 +377,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic void setComment(String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setString(FunctionDefinitionDBAdapter.FUNCTION_DEF_COMMENT_COL, comment);funDefAdapter.updateRecord(record, true);@@ -405,9 +386,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -415,8 +393,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();boolean changed = false;int n = parameters.size();@@ -440,9 +417,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {dataMgr.dataTypeChanged(this, true);}}- finally {- lock.release();- }}@Override@@ -482,10 +456,13 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {isEquivalent = isEquivalentSignature(sig, handler);}}+ catch (Exception e) {+ Msg.debug(this, "got exception isEquivalent funcitondefinition" + e);+ }finally {dataMgr.putCachedEquivalence(this, dataType, isEquivalent);}- return isEquivalent;+ return isEquivalent == null ? false : isEquivalent;}@Override@@ -545,8 +522,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {return;}DataTypeUtilities.checkValidReplacement(oldDt, newDt);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (newDt == this) {// avoid creating circular dependency@@ -578,9 +554,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {}}}- finally {- lock.release();- }}@Override@@ -589,8 +562,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {if (dt.getLength() <= 0) {throw new IllegalArgumentException("Fixed length data type expected");}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (ordinal >= parameters.size()) {for (int i = parameters.size(); i < ordinal; i++) {@@ -612,9 +584,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -624,49 +593,37 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic boolean hasVarArgs() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return false;}byte flags = record.getByteValue(FunctionDefinitionDBAdapter.FUNCTION_DEF_FLAGS_COL);return ((flags & FunctionDefinitionDBAdapter.FUNCTION_DEF_VARARG_FLAG) != 0);}- finally {- lock.release();- }}@Overridepublic boolean hasNoReturn() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return false;}byte flags = record.getByteValue(FunctionDefinitionDBAdapter.FUNCTION_DEF_FLAGS_COL);return ((flags & FunctionDefinitionDBAdapter.FUNCTION_DEF_NORETURN_FLAG) != 0);}- finally {- lock.release();- }}@Overridepublic void setVarArgs(boolean hasVarArgs) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doSetVarArgs(hasVarArgs, true);}catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}private void doSetVarArgs(boolean hasVarArgs, boolean notify) throws IOException {@@ -692,17 +649,13 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic void setNoReturn(boolean hasNoReturn) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doSetNoReturn(hasNoReturn, true);}catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}private void doSetNoReturn(boolean hasNoReturn, boolean notify) throws IOException {@@ -728,8 +681,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic void setGenericCallingConvention(GenericCallingConvention genericCallingConvention) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doSetCallingConvention(genericCallingConvention.name(), false, true);}@@ -739,24 +691,17 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {catch (InvalidInputException e) {throw new AssertException(e);}- finally {- lock.release();- }}@Overridepublic void setCallingConvention(String conventionName) throws InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doSetCallingConvention(conventionName, true, true);}catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}private void doSetCallingConvention(String conventionName, boolean restrictive, boolean notify)@@ -782,9 +727,8 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic String getCallingConventionName() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return Function.UNKNOWN_CALLING_CONVENTION_STRING;}@@ -794,9 +738,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {}return dataMgr.getCallingConventionName(id);}- finally {- lock.release();- }}@Override@@ -811,8 +752,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overridepublic void setLastChangeTime(long lastChangeTime) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(FunctionDefinitionDBAdapter.FUNCTION_DEF_LAST_CHANGE_TIME_COL,lastChangeTime);@@ -822,15 +762,11 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void setLastChangeTimeInSourceArchive(long lastChangeTime) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(FunctionDefinitionDBAdapter.FUNCTION_DEF_SOURCE_SYNC_TIME_COL,lastChangeTime);@@ -840,9 +776,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -853,8 +786,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overrideprotected void setUniversalID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(FunctionDefinitionDBAdapter.FUNCTION_DEF_SOURCE_DT_ID_COL,id.getValue());@@ -864,9 +796,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -877,8 +806,7 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {@Overrideprotected void setSourceArchiveID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(FunctionDefinitionDBAdapter.FUNCTION_DEF_SOURCE_ARCHIVE_ID_COL,id.getValue());@@ -888,9 +816,6 @@ class FunctionDefinitionDB extends DataTypeDB implements FunctionDefinition {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/LazyLoadingCachingMap.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/LazyLoadingCachingMap.javaindex e491b57e7e..7b1df7b06e 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/LazyLoadingCachingMap.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/LazyLoadingCachingMap.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -19,6 +19,7 @@ import java.lang.ref.SoftReference;import java.util.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;/*** Instances of this class will provide a simple map interface to a cached set of key,value@@ -120,8 +121,7 @@ public abstract class LazyLoadingCachingMap<K, V> {// It should be safe here since it creates a new map and then in one operation it sets it// as the map to be used elsewhere.- lock.acquire();- try {+ try (Closeable c = lock.read()) {map = getMap();if (map == null) {map = loadMap();@@ -129,9 +129,6 @@ public abstract class LazyLoadingCachingMap<K, V> {}return map;}- finally {- lock.release();- }}/**@@ -146,5 +143,4 @@ public abstract class LazyLoadingCachingMap<K, V> {return softRef.get();}-}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.javaindex 6b8ac7ba27..f41b7ce19c 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.java@@ -20,11 +20,11 @@ import java.io.IOException;import db.DBRecord;import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsDefinition;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.address.Address;import ghidra.program.model.data.*;import ghidra.program.model.mem.MemBuffer;import ghidra.util.InvalidNameException;+import ghidra.util.Lock.Closeable;import ghidra.util.UniversalID;import ghidra.util.exception.DuplicateNameException;@@ -49,15 +49,12 @@ class PointerDB extends DataTypeDB implements Pointer {/*** Constructor- *- * @param dataMgr- * @param cache- * @param adapter- * @param record+ * @param dataMgr the DataTypeManager+ * @param adapter the pointer database adapter+ * @param record the pointer record*/- public PointerDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- PointerDBAdapter adapter, DBRecord record) {- super(dataMgr, cache, record);+ PointerDB(DataTypeManagerDB dataMgr, PointerDBAdapter adapter, DBRecord record) {+ super(dataMgr, record);this.adapter = adapter;}@@ -85,14 +82,10 @@ class PointerDB extends DataTypeDB implements Pointer {@Overridepublic DataType getDataType() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return dataMgr.getDataType(record.getLongValue(PointerDBAdapter.PTR_DT_ID_COL));}- finally {- lock.release();- }}@Override@@ -139,12 +132,11 @@ class PointerDB extends DataTypeDB implements Pointer {@Overridepublic String getDisplayName() {String localDisplayName = displayName;- if (localDisplayName != null && !isInvalid()) {+ if (localDisplayName != null && !needsRefreshing()) {return localDisplayName;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (displayName == null) {// NOTE: Pointer display name only specifies length if null base typeDataType dt = getDataType();@@ -160,53 +152,38 @@ class PointerDB extends DataTypeDB implements Pointer {}return displayName;}- finally {- lock.release();- }}@Overridepublic String getMnemonic(Settings settings) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();DataType dataType = getDataType();if (dataType == null || dataType == DataType.DEFAULT) {return "addr";}return dataType.getMnemonic(settings) + " *";}- finally {- lock.release();- }}@Overridepublic boolean hasLanguageDependantLength() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getByteValue(PointerDBAdapter.PTR_LENGTH_COL) <= 0;}- finally {- lock.release();- }}@Overridepublic int getLength() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();int len = record.getByteValue(PointerDBAdapter.PTR_LENGTH_COL);if (len <= 0) {len = dataMgr.getDataOrganization().getPointerSize();}return len;}- finally {- lock.release();- }}@Override@@ -217,9 +194,8 @@ class PointerDB extends DataTypeDB implements Pointer {@Overridepublic String getDescription() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();StringBuffer sbuf = new StringBuffer();if (!hasLanguageDependantLength()) {sbuf.append(Integer.toString(getLength() * 8));@@ -238,24 +214,17 @@ class PointerDB extends DataTypeDB implements Pointer {}return sbuf.toString();}- finally {- lock.release();- }}@Overridepublic Object getValue(MemBuffer buf, Settings settings, int length) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return PointerDataType.getAddressValue(buf, getLength(), settings);}catch (IllegalArgumentException exc) {return null;}- finally {- lock.release();- }}@Override@@ -270,9 +239,8 @@ class PointerDB extends DataTypeDB implements Pointer {@Overridepublic String getRepresentation(MemBuffer buf, Settings settings, int length) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();Address addr = (Address) getValue(buf, settings, length);if (addr == null) { // could not create address, so return "Not a pointer (NaP)"@@ -280,9 +248,6 @@ class PointerDB extends DataTypeDB implements Pointer {}return addr.toString();}- finally {- lock.release();- }}@Override@@ -360,10 +325,9 @@ class PointerDB extends DataTypeDB implements Pointer {return;}DataTypeUtilities.checkValidReplacement(oldDt, newDt);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();- if (oldDt == getDataType()) {+ if (getDataType() == oldDt) {if (newDt == this) {newDt = DataType.DEFAULT;}@@ -407,9 +371,6 @@ class PointerDB extends DataTypeDB implements Pointer {}}}- finally {- lock.release();- }}@Override@@ -417,17 +378,13 @@ class PointerDB extends DataTypeDB implements Pointer {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {dataMgr.addDataTypeToDelete(this, key);deleting = true;}}- finally {- lock.release();- }}/**@@ -457,8 +414,7 @@ class PointerDB extends DataTypeDB implements Pointer {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {String myOldName = getOldName();@@ -468,9 +424,6 @@ class PointerDB extends DataTypeDB implements Pointer {}}}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ProgramBasedDataTypeManagerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ProgramBasedDataTypeManagerDB.javaindex 8f9ab864ec..100fb4f4f0 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ProgramBasedDataTypeManagerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ProgramBasedDataTypeManagerDB.java@@ -27,6 +27,7 @@ import ghidra.program.model.address.*;import ghidra.program.model.data.*;import ghidra.program.model.listing.Data;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.CancelledException;import ghidra.util.exception.VersionException;@@ -77,17 +78,13 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDB@Overridepublic void invalidateCache() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.invalidateCache();if (instanceSettingsAdapter != null) {instanceSettingsAdapter.invalidateNameCache();instanceSettingsCache.clear();}}- finally {- lock.release();- }}/**@@ -179,8 +176,7 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBif (instanceSettingsAdapter == null) {throw new UnsupportedOperationException();}- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address dataAddr = getDataSettingsAddress(data);instanceSettingsCache.remove(dataAddr, name);long addr = addrMap.getKey(dataAddr, false);@@ -193,9 +189,6 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBerrHandler.dbError(e);}- finally {- lock.release();- }return false;}@@ -204,8 +197,7 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBif (instanceSettingsAdapter == null) {throw new UnsupportedOperationException();}- lock.acquire();- try {+ try (Closeable c = lock.write()) {instanceSettingsCache.clear();boolean changed = false;Address dataAddr = getDataSettingsAddress(data);@@ -222,9 +214,6 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBerrHandler.dbError(e);}- finally {- lock.release();- }}@Override@@ -235,8 +224,7 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDB}DBHandle scratchPad = null;- lock.acquire();- try {+ try (Closeable c = lock.write()) {instanceSettingsCache.clear();scratchPad = dbHandle.getScratchPad();Table tmpTable = scratchPad.createTable(INSTANCE_SETTINGS_TABLE_NAME,@@ -282,7 +270,6 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDB// ignore}}- lock.release();}}@@ -291,17 +278,13 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBif (instanceSettingsAdapter == null) {throw new UnsupportedOperationException();}- lock.acquire();- try {+ try (Closeable c = lock.read()) {Address dataAddr = getDataSettingsAddress(data);return instanceSettingsAdapter.getSettingsNames(addrMap.getKey(dataAddr, false));}catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return new String[0];}@@ -310,8 +293,7 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBif (instanceSettingsAdapter == null) {throw new UnsupportedOperationException();}- lock.acquire();- try {+ try (Closeable c = lock.read()) {Address dataAddr = getDataSettingsAddress(data);return instanceSettingsAdapter.getSettingsKeys(addrMap.getKey(dataAddr, false)).length == 0;@@ -319,9 +301,6 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBcatch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return true;}@@ -330,8 +309,7 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBboolean wasChanged = false;- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (instanceSettingsAdapter == null) {throw new UnsupportedOperationException();}@@ -353,9 +331,6 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBcatch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return wasChanged;}@@ -379,8 +354,7 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDB}private SettingDB getSettingDB(Data data, String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (instanceSettingsAdapter == null) {throw new UnsupportedOperationException();}@@ -400,9 +374,6 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBcatch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return null;}@@ -413,8 +384,7 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDBthrow new UnsupportedOperationException();}AddressRange.checkValidRange(startAddr, endAddr);- lock.acquire();- try {+ try (Closeable c = lock.write()) {List<?> addrKeyRanges = addrMap.getKeyRanges(startAddr, endAddr, false);int cnt = addrKeyRanges.size();for (int i = 0; i < cnt; i++) {@@ -427,7 +397,6 @@ public abstract class ProgramBasedDataTypeManagerDB extends DataTypeManagerDB}finally {instanceSettingsCache.clear();- lock.release();}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SettingsCache.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SettingsCache.javaindex 256c86bfd3..acbc77e8c1 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SettingsCache.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SettingsCache.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -15,8 +15,7 @@*/package ghidra.program.database.data;-import java.util.LinkedHashMap;-import java.util.Objects;+import java.util.*;import ghidra.util.datastruct.FixedSizeHashMap;@@ -46,14 +45,14 @@ class SettingsCache<K> {}}- private LinkedHashMap<IdNamePair, SettingDB> map;+ private Map<IdNamePair, SettingDB> map;/*** Construct settings cache of a specified size* @param size cache size (maximum number of entries held)*/SettingsCache(int size) {- map = new FixedSizeHashMap<>(size, size);+ map = Collections.synchronizedMap(new FixedSizeHashMap<>(size, size));}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SourceArchiveDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SourceArchiveDB.javaindex 14c4ed9ea5..44122d36b8 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SourceArchiveDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/SourceArchiveDB.java@@ -18,22 +18,28 @@ package ghidra.program.database.data;import java.io.IOException;import db.DBRecord;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.data.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.UniversalID;-public class SourceArchiveDB extends DatabaseObject implements SourceArchive {+public class SourceArchiveDB extends DbObject implements SourceArchive {private UniversalID sourceID;private DBRecord record;private final SourceArchiveAdapter adapter;private final DataTypeManagerDB dtMgr;private Lock lock;- public SourceArchiveDB(DataTypeManagerDB dtMgr, DBObjectCache<SourceArchiveDB> cache,- SourceArchiveAdapter adapter, DBRecord record) {- super(cache, record.getKey());+ /**+ * Constructor+ * @param dtMgr the datatype manager+ * @param adapter the source archive database adapter+ * @param record the source archive record+ */+ SourceArchiveDB(DataTypeManagerDB dtMgr, SourceArchiveAdapter adapter,+ DBRecord record) {+ super(record.getKey());this.dtMgr = dtMgr;this.adapter = adapter;this.record = record;@@ -125,9 +131,8 @@ public class SourceArchiveDB extends DatabaseObject implements SourceArchive {@Overridepublic void setLastSyncTime(long syncTime) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.write()) {+ checkDeleted();record.setLongValue(SourceArchiveAdapter.ARCHIVE_ID_LAST_SYNC_TIME_COL, syncTime);adapter.updateRecord(record);dtMgr.sourceArchiveChanged(getSourceArchiveID());@@ -135,16 +140,12 @@ public class SourceArchiveDB extends DatabaseObject implements SourceArchive {catch (IOException e) {dtMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void setDirtyFlag(boolean isDirty) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.write()) {+ checkDeleted();record.setBooleanValue(SourceArchiveAdapter.ARCHIVE_ID_DIRTY_FLAG_COL, isDirty);adapter.updateRecord(record);dtMgr.sourceArchiveChanged(getSourceArchiveID());@@ -152,9 +153,6 @@ public class SourceArchiveDB extends DatabaseObject implements SourceArchive {catch (IOException e) {dtMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -162,9 +160,8 @@ public class SourceArchiveDB extends DatabaseObject implements SourceArchive {if (getName().equals(newName)) {return;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.write()) {+ checkDeleted();record.setString(SourceArchiveAdapter.ARCHIVE_ID_NAME_COL, newName);adapter.updateRecord(record);dtMgr.sourceArchiveChanged(getSourceArchiveID());@@ -172,9 +169,6 @@ public class SourceArchiveDB extends DatabaseObject implements SourceArchive {catch (IOException e) {dtMgr.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/StructureDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/StructureDB.javaindex 0643501575..73c8456871 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/StructureDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/StructureDB.java@@ -22,11 +22,11 @@ import java.util.function.Consumer;import db.DBRecord;import db.Field;import ghidra.docking.settings.Settings;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.data.*;import ghidra.program.model.data.AlignedStructurePacker.StructurePackResult;import ghidra.program.model.data.DataTypeConflictHandler.ConflictResult;import ghidra.program.model.mem.MemBuffer;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.AssertException;import ghidra.util.task.TaskMonitor;@@ -43,10 +43,16 @@ class StructureDB extends CompositeDB implements StructureInternal {private int numComponents; // If packed, this does not include the undefined components.private List<DataTypeComponentDB> components;- public StructureDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- CompositeDBAdapter compositeAdapter, ComponentDBAdapter componentAdapter,- DBRecord record) {- super(dataMgr, cache, compositeAdapter, componentAdapter, record);+ /**+ * Constructor+ * @param dataMgr the datatypes manager+ * @param compositeAdapter the composites database adapter+ * @param componentAdapter the components database adapter+ * @param record the record for the structure+ */+ StructureDB(DataTypeManagerDB dataMgr, CompositeDBAdapter compositeAdapter,+ ComponentDBAdapter componentAdapter, DBRecord record) {+ super(dataMgr, compositeAdapter, componentAdapter, record);}@Override@@ -187,8 +193,7 @@ class StructureDB extends CompositeDB implements StructureInternal {String comment, boolean validatePackAndNotify)throws DataTypeDependencyException, IllegalArgumentException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (validatePackAndNotify) {@@ -237,9 +242,6 @@ class StructureDB extends CompositeDB implements StructureInternal {}return dtc;}- finally {- lock.release();- }}@Override@@ -250,8 +252,7 @@ class StructureDB extends CompositeDB implements StructureInternal {if (len == structLength || isPackingEnabled()) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (len < structLength) {// identify index of first defined-component to be removed@@ -283,9 +284,6 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -296,16 +294,12 @@ class StructureDB extends CompositeDB implements StructureInternal {if (amount == 0 || isPackingEnabled()) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();doGrowStructure(amount);repack(false, false);notifySizeChanged(false);}- finally {- lock.release();- }}private void doGrowStructure(int amount) {@@ -326,8 +320,7 @@ class StructureDB extends CompositeDB implements StructureInternal {@Overridepublic DataTypeComponent insert(int ordinal, DataType dataType, int length, String name,String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (ordinal < 0 || ordinal > numComponents) {throw new IndexOutOfBoundsException(ordinal);@@ -387,9 +380,6 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (DataTypeDependencyException e) {throw new IllegalArgumentException(e.getMessage(), e);}- finally {- lock.release();- }}@Override@@ -408,8 +398,7 @@ class StructureDB extends CompositeDB implements StructureInternal {DataType baseDataType, int bitSize, String componentName, String comment)throws InvalidDataTypeException, IndexOutOfBoundsException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (ordinal < 0 || ordinal > numComponents) {throw new IndexOutOfBoundsException(ordinal);@@ -428,17 +417,13 @@ class StructureDB extends CompositeDB implements StructureInternal {BitFieldDataType bitFieldDt = new BitFieldDBDataType(baseDataType, bitSize, 0);return insert(ordinal, bitFieldDt, bitFieldDt.getStorageSize(), componentName, comment);}- finally {- lock.release();- }}@Overridepublic DataTypeComponent insertBitFieldAt(int byteOffset, int byteWidth, int bitOffset,DataType baseDataType, int bitSize, String componentName, String comment)throws InvalidDataTypeException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();BitFieldDataType.checkBaseDataType(baseDataType);baseDataType = baseDataType.clone(dataMgr);@@ -554,15 +539,11 @@ class StructureDB extends CompositeDB implements StructureInternal {notifySizeChanged(false);return dtc;}- finally {- lock.release();- }}@Overridepublic void delete(int ordinal) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (ordinal < 0 || ordinal >= numComponents) {throw new IndexOutOfBoundsException(ordinal);@@ -586,9 +567,6 @@ class StructureDB extends CompositeDB implements StructureInternal {repack(false, false);notifySizeChanged(false);}- finally {- lock.release();- }}/**@@ -644,8 +622,7 @@ class StructureDB extends CompositeDB implements StructureInternal {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();TreeSet<Integer> sortedOrdinals = new TreeSet<>(ordinals);@@ -734,16 +711,12 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic boolean isPartOf(DataType dataType) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (equals(dataType)) {return true;}@@ -760,39 +733,27 @@ class StructureDB extends CompositeDB implements StructureInternal {}return false;}- finally {- lock.release();- }}@Overridepublic int getNumComponents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return numComponents;}- finally {- lock.release();- }}@Overridepublic int getNumDefinedComponents() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return components.size();}- finally {- lock.release();- }}@Overridepublic DataTypeComponentDB getComponent(int ordinal) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (ordinal < 0 || ordinal >= numComponents) {throw new IndexOutOfBoundsException(ordinal);}@@ -819,25 +780,18 @@ class StructureDB extends CompositeDB implements StructureInternal {// return undefined componentreturn new DataTypeComponentDB(dataMgr, this, ordinal, offset);}- finally {- lock.release();- }}@Overridepublic DataTypeComponent[] getComponents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();DataTypeComponent[] comps = new DataTypeComponent[numComponents];for (int i = 0; i < comps.length; i++) {comps[i] = getComponent(i);}return comps;}- finally {- lock.release();- }}/**@@ -918,17 +872,13 @@ class StructureDB extends CompositeDB implements StructureInternal {@Overridepublic int getLength() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (structLength == 0) {return 1; // positive length required}return structLength;}- finally {- lock.release();- }}@Override@@ -938,8 +888,7 @@ class StructureDB extends CompositeDB implements StructureInternal {@Overridepublic void clearComponent(int ordinal) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (isPackingEnabled()) {delete(ordinal);@@ -966,9 +915,6 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}/**@@ -1056,8 +1002,7 @@ class StructureDB extends CompositeDB implements StructureInternal {@Overridepublic void deleteAtOffset(int offset) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (offset < 0) {throw new IllegalArgumentException("Offset cannot be negative.");@@ -1091,15 +1036,11 @@ class StructureDB extends CompositeDB implements StructureInternal {repack(false, false);notifySizeChanged(false);}- finally {- lock.release();- }}@Overridepublic void clearAtOffset(int offset) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (offset < 0) {throw new IllegalArgumentException("Offset cannot be negative.");@@ -1128,16 +1069,12 @@ class StructureDB extends CompositeDB implements StructureInternal {repack(false, false);notifySizeChanged(false);}- finally {- lock.release();- }}@Overridepublic DataTypeComponent getDefinedComponentAtOrAfterOffset(int offset) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (offset > structLength || offset < 0) {return null;}@@ -1155,16 +1092,12 @@ class StructureDB extends CompositeDB implements StructureInternal {}return null;}- finally {- lock.release();- }}@Overridepublic DataTypeComponent getComponentContaining(int offset) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (offset > structLength || offset < 0) {return null;}@@ -1187,16 +1120,12 @@ class StructureDB extends CompositeDB implements StructureInternal {}return null;}- finally {- lock.release();- }}@Overridepublic List<DataTypeComponent> getComponentsContaining(int offset) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();ArrayList<DataTypeComponent> list = new ArrayList<>();if (offset > structLength || offset < 0) {return list;@@ -1228,9 +1157,6 @@ class StructureDB extends CompositeDB implements StructureInternal {}return list;}- finally {- lock.release();- }}/**@@ -1259,8 +1185,7 @@ class StructureDB extends CompositeDB implements StructureInternal {@Overridepublic DataTypeComponent getDataTypeAt(int offset) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DataTypeComponent dtc = getComponentContaining(offset);if (dtc != null) {DataType dt = dtc.getDataType();@@ -1270,21 +1195,14 @@ class StructureDB extends CompositeDB implements StructureInternal {}return dtc;}- finally {- lock.release();- }}@Overridepublic DataTypeComponentDB[] getDefinedComponents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return components.toArray(new DataTypeComponentDB[components.size()]);}- finally {- lock.release();- }}@Override@@ -1320,8 +1238,7 @@ class StructureDB extends CompositeDB implements StructureInternal {}}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();dataType = validateDataType(dataType);@@ -1389,9 +1306,6 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (DataTypeDependencyException e) {throw new IllegalArgumentException(e.getMessage(), e);}- finally {- lock.release();- }}/**@@ -1452,8 +1366,7 @@ class StructureDB extends CompositeDB implements StructureInternal {public DataTypeComponent replace(int ordinal, DataType dataType, int length,String componentName, String comment)throws IllegalArgumentException, IndexOutOfBoundsException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (ordinal < 0 || ordinal >= numComponents) {@@ -1552,9 +1465,6 @@ class StructureDB extends CompositeDB implements StructureInternal {dataMgr.dbError(e);return null;}- finally {- lock.release();- }}@Override@@ -1564,8 +1474,7 @@ class StructureDB extends CompositeDB implements StructureInternal {throw new IllegalArgumentException("Offset cannot be negative.");}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (offset >= structLength) {@@ -1657,9 +1566,6 @@ class StructureDB extends CompositeDB implements StructureInternal {dataMgr.dbError(e);return null;}- finally {- lock.release();- }}/**@@ -1677,9 +1583,9 @@ class StructureDB extends CompositeDB implements StructureInternal {if (!(dataType instanceof StructureInternal)) {throw new IllegalArgumentException();}- lock.acquire();- boolean isResolveCacheOwner = dataMgr.activateResolveCache();- try {+ boolean isResolveCacheOwner = false;+ try (Closeable c = lock.write()) {+ isResolveCacheOwner = dataMgr.activateResolveCache();checkDeleted();doReplaceWith((StructureInternal) dataType, true);}@@ -1693,7 +1599,6 @@ class StructureDB extends CompositeDB implements StructureInternal {if (isResolveCacheOwner) {dataMgr.processResolveQueue(true);}- lock.release();}}@@ -1856,8 +1761,7 @@ class StructureDB extends CompositeDB implements StructureInternal {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();boolean changed = false;int n = components.size();@@ -1893,9 +1797,6 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}/**@@ -1930,8 +1831,7 @@ class StructureDB extends CompositeDB implements StructureInternal {if (dt instanceof BitFieldDataType) {return; // unsupported}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (isPackingEnabled()) {if (!repack(true, true)) {@@ -1983,9 +1883,6 @@ class StructureDB extends CompositeDB implements StructureInternal {}}}- finally {- lock.release();- }}@Override@@ -2052,16 +1949,12 @@ class StructureDB extends CompositeDB implements StructureInternal {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (isPackingEnabled()) {repack(true, true);}}- finally {- lock.release();- }}@Override@@ -2383,8 +2276,7 @@ class StructureDB extends CompositeDB implements StructureInternal {return;}DataTypeUtilities.checkValidReplacement(oldDt, newDt);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();DataType replacementDt = newDt;try {@@ -2417,9 +2309,6 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}private void setComponentDataType(DataTypeComponentDB comp, DataType newDt, int index)@@ -2457,8 +2346,7 @@ class StructureDB extends CompositeDB implements StructureInternal {@Overridepublic void deleteAll() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();for (DataTypeComponentDB dtc : components) {doDelete(dtc);@@ -2474,9 +2362,6 @@ class StructureDB extends CompositeDB implements StructureInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}/**@@ -2486,8 +2371,7 @@ class StructureDB extends CompositeDB implements StructureInternal {@Overrideprotected boolean repack(boolean isAutoChange, boolean notify) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();int oldLength = structLength;@@ -2518,9 +2402,6 @@ class StructureDB extends CompositeDB implements StructureInternal {}return changed;}- finally {- lock.release();- }}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/TypedefDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/TypedefDB.javaindex 311986177d..40680989fd 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/TypedefDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/TypedefDB.java@@ -20,10 +20,10 @@ import java.io.IOException;import db.DBRecord;import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsDefinition;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.data.*;import ghidra.program.model.data.DataTypeConflictHandler.ConflictResult;import ghidra.program.model.mem.MemBuffer;+import ghidra.util.Lock.Closeable;import ghidra.util.UniversalID;import ghidra.util.exception.DuplicateNameException;@@ -39,15 +39,13 @@ class TypedefDB extends DataTypeDB implements TypeDef {private SettingsDefinition[] settingsDef; // lazy initialization/**- * Construct TypeDefDB instance+ * Constructor* @param dataMgr datatype manager- * @param cache DataTypeDB cache* @param adapter TypeDef record adapter* @param record TypeDefDB record*/- public TypedefDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- TypedefDBAdapter adapter, DBRecord record) {- super(dataMgr, cache, record);+ TypedefDB(DataTypeManagerDB dataMgr, TypedefDBAdapter adapter, DBRecord record) {+ super(dataMgr, record);this.adapter = adapter;this.defaultSettings = null; // ensure lazy initialization}@@ -65,8 +63,7 @@ class TypedefDB extends DataTypeDB implements TypeDef {if (isAutoNamed()) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();String oldName = getName();@@ -98,25 +95,15 @@ class TypedefDB extends DataTypeDB implements TypeDef {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic boolean isAutoNamed() {- int flags = getFlags();- if (isInvalid()) {- lock.acquire();- try {- checkIsValid();- flags = getFlags();- }- finally {- lock.release();- }+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ int flags = getFlags();+ return (flags & TypedefDBAdapter.TYPEDEF_FLAG_AUTONAME) != 0;}- return (flags & TypedefDBAdapter.TYPEDEF_FLAG_AUTONAME) != 0;}@Override@@ -186,16 +173,13 @@ class TypedefDB extends DataTypeDB implements TypeDef {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {notifySizeChanged(true);}}- finally {- lock.release();- }+}@Override@@ -203,39 +187,30 @@ class TypedefDB extends DataTypeDB implements TypeDef {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {notifyAlignmentChanged(true);}}- finally {- lock.release();- }}@Overridepublic DataType getBaseDataType() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();DataType dataType = getDataType();if (dataType instanceof TypeDef) {return ((TypeDef) dataType).getBaseDataType();}return dataType;}- finally {- lock.release();- }}@Overridepublic DataType getDataType() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();long dataTypeID = record.getLongValue(TypedefDBAdapter.TYPEDEF_DT_ID_COL);DataType dt = dataMgr.getDataType(dataTypeID);if (dt == null) {@@ -243,9 +218,6 @@ class TypedefDB extends DataTypeDB implements TypeDef {}return dt;}- finally {- lock.release();- }}@Override@@ -325,8 +297,7 @@ class TypedefDB extends DataTypeDB implements TypeDef {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (oldDt == getDataType()) {DataTypeUtilities.checkValidReplacement(oldDt, newDt);@@ -357,9 +328,6 @@ class TypedefDB extends DataTypeDB implements TypeDef {}}}- finally {- lock.release();- }}@Override@@ -367,17 +335,13 @@ class TypedefDB extends DataTypeDB implements TypeDef {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {dataMgr.addDataTypeToDelete(this, key);deleting = true;}}- finally {- lock.release();- }}@Override@@ -385,16 +349,12 @@ class TypedefDB extends DataTypeDB implements TypeDef {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dt == getDataType()) {updateAutoName(true);}}- finally {- lock.release();- }}@Override@@ -421,9 +381,8 @@ class TypedefDB extends DataTypeDB implements TypeDef {@Overridepublic SettingsDefinition[] getSettingsDefinitions() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (settingsDef == null) {DataType dt = getDataType();SettingsDefinition[] settingsDefinitions = dt.getSettingsDefinitions();@@ -438,9 +397,6 @@ class TypedefDB extends DataTypeDB implements TypeDef {}return settingsDef;}- finally {- lock.release();- }}@Override@@ -532,8 +488,7 @@ class TypedefDB extends DataTypeDB implements TypeDef {@Overrideprotected void setUniversalID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(TypedefDBAdapter.TYPEDEF_UNIVERSAL_DT_ID_COL, id.getValue());adapter.updateRecord(record, false);@@ -542,9 +497,6 @@ class TypedefDB extends DataTypeDB implements TypeDef {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -554,8 +506,7 @@ class TypedefDB extends DataTypeDB implements TypeDef {@Overrideprotected void setSourceArchiveID(UniversalID id) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(TypedefDBAdapter.TYPEDEF_SOURCE_ARCHIVE_ID_COL, id.getValue());adapter.updateRecord(record, false);@@ -564,15 +515,11 @@ class TypedefDB extends DataTypeDB implements TypeDef {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void setLastChangeTime(long lastChangeTime) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(TypedefDBAdapter.TYPEDEF_LAST_CHANGE_TIME_COL, lastChangeTime);adapter.updateRecord(record, false);@@ -581,15 +528,11 @@ class TypedefDB extends DataTypeDB implements TypeDef {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void setLastChangeTimeInSourceArchive(long lastChangeTimeInSourceArchive) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setLongValue(TypedefDBAdapter.TYPEDEF_SOURCE_SYNC_TIME_COL,lastChangeTimeInSourceArchive);@@ -599,9 +542,6 @@ class TypedefDB extends DataTypeDB implements TypeDef {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -612,16 +552,12 @@ class TypedefDB extends DataTypeDB implements TypeDef {if (dataType == this) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {TypeDef td = (TypeDef) dataType;dataTypeReplaced(getDataType(), td.getDataType());TypedefDataType.copyTypeDefSettings(td, this, true);// NOTE: as with the name, auto-name setting is left unchanged}- finally {- lock.release();- }}@Override@@ -672,9 +608,8 @@ class TypedefDB extends DataTypeDB implements TypeDef {}boolean updateAutoName(boolean notify) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.write()) {+ checkDeleted();if (!isAutoNamed()) {return false;@@ -697,9 +632,6 @@ class TypedefDB extends DataTypeDB implements TypeDef {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }return true;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/UnionDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/UnionDB.javaindex 2823b06dbc..815b46c695 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/UnionDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/UnionDB.java@@ -22,10 +22,10 @@ import java.util.function.Consumer;import db.DBRecord;import db.Field;import ghidra.docking.settings.Settings;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.data.*;import ghidra.program.model.data.DataTypeConflictHandler.ConflictResult;import ghidra.program.model.mem.MemBuffer;+import ghidra.util.Lock.Closeable;/*** Database implementation for the Union data type.@@ -40,17 +40,14 @@ class UnionDB extends CompositeDB implements UnionInternal {/*** Constructor- *- * @param dataMgr- * @param cache- * @param compositeAdapter- * @param componentAdapter- * @param record+ * @param dataMgr the datatypes manager+ * @param compositeAdapter the composites database adapter+ * @param componentAdapter the components database adapter+ * @param record the record for the union*/- public UnionDB(DataTypeManagerDB dataMgr, DBObjectCache<DataTypeDB> cache,- CompositeDBAdapter compositeAdapter, ComponentDBAdapter componentAdapter,- DBRecord record) {- super(dataMgr, cache, compositeAdapter, componentAdapter, record);+ UnionDB(DataTypeManagerDB dataMgr, CompositeDBAdapter compositeAdapter,+ ComponentDBAdapter componentAdapter, DBRecord record) {+ super(dataMgr, compositeAdapter, componentAdapter, record);}@Override@@ -87,8 +84,7 @@ class UnionDB extends CompositeDB implements UnionInternal {@Overridepublic DataTypeComponent add(DataType dataType, int length, String componentName,String comment) throws IllegalArgumentException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();getComputedAlignment(true); // ensure previous alignment has been storedDataTypeComponent dtc = doAdd(dataType, length, componentName, comment, true);@@ -100,9 +96,6 @@ class UnionDB extends CompositeDB implements UnionInternal {catch (DataTypeDependencyException e) {throw new IllegalArgumentException(e.getMessage(), e);}- finally {- lock.release();- }}private int getBitFieldAllocation(BitFieldDataType bitfieldDt) {@@ -151,8 +144,7 @@ class UnionDB extends CompositeDB implements UnionInternal {@Overridepublic DataTypeComponent insert(int ordinal, DataType dataType, int length, String name,String comment) throws IllegalArgumentException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();dataType = validateDataType(dataType);@@ -179,9 +171,6 @@ class UnionDB extends CompositeDB implements UnionInternal {catch (DataTypeDependencyException e) {throw new IllegalArgumentException(e.getMessage(), e);}- finally {- lock.release();- }}@Override@@ -205,8 +194,7 @@ class UnionDB extends CompositeDB implements UnionInternal {@Overridepublic void delete(int ordinal) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();getComputedAlignment(true); // ensure previous alignment has been stored@@ -223,9 +211,6 @@ class UnionDB extends CompositeDB implements UnionInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -240,8 +225,7 @@ class UnionDB extends CompositeDB implements UnionInternal {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (isPackingEnabled()) {@@ -292,9 +276,6 @@ class UnionDB extends CompositeDB implements UnionInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -302,9 +283,9 @@ class UnionDB extends CompositeDB implements UnionInternal {if (!(dataType instanceof UnionInternal)) {throw new IllegalArgumentException();}- lock.acquire();- boolean isResolveCacheOwner = dataMgr.activateResolveCache();- try {+ boolean isResolveCacheOwner = false;+ try (Closeable c = lock.write()) {+ isResolveCacheOwner = dataMgr.activateResolveCache();checkDeleted();doReplaceWith((UnionInternal) dataType, true);}@@ -318,7 +299,6 @@ class UnionDB extends CompositeDB implements UnionInternal {if (isResolveCacheOwner) {dataMgr.processResolveQueue(true);}- lock.release();}}@@ -375,9 +355,8 @@ class UnionDB extends CompositeDB implements UnionInternal {@Overridepublic boolean isPartOf(DataType dataType) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (equals(dataType)) {return true;}@@ -394,21 +373,14 @@ class UnionDB extends CompositeDB implements UnionInternal {}return false;}- finally {- lock.release();- }}@Overridepublic int getNumComponents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return components.size();}- finally {- lock.release();- }}@Override@@ -418,29 +390,21 @@ class UnionDB extends CompositeDB implements UnionInternal {@Overridepublic DataTypeComponentDB getComponent(int ordinal) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (ordinal < 0 || ordinal >= components.size()) {return null;}return components.get(ordinal);}- finally {- lock.release();- }}@Overridepublic DataTypeComponentDB[] getComponents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return components.toArray(new DataTypeComponentDB[components.size()]);}- finally {- lock.release();- }}@Override@@ -480,17 +444,13 @@ class UnionDB extends CompositeDB implements UnionInternal {@Overridepublic int getLength() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (unionLength == 0) {return 1; // positive length required}return unionLength;}- finally {- lock.release();- }}@Override@@ -532,16 +492,15 @@ class UnionDB extends CompositeDB implements UnionInternal {if (deleting) {return;}- lock.acquire();- try {+ if (dt instanceof BitFieldDataType) {+ return; // unsupported+ }+ try (Closeable c = lock.write()) {checkDeleted();if (isPackingEnabled()) {repack(true, true);}}- finally {- lock.release();- }}@Override@@ -552,8 +511,7 @@ class UnionDB extends CompositeDB implements UnionInternal {if (dt instanceof BitFieldDataType) {return; // unsupported}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();boolean changed = false;for (DataTypeComponentDB dtc : components) {@@ -569,9 +527,6 @@ class UnionDB extends CompositeDB implements UnionInternal {dataMgr.dataTypeChanged(this, true);}}- finally {- lock.release();- }}private DataType adjustBitField(DataType dataType) {@@ -655,8 +610,7 @@ class UnionDB extends CompositeDB implements UnionInternal {*/@Overrideprotected boolean repack(boolean isAutoChange, boolean notify) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();int oldLength = unionLength;@@ -708,9 +662,6 @@ class UnionDB extends CompositeDB implements UnionInternal {}return changed;}- finally {- lock.release();- }}@Override@@ -718,8 +669,7 @@ class UnionDB extends CompositeDB implements UnionInternal {if (deleting) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();boolean changed = false;for (int i = components.size() - 1; i >= 0; i--) { // reverse order@@ -756,9 +706,6 @@ class UnionDB extends CompositeDB implements UnionInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -838,8 +785,7 @@ class UnionDB extends CompositeDB implements UnionInternal {return;}DataTypeUtilities.checkValidReplacement(oldDt, newDt);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();DataType replacementDt = newDt;try {@@ -875,9 +821,6 @@ class UnionDB extends CompositeDB implements UnionInternal {catch (IOException e) {dataMgr.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/external/ExternalManagerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/external/ExternalManagerDB.javaindex ba2287b263..5de0ecdfe8 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/external/ExternalManagerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/external/ExternalManagerDB.java@@ -36,6 +36,7 @@ import ghidra.program.model.listing.Function;import ghidra.program.model.listing.Library;import ghidra.program.model.symbol.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -198,51 +199,38 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {public ExternalLocation addExtLocation(String extLibraryName, String extLabel, Address extAddr,SourceType sourceType) throws InvalidInputException, DuplicateNameException {SourceType locSourceType = checkExternalLabel(extLabel, extAddr, sourceType);- lock.acquire();- try {+ try (Closeable c = lock.write()) {Library libraryScope = getLibraryScope(extLibraryName);if (libraryScope == null) {libraryScope = addExternalName(extLibraryName, null, sourceType);}return addExtLocation(libraryScope, extLabel, extAddr, false, locSourceType, true);}- finally {- lock.release();- }}@Overridepublic ExternalLocation addExtLocation(Namespace extParentNamespace, String extLabel,Address extAddr, SourceType sourceType) throws InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {return addExtLocation(extParentNamespace, extLabel, extAddr, false, sourceType, true);}- finally {- lock.release();- }}@Overridepublic ExternalLocation addExtLocation(Namespace extParentNamespace, String extLabel,Address extAddr, SourceType sourceType, boolean reuseExisting)throws InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {return addExtLocation(extParentNamespace, extLabel, extAddr, false, sourceType,reuseExisting);}- finally {- lock.release();- }}@Overridepublic ExternalLocation addExtFunction(String extLibraryName, String extLabel, Address extAddr,SourceType sourceType) throws InvalidInputException, DuplicateNameException {SourceType locSourceType = checkExternalLabel(extLabel, extAddr, sourceType);- lock.acquire();- try {+ try (Closeable c = lock.write()) {Library libraryScope = getLibraryScope(extLibraryName);if (libraryScope == null) {libraryScope = addExternalName(extLibraryName, null,@@ -250,34 +238,22 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {}return addExtLocation(libraryScope, extLabel, extAddr, true, locSourceType, true);}- finally {- lock.release();- }}@Overridepublic ExternalLocation addExtFunction(Namespace extParentNamespace, String extLabel,Address extAddr, SourceType sourceType) throws InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {return addExtLocation(extParentNamespace, extLabel, extAddr, true, sourceType, true);}- finally {- lock.release();- }}@Overridepublic ExternalLocation addExtFunction(Namespace extNamespace, String extLabel, Address extAddr,SourceType sourceType, boolean reuseExisting) throws InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {return addExtLocation(extNamespace, extLabel, extAddr, true, sourceType, reuseExisting);}- finally {- lock.release();- }-}private SourceType checkExternalLabel(String extLabel, Address extAddr, SourceType source)@@ -322,8 +298,7 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {"Memory address not defined for program: " + extAddr);}}- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (sourceType == SourceType.DEFAULT) {extLabel = null;}@@ -368,9 +343,6 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {program.dbError(e); // will not returnreturn null;}- finally {- lock.release();- }}/**@@ -563,8 +535,7 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {if (externalAddr.getAddressSpace() != AddressSpace.EXTERNAL_SPACE) {throw new IllegalArgumentException("Expected external address");}- lock.acquire();- try {+ try (Closeable c = lock.read()) {Symbol[] symbols = symbolMgr.getSymbols(externalAddr);if (symbols.length == 1) {return new ExternalLocationDB(this, (MemorySymbol) symbols[0]);@@ -576,9 +547,6 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {}return null;}- finally {- lock.release();- }}@Override@@ -601,17 +569,13 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {* @return true if external location was successfully removed else false*/public boolean removeExternalLocation(Address externalAddr) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {ExternalLocationDB loc = (ExternalLocationDB) getExtLocation(externalAddr);if (loc != null) {loc.getSymbol().delete();return true;}}- finally {- lock.release();- }return false;}@@ -621,8 +585,7 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {@Overridepublic boolean removeExternalLibrary(String name) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Symbol s = symbolMgr.getLibrarySymbol(name);if (s != null) {if (symbolMgr.getChildren(s).hasNext()) {@@ -631,9 +594,6 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {s.delete();}}- finally {- lock.release();- }return true;}@@ -649,17 +609,13 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {@Overridepublic Library addExternalLibraryName(String name, SourceType source)throws DuplicateNameException, InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Symbol librarySymbol = symbolMgr.getLibrarySymbol(name);if (librarySymbol != null) {return (Library) librarySymbol.getObject();}return addExternalName(name, null, source);}- finally {- lock.release();- }}private Library addExternalName(String name, String pathname, SourceType source)@@ -718,8 +674,7 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {validateExternalPath(externalPath);- lock.acquire();- try {+ try (Closeable c = lock.write()) {LibrarySymbol s = symbolMgr.getLibrarySymbol(externalName);if (s == null) {try {@@ -734,9 +689,6 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {s.setExternalLibraryPath(externalPath);}}- finally {- lock.release();- }}private void validateExternalPath(String path) throws InvalidInputException {@@ -755,8 +707,7 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {if (extLoc.isFunction()) {return extLoc.getFunction();}- lock.acquire();- try {+ try (Closeable c = lock.write()) {SymbolDB symbol = (SymbolDB) extLoc.getSymbol();if (!(symbol instanceof CodeSymbol)) {throw new IllegalStateException("Expected external code symbol");@@ -776,9 +727,6 @@ public class ExternalManagerDB implements ManagerDB, ExternalManager {catch (Exception e) {throw new RuntimeException("Unexpected exception", e);}- finally {- lock.release();- }}AddressMap getAddressMap() {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.javaindex 289c7006e1..d8f5027990 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.java@@ -23,18 +23,22 @@ import java.util.*;import javax.help.UnsupportedOperationException;import db.DBRecord;-import ghidra.program.database.*;+import ghidra.program.database.DbObject;+import ghidra.program.database.ProgramDB;import ghidra.program.database.external.ExternalManagerDB;import ghidra.program.database.map.AddressMap;-import ghidra.program.database.symbol.*;+import ghidra.program.database.symbol.FunctionSymbol;+import ghidra.program.database.symbol.VariableSymbolDB;import ghidra.program.model.address.*;import ghidra.program.model.data.*;import ghidra.program.model.lang.*;import ghidra.program.model.listing.*;import ghidra.program.model.symbol.*;import ghidra.program.model.util.StringPropertyMap;+import ghidra.program.util.FunctionChangeRecord.FunctionChangeType;import ghidra.program.util.ProgramEvent;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -42,7 +46,7 @@ import ghidra.util.task.TaskMonitor;* Database implementation of a Function.**/-public class FunctionDB extends DatabaseObject implements Function {+public class FunctionDB extends DbObject implements Function {final FunctionManagerDB manager;@@ -58,53 +62,33 @@ public class FunctionDB extends DatabaseObject implements Function {// NOTE: FunctionDB discards the following data when invalidated/refreshed// All function variables instances should be dropped/re-acquired when// a domain object restored event occurs-- private Map<SymbolDB, VariableDB> symbolMap;- private ReturnParameterDB returnParam;- private List<AutoParameterImpl> autoParams;- private List<ParameterDB> params;- private List<VariableDB> locals;+ private FunctionVariables lazyVariables;// Tags associated with this function. This is here to keep db requests// to a minimum requesting all tags. Note that this list is invalidated// only when tags have been edited or deleted from the system.private Set<FunctionTag> tags;-- /**- * foundBadVariables is set true when one or more variable symbols are found- * which no longer decode a valid storage address (indicated by Address.NO_ADDRESS).- * Any time a variable is added while this flag is set, such bad variables should be purged.- */- private boolean foundBadVariables = false;-- /**- * Use of stack frame to compute parameter ordinals and validate stack offsets- * should not be done while <code>validateEnabled</code> is false. This may be- * necessary during a language upgrade in which case a dummy compiler-spec- * may be in-use.- */- private boolean validateEnabled = true;-private int updateInProgressCount = 0;private boolean updateRefreshRequired = false;+ private Lock lock;- FunctionDB(FunctionManagerDB manager, DBObjectCache<FunctionDB> cache, AddressMap addrMap,- DBRecord rec) {- super(cache, rec.getKey());+ FunctionDB(FunctionManagerDB manager, AddressMap addrMap, DBRecord rec) {+ super(rec.getKey());this.manager = manager;program = manager.getProgram();this.rec = rec;init();frame = new FunctionStackFrame(this);+ lock = manager.lock;}@Overridepublic boolean isDeleted() {- return isDeleted(manager.lock);+ return isDeleted(lock);}public void setValidationEnabled(boolean state) {- validateEnabled = state;+ getFunctionVariables().setValidataionEnabled(state);}private void init() {@@ -121,13 +105,13 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic boolean isThunk() {- validate(manager.lock);+ validate(lock);return thunkedFunction != null;}@Overridepublic Function getThunkedFunction(boolean recursive) {- validate(manager.lock);+ validate(lock);FunctionDB localThunkFunc = thunkedFunction;if (!recursive || localThunkFunc == null) {return localThunkFunc;@@ -147,8 +131,7 @@ public class FunctionDB extends DatabaseObject implements Function {if ((referencedFunction != null) && !(referencedFunction instanceof FunctionDB)) {throw new IllegalArgumentException("FunctionDB expected for referenced function");}- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();// TODO: Removal all children / reset flags, etc. ??@@ -156,7 +139,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -182,18 +164,14 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic Address[] getFunctionThunkAddresses(boolean recursive) {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();List<Address> thunkAddrList = getFunctionThunkAddresses(key, recursive);if (thunkAddrList == null) {return null;}return thunkAddrList.toArray(new Address[thunkAddrList.size()]);}- finally {- manager.lock.release();- }}@Override@@ -231,46 +209,36 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic String getName() {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return functionSymbol.getName();}- finally {- manager.lock.release();- }}@Overridepublic void setName(String name, SourceType source)throws DuplicateNameException, InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();functionSymbol.setName(name, source);}finally {endUpdate();- manager.lock.release();}}@Override- public Program getProgram() {+ public ProgramDB getProgram() {return manager.getProgram();}@Overridepublic String getComment() {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return manager.getCodeManager().getComment(CommentType.PLATE, getEntryPoint());}- finally {- manager.lock.release();- }}@Override@@ -280,28 +248,22 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setComment(String comment) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();manager.getCodeManager().setComment(getEntryPoint(), CommentType.PLATE, comment);}finally {endUpdate();- manager.lock.release();}}@Overridepublic String getRepeatableComment() {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return manager.getCodeManager().getComment(CommentType.REPEATABLE, getEntryPoint());}- finally {- manager.lock.release();- }}@Override@@ -311,48 +273,38 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setRepeatableComment(String comment) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();manager.getCodeManager().setComment(getEntryPoint(), CommentType.REPEATABLE, comment);}- finally {- manager.lock.release();- }}@Overridepublic Address getEntryPoint() {- validate(manager.lock);+ validate(lock);return entryPoint;}@Overridepublic AddressSetView getBody() {- manager.lock.acquire();- try {- if (!checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (!refreshIfNeeded()) {// Function or its symbol has been deletedreturn new AddressSet(entryPoint, entryPoint);}return program.getNamespaceManager().getAddressSet(this);}- finally {- manager.lock.release();- }}@Overridepublic void setBody(AddressSetView set) throws OverlappingFunctionException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();manager.setFunctionBody(this, set);}finally {endUpdate();- manager.lock.release();}}@@ -363,31 +315,30 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic ReturnParameterDB getReturn() {- validate(manager.lock);- FunctionDB localThunkFunc = thunkedFunction;- if (localThunkFunc != null) {- return localThunkFunc.getReturn();- }- ReturnParameterDB rp = returnParam;- if (rp != null) {- return rp;- }- manager.lock.acquire();- try {- // NOTE: lock required to avoid returning null returnParam- loadVariables();- return returnParam;+ try (Closeable c = lock.read()) {+ validate(lock);+ FunctionDB localThunkFunc = thunkedFunction;+ if (localThunkFunc != null) {+ return localThunkFunc.getReturn();+ }+ FunctionVariables vars = getFunctionVariables();+ return vars.getReturnParam();}- finally {- manager.lock.release();+ }++ private FunctionVariables getFunctionVariables() {+ FunctionVariables local = lazyVariables;+ if (local == null) {+ local = new FunctionVariables(this, hasCustomVariableStorage());+ lazyVariables = local;}+ return local;}@Overridepublic void setReturn(DataType type, VariableStorage storage, SourceType source)throws InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -410,14 +361,12 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@Overridepublic void setReturnType(DataType type, SourceType source) throws InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -428,7 +377,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -462,7 +410,18 @@ public class FunctionDB extends DatabaseObject implements Function {return dt;}- private VariableStorage deserializeStorage(String serializedStorage) {+ VariableStorage getReturnStorage(boolean hasCustomStorage) {+ VariableStorage returnStorage = VariableStorage.UNASSIGNED_STORAGE;+ if (hasCustomStorage) {+ String serializedStorage = rec.getString(FunctionAdapter.RETURN_STORAGE_COL);+ if (serializedStorage != null) {+ returnStorage = deserializeStorage(serializedStorage);+ }+ }+ return returnStorage;+ }++ VariableStorage deserializeStorage(String serializedStorage) {if (serializedStorage == null) {return VariableStorage.UNASSIGNED_STORAGE;}@@ -474,44 +433,9 @@ public class FunctionDB extends DatabaseObject implements Function {}}-// VariableStorage getReturnStorage(DataType type) {-// VariableStorage returnStorage;-// String serializedStorage = rec.getString(FunctionAdapter.RETURN_STORAGE_COL);-// if (serializedStorage == null) {-// // Use compiler spec to determine return storage-// PrototypeModel callingConvention = getCallingConvention();-// if (callingConvention == null) {-// callingConvention = getDefaultCallingConvention();-// }-// if (callingConvention == null) {-// returnStorage = VariableStorage.UNASSIGNED_STORAGE;-// }-// else {-// returnStorage = callingConvention.getReturnLocation(type, program);-// }-// }-// else {-// returnStorage = deserializeStorage(serializedStorage);-// }-// return returnStorage;-// }-@Overridepublic FunctionSignature getSignature(boolean formalSignature) {- manager.lock.acquire();- try {- startUpdate();- checkIsValid();- if (thunkedFunction == null) {- // If not a thunk be sure to load variables first- loadVariables();- }- return new FunctionDefinitionDataType(this, formalSignature);- }- finally {- endUpdate();- manager.lock.release();- }+ return new FunctionDefinitionDataType(this, formalSignature);}@Override@@ -521,16 +445,10 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic String getPrototypeString(boolean formalSignature, boolean includeCallingConvention) {- manager.lock.acquire();- try {- if (!checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (!refreshIfNeeded()) {return "undefined " + getName() + "()";}- if (thunkedFunction == null) {- // If not a thunk be sure to load variables first- loadVariables();- }-StringBuffer buf = new StringBuffer();ReturnParameterDB rtn = getReturn();buf.append(formalSignature ? rtn.getFormalDataType().getDisplayName()@@ -574,9 +492,6 @@ public class FunctionDB extends DatabaseObject implements Function {return buf.toString();}- finally {- manager.lock.release();- }}void updateSignatureSourceAfterVariableChange(SourceType variableSourceType,@@ -622,7 +537,7 @@ public class FunctionDB extends DatabaseObject implements Function {*/@Overridepublic StackFrame getStackFrame() {- validate(manager.lock);+ validate(lock);FunctionDB localThunkFunc = thunkedFunction;if (localThunkFunc != null) {return thunkedFunction.getStackFrame();@@ -632,7 +547,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic int getStackPurgeSize() {- validate(manager.lock);+ validate(lock);FunctionDB localThunkFunc = thunkedFunction;if (localThunkFunc != null) {return localThunkFunc.getStackPurgeSize();@@ -642,8 +557,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setStackPurgeSize(int change) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();if (change == getStackPurgeSize()) {return;@@ -665,13 +579,12 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@Overridepublic boolean isStackPurgeSizeValid() {- validate(manager.lock);+ validate(lock);FunctionDB localThunkFunc = thunkedFunction;if (localThunkFunc != null) {return localThunkFunc.isStackPurgeSizeValid();@@ -684,225 +597,6 @@ public class FunctionDB extends DatabaseObject implements Function {return key;}- private static boolean isBadVariable(VariableSymbolDB varSym) {- return varSym.getAddress() == Address.NO_ADDRESS ||- varSym.getVariableStorage().isBadStorage();- }-- private void loadVariables() {- manager.lock.acquire();- try {- if (!loadSymbolBasedVariables()) {- return; // already loaded- }- boolean hasCustomVariableStorage = hasCustomVariableStorage();- loadReturn(hasCustomVariableStorage);- if (foundBadVariables) {- Msg.warn(this, "Found one or more bad variables in function " + getName() + " at " +- getEntryPoint());- }- if (!hasCustomVariableStorage) {- updateParametersAndReturn(); // assign dynamic storage (includes return and auto-params)- }- }- finally {- manager.lock.release();- }- }-- private boolean loadSymbolBasedVariables() {- if (symbolMap != null) {- return false;- }- symbolMap = new HashMap<>();- locals = new ArrayList<>();- params = new ArrayList<>();- autoParams = null;- SymbolIterator it = program.getSymbolTable().getChildren(functionSymbol);- while (it.hasNext()) {- SymbolDB s = (SymbolDB) it.next();- if (s instanceof VariableSymbolDB) {- VariableSymbolDB varSym = (VariableSymbolDB) s;- if (isBadVariable(varSym)) {- // silently ignore bad variable if address no longer decodes- // This can happen due to changes in stack address space dimensions- // TODO: it would be nice to cleanup such bad variables- foundBadVariables = true;- continue;- }- if (s.getSymbolType() == SymbolType.PARAMETER) {- ParameterDB p = new ParameterDB(this, s);- symbolMap.put(s, p);- params.add(p);- }- else {- VariableDB var = new LocalVariableDB(this, s);- symbolMap.put(s, var);- locals.add(var);- }- }- }- Collections.sort(params);- Collections.sort(locals);- return true;- }-- private boolean loadReturn(boolean hasCustomVariableStorage) {-- if (returnParam != null) {- return false;- }- DataType dt = getReturnDataType();-- VariableStorage returnStorage = VariableStorage.UNASSIGNED_STORAGE;- if (hasCustomVariableStorage) {- String serializedStorage = rec.getString(FunctionAdapter.RETURN_STORAGE_COL);- if (serializedStorage != null) {- returnStorage = deserializeStorage(serializedStorage);- if (returnStorage.isBadStorage()) {- foundBadVariables = true;- }- }- }-- returnParam = new ReturnParameterDB(this, dt, returnStorage);-- return true;- }-- /**- * Update parameter ordinals and re-assign dynamic parameter storage- * NOTE: loadVariables must have been called first- */- void updateParametersAndReturn() {-- if (params == null) {- loadVariables();- return;- }-- if (hasCustomVariableStorage()) {- autoParams = null;- renumberParameterOrdinals();- return;- }-- DataType[] dataTypes = new DataType[params.size() + 1];-- for (int i = 0; i < params.size(); i++) {- ParameterDB param = params.get(i);- param.setDynamicStorage(VariableStorage.UNASSIGNED_STORAGE);- dataTypes[i + 1] = param.getDataType();- }-- dataTypes[0] = returnParam.getFormalDataType();- returnParam.setDynamicStorage(- VoidDataType.isVoidDataType(dataTypes[0]) ? VariableStorage.VOID_STORAGE- : VariableStorage.UNASSIGNED_STORAGE);-- PrototypeModel callingConvention = getCallingConvention();- if (callingConvention == null) {- callingConvention = manager.getDefaultCallingConvention();- }- if (callingConvention == null) {- return;- }-- VariableStorage[] variableStorage =- callingConvention.getStorageLocations(program, dataTypes, true);- returnParam.setDynamicStorage(variableStorage[0]);-- int autoIndex = 0;- int paramIndex = 0;-- autoParams = null;-- for (int i = 1; i < variableStorage.length; i++) {- VariableStorage storage = variableStorage[i];- if (storage.isAutoStorage()) {- if (autoParams == null) {- autoParams = new ArrayList<>();- }- DataType dt = VariableUtilities.getAutoDataType(this,- returnParam.getFormalDataType(), storage);- try {- autoParams.add(new AutoParameterImpl(dt, autoIndex++, storage, this));- }- catch (InvalidInputException e) {- Msg.error(this,- "Unexpected error during dynamic storage assignment for function at " +- getEntryPoint(),- e);- break;- }- }- else {- ParameterDB parameterDB = params.get(paramIndex++);- parameterDB.setDynamicStorage(storage);- }- }-- renumberParameterOrdinals();- }-- int getAutoParamCount() {- return autoParams != null ? autoParams.size() : 0;- }-- private void renumberParameterOrdinals() {- int ordinal = autoParams != null ? autoParams.size() : 0;- for (ParameterDB param : params) {- param.setOrdinal(ordinal++);- }- }-- /**- * Re-assign dynamic storage for return- */-// void updateReturn() {-//-// if (returnParam == null) {-// return;-// }-//-// // allow lazy update of return dynamic storage-// returnParam.setDynamicStorage(null);-// }-- private void purgeBadVariables() {- if (!foundBadVariables) {- return;- }- List<Symbol> badSymbols = new ArrayList<>();- SymbolIterator it = program.getSymbolTable().getChildren(functionSymbol);- while (it.hasNext()) {- SymbolDB s = (SymbolDB) it.next();- if (s instanceof VariableSymbolDB) {- VariableSymbolDB varSym = (VariableSymbolDB) s;- if (isBadVariable(varSym)) {- badSymbols.add(s);- }- }- }- program.getBookmarkManager()- .setBookmark(getEntryPoint(), BookmarkType.ERROR, "Bad Variables Removed",- "Removed " + badSymbols.size() + " bad variables");- for (Symbol s : badSymbols) {- s.delete();- }- if (hasCustomVariableStorage()) {- ReturnParameterDB rtnParam = getReturn();- if (rtnParam.getVariableStorage().isBadStorage()) {- DataType dt = rtnParam.getDataType();- VariableStorage storage =- VoidDataType.isVoidDataType(dt) ? VariableStorage.VOID_STORAGE- : VariableStorage.UNASSIGNED_STORAGE;- rtnParam.setStorageAndDataType(storage, dt);- }- }- foundBadVariables = false;- }-FunctionManagerDB getFunctionManager() {return manager;}@@ -913,80 +607,16 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic VariableDB addLocalVariable(Variable var, SourceType source)throws DuplicateNameException, InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {return thunkedFunction.addLocalVariable(var, source);}- loadVariables();- purgeBadVariables();-- var = getResolvedVariable(var, false, false);-- String name = var.getName();- if (name == null || name.length() == 0 ||- SymbolUtilities.isDefaultParameterName(name)) {- name = DEFAULT_LOCAL_PREFIX;- source = SourceType.DEFAULT;- }-- VariableStorage storage = var.getVariableStorage();- int firstUseOffset = var.getFirstUseOffset();- if (var.hasStackStorage() && firstUseOffset != 0) {- Msg.info(this, "WARNING! Stack variable firstUseOffset forced to 0 for function " +- this + " at " + storage);- firstUseOffset = 0;- }-- // Check for duplicate storage address- VariableDB v = null;- for (VariableDB oldVar : locals) {- if (oldVar.getFirstUseOffset() == firstUseOffset &&- oldVar.getVariableStorage().intersects(storage)) {- v = oldVar;- break;- }- }-- try {- if (validateEnabled) {- VariableUtilities.checkVariableConflict(this, (v != null ? v : var), storage,- true);- }- if (v != null) {- // update existing variable- Msg.info(this, "WARNING! Adding overlapping local variable for function " +- this + " at " + v.getVariableStorage() + " - Modifying existing variable!");- if (!DEFAULT_LOCAL_PREFIX.equals(name)) {- v.setName(name, source);- }- v.setStorageAndDataType(storage, var.getDataType());- }- else {- SymbolManager symbolMgr = program.getSymbolTable();- VariableSymbolDB s = symbolMgr.createVariableSymbol(name, this,- SymbolType.LOCAL_VAR, firstUseOffset, storage, source);- s.setStorageAndDataType(storage, var.getDataType());- v = new LocalVariableDB(this, s);- locals.add(v);- Collections.sort(locals);- symbolMap.put(v.symbol, v);- }- if (var.getComment() != null) {- v.symbol.setSymbolComment(var.getComment());- }- manager.functionChanged(this, null);- return v;- }- finally {- frame.setInvalid();- }+ return getFunctionVariables().addLocalVariable(this, var, source);}finally {endUpdate();- manager.lock.release();}}@@ -1078,38 +708,15 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic Variable[] getVariables(VariableFilter filter) {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (thunkedFunction != null) {Variable[] variables =thunkedFunction.getVariables(new ThunkVariableFilter(filter));return adjustThunkThisParameter(variables);}- loadVariables();- ArrayList<Variable> list = new ArrayList<>();- if (autoParams != null) {- for (AutoParameterImpl p : autoParams) {- if (filter == null || filter.matches(p)) {- list.add(p);- }- }- }- for (ParameterDB p : params) {- if (filter == null || filter.matches(p)) {- list.add(p);- }- }- for (VariableDB var : locals) {- if (filter == null || filter.matches(var)) {- list.add(var);- }- }- Variable[] vars = new Variable[list.size()];- return list.toArray(vars);- }- finally {- manager.lock.release();+ FunctionVariables variables = getFunctionVariables();+ return variables.getVariables(filter);}}@@ -1120,32 +727,13 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic Parameter[] getParameters(VariableFilter filter) {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (thunkedFunction != null) {Parameter[] parameters = thunkedFunction.getParameters(filter);return adjustThunkThisParameter(parameters);}- loadVariables();- ArrayList<Parameter> list = new ArrayList<>();- if (autoParams != null) {- for (AutoParameterImpl p : autoParams) {- if (filter == null || filter.matches(p)) {- list.add(p);- }- }- }- for (ParameterDB p : params) {- if (filter == null || filter.matches(p)) {- list.add(p);- }- }- Parameter[] vars = new Parameter[list.size()];- return list.toArray(vars);- }- finally {- manager.lock.release();+ return getFunctionVariables().getParameters(filter);}}@@ -1156,24 +744,12 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic Variable[] getLocalVariables(VariableFilter filter) {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (thunkedFunction != null) {return new Variable[0];}- loadVariables();- ArrayList<Variable> list = new ArrayList<>();- for (VariableDB var : locals) {- if (filter == null || filter.matches(var)) {- list.add(var);- }- }- Variable[] vars = new Variable[list.size()];- return list.toArray(vars);- }- finally {- manager.lock.release();+ return getFunctionVariables().getLocalVariables(filter);}}@@ -1184,82 +760,24 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic int getParameterCount() {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (thunkedFunction != null) {return thunkedFunction.getParameterCount();}- loadVariables();- int count = params.size();- if (autoParams != null) {- count += autoParams.size();- }- return count;- }- finally {- manager.lock.release();+ return getFunctionVariables().getParameterCount();}}@Overridepublic int getAutoParameterCount() {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (thunkedFunction != null) {return thunkedFunction.getParameterCount();}- loadVariables();- if (autoParams != null) {- return autoParams.size();- }- return 0;- }- finally {- manager.lock.release();- }- }-- /**- * Resolve a variable's type and storage.- * @param var variable to be resolved- * @param voidOK if true the use of a 0-length {@link VoidDataType} for the specified- * variable is allowed (i.e., {@link ReturnParameterDB}), else false should be specified.- * @param useUnassignedStorage if true storage should be set to {@link VariableStorage#UNASSIGNED_STORAGE}- * else an attempt should be made to adjust the storage.- * @return resolved variable- * @throws InvalidInputException if unable to resize variable storage due to- * resolved datatype size change- */- Variable getResolvedVariable(Variable var, boolean voidOK, boolean useUnassignedStorage)- throws InvalidInputException {- DataType dt = var.getDataType();- if (var instanceof Parameter) {- dt = ((Parameter) var).getFormalDataType();- }- dt = VariableUtilities.checkDataType(dt, voidOK, Math.min(1, var.getLength()), program);- DataType resolvedDt = program.getDataTypeManager().resolve(dt, null);- VariableStorage storage = VariableStorage.UNASSIGNED_STORAGE;- if (!useUnassignedStorage) {- storage = var.getVariableStorage();- if (storage.isAutoStorage()) {- storage = new VariableStorage(program, storage.getVarnodes());- }- if (resolvedDt.getLength() != storage.size()) {- try {- storage = VariableUtilities.resizeStorage(storage, resolvedDt, true, this);- }- catch (Exception e) {- // ignore sizing issues- }- }+ return getFunctionVariables().getAutoParamCount();}-- LocalVariableImpl resolvedVar = new LocalVariableImpl(var.getName(),- var.getFirstUseOffset(), resolvedDt, storage, true, program, var.getSource());- resolvedVar.setComment(var.getComment());- return resolvedVar;}@Override@@ -1306,8 +824,7 @@ public class FunctionDB extends DatabaseObject implements Function {List<? extends Variable> newParams, FunctionUpdateType updateType, boolean force,SourceType source) throws DuplicateNameException, InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -1315,225 +832,42 @@ public class FunctionDB extends DatabaseObject implements Function {force, source);return;}-- loadVariables();- purgeBadVariables();-boolean useCustomStorage = (updateType == FunctionUpdateType.CUSTOM_STORAGE);setCustomVariableStorage(useCustomStorage);-if (callingConvention != null) {setCallingConvention(callingConvention);}callingConvention = getCallingConventionName();- if (returnVar == null) {- returnVar = returnParam;- }- else if (returnVar.isUniqueVariable()) {- throw new IllegalArgumentException(- "Invalid return specified: UniqueVariable not allowed");- }-- DataType returnType = returnVar.getDataType();- VariableStorage returnStorage = returnVar.getVariableStorage();-- if (!useCustomStorage) {- // remove auto params and forced-indirect return- newParams = new ArrayList<Variable>(newParams); // copy for edit- boolean thisParamRemoved =- removeExplicitThisParameter(newParams, callingConvention);- DataType dt = removeExplicitReturnStoragePtrParameter(newParams);- if (dt != null) {- returnVar = revertIndirectParameter(returnVar, dt, true);- }- if (returnVar instanceof Parameter) {- returnType = ((Parameter) returnVar).getFormalDataType();- }- returnStorage = VariableStorage.UNASSIGNED_STORAGE;-- if (updateType == FunctionUpdateType.DYNAMIC_STORAGE_ALL_PARAMS &&- !thisParamRemoved &&- CompilerSpec.CALLING_CONVENTION_thiscall.equals(callingConvention) &&- newParams.size() != 0) {- // Attempt to remove inferred unnamed 'this' parameter- // WARNING! This is a bit of a hack - not sure how to account for what may be auto-params- // within a list of parameters computed via analysis- Variable firstParam = newParams.get(0);- if (firstParam.getSource() == SourceType.DEFAULT &&- firstParam.getLength() == program.getDefaultPointerSize()) {- newParams.remove(0);- }- }- }-- // Update return data type- getReturn().setDataType(returnType, returnStorage, true, source);-- Set<String> nonParamNames = new HashSet<>();- for (Symbol s : program.getSymbolTable().getSymbols(this)) {- if (s.getSource() != SourceType.DEFAULT &&- s.getSymbolType() != SymbolType.PARAMETER) {- nonParamNames.add(s.getName());- }- }-- // Must ensure that all names do not conflict and that variable types are- // resolved to this program so that they have the proper sizes- List<Variable> clonedParams = new ArrayList<>();- for (int i = 0; i < newParams.size(); i++) {- Variable p = newParams.get(i);- if (!useCustomStorage && (p instanceof AutoParameterImpl)) {- continue;- }- if (p.isUniqueVariable()) {- throw new IllegalArgumentException(- "Invalid parameter specified: UniqueVariable not allowed");- }- checkForParameterNameConflict(p, newParams, nonParamNames);- clonedParams.add(getResolvedVariable(p, false, !useCustomStorage));- }- newParams = clonedParams;-- if (useCustomStorage) {- checkStorageConflicts(newParams, force);- }-- // Repopulate params list- List<ParameterDB> oldParams = params;- params = new ArrayList<>();-- // Clear current param names- for (ParameterDB param : oldParams) {- param.setName(null, SourceType.DEFAULT);- }-- int newParamIndex = 0;-- // Reassign old parameters if possible- while (newParamIndex < oldParams.size() && newParamIndex < newParams.size()) {- ParameterDB oldParam = oldParams.get(newParamIndex);- Variable newParam = newParams.get(newParamIndex++);- DataType dt = (newParam instanceof Parameter && !useCustomStorage)- ? ((Parameter) newParam).getFormalDataType()- : newParam.getDataType();- oldParam.setName(newParam.getName(), newParam.getSource());- oldParam.setStorageAndDataType(newParam.getVariableStorage(), dt);- oldParam.setComment(newParam.getComment());- params.add(oldParam); // re-add to list- }-- // Remove unused old parameters- for (int i = newParamIndex; i < oldParams.size(); i++) {- ParameterDB oldParam = oldParams.get(i);- Symbol s = oldParam.getSymbol();- symbolMap.remove(s);- s.delete();- }-- // Append new parameters if needed- SymbolManager symbolMgr = program.getSymbolTable();- for (int i = newParamIndex; i < newParams.size(); i++) {- Variable newParam = newParams.get(i);- DataType dt = (newParam instanceof Parameter && !useCustomStorage)- ? ((Parameter) newParam).getFormalDataType()- : newParam.getDataType();- VariableStorage storage = useCustomStorage ? newParam.getVariableStorage()- : VariableStorage.UNASSIGNED_STORAGE;- String name = newParam.getName();- if (name == null || name.length() == 0) {- name = SymbolUtilities.getDefaultParamName(i);- }- VariableSymbolDB s = symbolMgr.createVariableSymbol(name, this,- SymbolType.PARAMETER, i, storage, newParam.getSource());- s.setStorageAndDataType(storage, dt);- ParameterDB paramDb = new ParameterDB(this, s);- paramDb.setComment(newParam.getComment());- params.add(i, paramDb);- symbolMap.put(s, paramDb);- }+ getFunctionVariables().updateFunction(this, callingConvention, returnVar, newParams,+ updateType, force, source);if (source != getStoredSignatureSource()) {setSignatureSource(source);}-- // assign dynamic storage- updateParametersAndReturn();-manager.functionChanged(this, PARAMETERS_CHANGED);}finally {frame.setInvalid();endUpdate();- manager.lock.release();}}- private void checkForParameterNameConflict(Variable param, List<? extends Variable> newParams,- Set<String> nonParamNames) throws DuplicateNameException {-- String name = param.getName();- if (name == null || name.length() == 0 || SymbolUtilities.isDefaultParameterName(name)) {- return;- }-- // Check for duplicate names- for (Variable chkParam : newParams) {- if (param == chkParam) {- continue;- }- if (name.equals(chkParam.getName())) {- throw new DuplicateNameException("Duplicate parameter name '" + name + "'");- }- }-- if (nonParamNames.contains(name)) {- throw new DuplicateNameException(- "Parameter name conflicts with a symbol within function named '" + name + "'");- }- }-- private void checkStorageConflicts(List<? extends Variable> newParams,- boolean removeConflictingLocals) throws VariableSizeException {-- VariableUtilities.VariableConflictHandler localConflictHandler = null;- if (removeConflictingLocals) {- localConflictHandler = conflicts -> {- for (Variable var : conflicts) {- removeVariable(var);- }- return true;- };- }-- for (Variable p : newParams) {- // check for storage conflicts if custom storage used- VariableUtilities.checkVariableConflict(newParams, p, p.getVariableStorage(), null);- VariableUtilities.checkVariableConflict(locals, p, p.getVariableStorage(),- localConflictHandler);- }-- }-@Overridepublic Parameter addParameter(Variable var, SourceType source)throws DuplicateNameException, InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {return thunkedFunction.addParameter(var, source);}- loadVariables();- purgeBadVariables();+ return getFunctionVariables().insertParameter(this, getParameterCount(), var, source);- return insertParameter(getParameterCount(), var, source);}finally {endUpdate();- manager.lock.release();}}@@ -1544,199 +878,48 @@ public class FunctionDB extends DatabaseObject implements Function {public ParameterDB insertParameter(int ordinal, Variable var, SourceType source)throws DuplicateNameException, InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {return thunkedFunction.insertParameter(ordinal, var, source);}- loadVariables();- purgeBadVariables();-- int autoCnt = 0;- if (autoParams != null) {- autoCnt = autoParams.size();- if (ordinal < autoCnt) {- throw new InvalidInputException(- "Parameter may not be inserted before auto-parameter");- }- }-- ordinal -= autoCnt;- if (ordinal < 0 || ordinal > params.size()) {- throw new IndexOutOfBoundsException("Ordinal value must be " + autoCnt +- " <= ordinal < " + (params.size() + autoCnt) + ": " + (ordinal + autoCnt));- }-- if (var.isUniqueVariable()) {- throw new IllegalArgumentException(- "Invalid parameter specified: UniqueVariable not allowed");- }-- boolean hasCustomStorage = hasCustomVariableStorage();- if (hasCustomStorage) {- if (validateEnabled && var.hasStackStorage()) {- int stackOffset = (int) var.getLastStorageVarnode().getOffset();- if (!frame.isParameterOffset(stackOffset)) {- throw new InvalidInputException(- "Variable contains invalid stack parameter offset: " + var.getName() +- " offset " + stackOffset);- }- }- }-- var = getResolvedVariable(var, false, !hasCustomStorage);-- String name = var.getName();- SourceType paramSource = source;- if (name == null || name.length() == 0 || paramSource == SourceType.DEFAULT ||- SymbolUtilities.isDefaultParameterName(name)) {- name = DEFAULT_PARAM_PREFIX;- paramSource = SourceType.DEFAULT;- }-- VariableStorage storage = var.getVariableStorage();- if (!hasCustomStorage) {- storage = VariableStorage.UNASSIGNED_STORAGE;- }- else if (storage.isAutoStorage()) {- storage = new VariableStorage(program, storage.getVarnodes());- }-- try {-- // Check for duplicate storage address- ParameterDB p = null;- if (storage != VariableStorage.UNASSIGNED_STORAGE) {- for (ParameterDB oldParam : params) {- if (oldParam.getVariableStorage().intersects(storage)) {- p = oldParam;- break;- }- }- if (validateEnabled) {- VariableUtilities.checkVariableConflict(this, (p != null ? p : var),- storage, true);- }- }- if (p != null) {- // storage has been specified- // move and update existing parameter- if (ordinal >= params.size()) {- ordinal = params.size() - 1;- }- Msg.info(this, "WARNING! Inserting overlapping parameter for function " + this +- " at " + p.getVariableStorage() + " - Replacing existing parameter!");- if (p.getOrdinal() != ordinal) {- if (p != params.remove(p.getOrdinal())) {- throw new AssertException("Inconsistent function parameter cache");- }-- params.add(ordinal, p);- updateParametersAndReturn();- manager.functionChanged(this, PARAMETERS_CHANGED);- }- if (!DEFAULT_PARAM_PREFIX.equals(name)) {- p.setName(name, paramSource);- }- p.setStorageAndDataType(storage, var.getDataType());- }- else {- // create new parameter- if (ordinal > params.size()) {- ordinal = params.size();- }- if (ordinal != params.size()) {- // shift params to make room for inserted param- for (ParameterDB param : params) {- int paramOrdinal = param.getOrdinal();- if (paramOrdinal >= ordinal) {- param.setOrdinal(paramOrdinal + 1);- }- }- }- SymbolManager symbolMgr = program.getSymbolTable();- VariableSymbolDB s = symbolMgr.createVariableSymbol(name, this,- SymbolType.PARAMETER, ordinal, storage, paramSource);- s.setStorageAndDataType(storage, var.getDataType());- p = new ParameterDB(this, s);-- params.add(ordinal, p);- updateParametersAndReturn();- symbolMap.put(p.symbol, p);- manager.functionChanged(this, PARAMETERS_CHANGED);- }- if (var.getComment() != null) {- p.symbol.setSymbolComment(var.getComment());- }- updateSignatureSourceAfterVariableChange(source, p.getDataType());- return p;- }- finally {- frame.setInvalid();- }+ return getFunctionVariables().insertParameter(this, ordinal, var, source);}finally {endUpdate();- manager.lock.release();}}@Overridepublic void removeVariable(Variable variable) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {thunkedFunction.removeVariable(variable);return;}- loadVariables();-- if (variable instanceof VariableDB) {- Symbol s = ((VariableDB) variable).symbol;- if (symbolMap.containsKey(s)) {- s.delete(); // results in callback to doDeleteVariable- }- }+ getFunctionVariables().removeVariable(variable);}finally {endUpdate();- manager.lock.release();}}@Overridepublic void removeParameter(int ordinal) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {thunkedFunction.removeParameter(ordinal);return;}- loadVariables();- if (ordinal < 0) {- throw new IndexOutOfBoundsException();- }- if (autoParams != null) {- if (ordinal < autoParams.size()) {- return; // ignore- }- ordinal -= autoParams.size();- }- if (ordinal >= params.size()) {- throw new IndexOutOfBoundsException();- }- ParameterDB param = params.get(ordinal);- param.symbol.delete(); // results in callback to doDeleteVariable+ getFunctionVariables().removeParameter(ordinal);}finally {endUpdate();- manager.lock.release();}}@@ -1753,12 +936,7 @@ public class FunctionDB extends DatabaseObject implements Function {updateRefreshRequired = true;return true;}- symbolMap = null;- params = null;- locals = null;- autoParams = null;- returnParam = null;- foundBadVariables = false;+ lazyVariables = null;tags = null;try {if (refreshRec == null) {@@ -1786,59 +964,16 @@ public class FunctionDB extends DatabaseObject implements Function {* @param symbol variable symbol which is about to be deleted.*/public void doDeleteVariable(VariableSymbolDB symbol) {- manager.lock.acquire();try {startUpdate();- if (!checkIsValid()) {+ if (!refreshIfNeeded()) {return;}-- if (isBadVariable(symbol)) {- // don't do anything here with bad variable symbol- return;- }-- loadVariables();- VariableDB var = symbolMap.remove(symbol);-- if (var != null) {- if (var instanceof Parameter) {- if (removeVariable(params, var)) {- updateParametersAndReturn();- }- }- else {- removeVariable(locals, var);- }- }-- manager.functionChanged(this, (var instanceof Parameter) ? PARAMETERS_CHANGED : null);- frame.setInvalid();+ getFunctionVariables().doDeleteVariable(this, symbol);}finally {endUpdate();- manager.lock.release();- }- }-- /**- * Remove variable instance from list.- *- * @param list- * variable list- * @param var- * variable instance- * @return true if deleted- */- private boolean removeVariable(List<?> list, VariableDB var) {- int cnt = list.size();- for (int i = 0; i < cnt; i++) {- if (var == list.get(i)) {- list.remove(i);- return true;- }}- return false;}/**@@ -1848,46 +983,20 @@ public class FunctionDB extends DatabaseObject implements Function {* @return Variable which corresponds to specified symbol*/public Variable getVariable(VariableSymbolDB symbol) {- manager.lock.acquire();- try {- checkIsValid();- loadVariables();- return symbolMap.get(symbol);- }- finally {- manager.lock.release();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ return getFunctionVariables().getVariable(symbol);}}@Overridepublic Parameter getParameter(int ordinal) {- manager.lock.acquire();- try {- checkIsValid();- if (thunkedFunction != null) {- Parameter parameter = thunkedFunction.getParameter(ordinal);- return parameter != null ? adjustThunkThisParameter(parameter) : null;- }+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (ordinal == Parameter.RETURN_ORIDINAL) {return getReturn();}- if (ordinal < 0) {- return null;- }- loadVariables();- if (autoParams != null) {- if (ordinal < autoParams.size()) {- return autoParams.get(ordinal);- }- ordinal -= autoParams.size();- }- if (ordinal < params.size()) {- return params.get(ordinal);- }- return null;- }- finally {- manager.lock.release();+ return getFunctionVariables().getParameter(ordinal);}}@@ -1896,79 +1005,21 @@ public class FunctionDB extends DatabaseObject implements Function {if (toOrdinal < 0) {throw new InvalidInputException("invalid toOrdinal specified: " + toOrdinal);}- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {return thunkedFunction.moveParameter(fromOrdinal, toOrdinal);}- loadVariables();-- int autoCnt = 0;- if (autoParams != null) {- autoCnt = autoParams.size();- if (fromOrdinal < autoCnt) {- throw new InvalidInputException("Auto-parameter may not be moved");- }- if (toOrdinal < autoCnt) {- throw new InvalidInputException(- "Parameter may not be moved before an auto-parameter");- }- }-- fromOrdinal -= autoCnt;- toOrdinal -= autoCnt;-- if (fromOrdinal < 0 || fromOrdinal >= params.size()) {- return null;- }- ParameterDB param = params.get(fromOrdinal);- if (param.getOrdinal() == toOrdinal) {- return param;- }- params.remove(fromOrdinal);- if (toOrdinal >= params.size()) {- params.add(param);- }- else {- params.add(toOrdinal, param);- }- updateParametersAndReturn();- manager.functionChanged(this, PARAMETERS_CHANGED);- return param;+ return getFunctionVariables().moveParameter(this, fromOrdinal, toOrdinal);}finally {endUpdate();- manager.lock.release();- }- }--// int getLocalSize() {-// manager.lock.acquire();-// try {-// checkIsValid();-// return rec.getIntValue(FunctionAdapter.STACK_LOCAL_SIZE_COL);-// }-// finally {-// manager.lock.release();-// }-// }--// int getParameterOffset() {-// manager.lock.acquire();-// try {-// checkIsValid();-// return rec.getIntValue(FunctionAdapter.STACK_PARAM_OFFSET_COL);-// }-// finally {-// manager.lock.release();-// }-// }+ }+ }void setLocalSize(int size) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (size < 0) {@@ -1986,46 +1037,18 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();- }- }--// void setParameterOffset(int offset) {-// manager.lock.acquire();-// try {-// checkDeleted();-//// if (validateEnabled && !frame.isParameterOffset(offset)) {-//// throw new InvalidInputException("Invalid parameter offset " + offset);-//// }-// rec.setIntValue(FunctionAdapter.STACK_PARAM_OFFSET_COL, offset);-// try {-// manager.getFunctionAdapter().updateFunctionRecord(rec);-// manager.functionChanged(this);-// }-// catch (IOException e) {-// manager.dbError(e);-// }-// frame.setInvalid();-// }-// finally {-// manager.lock.release();-// }-// }+ }+ }int getReturnAddressOffset() {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return rec.getIntValue(FunctionAdapter.STACK_RETURN_OFFSET_COL);}- finally {- manager.lock.release();- }}void setReturnAddressOffset(int offset) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();rec.setIntValue(FunctionAdapter.STACK_RETURN_OFFSET_COL, offset);@@ -2040,25 +1063,14 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}- /*- * (non-Javadoc)- *- * @see ghidra.program.model.symbol.Namespace#getSymbol()- */@Overridepublic Symbol getSymbol() {return functionSymbol;}- /*- * (non-Javadoc)- *- * @see ghidra.program.model.listing.Function#setParentScope(ghidra.program.model.symbol.Scope)- */@Overridepublic void setParentNamespace(Namespace newParentScope)throws DuplicateNameException, InvalidInputException, CircularDependencyException {@@ -2104,8 +1116,7 @@ public class FunctionDB extends DatabaseObject implements Function {*/@Overridepublic void setVarArgs(boolean hasVarArgs) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -2118,7 +1129,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -2129,8 +1139,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setInline(boolean isInline) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -2144,7 +1153,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -2155,8 +1163,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setNoReturn(boolean hasNoReturn) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -2169,7 +1176,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -2178,129 +1184,9 @@ public class FunctionDB extends DatabaseObject implements Function {return isFunctionFlagSet(FunctionAdapter.FUNCTION_CUSTOM_PARAM_STORAGE_FLAG);}-// private DataType getPointer(DataType dt, VariableStorage storage) {-// if (program.getDefaultPointerSize() == storage.size()) {-// return program.getDataTypeManager().getPointer(dt);-// }-// ProgramDataTypeManager dtm = program.getDataTypeManager();-// int defaultPtrSize = dtm.getDataOrganization().getPointerSize();-// int ptrSize = storage.size();-// if (ptrSize == 0 || defaultPtrSize == ptrSize) {-// return dtm.getPointer(dt);-// }-// return dtm.getPointer(dt, ptrSize);-// }-- private static int findExplicitThisParameter(List<? extends Variable> params) {- for (int i = 0; i < params.size(); i++) {- Variable p = params.get(i);- if (THIS_PARAM_NAME.equals(p.getName()) && (p.getDataType() instanceof Pointer)) {- return i;- }- }- return -1;- }-- /**- * Remove 'this' parameter if using __thiscall and first non-auto parameter is- * a pointer and named 'this'.- * @param params list of parameters to search and affect- * @param callingConventionName current function calling convention- * @return true if 'this' parameter removed (applies to __thiscall callingConventionName only), else false- */- private static boolean removeExplicitThisParameter(List<? extends Variable> params,- String callingConventionName) {- if (CompilerSpec.CALLING_CONVENTION_thiscall.equals(callingConventionName)) {- int thisIndex = findExplicitThisParameter(params);- if (thisIndex >= 0) {- params.remove(thisIndex); // remove explicit 'this' parameter- return true;- }- }- return false;- }-- /**- * Remove 'this' parameter if using __thiscall and first non-auto parameter is- * a pointer and named 'this'. Variables must be pre-loaded.- * @return true if 'this' parameter removed- */- private boolean removeExplicitThisParameter() {- if (CompilerSpec.CALLING_CONVENTION_thiscall.equals(getCallingConventionName())) {- int thisIndex = findExplicitThisParameter(params);- if (thisIndex >= 0) {- removeParameter(thisIndex); // remove explicit 'this' parameter- return true;- }- }- return false;- }-- private static int findExplicitReturnStoragePtrParameter(List<? extends Variable> params) {- for (int i = 0; i < params.size(); i++) {- Variable p = params.get(i);- if (RETURN_PTR_PARAM_NAME.equals(p.getName()) && (p.getDataType() instanceof Pointer)) {- return i;- }- }- return -1;- }-- private static DataType removeExplicitReturnStoragePtrParameter(- List<? extends Variable> params) {- int paramIndex = findExplicitReturnStoragePtrParameter(params);- if (paramIndex >= 0) {- Variable returnStoragePtrParameter = params.remove(paramIndex); // remove return storage parameter- DataType dt = returnStoragePtrParameter.getDataType();- if (dt instanceof Pointer ptr) {- return ptr.getDataType();- }- }- return null;- }-- private DataType removeExplicitReturnStoragePtrParameter() {- int paramIndex = findExplicitReturnStoragePtrParameter(params);- if (paramIndex >= 0) {- ParameterDB returnStoragePtrParameter = params.get(paramIndex);- DataType dt = returnStoragePtrParameter.getDataType();- removeParameter(paramIndex); // remove return storage parameter- if (dt instanceof Pointer ptr) {- return ptr.getDataType();- }- }- return null;- }-- /**- * Strip indirect pointer data type from a parameter.- * @param param parameter to be examined and optionally modified- * @param dt return datatype to be applied- * @param create if true the specified param will not be affected and a new parameter- * instance will be returned if strip performed, otherwise orginal param will be changed- * if possible and returned.- * @return parameter with pointer stripped or original param if pointer not used.- * Returned parameter will have unassigned storage if affected.- */- private static Variable revertIndirectParameter(Variable param, DataType dt, boolean create) {- try {- if (create) {- param = new ParameterImpl(param.getName(), dt, param.getProgram());- }- else {- param.setDataType(dt, VariableStorage.UNASSIGNED_STORAGE, false, param.getSource());- }- }- catch (InvalidInputException e) {- throw new AssertException(e); // unexpected- }- return param;- }-@Overridepublic void setCustomVariableStorage(boolean hasCustomVariableStorage) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -2310,69 +1196,16 @@ public class FunctionDB extends DatabaseObject implements Function {if (hasCustomVariableStorage == hasCustomVariableStorage()) {return;}- loadVariables();-- if (!hasCustomVariableStorage) {- // remove explicit 'this' param and return storage use if switching to dynamic storage- removeExplicitThisParameter();- DataType returnDt = removeExplicitReturnStoragePtrParameter();- if (returnDt != null) {- revertIndirectParameter(returnParam, returnDt, false);- }- }-- // get params and return prior to change- Parameter[] parameters = getParameters();-- // remove auto-parameters- autoParams = null;-- setFunctionFlag(FunctionAdapter.FUNCTION_CUSTOM_PARAM_STORAGE_FLAG,- hasCustomVariableStorage);-- int ordinal = 0;- for (Parameter p : parameters) {- if (p.isAutoParameter()) {- // must insert auto-params when switching to custom storage- try {- insertParameter(ordinal, new ParameterImpl(p, program),- SourceType.ANALYSIS);- ++ordinal;- }- catch (DuplicateNameException e) {- // skip - we don't want to rename auto-param- }- }- else {- // if not an auto-param p is a ParameterDB object- // commit parameter storage and forced-indirect pointer when switching to custom- // or switch to UNASSIGNED_STORAGE when switching to dynamic- VariableStorage storage =- hasCustomVariableStorage ? p.getVariableStorage().clone(program)- : VariableStorage.UNASSIGNED_STORAGE;- ((ParameterDB) p).setStorageAndDataType(storage, p.getDataType());- }+ try {+ getFunctionVariables().setCustomVariableStorage(this, hasCustomVariableStorage);}-- // commit return storage and forced-indirect pointer when switching to custom- // or switch to UNASSIGNED_STORAGE when switching to dynamic- VariableStorage storage =- hasCustomVariableStorage ? returnParam.getVariableStorage().clone(program)- : VariableStorage.UNASSIGNED_STORAGE;- returnParam.setStorageAndDataType(storage, returnParam.getDataType());-- if (!hasCustomVariableStorage) {- updateParametersAndReturn(); // assign dynamic storage+ catch (InvalidInputException e) {+ throw new AssertException(e); // should not occur}-manager.functionChanged(this, PARAMETERS_CHANGED);}- catch (InvalidInputException e) {- throw new AssertException(e); // should not occur- }finally {endUpdate();- manager.lock.release();}}@@ -2387,7 +1220,7 @@ public class FunctionDB extends DatabaseObject implements Function {* @return true if the indicated flag is set*/private boolean isFunctionFlagSet(byte functionFlagIndicator) {- validate(manager.lock);+ validate(lock);FunctionDB localThunkFunc = thunkedFunction;if (localThunkFunc != null) {return localThunkFunc.isFunctionFlagSet(functionFlagIndicator);@@ -2408,7 +1241,7 @@ public class FunctionDB extends DatabaseObject implements Function {* @param shouldBeSet* true means the indicated flag should be set.*/- private void setFunctionFlag(byte functionFlagIndicator, boolean shouldBeSet) {+ void setFunctionFlag(byte functionFlagIndicator, boolean shouldBeSet) {byte flags = rec.getByteValue(FunctionAdapter.FUNCTION_FLAGS_COL);if (shouldBeSet) {flags |= functionFlagIndicator;@@ -2428,18 +1261,14 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic SourceType getSignatureSource() {- manager.lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (thunkedFunction != null) {return thunkedFunction.getSignatureSource();}return getStoredSignatureSource();}- finally {- manager.lock.release();- }}SourceType getStoredSignatureSource() {@@ -2451,8 +1280,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setSignatureSource(SourceType signatureSource) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -2475,7 +1303,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -2494,7 +1321,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic String getCallingConventionName() {- if (!validate(manager.lock)) {+ if (!validate(lock)) {return UNKNOWN_CALLING_CONVENTION_STRING;}FunctionDB localThunkFunc = thunkedFunction;@@ -2523,8 +1350,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setCallingConvention(String name) throws InvalidInputException {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -2541,24 +1367,17 @@ public class FunctionDB extends DatabaseObject implements Function {return; // no change}- loadVariables();-rec.setByteValue(FunctionAdapter.CALLING_CONVENTION_ID_COL, newCallingConventionID);manager.getFunctionAdapter().updateFunctionRecord(rec);boolean hasCustomStorage = hasCustomVariableStorage();- if (!hasCustomStorage) {- // remove 'this' param if switching to __thiscall with dynamic storage- removeExplicitThisParameter();- }-+ FunctionVariables variables = getFunctionVariables();frame.setInvalid();if (!hasCustomStorage) {createClassStructIfNeeded(); // TODO: How should thunks within Class namespace be handled?- loadVariables();- removeExplicitThisParameter();- updateParametersAndReturn(); // assign dynamic storage+ variables.removeExplicitThisParameter(this);+ variables.updateParametersAndReturn(this, hasCustomStorage); // assign dynamic storagemanager.functionChanged(this, PARAMETERS_CHANGED);manager.functionChanged(this, RETURN_TYPE_CHANGED);}@@ -2571,7 +1390,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -2607,7 +1425,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic String getCallFixup() {- if (!validate(manager.lock)) {+ if (!validate(lock)) {return null;}FunctionDB localThunkFunc = thunkedFunction;@@ -2623,8 +1441,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void setCallFixup(String name) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();if (thunkedFunction != null) {@@ -2653,7 +1470,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -2717,11 +1533,9 @@ public class FunctionDB extends DatabaseObject implements Function {// cache will have the current tag state unless tags have been deleted or edited; in// those cases the validity check will fail and we'll be forced to go back to// the db.- manager.lock.acquire();+ try (Closeable c = lock.read()) {- try {-- if (checkIsValid() && tags != null) {+ if (refreshIfNeeded() && tags != null) {return tags;}@@ -2733,17 +1547,13 @@ public class FunctionDB extends DatabaseObject implements Function {catch (IOException e) {manager.dbError(e);}- finally {- manager.lock.release();- }return tags;}@Overridepublic boolean addTag(String name) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();FunctionTagManagerDB tagManager =@@ -2767,7 +1577,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}return true;@@ -2775,8 +1584,7 @@ public class FunctionDB extends DatabaseObject implements Function {@Overridepublic void removeTag(String name) {- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {startUpdate();checkDeleted();FunctionTag tag = manager.getFunctionTagManager().getFunctionTag(name);@@ -2800,7 +1608,6 @@ public class FunctionDB extends DatabaseObject implements Function {}finally {endUpdate();- manager.lock.release();}}@@ -2809,8 +1616,7 @@ public class FunctionDB extends DatabaseObject implements Function {if (isExternal()) {return;}- manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();ArrayList<Symbol> list = new ArrayList<>(20);for (Symbol childSymbol : program.getSymbolTable().getSymbols(this)) {@@ -2833,8 +1639,21 @@ public class FunctionDB extends DatabaseObject implements Function {}}}- finally {- manager.lock.release();+ }++ void functionChanged(FunctionChangeType type) {+ manager.functionChanged(this, type);+ }++ void invalidateFrame() {+ frame.setInvalid();++ }++ void updateParametersAndReturn() {+ if (lazyVariables == null) {+ return;}+ getFunctionVariables().updateParametersAndReturn(this, hasCustomVariableStorage());}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionManagerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionManagerDB.javaindex 91ea9243be..94302efdb2 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionManagerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionManagerDB.java@@ -26,8 +26,7 @@ import org.apache.commons.lang3.StringUtils;import db.*;import generic.FilteredIterator;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.ProgramDB;+import ghidra.program.database.*;import ghidra.program.database.code.CodeManager;import ghidra.program.database.data.DataTypeManagerDB;import ghidra.program.database.map.AddressMap;@@ -43,6 +42,7 @@ import ghidra.program.model.util.StringPropertyMap;import ghidra.program.util.*;import ghidra.program.util.FunctionChangeRecord.FunctionChangeType;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -60,7 +60,7 @@ public class FunctionManagerDB implements FunctionManager {private ProgramDB program;private DBHandle dbHandle;private AddressMap addrMap;- private DBObjectCache<FunctionDB> cache;+ private DbCache<FunctionDB> cache;private FunctionAdapter adapter;private ThunkFunctionAdapter thunkAdapter;private NamespaceManager namespaceMgr;@@ -103,7 +103,7 @@ public class FunctionManagerDB implements FunctionManager {this.dbHandle = dbHandle;this.addrMap = addrMap;this.lock = lock;- cache = new DBObjectCache<>(20);+ cache = new DbCache<>(new FunctionFactory(), lock, 10);initializeAdapters(openMode, monitor);functionTagManager = new FunctionTagManagerDB(dbHandle, openMode, lock, monitor);}@@ -167,27 +167,24 @@ public class FunctionManagerDB implements FunctionManager {public FunctionDB createExternalFunction(Address extSpaceAddr, String name, Namespace nameSpace,String originalImportName, Address externalProgramAddress, SourceType source)throws InvalidInputException {- lock.acquire();- try {++ try (Closeable c = lock.write()) {FunctionSymbol symbol = symbolMgr.createExternalFunctionSymbol(extSpaceAddr, name,nameSpace, source, originalImportName, externalProgramAddress);long returnDataTypeId = program.getDataTypeManager().getResolvedID(DataType.DEFAULT);DBRecord rec = adapter.createFunctionRecord(symbol.getID(), returnDataTypeId);+ FunctionDB function = new FunctionDB(this, addrMap, rec);+ cache.add(function);- FunctionDB funcDB = new FunctionDB(this, cache, addrMap, rec);-- program.setObjChanged(ProgramEvent.FUNCTION_ADDED, extSpaceAddr, funcDB, null, null);- return funcDB;+ program.setObjChanged(ProgramEvent.FUNCTION_ADDED, extSpaceAddr, function, null, null);+ return function;}catch (IOException e) {dbError(e); // will not returnreturn null;}- finally {- lock.release();- }}@Override@@ -229,8 +226,7 @@ public class FunctionManagerDB implements FunctionManager {AddressSetView body, Function thunkedFunction, SourceType source)throws InvalidInputException, OverlappingFunctionException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (entryPoint == null || !entryPoint.isMemoryAddress()) {throw new IllegalArgumentException("Function entryPoint must be a memory address");}@@ -298,11 +294,13 @@ public class FunctionManagerDB implements FunctionManager {DBRecord rec = adapter.createFunctionRecord(symbol.getID(), returnDataTypeId);- FunctionDB funcDB = new FunctionDB(this, cache, addrMap, rec);- namespaceMgr.setBody(funcDB, body);+ FunctionDB function = new FunctionDB(this, addrMap, rec);+ cache.add(function);+ namespaceMgr.setBody(function, body);- program.setObjChanged(ProgramEvent.FUNCTION_ADDED, entryPoint, funcDB, null, null);- return funcDB;+ program.setObjChanged(ProgramEvent.FUNCTION_ADDED, entryPoint, function, null,+ null);+ return function;}catch (IOException e) {dbError(e);@@ -312,9 +310,6 @@ public class FunctionManagerDB implements FunctionManager {}return null;}- finally {- lock.release();- }}void setThunkedFunction(FunctionDB function, FunctionDB referencedFunction)@@ -390,23 +385,17 @@ public class FunctionManagerDB implements FunctionManager {}public void functionNamespaceChanged(long key) {- lock.acquire();- try {- FunctionDB func = cache.get(key);- if (func != null) {- func.checkDeleted();- func.createClassStructIfNeeded();- func.updateParametersAndReturn();- }+ FunctionDB func = cache.getIfValid(key);+ if (func == null) {+ return;}- finally {- lock.release();+ try (Closeable c = lock.write()) {+ func.createClassStructIfNeeded();+ func.updateParametersAndReturn();}}public boolean doRemoveFunction(long key) {- lock.acquire();-try {FunctionDB function = (FunctionDB) getFunction(key);@@ -455,9 +444,6 @@ public class FunctionManagerDB implements FunctionManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return false;}@@ -469,26 +455,11 @@ public class FunctionManagerDB implements FunctionManager {*/@Overridepublic Function getFunction(long key) {- lock.acquire();- try {+ FunctionDB function = cache.getCachedInstance(key);+ if (function != null) {lastFuncID = key;- FunctionDB func = cache.get(key);- if (func == null) {- try {- DBRecord rec = adapter.getFunctionRecord(key);- if (rec != null) {- func = new FunctionDB(this, cache, addrMap, rec);- }- }- catch (IOException e) {- dbError(e);- }- }- return func;- }- finally {- lock.release();}+ return function;}@Override@@ -507,23 +478,17 @@ public class FunctionManagerDB implements FunctionManager {@Overridepublic Function getFunctionAt(Address entryPoint) {- lock.acquire();- try {- if (lastFuncID != -1) {- FunctionDB function = cache.get(lastFuncID);- if (function != null && function.getEntryPoint().equals(entryPoint)) {- return function;- }- }+ FunctionDB function = cache.getIfValid(lastFuncID);+ if (function != null && function.getEntryPoint().equals(entryPoint)) {+ return function;+ }+ try (Closeable c = lock.read()) {Symbol symbol = program.getSymbolTable().getPrimarySymbol(entryPoint);if (symbol != null && symbol.getSymbolType() == SymbolType.FUNCTION) {return getFunction(symbol.getID());}+ return null;}- finally {- lock.release();- }- return null;}@Override@@ -532,15 +497,12 @@ public class FunctionManagerDB implements FunctionManager {if (addr.isExternalAddress()) {return getFunctionAt(addr);}+ FunctionDB function = cache.getIfValid(lastFuncID);+ if (function != null && function.getBody().contains(addr)) {+ return function;+ }- lock.acquire();- try {- if (lastFuncID != -1) {- FunctionDB func = cache.get(lastFuncID);- if (func != null && func.getBody().contains(addr)) {- return func;- }- }+ try (Closeable c = lock.read()) {Namespace scope = namespaceMgr.getNamespaceContaining(addr);Symbol symbol = scope.getSymbol();while (symbol != null && symbol.getSymbolType() != SymbolType.FUNCTION) {@@ -551,9 +513,6 @@ public class FunctionManagerDB implements FunctionManager {}return getFunction(symbol.getID());}- finally {- lock.release();- }}@Override@@ -619,8 +578,7 @@ public class FunctionManagerDB implements FunctionManager {@Overridepublic void deleteAddressRange(Address startAddr, Address endAddr, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {// Remove functions which overlap deleted address rangeIterator<Function> iter = getFunctionsOverlapping(new AddressSet(startAddr, endAddr));while (iter.hasNext()) {@@ -629,9 +587,6 @@ public class FunctionManagerDB implements FunctionManager {removeFunction(func.getEntryPoint());}}- finally {- lock.release();- }}@Override@@ -818,15 +773,11 @@ public class FunctionManagerDB implements FunctionManager {@Overridepublic void invalidateCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {functionTagManager.invalidateCache();lastFuncID = -1;cache.invalidate();}- finally {- lock.release();- }}StringPropertyMap getCallFixupMap(boolean create) {@@ -1106,8 +1057,7 @@ public class FunctionManagerDB implements FunctionManager {// TODO: Add caching !!!- lock.acquire();- try {+ try (Closeable c = lock.read()) {Function func = getFunctionContaining(instrAddr);if (func == null) {return null;@@ -1192,14 +1142,10 @@ public class FunctionManagerDB implements FunctionManager {}return bestVar;}- finally {- lock.release();- }}public void replaceDataTypes(Map<Long, Long> dataTypeReplacementMap) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {RecordIterator it = adapter.iterateFunctionRecords();while (it.hasNext()) {DBRecord rec = it.next();@@ -1213,10 +1159,7 @@ public class FunctionManagerDB implements FunctionManager {if (replacementId != null) {rec.setLongValue(FunctionAdapter.RETURN_DATA_TYPE_ID_COL, replacementId);adapter.updateFunctionRecord(rec);- FunctionDB functionDB = cache.get(rec);- if (functionDB == null) {- functionDB = new FunctionDB(this, cache, addrMap, rec);- }+ FunctionDB functionDB = cache.getCachedInstance(rec);functionChanged(functionDB, RETURN_TYPE_CHANGED);}}@@ -1226,51 +1169,39 @@ public class FunctionManagerDB implements FunctionManager {}finally {cache.invalidate();- lock.release();}}public boolean isThunk(long key) {- lock.acquire();- try {- FunctionDB function = cache.get(key);- if (function != null) {- return function.isThunk();- }- try {- return thunkAdapter.getThunkRecord(key) != null;- }- catch (IOException e) {- dbError(e);- }- return false;+ FunctionDB function = cache.getCachedInstance(key);+ if (function != null) {+ return function.isThunk();}- finally {- lock.release();++ try (Closeable c = lock.read()) {+ return thunkAdapter.getThunkRecord(key) != null;+ }+ catch (IOException e) {+ dbError(e);+ return false;}}public long getThunkedFunctionId(long functionId) {- lock.acquire();- try {- FunctionDB function = cache.get(functionId);- if (function != null) {- Function thunkedFunction = function.getThunkedFunction(false);- return thunkedFunction != null ? thunkedFunction.getID() : -1;- }- try {- DBRecord rec = thunkAdapter.getThunkRecord(functionId);- return rec != null ? rec.getLongValue(ThunkFunctionAdapter.LINKED_FUNCTION_ID_COL)- : -1;- }- catch (IOException e) {- dbError(e);- }- return -1;+ FunctionDB function = cache.getCachedInstance(functionId);+ if (function != null) {+ Function thunkedFunction = function.getThunkedFunction(false);+ return thunkedFunction != null ? thunkedFunction.getID() : -1;}- finally {- lock.release();++ try (Closeable c = lock.read()) {+ DBRecord rec = thunkAdapter.getThunkRecord(functionId);+ return rec != null ? rec.getLongValue(ThunkFunctionAdapter.LINKED_FUNCTION_ID_COL) : -1;+ }+ catch (IOException e) {+ dbError(e);}+ return -1;}/**@@ -1279,9 +1210,8 @@ public class FunctionManagerDB implements FunctionManager {* @return list of thunk function IDs or null*/public List<Long> getThunkFunctionIds(long referencedFunctionId) {- lock.acquire();List<Long> list = null;- try {+ try (Closeable c = lock.read()) {RecordIterator records = thunkAdapter.iterateThunkRecords(referencedFunctionId);while (records.hasNext()) {DBRecord rec = records.next();@@ -1294,9 +1224,6 @@ public class FunctionManagerDB implements FunctionManager {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return list;}@@ -1326,8 +1253,7 @@ public class FunctionManagerDB implements FunctionManager {throws CancelledException {monitor.initialize(adapter.getRecordCount());int cnt = 0;- lock.acquire();- try {+ try (Closeable c = lock.write()) {RecordIterator recIter = adapter.iterateFunctionRecords();while (recIter.hasNext()) {monitor.checkCancelled();@@ -1355,7 +1281,6 @@ public class FunctionManagerDB implements FunctionManager {}finally {invalidateCache(true);- lock.release();}}@@ -1363,4 +1288,23 @@ public class FunctionManagerDB implements FunctionManager {public FunctionTagManager getFunctionTagManager() {return functionTagManager;}++ private class FunctionFactory implements DbFactory<FunctionDB> {+ @Override+ public FunctionDB instantiate(long key) {+ try {+ DBRecord record = adapter.getFunctionRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public FunctionDB instantiate(DBRecord record) {+ return new FunctionDB(FunctionManagerDB.this, addrMap, record);+ }+ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionStackFrame.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionStackFrame.javaindex ebc2d9464f..4321485a2b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionStackFrame.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionStackFrame.java@@ -21,6 +21,8 @@ import ghidra.program.model.data.DataType;import ghidra.program.model.listing.*;import ghidra.program.model.pcode.Varnode;import ghidra.program.model.symbol.SourceType;+import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -32,6 +34,7 @@ class FunctionStackFrame implements StackFrame {private FunctionDB function;private boolean stackGrowsNegative;private boolean invalid;+ private Lock lock;/*** Construct a function stack frame.@@ -42,6 +45,7 @@ class FunctionStackFrame implements StackFrame {this.function = function;stackGrowsNegative = function.getProgram().getCompilerSpec().stackGrowsNegative();invalid = true;+ lock = function.getFunctionManager().lock;}boolean checkIsValid() {@@ -80,8 +84,7 @@ class FunctionStackFrame implements StackFrame {// WARNING! Stack parameters will be created even if calling convention// does not specify stack inputs- function.manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (dataType != null) {dataType = dataType.clone(function.getProgram().getDataTypeManager());@@ -125,37 +128,21 @@ class FunctionStackFrame implements StackFrame {return var;}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#getStackVariables()- */@Overridepublic Variable[] getStackVariables() {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {checkIsValid();Variable[] temp = new Variable[variables.length];System.arraycopy(variables, 0, temp, 0, variables.length);return temp;}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#getLocals()- */@Overridepublic Variable[] getLocals() {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {checkIsValid();ArrayList<Variable> list = new ArrayList<Variable>();for (Variable variable : variables) {@@ -166,19 +153,11 @@ class FunctionStackFrame implements StackFrame {Variable[] vars = new Variable[list.size()];return list.toArray(vars);}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#getParameters()- */@Overridepublic Parameter[] getParameters() {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {checkIsValid();ArrayList<Parameter> list = new ArrayList<Parameter>();for (Variable variable : variables) {@@ -189,34 +168,18 @@ class FunctionStackFrame implements StackFrame {Parameter[] vars = new Parameter[list.size()];return list.toArray(vars);}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#getFrameSize()- */@Overridepublic int getFrameSize() {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {return getParameterSize() + getLocalSize();}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#getLocalSize()- */@Overridepublic int getLocalSize() {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {checkIsValid();int baseOffset = 0;@@ -253,24 +216,13 @@ class FunctionStackFrame implements StackFrame {}return -baseOffset;}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#growsNegative()- */@Overridepublic boolean growsNegative() {return stackGrowsNegative;}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#setLocalSize(int)- */@Overridepublic void setLocalSize(int size) {// TODO: Has no real affect@@ -279,8 +231,7 @@ class FunctionStackFrame implements StackFrame {@Overridepublic int getParameterSize() {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {checkIsValid();// NOTE: This logic is sensitive to the existance of Local variables at the incorrect@@ -317,17 +268,13 @@ class FunctionStackFrame implements StackFrame {}return 0;}- finally {- function.manager.lock.release();- }}/*** @return the number of parameters which occupy stack storage*/int getParameterCount() {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {checkIsValid();int cnt = 0;for (Variable variable : variables) {@@ -337,23 +284,14 @@ class FunctionStackFrame implements StackFrame {}return cnt;}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- *- * @see ghidra.program.model.listing.StackFrame#clearVariable(int)- */@Overridepublic void clearVariable(int offset) {// WARNING! removing Stack parameters from the stack frame will enable custom storage- function.manager.lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();Variable var = getVariableContaining(offset);if (var != null) {@@ -363,15 +301,8 @@ class FunctionStackFrame implements StackFrame {function.removeVariable(var);}}- finally {- function.manager.lock.release();- }}- /*- * (non-Javadoc)- * @see ghidra.program.model.listing.StackFrame#getParameterOffset()- */@Overridepublic int getParameterOffset() {Integer baseOffset = VariableUtilities.getBaseStackParamOffset(function);@@ -414,8 +345,7 @@ class FunctionStackFrame implements StackFrame {*/@Overridepublic Variable getVariableContaining(int offset) {- function.manager.lock.acquire();- try {+ try (Closeable c = lock.read()) {checkIsValid();Object key = Integer.valueOf(offset);int index = Arrays.binarySearch(variables, key, StackVariableComparator.get());@@ -435,9 +365,6 @@ class FunctionStackFrame implements StackFrame {}return null;}- finally {- function.manager.lock.release();- }}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagDB.javaindex f46e0857de..556770e765 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -19,21 +19,20 @@ import java.io.IOException;import java.util.Objects;import db.DBRecord;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.listing.FunctionTag;+import ghidra.util.Lock.Closeable;/*** Database object for {@link FunctionTagAdapter} objects.*/-public class FunctionTagDB extends DatabaseObject implements FunctionTag {+public class FunctionTagDB extends DbObject implements FunctionTag {private FunctionTagManagerDB mgr;private DBRecord record;- public FunctionTagDB(FunctionTagManagerDB mgr, DBObjectCache<FunctionTagDB> cache,- DBRecord record) {- super(cache, record.getKey());+ public FunctionTagDB(FunctionTagManagerDB mgr, DBRecord record) {+ super(record.getKey());this.mgr = mgr;this.record = record;}@@ -45,8 +44,7 @@ public class FunctionTagDB extends DatabaseObject implements FunctionTag {@Overridepublic void setComment(String comment) {- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();if (comment == null) {@@ -63,15 +61,11 @@ public class FunctionTagDB extends DatabaseObject implements FunctionTag {catch (IOException e) {mgr.dbError(e);}- finally {- mgr.lock.release();- }}@Overridepublic void setName(String name) {- mgr.lock.acquire();- try {+ try (Closeable c = mgr.lock.write()) {checkDeleted();if (name == null) {@@ -87,33 +81,22 @@ public class FunctionTagDB extends DatabaseObject implements FunctionTag {catch (IOException e) {mgr.dbError(e);}- finally {- mgr.lock.release();- }}@Overridepublic String getComment() {- mgr.lock.acquire();- try {- checkIsValid();+ try (Closeable c = mgr.lock.read()) {+ refreshIfNeeded();return record.getString(FunctionTagAdapter.COMMENT_COL);}- finally {- mgr.lock.release();- }}@Overridepublic String getName() {- mgr.lock.acquire();- try {- checkIsValid();+ try (Closeable c = mgr.lock.read()) {+ refreshIfNeeded();return record.getString(FunctionTagAdapter.NAME_COL);}- finally {- mgr.lock.release();- }}/**@@ -158,18 +141,14 @@ public class FunctionTagDB extends DatabaseObject implements FunctionTag {@Overridepublic void delete() {- mgr.lock.acquire();- try {- if (checkIsValid()) {+ try (Closeable c = mgr.lock.write()) {+ if (refreshIfNeeded()) {mgr.doDeleteTag(this);}}catch (IOException e) {mgr.dbError(e);}- finally {- mgr.lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagManagerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagManagerDB.javaindex f5349c84f5..7b7513251c 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagManagerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionTagManagerDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -23,12 +23,12 @@ import org.apache.commons.collections4.map.LazyMap;import db.*;import db.util.ErrorHandler;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.ProgramDB;+import ghidra.program.database.*;import ghidra.program.model.listing.*;import ghidra.program.util.ChangeManager;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.datastruct.Counter;import ghidra.util.exception.CancelledException;import ghidra.util.exception.VersionException;@@ -44,7 +44,7 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {// Table mapping function tags to functionsprivate FunctionTagMappingAdapter functionTagMappingAdapter;- private DBObjectCache<FunctionTagDB> cache;+ private DbCache<FunctionTagDB> cache;private Map<FunctionTag, Counter> tagCountCache;@@ -69,7 +69,7 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {functionTagAdapter = FunctionTagAdapter.getAdapter(handle, openMode, monitor);functionTagMappingAdapter = FunctionTagMappingAdapter.getAdapter(handle, openMode, monitor);- cache = new DBObjectCache<>(100);+ cache = new DbCache<>(new TagFactory(), lock, 100);}public void setProgram(Program program) {@@ -82,57 +82,28 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {}@Override- public FunctionTag getFunctionTag(String name) {- lock.acquire();-- try {+ public FunctionTagDB getFunctionTag(String name) {+ try (Closeable c = lock.read()) {DBRecord rec = functionTagAdapter.getRecord(name);if (rec != null) {- return getFunctionTagFromCache(rec);+ return cache.getCachedInstance(rec);}}catch (IOException e) {dbError(e);}- finally {- lock.release();- }-return null;}@Overridepublic FunctionTag getFunctionTag(long id) {-- lock.acquire();-- try {- FunctionTag tag = cache.get(id);- if (tag != null) {- return tag;- }-- DBRecord rec = functionTagAdapter.getRecord(id);- if (rec != null) {- return new FunctionTagDB(this, cache, rec);- }- }- catch (IOException e) {- dbError(e);- }- finally {- lock.release();- }-- return null;+ return cache.getCachedInstance(id);}@Overridepublic boolean isTagAssigned(String name) {- lock.acquire();-- try {+ try (Closeable c = lock.read()) {FunctionTag tag = getFunctionTag(name);if (tag == null) {return false;@@ -142,9 +113,6 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return false;}@@ -152,18 +120,17 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {@Overridepublic FunctionTag createFunctionTag(String name, String comment) {- lock.acquire();-- try {+ try (Closeable c = lock.write()) {// First make sure a tag doesn't already exist with this name. If it does,// just return it.- FunctionTag tag = getFunctionTag(name);+ FunctionTagDB tag = getFunctionTag(name);if (tag != null) {return tag;}DBRecord record = functionTagAdapter.createTagRecord(name, comment);- tag = getFunctionTagFromCache(record);+ tag = new FunctionTagDB(this, record);+ cache.add(tag);fireTagCreatedNotification(ProgramEvent.FUNCTION_TAG_CREATED, tag);return tag;@@ -171,33 +138,23 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return null;}boolean isTagApplied(long functionId, long tagId) {- lock.acquire();-- try {+ try (Closeable c = lock.read()) {return functionTagMappingAdapter.getRecord(functionId, tagId) != null;}catch (IOException e) {dbError(e);}- finally {- lock.release();- }return false;}void applyFunctionTag(long functionId, long tagId) {- lock.acquire();-- try {+ try (Closeable c = lock.write()) {FunctionTag tag = getFunctionTag(tagId);if (tag == null) {return; // shouldn't happen@@ -209,9 +166,6 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {catch (IOException e) {dbError(e);}- finally {- lock.release();- }}private void incrementCountCache(FunctionTag tag) {@@ -228,8 +182,6 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {boolean removeFunctionTag(long functionId, long tagId) {- lock.acquire();-try {FunctionTag tag = getFunctionTag(tagId);if (tag == null) {@@ -244,9 +196,6 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return false;}@@ -263,14 +212,12 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {@Overridepublic List<? extends FunctionTag> getAllFunctionTags() {- lock.acquire();-- try {+ try (Closeable c = lock.read()) {List<FunctionTag> tags = new ArrayList<>();RecordIterator records = functionTagAdapter.getRecords();while (records.hasNext()) {DBRecord record = records.next();- tags.add(getFunctionTagFromCache(record));+ tags.add(cache.getCachedInstance(record));}return tags;@@ -278,9 +225,6 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return Collections.emptyList();}@@ -325,21 +269,6 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {program.tagChanged(tag, eventType, tag, null);}- /**- * Returns the cache object for the given Record. If the object is not in- * the cache, a new cache object is created.- *- * @param tagRecord the tag record to retrieve- * @return tag new cached tag object- */- private FunctionTag getFunctionTagFromCache(DBRecord tagRecord) {- FunctionTagDB tag = cache.get(tagRecord);- if (tag == null) {- tag = new FunctionTagDB(this, cache, tagRecord);- }- return tag;- }-/*** Deletes the given function tag.*@@ -386,7 +315,7 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {DBRecord mappingRecord = functionRecords.next();DBRecord tagRecord = functionTagAdapter.getRecord(mappingRecord.getLongValue(FunctionTagMappingAdapter.TAG_ID_COL));- tags.add(getFunctionTagFromCache(tagRecord));+ tags.add(cache.getCachedInstance(tagRecord));}return tags;}@@ -398,8 +327,7 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {@Overridepublic int getUseCount(FunctionTag tag) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (tagCountCache == null) {buildTagCountCache();}@@ -410,9 +338,6 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {dbError(e);return 0;}- finally {- lock.release();- }}private void buildTagCountCache() throws IOException {@@ -426,4 +351,25 @@ public class FunctionTagManagerDB implements FunctionTagManager, ErrorHandler {}tagCountCache = map;}++ private class TagFactory implements DbFactory<FunctionTagDB> {++ @Override+ public FunctionTagDB instantiate(long dataTypeId) {+ try {+ DBRecord record = functionTagAdapter.getRecord(dataTypeId);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public FunctionTagDB instantiate(DBRecord rec) {+ return new FunctionTagDB(FunctionTagManagerDB.this, rec);+ }+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionVariables.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionVariables.javanew file mode 100644index 0000000000..ce49273583--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionVariables.java@@ -0,0 +1,1023 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.database.function;++import static ghidra.program.util.FunctionChangeRecord.FunctionChangeType.*;++import java.util.*;++import ghidra.program.database.ProgramDB;+import ghidra.program.database.symbol.*;+import ghidra.program.model.address.Address;+import ghidra.program.model.data.*;+import ghidra.program.model.lang.CompilerSpec;+import ghidra.program.model.lang.PrototypeModel;+import ghidra.program.model.listing.*;+import ghidra.program.model.listing.Function.FunctionUpdateType;+import ghidra.program.model.symbol.*;+import ghidra.util.Msg;+import ghidra.util.exception.*;++class FunctionVariables {+ private Map<SymbolDB, VariableDB> symbolMap;+ private ReturnParameterDB returnParam;+ private List<AutoParameterImpl> autoParams;+ private List<ParameterDB> params;+ private List<VariableDB> locals;++ /**+ * foundBadVariables is set true when one or more variable symbols are found+ * which no longer decode a valid storage address (indicated by Address.NO_ADDRESS).+ * Any time a variable is added while this flag is set, such bad variables should be purged.+ */+ private boolean foundBadVariables;+ /**+ * Use of stack frame to compute parameter ordinals and validate stack offsets+ * should not be done while <code>validateEnabled</code> is false. This may be+ * necessary during a language upgrade in which case a dummy compiler-spec+ * may be in-use.+ */+ private boolean validateEnabled = true;++ FunctionVariables(FunctionDB function, boolean hasCustomStorage) {+ loadSymbolBasedVariables(function);+ loadReturn(function, hasCustomStorage);+ if (foundBadVariables) {+ Msg.warn(this,+ "Found one or more bad variables in function " + function.getName() + " at " ++ function.getEntryPoint());+ }+ if (!hasCustomStorage) {+ updateParametersAndReturn(function, hasCustomStorage); // assign dynamic storage (includes return and auto-params)+ }+ }++ ReturnParameterDB getReturnParam() {+ return returnParam;+ }++ private boolean loadSymbolBasedVariables(FunctionDB function) {+ if (symbolMap != null) {+ return false;+ }+ symbolMap = new HashMap<>();+ locals = new ArrayList<>();+ params = new ArrayList<>();+ autoParams = null;+ Symbol functionSymbol = function.getSymbol();+ SymbolIterator it = function.getProgram().getSymbolTable().getChildren(functionSymbol);+ while (it.hasNext()) {+ SymbolDB s = (SymbolDB) it.next();+ if (s instanceof VariableSymbolDB) {+ VariableSymbolDB varSym = (VariableSymbolDB) s;+ if (isBadVariable(varSym)) {+ // silently ignore bad variable if address no longer decodes+ // This can happen due to changes in stack address space dimensions+ // TODO: it would be nice to cleanup such bad variables+ foundBadVariables = true;+ continue;+ }+ if (s.getSymbolType() == SymbolType.PARAMETER) {+ ParameterDB p = new ParameterDB(function, s);+ symbolMap.put(s, p);+ params.add(p);+ }+ else {+ VariableDB var = new LocalVariableDB(function, s);+ symbolMap.put(s, var);+ locals.add(var);+ }+ }+ }+ Collections.sort(params);+ Collections.sort(locals);+ return true;+ }++ /**+ * Update parameter ordinals and re-assign dynamic parameter storage+ * NOTE: loadVariables must have been called first+ */+ void updateParametersAndReturn(FunctionDB function, boolean hasCustomStorage) {++ if (hasCustomStorage) {+ autoParams = null;+ renumberParameterOrdinals();+ return;+ }++ DataType[] dataTypes = new DataType[params.size() + 1];++ for (int i = 0; i < params.size(); i++) {+ ParameterDB param = params.get(i);+ param.setDynamicStorage(VariableStorage.UNASSIGNED_STORAGE);+ dataTypes[i + 1] = param.getDataType();+ }++ dataTypes[0] = returnParam.getFormalDataType();+ returnParam.setDynamicStorage(+ VoidDataType.isVoidDataType(dataTypes[0]) ? VariableStorage.VOID_STORAGE+ : VariableStorage.UNASSIGNED_STORAGE);++ PrototypeModel callingConvention = function.getCallingConvention();+ if (callingConvention == null) {+ callingConvention = function.getFunctionManager().getDefaultCallingConvention();+ }+ if (callingConvention == null) {+ return;+ }++ VariableStorage[] variableStorage =+ callingConvention.getStorageLocations(function.getProgram(), dataTypes, true);+ returnParam.setDynamicStorage(variableStorage[0]);++ int autoIndex = 0;+ int paramIndex = 0;++ autoParams = null;++ for (int i = 1; i < variableStorage.length; i++) {+ VariableStorage storage = variableStorage[i];+ if (storage.isAutoStorage()) {+ if (autoParams == null) {+ autoParams = new ArrayList<>();+ }+ DataType dt = VariableUtilities.getAutoDataType(function,+ returnParam.getFormalDataType(), storage);+ try {+ autoParams.add(new AutoParameterImpl(dt, autoIndex++, storage, function));+ }+ catch (InvalidInputException e) {+ Msg.error(this,+ "Unexpected error during dynamic storage assignment for function at " ++ function.getEntryPoint(),+ e);+ break;+ }+ }+ else {+ ParameterDB parameterDB = params.get(paramIndex++);+ parameterDB.setDynamicStorage(storage);+ }+ }++ renumberParameterOrdinals();+ }++ private boolean loadReturn(FunctionDB function, boolean hasCustomStorage) {+ DataType dt = function.getReturnDataType();+ VariableStorage returnStorage = function.getReturnStorage(hasCustomStorage);+ if (returnStorage.isBadStorage()) {+ foundBadVariables = true;+ }+ returnParam = new ReturnParameterDB(function, dt, returnStorage);+ return true;+ }++ private void renumberParameterOrdinals() {+ int autoParamCount = getAutoParamCount();+ int ordinal = autoParamCount;++ for (ParameterDB param : params) {+ param.setOrdinal(ordinal++, autoParamCount);+ }+ }++ int getAutoParamCount() {+ return autoParams != null ? autoParams.size() : 0;+ }++ Variable[] getLocalVariables(VariableFilter filter) {+ ArrayList<Variable> list = new ArrayList<>();+ for (VariableDB var : locals) {+ if (filter == null || filter.matches(var)) {+ list.add(var);+ }+ }+ Variable[] vars = new Variable[list.size()];+ return list.toArray(vars);+ }++ Variable[] getVariables(VariableFilter filter) {+ ArrayList<Variable> list = new ArrayList<>();+ if (autoParams != null) {+ for (AutoParameterImpl p : autoParams) {+ if (filter == null || filter.matches(p)) {+ list.add(p);+ }+ }+ }+ for (ParameterDB p : params) {+ if (filter == null || filter.matches(p)) {+ list.add(p);+ }+ }+ for (VariableDB var : locals) {+ if (filter == null || filter.matches(var)) {+ list.add(var);+ }+ }+ Variable[] vars = new Variable[list.size()];+ return list.toArray(vars);++ }++ private static boolean isBadVariable(VariableSymbolDB varSym) {+ return varSym.getAddress() == Address.NO_ADDRESS ||+ varSym.getVariableStorage().isBadStorage();+ }++ private void purgeBadVariables(FunctionDB function) {+ if (!foundBadVariables) {+ return;+ }+ Program program = function.getProgram();+ List<Symbol> badSymbols = new ArrayList<>();+ SymbolIterator it = program.getSymbolTable().getChildren(function.getSymbol());+ while (it.hasNext()) {+ SymbolDB s = (SymbolDB) it.next();+ if (s instanceof VariableSymbolDB) {+ VariableSymbolDB varSym = (VariableSymbolDB) s;+ if (isBadVariable(varSym)) {+ badSymbols.add(s);+ }+ }+ }+ program.getBookmarkManager()+ .setBookmark(function.getEntryPoint(), BookmarkType.ERROR, "Bad Variables Removed",+ "Removed " + badSymbols.size() + " bad variables");+ for (Symbol s : badSymbols) {+ s.delete();+ }+ if (function.hasCustomVariableStorage()) {+ ReturnParameterDB rtnParam = getReturnParam();+ if (rtnParam.getVariableStorage().isBadStorage()) {+ DataType dt = rtnParam.getDataType();+ VariableStorage storage =+ VoidDataType.isVoidDataType(dt) ? VariableStorage.VOID_STORAGE+ : VariableStorage.UNASSIGNED_STORAGE;+ rtnParam.setStorageAndDataType(storage, dt);+ }+ }+ foundBadVariables = false;+ }++ Parameter[] getParameters(VariableFilter filter) {+ ArrayList<Parameter> list = new ArrayList<>();+ if (autoParams != null) {+ for (AutoParameterImpl p : autoParams) {+ if (filter == null || filter.matches(p)) {+ list.add(p);+ }+ }+ }+ for (ParameterDB p : params) {+ if (filter == null || filter.matches(p)) {+ list.add(p);+ }+ }+ Parameter[] vars = new Parameter[list.size()];+ return list.toArray(vars);+ }++ VariableDB addLocalVariable(FunctionDB function, Variable var, SourceType source)+ throws DuplicateNameException, InvalidInputException {+ purgeBadVariables(function);++ var = getResolvedVariable(function, var, false, false);++ String name = var.getName();+ if (name == null || name.length() == 0 ||+ SymbolUtilities.isDefaultParameterName(name)) {+ name = Function.DEFAULT_LOCAL_PREFIX;+ source = SourceType.DEFAULT;+ }++ VariableStorage storage = var.getVariableStorage();+ int firstUseOffset = var.getFirstUseOffset();+ if (var.hasStackStorage() && firstUseOffset != 0) {+ Msg.info(this, "WARNING! Stack variable firstUseOffset forced to 0 for function " ++ this + " at " + storage);+ firstUseOffset = 0;+ }++ // Check for duplicate storage address+ VariableDB v = null;+ for (VariableDB oldVar : locals) {+ if (oldVar.getFirstUseOffset() == firstUseOffset &&+ oldVar.getVariableStorage().intersects(storage)) {+ v = oldVar;+ break;+ }+ }++ try {+ if (validateEnabled) {+ VariableUtilities.checkVariableConflict(function, (v != null ? v : var), storage,+ true);+ }+ if (v != null) {+ // update existing variable+ Msg.info(this, "WARNING! Adding overlapping local variable for function " ++ this + " at " + v.getVariableStorage() + " - Modifying existing variable!");+ if (!Function.DEFAULT_LOCAL_PREFIX.equals(name)) {+ v.setName(name, source);+ }+ v.setStorageAndDataType(storage, var.getDataType());+ }+ else {+ SymbolManager symbolMgr = function.getProgram().getSymbolTable();+ VariableSymbolDB s = symbolMgr.createVariableSymbol(name, function,+ SymbolType.LOCAL_VAR, firstUseOffset, storage, source);+ s.setStorageAndDataType(storage, var.getDataType());+ v = new LocalVariableDB(function, s);+ locals.add(v);+ Collections.sort(locals);+ symbolMap.put(v.symbol, v);+ }+ if (var.getComment() != null) {+ v.symbol.setSymbolComment(var.getComment());+ }+ function.functionChanged(null);+ return v;+ }+ finally {+ function.invalidateFrame();+ }+ }++ /**+ * Resolve a variable's type and storage.+ * @param var variable to be resolved+ * @param voidOK if true the use of a 0-length {@link VoidDataType} for the specified+ * variable is allowed (i.e., {@link ReturnParameterDB}), else false should be specified.+ * @param useUnassignedStorage if true storage should be set to {@link VariableStorage#UNASSIGNED_STORAGE}+ * else an attempt should be made to adjust the storage.+ * @return resolved variable+ * @throws InvalidInputException if unable to resize variable storage due to+ * resolved datatype size change+ */+ Variable getResolvedVariable(FunctionDB function, Variable var, boolean voidOK,+ boolean useUnassignedStorage)+ throws InvalidInputException {+ Program program = function.getProgram();+ DataType dt = var.getDataType();+ if (var instanceof Parameter) {+ dt = ((Parameter) var).getFormalDataType();+ }+ dt = VariableUtilities.checkDataType(dt, voidOK, Math.min(1, var.getLength()), program);+ DataType resolvedDt = program.getDataTypeManager().resolve(dt, null);+ VariableStorage storage = VariableStorage.UNASSIGNED_STORAGE;+ if (!useUnassignedStorage) {+ storage = var.getVariableStorage();+ if (storage.isAutoStorage()) {+ storage = new VariableStorage(program, storage.getVarnodes());+ }+ if (resolvedDt.getLength() != storage.size()) {+ try {+ storage = VariableUtilities.resizeStorage(storage, resolvedDt, true, function);+ }+ catch (Exception e) {+ // ignore sizing issues+ }+ }+ }++ LocalVariableImpl resolvedVar = new LocalVariableImpl(var.getName(),+ var.getFirstUseOffset(), resolvedDt, storage, true, program, var.getSource());+ resolvedVar.setComment(var.getComment());+ return resolvedVar;+ }++ void setValidataionEnabled(boolean state) {+ validateEnabled = state;+ }++ int getParameterCount() {+ int count = params.size();+ if (autoParams != null) {+ count += autoParams.size();+ }+ return count;+ }++ void updateFunction(FunctionDB function, String callingConvention,+ Variable returnVar, List<? extends Variable> newParams, FunctionUpdateType updateType,+ boolean force, SourceType source) throws DuplicateNameException, InvalidInputException {++ if (returnVar == null) {+ returnVar = returnParam;+ }+ else if (returnVar.isUniqueVariable()) {+ throw new IllegalArgumentException(+ "Invalid return specified: UniqueVariable not allowed");+ }+ ProgramDB program = function.getProgram();+ boolean useCustomStorage = function.hasCustomVariableStorage();+ DataType returnType = returnVar.getDataType();+ VariableStorage returnStorage = returnVar.getVariableStorage();++ if (!function.hasCustomVariableStorage()) {+ // remove auto params and forced-indirect return+ newParams = new ArrayList<Variable>(newParams); // copy for edit+ boolean thisParamRemoved = removeExplicitThisParameter(newParams, callingConvention);+ DataType dt = removeExplicitReturnStoragePtrParameter(newParams);+ if (dt != null) {+ returnVar = revertIndirectParameter(returnVar, dt, true);+ }+ if (returnVar instanceof Parameter) {+ returnType = ((Parameter) returnVar).getFormalDataType();+ }+ returnStorage = VariableStorage.UNASSIGNED_STORAGE;++ if (updateType == FunctionUpdateType.DYNAMIC_STORAGE_ALL_PARAMS &&+ !thisParamRemoved &&+ CompilerSpec.CALLING_CONVENTION_thiscall.equals(callingConvention) &&+ newParams.size() != 0) {+ // Attempt to remove inferred unnamed 'this' parameter+ // WARNING! This is a bit of a hack - not sure how to account for what may be auto-params+ // within a list of parameters computed via analysis+ Variable firstParam = newParams.get(0);+ if (firstParam.getSource() == SourceType.DEFAULT &&+ firstParam.getLength() == program.getDefaultPointerSize()) {+ newParams.remove(0);+ }+ }+ }++ // Update return data type+ returnParam.setDataType(returnType, returnStorage, true, source);++ Set<String> nonParamNames = new HashSet<>();+ for (Symbol s : program.getSymbolTable().getSymbols(function)) {+ if (s.getSource() != SourceType.DEFAULT &&+ s.getSymbolType() != SymbolType.PARAMETER) {+ nonParamNames.add(s.getName());+ }+ }++ // Must ensure that all names do not conflict and that variable types are+ // resolved to this program so that they have the proper sizes+ List<Variable> clonedParams = new ArrayList<>();+ for (int i = 0; i < newParams.size(); i++) {+ Variable p = newParams.get(i);+ if (!useCustomStorage && (p instanceof AutoParameterImpl)) {+ continue;+ }+ if (p.isUniqueVariable()) {+ throw new IllegalArgumentException(+ "Invalid parameter specified: UniqueVariable not allowed");+ }+ checkForParameterNameConflict(p, newParams, nonParamNames);+ clonedParams.add(getResolvedVariable(function, p, false, !useCustomStorage));+ }+ newParams = clonedParams;++ if (useCustomStorage) {+ checkStorageConflicts(function, newParams, force);+ }++ // Repopulate params list+ List<ParameterDB> oldParams = params;+ params = new ArrayList<>();++ // Clear current param names+ for (ParameterDB param : oldParams) {+ param.setName(null, SourceType.DEFAULT);+ }++ int newParamIndex = 0;++ // Reassign old parameters if possible+ while (newParamIndex < oldParams.size() && newParamIndex < newParams.size()) {+ ParameterDB oldParam = oldParams.get(newParamIndex);+ Variable newParam = newParams.get(newParamIndex++);+ DataType dt = (newParam instanceof Parameter && !useCustomStorage)+ ? ((Parameter) newParam).getFormalDataType()+ : newParam.getDataType();+ oldParam.setName(newParam.getName(), newParam.getSource());+ oldParam.setStorageAndDataType(newParam.getVariableStorage(), dt);+ oldParam.setComment(newParam.getComment());+ params.add(oldParam); // re-add to list+ }++ // Remove unused old parameters+ for (int i = newParamIndex; i < oldParams.size(); i++) {+ ParameterDB oldParam = oldParams.get(i);+ Symbol s = oldParam.getSymbol();+ symbolMap.remove(s);+ s.delete();+ }++ // Append new parameters if needed+ SymbolManager symbolMgr = program.getSymbolTable();+ for (int i = newParamIndex; i < newParams.size(); i++) {+ Variable newParam = newParams.get(i);+ DataType dt = (newParam instanceof Parameter && !useCustomStorage)+ ? ((Parameter) newParam).getFormalDataType()+ : newParam.getDataType();+ VariableStorage storage = useCustomStorage ? newParam.getVariableStorage()+ : VariableStorage.UNASSIGNED_STORAGE;+ String name = newParam.getName();+ if (name == null || name.length() == 0) {+ name = SymbolUtilities.getDefaultParamName(i);+ }+ VariableSymbolDB s = symbolMgr.createVariableSymbol(name, function,+ SymbolType.PARAMETER, i, storage, newParam.getSource());+ s.setStorageAndDataType(storage, dt);+ ParameterDB paramDb = new ParameterDB(function, s);+ paramDb.setComment(newParam.getComment());+ params.add(i, paramDb);+ symbolMap.put(s, paramDb);+ }++ // assign dynamic storage+ updateParametersAndReturn(function, useCustomStorage);++ }++ /**+ * Remove 'this' parameter if using __thiscall and first non-auto parameter is+ * a pointer and named 'this'.+ * @param params list of parameters to search and affect+ * @param callingConventionName current function calling convention+ * @return true if 'this' parameter removed (applies to __thiscall callingConventionName only), else false+ */+ private static boolean removeExplicitThisParameter(List<? extends Variable> params,+ String callingConventionName) {+ if (CompilerSpec.CALLING_CONVENTION_thiscall.equals(callingConventionName)) {+ int thisIndex = findExplicitThisParameter(params);+ if (thisIndex >= 0) {+ params.remove(thisIndex); // remove explicit 'this' parameter+ return true;+ }+ }+ return false;+ }++ /**+ * Remove 'this' parameter if using __thiscall and first non-auto parameter is+ * a pointer and named 'this'. Variables must be pre-loaded.+ * @return true if 'this' parameter removed+ */+ boolean removeExplicitThisParameter(FunctionDB function) {+ if (CompilerSpec.CALLING_CONVENTION_thiscall.equals(function.getCallingConventionName())) {+ int thisIndex = findExplicitThisParameter(params);+ if (thisIndex >= 0) {+ removeParameter(thisIndex); // remove explicit 'this' parameter+ return true;+ }+ }+ return false;+ }++ private static int findExplicitReturnStoragePtrParameter(List<? extends Variable> params) {+ for (int i = 0; i < params.size(); i++) {+ Variable p = params.get(i);+ if (Function.RETURN_PTR_PARAM_NAME.equals(p.getName()) &&+ (p.getDataType() instanceof Pointer)) {+ return i;+ }+ }+ return -1;+ }++ private static DataType removeExplicitReturnStoragePtrParameter(+ List<? extends Variable> params) {+ int paramIndex = findExplicitReturnStoragePtrParameter(params);+ if (paramIndex >= 0) {+ Variable returnStoragePtrParameter = params.remove(paramIndex); // remove return storage parameter+ DataType dt = returnStoragePtrParameter.getDataType();+ if (dt instanceof Pointer ptr) {+ return ptr.getDataType();+ }+ }+ return null;+ }++ private DataType removeExplicitReturnStoragePtrParameter() {+ int paramIndex = findExplicitReturnStoragePtrParameter(params);+ if (paramIndex >= 0) {+ ParameterDB returnStoragePtrParameter = params.get(paramIndex);+ DataType dt = returnStoragePtrParameter.getDataType();+ removeParameter(paramIndex); // remove return storage parameter+ if (dt instanceof Pointer ptr) {+ return ptr.getDataType();+ }+ }+ return null;+ }++ /**+ * Strip indirect pointer data type from a parameter.+ * @param param parameter to be examined and optionally modified+ * @param dt return datatype to be applied+ * @param create if true the specified param will not be affected and a new parameter+ * instance will be returned if strip performed, otherwise orginal param will be changed+ * if possible and returned.+ * @return parameter with pointer stripped or original param if pointer not used.+ * Returned parameter will have unassigned storage if affected.+ */+ private static Variable revertIndirectParameter(Variable param, DataType dt, boolean create) {+ try {+ if (create) {+ param = new ParameterImpl(param.getName(), dt, param.getProgram());+ }+ else {+ param.setDataType(dt, VariableStorage.UNASSIGNED_STORAGE, false, param.getSource());+ }+ }+ catch (InvalidInputException e) {+ throw new AssertException(e); // unexpected+ }+ return param;+ }++ private void checkForParameterNameConflict(Variable param, List<? extends Variable> newParams,+ Set<String> nonParamNames) throws DuplicateNameException {++ String name = param.getName();+ if (name == null || name.length() == 0 || SymbolUtilities.isDefaultParameterName(name)) {+ return;+ }++ // Check for duplicate names+ for (Variable chkParam : newParams) {+ if (param == chkParam) {+ continue;+ }+ if (name.equals(chkParam.getName())) {+ throw new DuplicateNameException("Duplicate parameter name '" + name + "'");+ }+ }++ if (nonParamNames.contains(name)) {+ throw new DuplicateNameException(+ "Parameter name conflicts with a symbol within function named '" + name + "'");+ }+ }++ private void checkStorageConflicts(FunctionDB function, List<? extends Variable> newParams,+ boolean removeConflictingLocals) throws VariableSizeException {++ VariableUtilities.VariableConflictHandler localConflictHandler = null;+ if (removeConflictingLocals) {+ localConflictHandler = conflicts -> {+ for (Variable var : conflicts) {+ removeVariable(var);+ }+ return true;+ };+ }++ for (Variable p : newParams) {+ // check for storage conflicts if custom storage used+ VariableUtilities.checkVariableConflict(newParams, p, p.getVariableStorage(), null);+ VariableUtilities.checkVariableConflict(locals, p, p.getVariableStorage(),+ localConflictHandler);+ }++ }++ void removeVariable(Variable variable) {+ if (variable instanceof VariableDB) {+ Symbol s = ((VariableDB) variable).symbol;+ if (symbolMap.containsKey(s)) {+ s.delete(); // results in callback to doDeleteVariable+ }+ }+ }++ private static int findExplicitThisParameter(List<? extends Variable> params) {+ for (int i = 0; i < params.size(); i++) {+ Variable p = params.get(i);+ if (Function.THIS_PARAM_NAME.equals(p.getName()) &&+ (p.getDataType() instanceof Pointer)) {+ return i;+ }+ }+ return -1;+ }++ void removeParameter(int ordinal) {+ if (ordinal < 0) {+ throw new IndexOutOfBoundsException();+ }+ if (autoParams != null) {+ if (ordinal < autoParams.size()) {+ return; // ignore+ }+ ordinal -= autoParams.size();+ }+ if (ordinal >= params.size()) {+ throw new IndexOutOfBoundsException();+ }+ ParameterDB param = params.get(ordinal);+ param.symbol.delete(); // results in callback to doDeleteVariable+ }++ Parameter addParameter(FunctionDB function, Variable var, SourceType source)+ throws InvalidInputException, DuplicateNameException {+ purgeBadVariables(function);++ return insertParameter(function, getParameterCount(), var, source);+ }++ ParameterDB insertParameter(FunctionDB function, int ordinal, Variable var,+ SourceType source) throws InvalidInputException, DuplicateNameException {+ purgeBadVariables(function);+ boolean hasCustomStorage = function.hasCustomVariableStorage();+ int autoCnt = 0;+ if (autoParams != null) {+ autoCnt = autoParams.size();+ if (ordinal < autoCnt) {+ throw new InvalidInputException(+ "Parameter may not be inserted before auto-parameter");+ }+ }++ ordinal -= autoCnt;+ if (ordinal < 0 || ordinal > params.size()) {+ throw new IndexOutOfBoundsException("Ordinal value must be " + autoCnt ++ " <= ordinal < " + (params.size() + autoCnt) + ": " + (ordinal + autoCnt));+ }++ if (var.isUniqueVariable()) {+ throw new IllegalArgumentException(+ "Invalid parameter specified: UniqueVariable not allowed");+ }++ if (hasCustomStorage) {+ if (validateEnabled && var.hasStackStorage()) {+ int stackOffset = (int) var.getLastStorageVarnode().getOffset();+ if (!function.getStackFrame().isParameterOffset(stackOffset)) {+ throw new InvalidInputException(+ "Variable contains invalid stack parameter offset: " + var.getName() ++ " offset " + stackOffset);+ }+ }+ }++ var = getResolvedVariable(function, var, false, !hasCustomStorage);+ ProgramDB program = function.getProgram();++ String name = var.getName();+ SourceType paramSource = source;+ if (name == null || name.length() == 0 || paramSource == SourceType.DEFAULT ||+ SymbolUtilities.isDefaultParameterName(name)) {+ name = Function.DEFAULT_PARAM_PREFIX;+ paramSource = SourceType.DEFAULT;+ }++ VariableStorage storage = var.getVariableStorage();+ if (!hasCustomStorage) {+ storage = VariableStorage.UNASSIGNED_STORAGE;+ }+ else if (storage.isAutoStorage()) {+ storage = new VariableStorage(program, storage.getVarnodes());+ }++ try {++ // Check for duplicate storage address+ ParameterDB p = null;+ if (storage != VariableStorage.UNASSIGNED_STORAGE) {+ for (ParameterDB oldParam : params) {+ if (oldParam.getVariableStorage().intersects(storage)) {+ p = oldParam;+ break;+ }+ }+ if (validateEnabled) {+ VariableUtilities.checkVariableConflict(function, (p != null ? p : var),+ storage, true);+ }+ }+ if (p != null) {+ // storage has been specified+ // move and update existing parameter+ if (ordinal >= params.size()) {+ ordinal = params.size() - 1;+ }+ Msg.info(this, "WARNING! Inserting overlapping parameter for function " + this ++ " at " + p.getVariableStorage() + " - Replacing existing parameter!");+ if (p.getOrdinal() != ordinal) {+ if (p != params.remove(p.getOrdinal())) {+ throw new AssertException("Inconsistent function parameter cache");+ }++ params.add(ordinal, p);+ updateParametersAndReturn(function, hasCustomStorage);+ function.functionChanged(PARAMETERS_CHANGED);+ }+ if (!Function.DEFAULT_PARAM_PREFIX.equals(name)) {+ p.setName(name, paramSource);+ }+ p.setStorageAndDataType(storage, var.getDataType());+ }+ else {+ // create new parameter+ if (ordinal > params.size()) {+ ordinal = params.size();+ }+ if (ordinal != params.size()) {+ // shift params to make room for inserted param+ for (ParameterDB param : params) {+ int paramOrdinal = param.getOrdinal();+ if (paramOrdinal >= ordinal) {+ param.setOrdinal(paramOrdinal + 1, autoCnt);+ }+ }+ }+ SymbolManager symbolMgr = program.getSymbolTable();+ VariableSymbolDB s =+ symbolMgr.createVariableSymbol(name, function, SymbolType.PARAMETER, ordinal,+ storage, paramSource);+ s.setStorageAndDataType(storage, var.getDataType());+ p = new ParameterDB(function, s);++ params.add(ordinal, p);+ updateParametersAndReturn(function, hasCustomStorage);+ symbolMap.put(p.symbol, p);+ function.functionChanged(PARAMETERS_CHANGED);+ }+ if (var.getComment() != null) {+ p.symbol.setSymbolComment(var.getComment());+ }+ function.updateSignatureSourceAfterVariableChange(source, p.getDataType());+ return p;+ }+ finally {+ function.invalidateFrame();+ }++ }++ void doDeleteVariable(FunctionDB function, VariableSymbolDB symbol) {+ if (isBadVariable(symbol)) {+ // don't do anything here with bad variable symbol+ return;+ }+ VariableDB var = symbolMap.remove(symbol);++ if (var != null) {+ if (var instanceof Parameter) {+ if (removeVariable(params, var)) {+ updateParametersAndReturn(function, function.hasCustomVariableStorage());+ }+ }+ else {+ removeVariable(locals, var);+ }+ }++ function.functionChanged((var instanceof Parameter) ? PARAMETERS_CHANGED : null);+ function.invalidateFrame();+ }++ /**+ * Remove variable instance from list.+ *+ * @param list+ * variable list+ * @param var+ * variable instance+ * @return true if deleted+ */+ private static boolean removeVariable(List<?> list, VariableDB var) {+ int cnt = list.size();+ for (int i = 0; i < cnt; i++) {+ if (var == list.get(i)) {+ list.remove(i);+ return true;+ }+ }+ return false;+ }++ Variable getVariable(VariableSymbolDB symbol) {+ return symbolMap.get(symbol);+ }++ Parameter getParameter(int ordinal) {+ if (autoParams != null) {+ if (ordinal < autoParams.size()) {+ return autoParams.get(ordinal);+ }+ ordinal -= autoParams.size();+ }+ if (ordinal < params.size()) {+ return params.get(ordinal);+ }+ return null;+ }++ Parameter moveParameter(FunctionDB function, int fromOrdinal, int toOrdinal)+ throws InvalidInputException {+ int autoCnt = 0;+ if (autoParams != null) {+ autoCnt = autoParams.size();+ if (fromOrdinal < autoCnt) {+ throw new InvalidInputException("Auto-parameter may not be moved");+ }+ if (toOrdinal < autoCnt) {+ throw new InvalidInputException(+ "Parameter may not be moved before an auto-parameter");+ }+ }++ fromOrdinal -= autoCnt;+ toOrdinal -= autoCnt;++ if (fromOrdinal < 0 || fromOrdinal >= params.size()) {+ return null;+ }+ ParameterDB param = params.get(fromOrdinal);+ if (param.getOrdinal() == toOrdinal) {+ return param;+ }+ params.remove(fromOrdinal);+ if (toOrdinal >= params.size()) {+ params.add(param);+ }+ else {+ params.add(toOrdinal, param);+ }+ updateParametersAndReturn(function, function.hasCustomVariableStorage());+ function.functionChanged(PARAMETERS_CHANGED);+ return param;++ }++ void setCustomVariableStorage(FunctionDB function, boolean hasCustomVariableStorage)+ throws InvalidInputException {+ Program program = function.getProgram();+ if (!hasCustomVariableStorage) {+ // remove explicit 'this' param and return storage use if switching to dynamic storage+ removeExplicitThisParameter(function);+ DataType returnDt = removeExplicitReturnStoragePtrParameter();+ if (returnDt != null) {+ revertIndirectParameter(returnParam, returnDt, false);+ }+ }++ // get params and return prior to change+ Parameter[] parameters = getParameters(null);++ // remove auto-parameters+ autoParams = null;++ function.setFunctionFlag(FunctionAdapter.FUNCTION_CUSTOM_PARAM_STORAGE_FLAG,+ hasCustomVariableStorage);++ int ordinal = 0;+ for (Parameter p : parameters) {+ if (p.isAutoParameter()) {+ // must insert auto-params when switching to custom storage+ try {+ insertParameter(function, ordinal, new ParameterImpl(p, program),+ SourceType.ANALYSIS);+ ++ordinal;+ }+ catch (DuplicateNameException e) {+ // skip - we don't want to rename auto-param+ }+ }+ else {+ // if not an auto-param p is a ParameterDB object+ // commit parameter storage and forced-indirect pointer when switching to custom+ // or switch to UNASSIGNED_STORAGE when switching to dynamic+ VariableStorage storage =+ hasCustomVariableStorage ? p.getVariableStorage().clone(program)+ : VariableStorage.UNASSIGNED_STORAGE;+ ((ParameterDB) p).setStorageAndDataType(storage, p.getDataType());+ }+ }++ // commit return storage and forced-indirect pointer when switching to custom+ // or switch to UNASSIGNED_STORAGE when switching to dynamic+ VariableStorage storage =+ hasCustomVariableStorage ? returnParam.getVariableStorage().clone(program)+ : VariableStorage.UNASSIGNED_STORAGE;+ returnParam.setStorageAndDataType(storage, returnParam.getDataType());++ if (!hasCustomVariableStorage) {+ updateParametersAndReturn(function, hasCustomVariableStorage); // assign dynamic storage+ }++ }++}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/LocalVariableDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/LocalVariableDB.javaindex b121841b87..495aa502a0 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/LocalVariableDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/LocalVariableDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -17,6 +17,7 @@ package ghidra.program.database.function;import ghidra.program.database.symbol.SymbolDB;import ghidra.program.model.listing.LocalVariable;+import ghidra.util.Lock.Closeable;public class LocalVariableDB extends VariableDB implements LocalVariable {@@ -31,8 +32,7 @@ public class LocalVariableDB extends VariableDB implements LocalVariable {@Overridepublic boolean setFirstUseOffset(int firstUseOffset) {- functionMgr.lock.acquire();- try {+ try (Closeable c = functionMgr.lock.write()) {function.startUpdate();function.checkDeleted();symbol.setFirstUseOffset(firstUseOffset);@@ -40,7 +40,6 @@ public class LocalVariableDB extends VariableDB implements LocalVariable {}finally {function.endUpdate();- functionMgr.lock.release();}return true;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ParameterDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ParameterDB.javaindex a5cad40ed9..994f3055d6 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ParameterDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ParameterDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -21,6 +21,7 @@ import ghidra.program.model.data.DataTypeManager;import ghidra.program.model.listing.*;class ParameterDB extends VariableDB implements Parameter {+ int autoParamCount = 0;ParameterDB(FunctionDB function, SymbolDB s) {super(function, s);@@ -33,17 +34,15 @@ class ParameterDB extends VariableDB implements Parameter {@Overridepublic int getOrdinal() {- int baseOrdinal = function.getAutoParamCount();- int ordinal = symbol.getOrdinal();- return baseOrdinal + ordinal;+ return symbol.getOrdinal() + autoParamCount;}- void setOrdinal(int ordinal) {- if (getOrdinal() == ordinal) {- return;+ void setOrdinal(int ordinal, int autoParamCount) {+ this.autoParamCount = autoParamCount;+ int symbolOrdinal = ordinal - autoParamCount;+ if (symbol.getOrdinal() != symbolOrdinal) {+ symbol.setOrdinal(symbolOrdinal);}- int baseOrdinal = function.getAutoParamCount();- symbol.setOrdinal(ordinal - baseOrdinal);}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ReturnParameterDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ReturnParameterDB.javaindex 4e9c6b240d..61e91eb821 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ReturnParameterDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/ReturnParameterDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -23,6 +23,7 @@ import ghidra.program.model.data.*;import ghidra.program.model.lang.DynamicVariableStorage;import ghidra.program.model.listing.*;import ghidra.program.model.symbol.SourceType;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -68,15 +69,14 @@ public class ReturnParameterDB extends ParameterDB {}@Override- final void setOrdinal(int ordinal) {+ final void setOrdinal(int ordinal, int autoParamCount) {throw new UnsupportedOperationException();}@Overridepublic void setDataType(DataType type, VariableStorage newStorage, boolean force,SourceType source) throws InvalidInputException, VariableSizeException {- functionMgr.lock.acquire();- try {+ try (Closeable c = functionMgr.lock.write()) {function.checkDeleted();boolean hasCustomStorage = function.hasCustomVariableStorage();if (!hasCustomStorage) {@@ -104,9 +104,6 @@ public class ReturnParameterDB extends ParameterDB {catch (IOException e) {functionMgr.dbError(e);}- finally {- functionMgr.lock.release();- }}@Override@@ -117,8 +114,7 @@ public class ReturnParameterDB extends ParameterDB {@Overridepublic void setDataType(DataType type, boolean alignStack, boolean force, SourceType source)throws InvalidInputException {- functionMgr.lock.acquire();- try {+ try (Closeable c = functionMgr.lock.write()) {function.checkDeleted();Program program = function.getProgram();type = VariableUtilities.checkDataType(type, true, 0, program);@@ -154,9 +150,6 @@ public class ReturnParameterDB extends ParameterDB {catch (IOException e) {functionMgr.dbError(e);}- finally {- functionMgr.lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/VariableDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/VariableDB.javaindex 8936150dfd..5de1fc04b7 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/VariableDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/VariableDB.java@@ -30,6 +30,7 @@ import ghidra.program.model.listing.*;import ghidra.program.model.pcode.Varnode;import ghidra.program.model.symbol.SourceType;import ghidra.program.model.symbol.Symbol;+import ghidra.util.Lock.Closeable;import ghidra.util.SystemUtilities;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -83,8 +84,7 @@ public abstract class VariableDB implements Variable {@Overridepublic void setDataType(DataType type, VariableStorage newStorage, boolean force,SourceType source) throws InvalidInputException, VariableSizeException {- functionMgr.lock.acquire();- try {+ try (Closeable c = functionMgr.lock.write()) {function.startUpdate();function.checkDeleted();if ((this instanceof Parameter) && !function.hasCustomVariableStorage()) {@@ -109,15 +109,13 @@ public abstract class VariableDB implements Variable {}finally {function.endUpdate();- functionMgr.lock.release();}}@Overridepublic void setDataType(DataType type, boolean alignStack, boolean force, SourceType source)throws InvalidInputException {- functionMgr.lock.acquire();- try {+ try (Closeable c = functionMgr.lock.write()) {function.startUpdate();function.checkDeleted();// VARDO: Is there concern about variable no longer be contained within function?@@ -149,7 +147,6 @@ public abstract class VariableDB implements Variable {}finally {function.endUpdate();- functionMgr.lock.release();}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.javaindex b16b173f00..f587053ea1 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java@@ -27,6 +27,7 @@ import ghidra.program.database.map.AddressMap;import ghidra.program.database.map.AddressMapDB;import ghidra.program.model.address.*;import ghidra.program.model.mem.*;+import ghidra.util.Lock.Closeable;import ghidra.util.NumericUtilities;import ghidra.util.exception.AssertException;@@ -107,7 +108,8 @@ public class MemoryBlockDB implements MemoryBlock {* @return collection of blocks which map onto this block or null if none identified*/Collection<MemoryBlockDB> getMappedBlocks() {- memMap.buildAddressSets(false); // updates mappedBlocks if needed+ // this call will force mapped blocks to be rebuilt if out of date+ memMap.getMemoryAddressSetViews();return mappedBlocks;}@@ -177,8 +179,7 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setName(String name) throws LockException {String oldName = getName();- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();if (oldName.equals(name)) {return;@@ -193,9 +194,6 @@ public class MemoryBlockDB implements MemoryBlock {}memMap.fireBlockChanged(this);}- finally {- memMap.lock.release();- }}@Override@@ -205,8 +203,7 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setComment(String comment) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();try {record.setString(MemoryMapDBAdapter.COMMENTS_COL, comment);@@ -217,9 +214,6 @@ public class MemoryBlockDB implements MemoryBlock {memMap.dbError(e);}}- finally {- memMap.lock.release();- }}@Override@@ -229,16 +223,12 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setRead(boolean r) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();if (setFlagBit(READ, r)) {memMap.fireBlockChanged(this);}}- finally {- memMap.lock.release();- }}@Override@@ -248,16 +238,12 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setWrite(boolean w) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();if (setFlagBit(WRITE, w)) {memMap.fireBlockChanged(this);}}- finally {- memMap.lock.release();- }}@Override@@ -267,23 +253,18 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setExecute(boolean x) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();if (setFlagBit(EXECUTE, x)) {memMap.blockExecuteChanged(this);memMap.fireBlockChanged(this);}}- finally {- memMap.lock.release();- }}@Overridepublic void setPermissions(boolean read, boolean write, boolean execute) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();boolean changed = setFlagBit(READ, read);changed |= setFlagBit(WRITE, write);@@ -293,9 +274,6 @@ public class MemoryBlockDB implements MemoryBlock {memMap.fireBlockChanged(this);}}- finally {- memMap.lock.release();- }}@Override@@ -305,16 +283,12 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setVolatile(boolean v) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();if (setFlagBit(VOLATILE, v)) {memMap.fireBlockChanged(this);}}- finally {- memMap.lock.release();- }}@Override@@ -324,16 +298,12 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setArtificial(boolean a) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();if (setFlagBit(ARTIFICIAL, a)) {memMap.fireBlockChanged(this);}}- finally {- memMap.lock.release();- }}@Override@@ -343,8 +313,7 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void setSourceName(String sourceName) {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();try {record.setString(MemoryMapDBAdapter.SOURCE_COL, sourceName);@@ -355,9 +324,6 @@ public class MemoryBlockDB implements MemoryBlock {}memMap.fireBlockChanged(this);}- finally {- memMap.lock.release();- }}@Override@@ -383,16 +349,12 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic void putByte(Address addr, byte b) throws MemoryAccessException {long offset = getBlockOffset(addr);- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();memMap.checkMemoryWrite(this, addr, 1);putByte(offset, b);memMap.fireBytesChanged(addr, 1);}- finally {- memMap.lock.release();- }}@Override@@ -403,8 +365,7 @@ public class MemoryBlockDB implements MemoryBlock {@Overridepublic int putBytes(Address addr, byte[] b, int off, int len)throws IndexOutOfBoundsException, MemoryAccessException {- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {checkValid();memMap.checkMemoryWrite(this, addr, len);@@ -414,9 +375,6 @@ public class MemoryBlockDB implements MemoryBlock {memMap.fireBytesChanged(addr, n);return n;}- finally {- memMap.lock.release();- }}@Override@@ -527,16 +485,12 @@ public class MemoryBlockDB implements MemoryBlock {private void putByte(long offset, byte b) throws MemoryAccessException {SubMemoryBlock subBlock = getSubBlock(offset);- memMap.lock.acquire();- try {+ try (Closeable c = memMap.lock.write()) {subBlock.putByte(offset, b);}catch (IOException e) {memMap.dbError(e);}- finally {- memMap.lock.release();- }}private int putBytes(long offset, byte[] b, int off, int len)diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.javaindex 6d609c8eb1..66cf570f33 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryMapDB.java@@ -34,6 +34,7 @@ import ghidra.program.model.listing.Program;import ghidra.program.model.mem.*;import ghidra.program.util.ProgramEvent;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -56,17 +57,6 @@ public class MemoryMapDB implements Memory, ManagerDB {private MemoryAddressSetViews addrSetViews;- /**- * Address set views into program memory which are iterator safe- * for public API methods.- */- private class MemoryAddressSetViews {- private AddressSet initializedAndLoaded = new AddressSet();- private AddressSet initialized = new AddressSet();- private AddressSet externalBlock = new AddressSet();- private AddressSet execute = new AddressSet(); // may be replaced when block permissions change- }-private MemoryBlock lastBlock;// the last accessed block// lazy hashmap of block names to blocks, must be reloaded if blocks are removed or added@@ -102,7 +92,7 @@ public class MemoryMapDB implements Memory, ManagerDB {adapter = MemoryMapDBAdapter.getAdapter(handle, openMode, this, monitor);fileBytesAdapter = FileBytesAdapter.getAdapter(handle, openMode, monitor);initializeBlocks();- buildAddressSets(true);+ allAddrSet = buildFullMemoryAddressSetView();}// for testing@@ -125,13 +115,17 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic void invalidateCache(boolean all) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {reloadAll();}- finally {- lock.release();+ }++ private AddressSetView buildFullMemoryAddressSetView() {+ AddressSet set = new AddressSet();+ for (MemoryBlockDB block : blocks) {+ set.add(block.getStart(), block.getEnd());}+ return new AddressSetViewAdapter(set);}/**@@ -147,97 +141,22 @@ public class MemoryMapDB implements Memory, ManagerDB {* address set view object.* @return the address set view object*/- MemoryAddressSetViews buildAddressSets(boolean rebuildAllAddrSets) {+ MemoryAddressSetViews getMemoryAddressSetViews() {+ MemoryAddressSetViews localViews = addrSetViews;+ if (localViews == null) {+ localViews = lock.withRead(() -> buildMemoryAddressSetViews());+ }+ return localViews;+ }++ private synchronized MemoryAddressSetViews buildMemoryAddressSetViews() {MemoryAddressSetViews localAddrSetViews = addrSetViews;- if (!rebuildAllAddrSets && localAddrSetViews != null) {+ if (localAddrSetViews != null) {return localAddrSetViews;}- lock.acquire();- try {- // have to try and get it again, another thread may have already filled it out- if (!rebuildAllAddrSets && addrSetViews != null) {- return addrSetViews;- }- // Begin rebuilding a complete set of address set views- MemoryAddressSetViews newAddrSetViews = new MemoryAddressSetViews();+ return new MemoryAddressSetViews();- // The allAddrSet instance is generally kept up-to-date with all memory- // block addresses and need only be rebuilt under certain conditions as- // signaled by the rebuildAllAddrs parameter.- AddressSet newAllAddrs = null;- if (rebuildAllAddrSets) {- newAllAddrs = new AddressSet();- }-- // we have to process the non-mapped blocks first because to process the mapped- // blocks we need the address sets for the non-mapped blocks to be complete- for (MemoryBlockDB block : blocks) {- block.clearMappedBlockList();- if (!block.isMapped()) {- addBlockAddresses(block, newAddrSetViews, newAllAddrs);- }- }- // process all mapped blocks after non-mapped-blocks above- for (MemoryBlockDB block : blocks) {- if (block.isMapped()) {- addBlockAddresses(block, newAddrSetViews, newAllAddrs);- }- }- if (newAllAddrs != null) {- // replace allAddrSet with fully updated address set- allAddrSet = new AddressSetViewAdapter(newAllAddrs);- }- addrSetViews = newAddrSetViews;- return addrSetViews;- }- finally {- lock.release();- }- }-- /**- * Update address sets with relevant addresses from the specified memory block.- * In addition, allAddrSet will be updated if addToAll parameter is true.- *- * @param block memory block to be added- * @param newAddrSetViews address set views which should be built-up- * @param newAllAddrs if not null this set will be updated with the specified block's address range,- * otherwise only the {@code addrSetView} sets will be updated.- */- private void addBlockAddresses(MemoryBlockDB block, MemoryAddressSetViews newAddrSetViews,- AddressSet newAllAddrs) {- Address start = block.getStart();- Address end = block.getEnd();- if (newAllAddrs != null) {- newAllAddrs.add(start, end);- }- if (block.isExternalBlock()) {- newAddrSetViews.externalBlock.add(start, end);- }- else if (block.isExecute()) {- newAddrSetViews.execute.add(start, end);- }- if (block.isMapped()) {- // Identify source-blocks which block maps onto and add as a mapped-block to each of these- AddressRange mappedRange = block.getSourceInfos().get(0).getMappedRange().get();- for (MemoryBlockDB b : getBlocks(mappedRange.getMinAddress(),- mappedRange.getMaxAddress())) {- if (!b.isMapped()) {- b.addMappedBlock(block);- }- }- AddressSet mappedSet = getMappedIntersection(block, newAddrSetViews.initialized);- newAddrSetViews.initialized.add(mappedSet);- newAddrSetViews.initializedAndLoaded- .add(getMappedIntersection(block, newAddrSetViews.initializedAndLoaded));- }- else if (block.isInitialized()) {- newAddrSetViews.initialized.add(block.getStart(), block.getEnd());- if (block.isLoaded()) {- newAddrSetViews.initializedAndLoaded.add(block.getStart(), block.getEnd());- }- }}private void addToAllAddressSet(Address minAddr, Address maxAddr) {@@ -257,7 +176,7 @@ public class MemoryMapDB implements Memory, ManagerDB {fileBytesAdapter.refresh();adapter.refreshMemory();initializeBlocks();- buildAddressSets(true);+ allAddrSet = buildFullMemoryAddressSetView();}addrMap.memoryMapChanged(this);}@@ -349,25 +268,25 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic AddressSetView getAllInitializedAddressSet() {- MemoryAddressSetViews localAddrSetViews = buildAddressSets(false);+ MemoryAddressSetViews localAddrSetViews = getMemoryAddressSetViews();return new AddressSetViewAdapter(localAddrSetViews.initialized);}@Overridepublic AddressSetView getLoadedAndInitializedAddressSet() {- MemoryAddressSetViews localAddrSetViews = buildAddressSets(false);+ MemoryAddressSetViews localAddrSetViews = getMemoryAddressSetViews();return new AddressSetViewAdapter(localAddrSetViews.initializedAndLoaded);}@Overridepublic boolean isExternalBlockAddress(Address addr) {- MemoryAddressSetViews localAddrSetViews = buildAddressSets(false);+ MemoryAddressSetViews localAddrSetViews = getMemoryAddressSetViews();return localAddrSetViews.externalBlock.contains(addr);}@Overridepublic AddressSetView getExecuteSet() {- MemoryAddressSetViews localAddrSetViews = buildAddressSets(false);+ MemoryAddressSetViews localAddrSetViews = getMemoryAddressSetViews();return new AddressSetViewAdapter(localAddrSetViews.execute);}@@ -556,7 +475,6 @@ public class MemoryMapDB implements Memory, ManagerDB {}void fireBytesChanged(Address addr, int count) {- lock.acquire();try {Address end = addr.addNoWrap(count - 1);@@ -568,9 +486,6 @@ public class MemoryMapDB implements Memory, ManagerDB {// shouldn't happenthrow new AssertException(e.getMessage());}- finally {- lock.release();- }}@Override@@ -663,8 +578,7 @@ public class MemoryMapDB implements Memory, ManagerDB {long length, TaskMonitor monitor, boolean overlay) throws MemoryConflictException,AddressOverflowException, CancelledException, LockException {checkBlockName(name);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkBlockSize(length, true);program.checkExclusiveAccess();@@ -700,9 +614,6 @@ public class MemoryMapDB implements Memory, ManagerDB {return null;}- finally {- lock.release();- }}@Override@@ -711,8 +622,7 @@ public class MemoryMapDB implements Memory, ManagerDB {MemoryConflictException, AddressOverflowException, IndexOutOfBoundsException {checkBlockName(name);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkBlockSize(length, true);program.checkExclusiveAccess();checkFileBytesRange(fileBytes, offset, length);@@ -737,9 +647,6 @@ public class MemoryMapDB implements Memory, ManagerDB {return null;}- finally {- lock.release();- }}private void checkFileBytesRange(FileBytes fileBytes, long offset, long length) {@@ -764,8 +671,7 @@ public class MemoryMapDB implements Memory, ManagerDB {throws MemoryConflictException, AddressOverflowException, LockException {checkBlockName(name);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkBlockSize(size, false);program.checkExclusiveAccess();@@ -788,9 +694,6 @@ public class MemoryMapDB implements Memory, ManagerDB {program.dbError(e);}}- finally {- lock.release();- }return null;}@@ -800,8 +703,7 @@ public class MemoryMapDB implements Memory, ManagerDB {LockException, IllegalArgumentException {checkBlockName(name);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkBlockSize(length, false);program.checkExclusiveAccess();mappedAddress.addNoWrap((length - 1) / 8);// just to check if length fits in address space@@ -823,9 +725,6 @@ public class MemoryMapDB implements Memory, ManagerDB {program.dbError(e);}}- finally {- lock.release();- }return null;}@@ -844,8 +743,7 @@ public class MemoryMapDB implements Memory, ManagerDB {mappingScheme = byteMappingScheme.getEncodedMappingScheme();}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkBlockSize(length, false);program.checkExclusiveAccess();byteMappingScheme.getMappedSourceAddress(mappedAddress, length - 1); // source fit check@@ -867,9 +765,6 @@ public class MemoryMapDB implements Memory, ManagerDB {program.dbError(e);}}- finally {- lock.release();- }return null;}@@ -889,8 +784,7 @@ public class MemoryMapDB implements Memory, ManagerDB {public MemoryBlock createBlock(MemoryBlock block, String name, Address start, long length)throws MemoryConflictException, AddressOverflowException, LockException {checkBlockName(name);- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkBlockSize(length, block.isInitialized());program.checkExclusiveAccess();checkRange(start, length);@@ -917,9 +811,6 @@ public class MemoryMapDB implements Memory, ManagerDB {return null;}- finally {- lock.release();- }}@Override@@ -929,21 +820,14 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic MemoryBlock[] getBlocks() {- lock.acquire();- try {- return blocks.toArray(new MemoryBlock[blocks.size()]);- }- finally {- lock.release();- }+ return lock.withRead(() -> blocks.toArray(new MemoryBlock[blocks.size()]));}@Overridepublic void moveBlock(MemoryBlock block, Address newStartAddr, TaskMonitor monitor)throws MemoryBlockException, MemoryConflictException, AddressOverflowException,LockException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {program.checkExclusiveAccess();checkBlock(block);MemoryBlockDB memBlock = (MemoryBlockDB) block;@@ -980,15 +864,11 @@ public class MemoryMapDB implements Memory, ManagerDB {fireBlockMoved(memBlock, oldStartAddr);}- finally {- lock.release();- }}@Overridepublic void split(MemoryBlock block, Address addr) throws MemoryBlockException, LockException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {program.checkExclusiveAccess();checkBlock(block);MemoryBlockDB memBlock = (MemoryBlockDB) block;@@ -1026,19 +906,13 @@ public class MemoryMapDB implements Memory, ManagerDB {catch (IOException e) {program.dbError(e);}-}- finally {- lock.release();- }-}@Overridepublic MemoryBlock join(MemoryBlock blockOne, MemoryBlock blockTwo)throws MemoryBlockException, LockException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {// swap if second block is before first blockif (blockOne.getStart().compareTo(blockTwo.getStart()) > 0) {MemoryBlock tmp = blockOne;@@ -1065,10 +939,6 @@ public class MemoryMapDB implements Memory, ManagerDB {}return newBlock;}- finally {- lock.release();- }-}private void checkPreconditionsForJoining(MemoryBlock block1, MemoryBlock block2)@@ -1111,8 +981,7 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic MemoryBlock convertToInitialized(MemoryBlock uninitializedBlock, byte initialValue)throws MemoryBlockException, LockException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkBlock(uninitializedBlock);program.checkExclusiveAccess();if (uninitializedBlock.isInitialized()) {@@ -1140,17 +1009,12 @@ public class MemoryMapDB implements Memory, ManagerDB {return null;}- finally {- lock.release();- }-}@Overridepublic MemoryBlock convertToUninitialized(MemoryBlock initializedBlock)throws MemoryBlockException, LockException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {program.checkExclusiveAccess();checkBlock(initializedBlock);if (!initializedBlock.isInitialized()) {@@ -1176,10 +1040,6 @@ public class MemoryMapDB implements Memory, ManagerDB {return null;}- finally {- lock.release();- }-}@Override@@ -1635,8 +1495,7 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic void setByte(Address addr, byte value) throws MemoryAccessException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {MemoryBlock block = getBlock(addr);if (block == null) {throw new MemoryAccessException(@@ -1645,10 +1504,6 @@ public class MemoryMapDB implements Memory, ManagerDB {block.putByte(addr, value);}- finally {- lock.release();- }-}@Override@@ -1659,8 +1514,7 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic void setBytes(Address address, byte[] source, int sIndex, int size)throws MemoryAccessException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address addr = address;int n = size;// loop first just to make sure the operation can complete before making any changes@@ -1698,10 +1552,6 @@ public class MemoryMapDB implements Memory, ManagerDB {}}- finally {- lock.release();- }-}@Override@@ -1870,8 +1720,7 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic void removeBlock(MemoryBlock block, TaskMonitor monitor) throws LockException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {program.checkExclusiveAccess();checkBlock(block);MemoryBlockDB memBlock = (MemoryBlockDB) block;@@ -1901,9 +1750,6 @@ public class MemoryMapDB implements Memory, ManagerDB {attemptOverlaySpaceRemoval(os);}}- finally {- lock.release();- }}/**@@ -1913,17 +1759,12 @@ public class MemoryMapDB implements Memory, ManagerDB {* @param addressSpace overlay address space to be removed*/private void attemptOverlaySpaceRemoval(OverlayAddressSpace addressSpace) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {program.removeOverlaySpace(addressSpace.getName());}catch (LockException | NotFoundException e) {throw new AssertException();}- finally {- lock.release();- }-}private void checkRange(Address start, long size)@@ -2040,8 +1881,7 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic final String toString() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (blocks == null || blocks.isEmpty()) {return "[empty]\n";}@@ -2055,32 +1895,19 @@ public class MemoryMapDB implements Memory, ManagerDB {}return buffer.toString();}- finally {- lock.release();- }}@Overridepublic boolean equals(Object obj) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (obj instanceof Memory) {return obj == this;}if (obj instanceof AddressSetView) {- lock.acquire();- try {- return allAddrSet.equals(obj);- }- finally {- lock.release();- }+ return allAddrSet.equals(obj);}return false;}- finally {- lock.release();- }}@Override@@ -2157,15 +1984,13 @@ public class MemoryMapDB implements Memory, ManagerDB {oldProgressMax = monitor.getMaximum();oldProgress = monitor.getProgress();}- lock.acquire();- try {+ try (Closeable c = lock.write()) {return fileBytesAdapter.createFileBytes(filename, offset, size, is, monitor);}catch (IOCancelledException e) {throw new CancelledException();}finally {- lock.release();if (monitor != null) {monitor.setMaximum(oldProgressMax);monitor.setProgress(oldProgress);@@ -2189,8 +2014,7 @@ public class MemoryMapDB implements Memory, ManagerDB {@Overridepublic boolean deleteFileBytes(FileBytes fileBytes) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkFileBytes(fileBytes);if (inUse(fileBytes)) {return false;@@ -2198,9 +2022,6 @@ public class MemoryMapDB implements Memory, ManagerDB {// TODO: may need to generate a domain object eventreturn fileBytesAdapter.deleteFileBytes(fileBytes);}- finally {- lock.release();- }}private boolean inUse(FileBytes fileBytes) {@@ -2273,4 +2094,71 @@ public class MemoryMapDB implements Memory, ManagerDB {}}+ /**+ * Address set views into program memory which are iterator safe+ * for public API methods.+ */+ private class MemoryAddressSetViews {+ private AddressSet initializedAndLoaded = new AddressSet();+ private AddressSet initialized = new AddressSet();+ private AddressSet externalBlock = new AddressSet();+ private AddressSet execute = new AddressSet(); // replaced when block permissions change++ MemoryAddressSetViews() {+ // we have to process the non-mapped blocks first because to process the mapped+ // blocks we need the address sets for the non-mapped blocks to be complete+ for (MemoryBlockDB block : blocks) {+ block.clearMappedBlockList();+ if (!block.isMapped()) {+ addBlockAddresses(block);+ }+ }+ // process all mapped blocks after non-mapped-blocks above+ for (MemoryBlockDB block : blocks) {+ if (block.isMapped()) {+ addBlockAddresses(block);+ }+ }+ }++ /**+ * Update address sets with relevant addresses from the specified memory block.+ * In addition, allAddrSet will be updated if addToAll parameter is true.+ *+ * @param block memory block to be added+ * @param newAddrSetViews address set views which should be built-up+ * @param newAllAddrs if not null this set will be updated with the specified block's address range,+ * otherwise only the {@code addrSetView} sets will be updated.+ */+ private void addBlockAddresses(MemoryBlockDB block) {+ Address start = block.getStart();+ Address end = block.getEnd();+ if (block.isExternalBlock()) {+ externalBlock.add(start, end);+ }+ else if (block.isExecute()) {+ execute.add(start, end);+ }+ if (block.isMapped()) {+ // Identify source-blocks which block maps onto and add as a mapped-block to each of these+ AddressRange mappedRange = block.getSourceInfos().get(0).getMappedRange().get();+ for (MemoryBlockDB b : getBlocks(mappedRange.getMinAddress(),+ mappedRange.getMaxAddress())) {+ if (!b.isMapped()) {+ b.addMappedBlock(block);+ }+ }+ AddressSet mappedSet = getMappedIntersection(block, initialized);+ initialized.add(mappedSet);+ initializedAndLoaded.add(getMappedIntersection(block, initializedAndLoaded));+ }+ else if (block.isInitialized()) {+ initialized.add(start, end);+ if (block.isLoaded()) {+ initializedAndLoaded.add(start, end);+ }+ }+ }++ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/FragmentDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/FragmentDB.javaindex ae2f51acf4..4132f3a446 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/FragmentDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/FragmentDB.java@@ -20,11 +20,11 @@ import java.util.Iterator;import db.DBRecord;import db.Field;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.address.*;import ghidra.program.model.listing.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.NotFoundException;@@ -33,7 +33,7 @@ import ghidra.util.exception.NotFoundException;* Database implementation for Fragment.**/-class FragmentDB extends DatabaseObject implements ProgramFragment {+class FragmentDB extends DbObject implements ProgramFragment {private DBRecord record;private ModuleManager moduleMgr;@@ -45,13 +45,11 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {/*** Constructor* @param moduleMgr module manager- * @param cache fragment DB cache* @param record fragment record* @param addrSet fragment address set*/- FragmentDB(ModuleManager moduleMgr, DBObjectCache<FragmentDB> cache, DBRecord record,- AddressSet addrSet) {- super(cache, record.getKey());+ FragmentDB(ModuleManager moduleMgr, DBRecord record, AddressSet addrSet) {+ super(record.getKey());this.moduleMgr = moduleMgr;this.record = record;this.addrSet = addrSet;@@ -84,39 +82,32 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {@Overridepublic CodeUnitIterator getCodeUnits() {- checkIsValid();- return moduleMgr.getCodeUnits(this);+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ return moduleMgr.getCodeUnits(this);+ }}@Overridepublic String getComment() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getString(FragmentDBAdapter.FRAGMENT_COMMENTS_COL);}- finally {- lock.release();- }}@Overridepublic String getName() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getString(FragmentDBAdapter.FRAGMENT_NAME_COL);}- finally {- lock.release();- }}@Overridepublic int getNumParents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();Field[] keys =parentChildAdapter.getParentChildKeys(-key, ParentChildDBAdapter.CHILD_ID_COL);return keys.length;@@ -124,9 +115,6 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return 0;}@@ -142,20 +130,15 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {@Overridepublic void move(Address min, Address max) throws NotFoundException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();moduleMgr.move(this, min, max);}- finally {- lock.release();- }}@Overridepublic void setComment(String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();String oldComments = record.getString(FragmentDBAdapter.FRAGMENT_COMMENTS_COL);if (oldComments == null || !oldComments.equals(comment)) {@@ -170,16 +153,12 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {}}- finally {- lock.release();- }}@Overridepublic void setName(String name) throws DuplicateNameException {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.write()) {+ refreshIfNeeded();DBRecord r = fragmentAdapter.getFragmentRecord(name);if (r != null) {if (key != r.getKey()) {@@ -199,9 +178,6 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {moduleMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -211,86 +187,58 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {@Overridepublic boolean contains(Address start, Address end) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.contains(start, end);}- finally {- lock.release();- }}@Overridepublic boolean contains(Address addr) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.contains(addr);}- finally {- lock.release();- }}@Overridepublic boolean contains(AddressSetView rangeSet) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.contains(rangeSet);}- finally {- lock.release();- }}@Overridepublic boolean hasSameAddresses(AddressSetView view) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.hasSameAddresses(view);}- finally {- lock.release();- }}@Overridepublic AddressIterator getAddresses(boolean forward) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getAddresses(forward);}- finally {- lock.release();- }}@Overridepublic AddressIterator getAddresses(Address start, boolean forward) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getAddresses(start, forward);}- finally {- lock.release();- }}@Overridepublic AddressRangeIterator getAddressRanges() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getAddressRanges();}- finally {- lock.release();- }}@Override@@ -300,158 +248,106 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {@Overridepublic AddressRangeIterator getAddressRanges(boolean atStart) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getAddressRanges(atStart);}- finally {- lock.release();- }}@Overridepublic Address getMaxAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getMaxAddress();}- finally {- lock.release();- }}@Overridepublic Address getMinAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getMinAddress();}- finally {- lock.release();- }}@Overridepublic long getNumAddresses() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getNumAddresses();}- finally {- lock.release();- }}@Overridepublic int getNumAddressRanges() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getNumAddressRanges();}- finally {- lock.release();- }}@Overridepublic AddressSet intersect(AddressSetView view) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.intersect(view);}- finally {- lock.release();- }}@Overridepublic AddressSet intersectRange(Address start, Address end) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.intersectRange(start, end);}- finally {- lock.release();- }}@Overridepublic boolean intersects(Address start, Address end) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.intersects(start, end);}- finally {- lock.release();- }}@Overridepublic boolean intersects(AddressSetView set) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.intersects(set);}- finally {- lock.release();- }}@Overridepublic boolean isEmpty() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.isEmpty();}- finally {- lock.release();- }}@Overridepublic AddressSet subtract(AddressSetView set) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.subtract(set);}- finally {- lock.release();- }}@Overridepublic AddressSet union(AddressSetView set) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.union(set);}- finally {- lock.release();- }}@Overridepublic AddressSet xor(AddressSetView set) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.xor(set);}- finally {- lock.release();- }}ModuleManager getModuleManager() {@@ -474,86 +370,58 @@ class FragmentDB extends DatabaseObject implements ProgramFragment {@Overridepublic AddressRangeIterator getAddressRanges(Address start, boolean forward) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getAddressRanges(start, forward);}- finally {- lock.release();- }}@Overridepublic AddressRange getFirstRange() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getFirstRange();}- finally {- lock.release();- }}@Overridepublic AddressRange getLastRange() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getLastRange();}- finally {- lock.release();- }}@Overridepublic AddressRange getRangeContaining(Address address) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.getRangeContaining(address);}- finally {- lock.release();- }}@Overridepublic Iterator<AddressRange> iterator(boolean forward) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.iterator(forward);}- finally {- lock.release();- }}@Overridepublic Iterator<AddressRange> iterator(Address start, boolean forward) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.iterator(start, forward);}- finally {- lock.release();- }}@Overridepublic Address findFirstAddressInCommon(AddressSetView set) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return addrSet.findFirstAddressInCommon(set);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleDB.javaindex 275ab55fb8..ec3cbf12e9 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleDB.java@@ -20,11 +20,11 @@ import java.util.*;import db.DBRecord;import db.Field;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.address.*;import ghidra.program.model.listing.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;/**@@ -33,7 +33,7 @@ import ghidra.util.exception.*;***/-class ModuleDB extends DatabaseObject implements ProgramModule {+class ModuleDB extends DbObject implements ProgramModule {private DBRecord record;private ModuleManager moduleMgr;@@ -49,13 +49,10 @@ class ModuleDB extends DatabaseObject implements ProgramModule {** Constructor* @param moduleMgr module manager- * @param cache ModuleDB cache* @param record database record for this module- * @throws IOException if database IO error occurs*/- ModuleDB(ModuleManager moduleMgr, DBObjectCache<ModuleDB> cache, DBRecord record)- throws IOException {- super(cache, record.getKey());+ ModuleDB(ModuleManager moduleMgr, DBRecord record) {+ super(record.getKey());this.moduleMgr = moduleMgr;this.record = record;childCount = record.getIntValue(ModuleDBAdapter.MODULE_CHILD_COUNT_COL);@@ -83,8 +80,7 @@ class ModuleDB extends DatabaseObject implements ProgramModule {@Overridepublic void add(ProgramFragment fragment) throws DuplicateGroupException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();FragmentDB frag = (FragmentDB) fragment;long fragID = frag.getKey();@@ -103,17 +99,13 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic void add(ProgramModule module)throws CircularDependencyException, DuplicateGroupException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();ModuleDB moduleDB = (ModuleDB) module;long moduleID = moduleDB.getKey();@@ -136,9 +128,6 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -167,9 +156,7 @@ class ModuleDB extends DatabaseObject implements ProgramModule {@Overridepublic ProgramFragment createFragment(String fragmentName) throws DuplicateNameException {-- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (moduleAdapter.getModuleRecord(fragmentName) != null ||fragmentAdapter.getFragmentRecord(fragmentName) != null) {@@ -187,17 +174,12 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic ProgramModule createModule(String moduleName) throws DuplicateNameException {-- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (moduleAdapter.getModuleRecord(moduleName) != null ||fragmentAdapter.getFragmentRecord(moduleName) != null) {@@ -216,17 +198,13 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic Group[] getChildren() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();List<DBRecord> list = getParentChildRecords();Group[] kids = new Group[list.size()];if (kids.length != childCount) {@@ -249,45 +227,33 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return new Group[0];}@Overridepublic String getComment() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getString(ModuleDBAdapter.MODULE_COMMENTS_COL);}- finally {- lock.release();- }}@Overridepublic Address getFirstAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return findFirstAddress(this);}catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic int getIndex(String name) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();DBRecord fragmentRecord = fragmentAdapter.getFragmentRecord(name);DBRecord pcRec = null;if (fragmentRecord != null) {@@ -306,57 +272,42 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return -1;}@Overridepublic Address getLastAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return findLastAddress(this);}catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic Address getMaxAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return findMaxAddress(this, null);}catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic Address getMinAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return findMinAddress(this, null);}catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return null;}@@ -378,14 +329,10 @@ class ModuleDB extends DatabaseObject implements ProgramModule {@Overridepublic int getNumChildren() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return childCount;}- finally {- lock.release();- }}@Override@@ -393,18 +340,14 @@ class ModuleDB extends DatabaseObject implements ProgramModule {if (!(fragment instanceof FragmentDB)) {return false;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();FragmentDB frag = (FragmentDB) fragment;return moduleMgr.isDescendant(-frag.getKey(), key);}catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return false;}@@ -425,8 +368,7 @@ class ModuleDB extends DatabaseObject implements ProgramModule {@Overridepublic void moveChild(String name, int index) throws NotFoundException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();int currentIndex = 0;boolean foundName = false;@@ -470,16 +412,11 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }}@Overridepublic boolean removeChild(String name) throws NotEmptyException {-- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();DBRecord rec = fragmentAdapter.getFragmentRecord(name);boolean deleteChild = false;@@ -508,9 +445,6 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return false;}@@ -546,8 +480,7 @@ class ModuleDB extends DatabaseObject implements ProgramModule {Group group = null;ProgramFragment f = null;- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();long childID;ProgramModule m = moduleMgr.getModule(name);@@ -581,9 +514,6 @@ class ModuleDB extends DatabaseObject implements ProgramModule {moduleMgr.dbError(e);}- finally {- lock.release();- }}@Override@@ -597,21 +527,16 @@ class ModuleDB extends DatabaseObject implements ProgramModule {@Overridepublic String getName() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return record.getString(ModuleDBAdapter.MODULE_NAME_COL);}- finally {- lock.release();- }}@Overridepublic int getNumParents() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();Field[] keys =parentChildAdapter.getParentChildKeys(key, ParentChildDBAdapter.CHILD_ID_COL);return keys.length;@@ -619,9 +544,6 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }return 0;}@@ -642,8 +564,7 @@ class ModuleDB extends DatabaseObject implements ProgramModule {@Overridepublic void setComment(String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();String oldComments = record.getString(ModuleDBAdapter.MODULE_COMMENTS_COL);if (oldComments == null || !oldComments.equals(comment)) {@@ -657,15 +578,11 @@ class ModuleDB extends DatabaseObject implements ProgramModule {}}}- finally {- lock.release();- }}@Overridepublic void setName(String name) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (key == ModuleManager.ROOT_MODULE_ID) {moduleMgr.getProgram().setName(name);@@ -689,9 +606,6 @@ class ModuleDB extends DatabaseObject implements ProgramModule {catch (IOException e) {moduleMgr.dbError(e);}- finally {- lock.release();- }}////////////////////////////////////////////////////////////diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleManager.javaindex fc56aec76c..0f6f3d1a80 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/ModuleManager.java@@ -22,14 +22,14 @@ import java.util.HashSet;import db.*;import db.util.ErrorHandler;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.ProgramDB;+import ghidra.program.database.*;import ghidra.program.database.map.AddressMap;import ghidra.program.database.util.AddressRangeMapDB;import ghidra.program.model.address.*;import ghidra.program.model.listing.*;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -48,8 +48,8 @@ class ModuleManager {private ModuleDBAdapter moduleAdapter;private FragmentDBAdapter fragmentAdapter;private ParentChildDBAdapter parentChildAdapter;- private DBObjectCache<ModuleDB> moduleCache;- private DBObjectCache<FragmentDB> fragCache;+ private DbCache<ModuleDB> moduleCache;+ private DbCache<FragmentDB> fragCache;private TreeManager treeMgr;private HashSet<String> nameSet;private AddressRangeMapDB fragMap;@@ -87,8 +87,8 @@ class ModuleManager {initializeAdapters(openMode, monitor);- moduleCache = new DBObjectCache<>(100);- fragCache = new DBObjectCache<>(100);+ moduleCache = new DbCache<>(new ModuleFactory(), lock, 10);+ fragCache = new DbCache<>(new FragmentFactory(), lock, 10);if (openMode == OpenMode.CREATE) {createRootModule();@@ -226,32 +226,21 @@ class ModuleManager {}void setName(String name) {- lock.acquire();- try {- record.setString(ProgramTreeDBAdapter.TREE_NAME_COL, name);- treeMgr.updateTreeRecord(record, false);- }- finally {- lock.release();- }+ record.setString(ProgramTreeDBAdapter.TREE_NAME_COL, name);+ treeMgr.updateTreeRecord(record, false);}void imageBaseChanged(boolean commit) {- lock.acquire();- try {- if (commit) {- treeMgr.updateTreeRecord(record, true);- }- invalidateCache();- }- finally {- lock.release();+ if (commit) {+ treeMgr.updateTreeRecord(record, true);}+ invalidateCache();}private void createRootModule() throws IOException {DBRecord rootRecord = moduleAdapter.createModuleRecord(0, getProgram().getName());- ModuleDB root = new ModuleDB(this, moduleCache, rootRecord);+ ModuleDB root = new ModuleDB(this, rootRecord);+ moduleCache.add(root);nameSet.add(root.getName());}@@ -260,9 +249,8 @@ class ModuleManager {}void setProgramName(String oldName, String newName) {- lock.acquire();- try {- ModuleDB root = getModuleDB(ROOT_MODULE_ID);+ try (Closeable c = lock.write()) {+ ModuleDB root = moduleCache.getCachedInstance(ROOT_MODULE_ID);DBRecord rec = root.getRecord();rec.setString(ModuleDBAdapter.MODULE_NAME_COL, newName);moduleAdapter.updateModuleRecord(rec);@@ -272,50 +260,40 @@ class ModuleManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }}ProgramModule getRootModule() throws IOException {- return getModuleDB(0);+ return moduleCache.getCachedInstance((long) 0);}ProgramModule getModule(String name) throws IOException {DBRecord moduleRecord = moduleAdapter.getModuleRecord(name);- return getModuleDB(moduleRecord);+ return moduleCache.getCachedInstance(moduleRecord);}ProgramFragment getFragment(String name) throws IOException {DBRecord fragmentRecord = fragmentAdapter.getFragmentRecord(name);- return getFragmentDB(fragmentRecord);+ return fragCache.getCachedInstance(fragmentRecord);}ProgramFragment getFragment(Address addr) throws IOException {Field field = fragMap.getValue(addr);if (field != null) {- return getFragmentDB(field.getLongValue());+ return fragCache.getCachedInstance(field.getLongValue());}return null;}void addMemoryBlock(String name, AddressRange range) throws IOException {- lock.acquire();-- try {- FragmentDB frag = (FragmentDB) getFragment(name);- if (frag == null) {- frag = createFragmentAdjustNameAsNeeded(name);- }- frag.addRange(range);- fragMap.paintRange(range.getMinAddress(), range.getMaxAddress(),- new LongField(frag.getKey()));- treeMgr.updateTreeRecord(record);- versionTag = new Object(); //update the version tag- }- finally {- lock.release();+ FragmentDB frag = (FragmentDB) getFragment(name);+ if (frag == null) {+ frag = createFragmentAdjustNameAsNeeded(name);}+ frag.addRange(range);+ fragMap.paintRange(range.getMinAddress(), range.getMaxAddress(),+ new LongField(frag.getKey()));+ treeMgr.updateTreeRecord(record);+ versionTag = new Object(); //update the version tag}private FragmentDB createFragmentAdjustNameAsNeeded(String baseName) throws IOException {@@ -334,188 +312,121 @@ class ModuleManager {}}- void removeMemoryBlock(Address startAddr, Address endAddr, TaskMonitor monitor)- throws IOException {-- lock.acquire();- try {- AddressRangeIterator iter = fragMap.getAddressRanges(startAddr, endAddr);- while (iter.hasNext() && !monitor.isCancelled()) {- AddressRange range = iter.next();- Field field = fragMap.getValue(range.getMinAddress());- FragmentDB frag = getFragmentDB(field.getLongValue());- frag.removeRange(- new AddressRangeImpl(range.getMinAddress(), range.getMaxAddress()));- if (frag.isEmpty()) {- removeFragment(frag);- }- }- if (monitor.isCancelled()) {- return;+ void removeMemoryBlock(Address startAddr, Address endAddr, TaskMonitor monitor) {+ AddressRangeIterator iter = fragMap.getAddressRanges(startAddr, endAddr);+ while (iter.hasNext() && !monitor.isCancelled()) {+ AddressRange range = iter.next();+ Field field = fragMap.getValue(range.getMinAddress());+ FragmentDB frag = fragCache.getCachedInstance(field.getLongValue());+ frag.removeRange(+ new AddressRangeImpl(range.getMinAddress(), range.getMaxAddress()));+ if (frag.isEmpty()) {+ removeFragment(frag);}- fragMap.clearRange(startAddr, endAddr);- treeMgr.updateTreeRecord(record);- invalidateCache();-}- finally {- lock.release();+ if (monitor.isCancelled()) {+ return;}-+ fragMap.clearRange(startAddr, endAddr);+ treeMgr.updateTreeRecord(record);+ invalidateCache();}void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws AddressOverflowException, CancelledException {- lock.acquire();- try {- Address rangeEnd = fromAddr.addNoWrap(length - 1);-- AddressSet addrSet = new AddressSet(fromAddr, rangeEnd);- ArrayList<FragmentHolder> list = new ArrayList<>();-- AddressRangeIterator rangeIter = fragMap.getAddressRanges(fromAddr, rangeEnd);- while (rangeIter.hasNext() && !addrSet.isEmpty()) {- monitor.checkCancelled();- AddressRange range = rangeIter.next();- Field field = fragMap.getValue(range.getMinAddress());- try {- FragmentDB frag = getFragmentDB(field.getLongValue());- AddressSet intersection = addrSet.intersect(frag);- AddressRangeIterator fragRangeIter = intersection.getAddressRanges();- while (fragRangeIter.hasNext() && !monitor.isCancelled()) {- AddressRange fragRange = fragRangeIter.next();-- Address startAddr = fragRange.getMinAddress();- Address endAddr = fragRange.getMaxAddress();-- long offset = startAddr.subtract(fromAddr);- startAddr = toAddr.add(offset);- offset = endAddr.subtract(fromAddr);- endAddr = toAddr.add(offset);-- AddressRange newRange = new AddressRangeImpl(startAddr, endAddr);-- frag.removeRange(fragRange);- list.add(new FragmentHolder(frag, newRange));- }- addrSet = addrSet.subtract(intersection);- }- catch (IOException e) {- errHandler.dbError(e);- return;- }+ Address rangeEnd = fromAddr.addNoWrap(length - 1);- }+ AddressSet addrSet = new AddressSet(fromAddr, rangeEnd);+ ArrayList<FragmentHolder> list = new ArrayList<>();+ AddressRangeIterator rangeIter = fragMap.getAddressRanges(fromAddr, rangeEnd);+ while (rangeIter.hasNext() && !addrSet.isEmpty()) {monitor.checkCancelled();- fragMap.clearRange(fromAddr, rangeEnd);-- for (FragmentHolder fh : list) {- monitor.checkCancelled();- fragMap.paintRange(fh.range.getMinAddress(), fh.range.getMaxAddress(),- new LongField(fh.frag.getKey()));- fh.frag.addRange(fh.range);+ AddressRange range = rangeIter.next();+ Field field = fragMap.getValue(range.getMinAddress());+ FragmentDB frag = fragCache.getCachedInstance(field.getLongValue());+ AddressSet intersection = addrSet.intersect(frag);+ AddressRangeIterator fragRangeIter = intersection.getAddressRanges();+ while (fragRangeIter.hasNext() && !monitor.isCancelled()) {+ AddressRange fragRange = fragRangeIter.next();++ Address startAddr = fragRange.getMinAddress();+ Address endAddr = fragRange.getMaxAddress();++ long offset = startAddr.subtract(fromAddr);+ startAddr = toAddr.add(offset);+ offset = endAddr.subtract(fromAddr);+ endAddr = toAddr.add(offset);++ AddressRange newRange = new AddressRangeImpl(startAddr, endAddr);++ frag.removeRange(fragRange);+ list.add(new FragmentHolder(frag, newRange));}- treeMgr.updateTreeRecord(record);+ addrSet = addrSet.subtract(intersection);+ }- // generate an event...- getProgram().programTreeChanged(treeID, ProgramEvent.FRAGMENT_MOVED, null,- new AddressRangeImpl(fromAddr, rangeEnd),- new AddressRangeImpl(toAddr, toAddr.addNoWrap(length - 1)));+ monitor.checkCancelled();+ fragMap.clearRange(fromAddr, rangeEnd);+ for (FragmentHolder fh : list) {+ monitor.checkCancelled();+ fragMap.paintRange(fh.range.getMinAddress(), fh.range.getMaxAddress(),+ new LongField(fh.frag.getKey()));+ fh.frag.addRange(fh.range);}- finally {- lock.release();- }+ treeMgr.updateTreeRecord(record);++ // generate an event...+ getProgram().programTreeChanged(treeID, ProgramEvent.FRAGMENT_MOVED, null,+ new AddressRangeImpl(fromAddr, rangeEnd),+ new AddressRangeImpl(toAddr, toAddr.addNoWrap(length - 1)));}void fragmentAdded(long parentID, ProgramFragment fragment) {- lock.acquire();- try {- ProgramModule parent = getModuleDB(parentID);- nameSet.add(fragment.getName());- treeMgr.updateTreeRecord(record);- getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_ADDED, null, parent,- fragment);- }- catch (IOException e) {- errHandler.dbError(e);-- }- finally {- lock.release();- }+ ProgramModule parent = moduleCache.getCachedInstance(parentID);+ nameSet.add(fragment.getName());+ treeMgr.updateTreeRecord(record);+ getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_ADDED, null, parent,+ fragment);}void moduleAdded(long parentID, ProgramModule module) {- lock.acquire();- try {- ProgramModule parent = getModuleDB(parentID);- nameSet.add(module.getName());- treeMgr.updateTreeRecord(record);- getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_ADDED, null, parent, module);- }- catch (IOException e) {- errHandler.dbError(e);-- }- finally {- lock.release();- }+ ProgramModule parent = moduleCache.getCachedInstance(parentID);+ nameSet.add(module.getName());+ treeMgr.updateTreeRecord(record);+ getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_ADDED, null, parent, module);}void groupRemoved(ModuleDB parentModule, long childID, String childName, boolean isFragment,boolean deleteChild) {- lock.acquire();- try {- if (deleteChild && isFragment) {- nameSet.remove(childName);- fragCache.delete(childID);- }- else if (deleteChild) {- nameSet.remove(childName);- moduleCache.delete(childID);-- }- treeMgr.updateTreeRecord(record);- getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_REMOVED, null, parentModule,- childName);-+ if (deleteChild && isFragment) {+ nameSet.remove(childName);+ fragCache.delete(childID);}- finally {- lock.release();+ else if (deleteChild) {+ nameSet.remove(childName);+ moduleCache.delete(childID);+}+ treeMgr.updateTreeRecord(record);+ getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_REMOVED, null, parentModule,+ childName);}void commentsChanged(String oldComments, Group group) {- lock.acquire();- try {- treeMgr.updateTreeRecord(record);-- getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_COMMENT_CHANGED, null,- oldComments, group);-- }- finally {- lock.release();- }+ treeMgr.updateTreeRecord(record);+ getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_COMMENT_CHANGED, null,+ oldComments, group);}void nameChanged(String oldName, Group group) {- lock.acquire();- try {- nameSet.remove(oldName);- nameSet.add(group.getName());- treeMgr.updateTreeRecord(record);- getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_RENAMED, null, oldName,- group);-- }- finally {- lock.release();- }+ nameSet.remove(oldName);+ nameSet.add(group.getName());+ treeMgr.updateTreeRecord(record);+ getProgram().programTreeChanged(treeID, ProgramEvent.GROUP_RENAMED, null, oldName, group);}/**@@ -548,60 +459,6 @@ class ModuleManager {return false;}- FragmentDB getFragmentDB(DBRecord fragmentRecord) {- lock.acquire();- try {- if (fragmentRecord == null) {- return null;- }- FragmentDB f = fragCache.get(fragmentRecord.getKey());- if (f != null) {- return f;- }- return createFragmentDB(fragmentRecord);-- }- finally {- lock.release();- }- }-- FragmentDB getFragmentDB(long fragID) throws IOException {- lock.acquire();- try {- FragmentDB frag = fragCache.get(fragID);- if (frag != null) {- return frag;- }- DBRecord fragmentRecord = fragmentAdapter.getFragmentRecord(fragID);- return createFragmentDB(fragmentRecord);-- }- finally {- lock.release();- }- }-- ModuleDB getModuleDB(DBRecord moduleRecord) throws IOException {- if (moduleRecord == null) {- return null;- }- ModuleDB moduleDB = moduleCache.get(moduleRecord.getKey());- if (moduleDB != null) {- return moduleDB;- }- return createModuleDB(moduleRecord);- }-- ModuleDB getModuleDB(long moduleID) throws IOException {- ModuleDB moduleDB = moduleCache.get(moduleID);- if (moduleDB != null) {- return moduleDB;- }- DBRecord moduleRecord = moduleAdapter.getModuleRecord(moduleID);- return createModuleDB(moduleRecord);- }-String getTreeName() {return treeMgr.getTreeName(treeID);}@@ -618,75 +475,49 @@ class ModuleManager {* @throws NotFoundException if address range not fully contained within program memory*/void move(FragmentDB destFrag, Address min, Address max) throws NotFoundException {-- lock.acquire();- try {- if (!getProgram().getMemory().contains(min, max)) {- throw new NotFoundException(- "Address range for " + min + ", " + max + " is not contained in memory");- }- AddressSet set = new AddressSet();-- AddressRangeIterator iter = fragMap.getAddressRanges(min, max);- AddressSet addrSet = new AddressSet(min, max);-- while (iter.hasNext() && !addrSet.isEmpty()) {- AddressRange range = iter.next();- Field field = fragMap.getValue(range.getMinAddress());- try {- FragmentDB frag = getFragmentDB(field.getLongValue());- if (frag != destFrag) {- AddressSet intersection = addrSet.intersect(frag);- AddressRangeIterator fragRangeIter = intersection.getAddressRanges();- while (fragRangeIter.hasNext()) {- AddressRange fragRange = fragRangeIter.next();- set.add(fragRange);- frag.removeRange(fragRange);- }- addrSet = addrSet.subtract(intersection);- }+ if (!getProgram().getMemory().contains(min, max)) {+ throw new NotFoundException(+ "Address range for " + min + ", " + max + " is not contained in memory");+ }+ AddressSet set = new AddressSet();++ AddressRangeIterator iter = fragMap.getAddressRanges(min, max);+ AddressSet addrSet = new AddressSet(min, max);++ while (iter.hasNext() && !addrSet.isEmpty()) {+ AddressRange range = iter.next();+ Field field = fragMap.getValue(range.getMinAddress());+ FragmentDB frag = fragCache.getCachedInstance(field.getLongValue());+ if (frag != destFrag) {+ AddressSet intersection = addrSet.intersect(frag);+ AddressRangeIterator fragRangeIter = intersection.getAddressRanges();+ while (fragRangeIter.hasNext()) {+ AddressRange fragRange = fragRangeIter.next();+ set.add(fragRange);+ frag.removeRange(fragRange);}- catch (IOException e) {- errHandler.dbError(e);- return;- }- }-- Field field = new LongField(destFrag.getKey());-- AddressRangeIterator rangeIter = set.getAddressRanges();- while (rangeIter.hasNext()) {- AddressRange range = rangeIter.next();- fragMap.paintRange(range.getMinAddress(), range.getMaxAddress(), field);- destFrag.addRange(range);+ addrSet = addrSet.subtract(intersection);}- treeMgr.updateTreeRecord(record);+ }- getProgram().programTreeChanged(treeID, ProgramEvent.FRAGMENT_CHANGED, null, min, max);+ Field field = new LongField(destFrag.getKey());+ AddressRangeIterator rangeIter = set.getAddressRanges();+ while (rangeIter.hasNext()) {+ AddressRange range = rangeIter.next();+ fragMap.paintRange(range.getMinAddress(), range.getMaxAddress(), field);+ destFrag.addRange(range);}- finally {- lock.release();- }+ treeMgr.updateTreeRecord(record);++ getProgram().programTreeChanged(treeID, ProgramEvent.FRAGMENT_CHANGED, null, min, max);}FragmentDB getFragment(CodeUnit cu) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Field field = fragMap.getValue(cu.getMinAddress());- if (field != null) {- try {- return getFragmentDB(field.getLongValue());- }- catch (IOException e) {- errHandler.dbError(e);- }- }+ return fragCache.getCachedInstance(field.getLongValue());}- finally {- lock.release();- }- return null;}void childReordered(ModuleDB parentModule, Group child) {@@ -702,8 +533,7 @@ class ModuleManager {}String[] getParentNames(long childID) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Field[] keys =parentChildAdapter.getParentChildKeys(childID, ParentChildDBAdapter.CHILD_ID_COL);String[] names = new String[keys.length];@@ -719,16 +549,12 @@ class ModuleManager {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return new String[0];}ProgramModule[] getParents(long childID) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Field[] keys =parentChildAdapter.getParentChildKeys(childID, ParentChildDBAdapter.CHILD_ID_COL);ProgramModule[] modules = new ProgramModule[keys.length];@@ -737,41 +563,18 @@ class ModuleManager {parentChildAdapter.getParentChildRecord(keys[i].getLongValue());DBRecord mrec = moduleAdapter.getModuleRecord(parentChildRecord.getLongValue(ParentChildDBAdapter.PARENT_ID_COL));- modules[i] = getModuleDB(mrec);+ modules[i] = moduleCache.getCachedInstance(mrec);}return modules;}catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return new ProgramModule[0];}- private ModuleDB createModuleDB(DBRecord moduleRecord) throws IOException {- if (moduleRecord != null) {- ModuleDB moduleDB;- moduleDB = new ModuleDB(this, moduleCache, moduleRecord);- nameSet.add(moduleDB.getName());- return moduleDB;- }- return null;- }-- private FragmentDB createFragmentDB(DBRecord fragmentRecord) {- if (fragmentRecord != null) {- FragmentDB f = new FragmentDB(this, fragCache, fragmentRecord,- getFragmentAddressSet(fragmentRecord.getKey()));- nameSet.add(f.getName());- return f;- }- return null;- }-private void removeFragment(FragmentDB frag) {- // remove frag from all of its parents+ // remove fragment from all of its parentsProgramModule[] parents = frag.getParents();for (ProgramModule parent : parents) {try {@@ -785,6 +588,7 @@ class ModuleManager {}AddressSet getFragmentAddressSet(long fragID) {+ // assumes the lock is acquiredreturn fragMap.getAddressSet(new LongField(fragID));}@@ -799,17 +603,10 @@ class ModuleManager {}void invalidateCache() {- lock.acquire();- try {- versionTag = new Object();- moduleCache.invalidate();- fragCache.invalidate();- record = treeMgr.getTreeRecord(treeID);- }- finally {- lock.release();- }-+ versionTag = new Object();+ moduleCache.invalidate();+ fragCache.invalidate();+ record = treeMgr.getTreeRecord(treeID);}Object getVersionTag() {@@ -835,4 +632,61 @@ class ModuleManager {DBHandle getDatabaseHandle() {return treeMgr.getDatabaseHandle();}++ FragmentDB getFragmentDB(long key) {+ return fragCache.getCachedInstance(key);+ }++ ModuleDB getModuleDB(long key) {+ return moduleCache.getCachedInstance(key);+ }++ FragmentDB getFragmentDB(DBRecord fragmentRecord) {+ return fragCache.getCachedInstance(fragmentRecord);+ }++ ModuleDB getModuleDB(DBRecord moduleRecord) {+ return moduleCache.getCachedInstance(moduleRecord);+ }++ class ModuleFactory implements DbFactory<ModuleDB> {++ @Override+ public ModuleDB instantiate(long key) {+ try {+ DBRecord rec = moduleAdapter.getModuleRecord(key);+ return rec == null ? null : instantiate(rec);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public ModuleDB instantiate(DBRecord rec) {+ return new ModuleDB(ModuleManager.this, rec);+ }+ }++ class FragmentFactory implements DbFactory<FragmentDB> {++ @Override+ public FragmentDB instantiate(long key) {+ try {+ DBRecord rec = fragmentAdapter.getFragmentRecord(key);+ return rec == null ? null : instantiate(rec);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public FragmentDB instantiate(DBRecord rec) {+ return new FragmentDB(ModuleManager.this, rec, getFragmentAddressSet(rec.getKey()));+ }+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/TreeManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/TreeManager.javaindex 4aeb71cd48..db7e3acb40 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/TreeManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/module/TreeManager.java@@ -31,6 +31,7 @@ import ghidra.program.model.mem.Memory;import ghidra.program.model.mem.MemoryBlock;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -97,15 +98,11 @@ public class TreeManager implements ManagerDB {}public void imageBaseChanged(boolean commit) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {for (ModuleManager m : treeMap.values()) {m.imageBaseChanged(commit);}}- finally {- lock.release();- }}/**@@ -117,8 +114,7 @@ public class TreeManager implements ManagerDB {*/public ProgramModule createRootModule(String treeName) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (treeMap.containsKey(treeName)) {throw new DuplicateNameException("Root module named " + treeName + " already exists");@@ -140,9 +136,6 @@ public class TreeManager implements ManagerDB {catch (VersionException | CancelledException e) {throw new RuntimeException(e); // unexpected exception}- finally {- lock.release();- }return null;}@@ -153,8 +146,7 @@ public class TreeManager implements ManagerDB {* given name*/public ProgramModule getRootModule(String treeName) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {ModuleManager m = treeMap.get(treeName);if (m != null) {return m.getRootModule();@@ -163,9 +155,6 @@ public class TreeManager implements ManagerDB {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return null;}@@ -225,8 +214,7 @@ public class TreeManager implements ManagerDB {* for another root*/public void renameTree(String oldName, String newName) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (treeMap.containsKey(newName)) {throw new DuplicateNameException("Name " + newName + " already exists");}@@ -242,9 +230,6 @@ public class TreeManager implements ManagerDB {null, oldName, newName);}- finally {- lock.release();- }}/**@@ -253,8 +238,7 @@ public class TreeManager implements ManagerDB {* @return true if the tree was removed*/public boolean removeTree(String treeName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (treeMap.containsKey(treeName)) {DBRecord rec = treeAdapter.getRecord(treeName);treeAdapter.deleteRecord(rec.getKey());@@ -269,9 +253,6 @@ public class TreeManager implements ManagerDB {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return false;}@@ -283,8 +264,7 @@ public class TreeManager implements ManagerDB {* @return null if there is no module with the given name in the tree*/public ProgramModule getModule(String treeName, String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {ModuleManager m = treeMap.get(treeName);if (m != null) {return m.getModule(name);@@ -294,10 +274,6 @@ public class TreeManager implements ManagerDB {errHandler.dbError(e);}- finally {- lock.release();- }-return null;}@@ -309,8 +285,7 @@ public class TreeManager implements ManagerDB {* @return null if there is no fragment with the given name in the tree*/public ProgramFragment getFragment(String treeName, String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {ModuleManager m = treeMap.get(treeName);if (m != null) {return m.getFragment(name);@@ -320,9 +295,7 @@ public class TreeManager implements ManagerDB {errHandler.dbError(e);}- finally {- lock.release();- }+return null;}@@ -335,8 +308,7 @@ public class TreeManager implements ManagerDB {* exist in memory*/public ProgramFragment getFragment(String treeName, Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {ModuleManager m = treeMap.get(treeName);if (m != null) {return m.getFragment(addr);@@ -345,10 +317,6 @@ public class TreeManager implements ManagerDB {}catch (IOException e) {errHandler.dbError(e);-- }- finally {- lock.release();}return null;}@@ -359,8 +327,7 @@ public class TreeManager implements ManagerDB {* @param range memory block address range*/public void addMemoryBlock(String name, AddressRange range) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Iterator<String> keys = treeMap.keySet().iterator();while (keys.hasNext()) {ModuleManager m = treeMap.get(keys.next());@@ -370,17 +337,13 @@ public class TreeManager implements ManagerDB {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }}@Overridepublic void deleteAddressRange(Address startAddr, Address endAddr, TaskMonitor monitor)throws CancelledException {AddressRange.checkValidRange(startAddr, endAddr);- lock.acquire();- try {+ try (Closeable c = lock.write()) {Iterator<String> keys = treeMap.keySet().iterator();while (keys.hasNext()) {if (monitor.isCancelled()) {@@ -390,22 +353,13 @@ public class TreeManager implements ManagerDB {m.removeMemoryBlock(startAddr, endAddr, monitor);}}- catch (IOException e) {- errHandler.dbError(e);-- }- finally {- lock.release();- }}@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws AddressOverflowException, CancelledException {- lock.acquire();-- try {+ try (Closeable c = lock.write()) {Iterator<String> keys = treeMap.keySet().iterator();monitor.setMessage("Moving folders/fragments...");while (keys.hasNext()) {@@ -421,9 +375,6 @@ public class TreeManager implements ManagerDB {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }}AddressMap getAddressMap() {@@ -536,13 +487,9 @@ public class TreeManager implements ManagerDB {@Overridepublic void invalidateCache(boolean all) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {refreshTreeMap(all);}- finally {- lock.release();- }}public void setProgramName(String oldName, String newName) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/DBPropertyMapManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/DBPropertyMapManager.javaindex dce0a9cc31..2f032b30d9 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/DBPropertyMapManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/DBPropertyMapManager.java@@ -35,6 +35,7 @@ import ghidra.program.model.util.*;import ghidra.program.util.ChangeManager;import ghidra.program.util.ProgramEvent;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.map.TypeMismatchException;import ghidra.util.task.TaskMonitor;@@ -115,8 +116,7 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {@Overridepublic void invalidateCache(boolean all) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {for (PropertyMapDB<?> map : propertyMapCache.values()) {map.invalidate();}@@ -130,9 +130,6 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {catch (VersionException e) {throw new AssertException();}- finally {- lock.release();- }}/**@@ -249,8 +246,7 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {@Overridepublic IntPropertyMap createIntPropertyMap(String propertyName) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (propertyMapCache.containsKey(propertyName)) {throw new DuplicateNameException();}@@ -272,9 +268,6 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {}return pm;}- finally {- lock.release();- }}/**@@ -287,8 +280,7 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {public LongPropertyMap createLongPropertyMap(String propertyName)throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (propertyMapCache.containsKey(propertyName)) {throw new DuplicateNameException();}@@ -311,9 +303,6 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {return pm;}- finally {- lock.release();- }}/**@@ -326,8 +315,7 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {public StringPropertyMap createStringPropertyMap(String propertyName)throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (propertyMapCache.containsKey(propertyName)) {throw new DuplicateNameException();}@@ -349,17 +337,13 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {}return pm;}- finally {- lock.release();- }}@Overridepublic <T extends Saveable> ObjectPropertyMap<T> createObjectPropertyMap(String propertyName,Class<T> objectClass) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (propertyMapCache.containsKey(propertyName)) {throw new DuplicateNameException();}@@ -382,9 +366,6 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {}return pm;}- finally {- lock.release();- }}/**@@ -397,8 +378,7 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {public VoidPropertyMap createVoidPropertyMap(String propertyName)throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (propertyMapCache.containsKey(propertyName)) {throw new DuplicateNameException();}@@ -420,9 +400,6 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {}return pm;}- finally {- lock.release();- }}/**@@ -512,8 +489,7 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {@Overridepublic boolean removePropertyMap(String propertyName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {PropertyMapDB<?> pm = propertyMapCache.remove(propertyName);if (pm != null) {pm.delete();@@ -528,69 +504,50 @@ public class DBPropertyMapManager implements PropertyMapManager, ManagerDB {program.dbError(e);}- finally {- lock.release();- }return false;}@Overridepublic Iterator<String> propertyManagers() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {// NOTE: infrequent use expectedreturn propertyMapCache.keySet().stream().sorted() // Sort keys in natural order.iterator();}- finally {- lock.release();- }}@Overridepublic void removeAll(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {for (PropertyMapDB<?> pm : propertyMapCache.values()) {pm.remove(addr);}}- finally {- lock.release();- }}@Overridepublic void removeAll(Address startAddr, Address endAddr, TaskMonitor monitor)throws CancelledException {AddressRange.checkValidRange(startAddr, endAddr);- lock.acquire();- try {+ try (Closeable c = lock.write()) {for (PropertyMapDB<?> pm : propertyMapCache.values()) {monitor.checkCancelled();pm.removeRange(startAddr, endAddr);}}- finally {- lock.release();- }}@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {for (PropertyMapDB<?> pm : propertyMapCache.values()) {monitor.checkCancelled();pm.moveRange(fromAddr, fromAddr.add(length - 1), toAddr);}}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/IntPropertyMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/IntPropertyMapDB.javaindex 9bf86e1e5a..835cb91e59 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/IntPropertyMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/IntPropertyMapDB.java@@ -25,6 +25,7 @@ import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.util.IntPropertyMap;import ghidra.program.util.ChangeManager;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -75,8 +76,7 @@ public class IntPropertyMapDB extends PropertyMapDB<Integer> implements IntPrope@Overridepublic void add(Address addr, int value) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();Integer oldValue = null;long addrKey = addrMap.getKey(addr, true);@@ -99,9 +99,6 @@ public class IntPropertyMapDB extends PropertyMapDB<Integer> implements IntPropecatch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/LongPropertyMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/LongPropertyMapDB.javaindex 50882cbff0..905615b99b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/LongPropertyMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/LongPropertyMapDB.java@@ -25,6 +25,7 @@ import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.util.LongPropertyMap;import ghidra.program.util.ChangeManager;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -75,8 +76,7 @@ public class LongPropertyMapDB extends PropertyMapDB<Long> implements LongProper@Overridepublic void add(Address addr, long value) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();Long oldValue = null;long addrKey = addrMap.getKey(addr, true);@@ -99,9 +99,6 @@ public class LongPropertyMapDB extends PropertyMapDB<Long> implements LongPropererrHandler.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/ObjectPropertyMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/ObjectPropertyMapDB.javaindex 24bf94bbd9..782a6185a9 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/ObjectPropertyMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/ObjectPropertyMapDB.java@@ -25,6 +25,7 @@ import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.util.ObjectPropertyMap;import ghidra.program.util.ChangeManager;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.Saveable;import ghidra.util.classfinder.ClassTranslator;@@ -293,8 +294,7 @@ public class ObjectPropertyMapDB<T extends Saveable> extends PropertyMapDB<T>@Overridepublic void add(Address addr, T value) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (!saveableObjectClass.isAssignableFrom(value.getClass())) {throw new IllegalArgumentException("value is not " + saveableObjectClass.getName());@@ -341,10 +341,6 @@ public class ObjectPropertyMapDB<T extends Saveable> extends PropertyMapDB<T>catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }-}private boolean isPrivate(Saveable value) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/PropertyMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/PropertyMapDB.javaindex 7a0cfe1d1f..faefdc617a 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/PropertyMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/PropertyMapDB.java@@ -21,7 +21,7 @@ import java.util.NoSuchElementException;import db.*;import db.util.ErrorHandler;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.database.map.*;import ghidra.program.database.util.DatabaseTableUtils;import ghidra.program.model.address.*;@@ -29,6 +29,7 @@ import ghidra.program.model.util.PropertyMap;import ghidra.program.util.ChangeManager;import ghidra.program.util.ChangeManagerAdapter;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.datastruct.ObjectCache;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -38,7 +39,7 @@ import ghidra.util.task.TaskMonitor;* The map is stored within a database table.* @param <T> property value type*/-public abstract class PropertyMapDB<T> extends DatabaseObject implements PropertyMap<T> {+public abstract class PropertyMapDB<T> extends DbObject implements PropertyMap<T> {private static final String PROPERTY_TABLE_PREFIX = "Property Map - ";@@ -77,8 +78,7 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert*/PropertyMapDB(DBHandle dbHandle, ErrorHandler errHandler, ChangeManager changeMgr,AddressMap addrMap, String name) {- super(null, 0); // DatabaseObject cache is not used-+ super(0);this.dbHandle = dbHandle;this.errHandler = errHandler;this.changeMgr = changeMgr;@@ -215,11 +215,12 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert@Overridepublic void clear() {- if (propertyTable == null) {- return;- }- cache = new ObjectCache<>(DEFAULT_CACHE_SIZE);- try {+ try (Closeable c = lock.write()) {+ checkDeleted();+ if (propertyTable == null) {+ return;+ }+ cache = new ObjectCache<>(DEFAULT_CACHE_SIZE);propertyTable.deleteAll();}catch (IOException e) {@@ -232,13 +233,10 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert* @param size the size of the cache.*/public void setCacheSize(int size) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ checkDeleted();cache.setHardCacheSize(size);}- finally {- lock.release();- }}/**@@ -248,8 +246,7 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert* @throws IOException if IO error occurs*/public void delete() throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();setDeleted();if (propertyTable != null) {@@ -258,9 +255,6 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements PropertpropertyTable = null;}}- finally {- lock.release();- }}@Override@@ -301,8 +295,7 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert@Overridepublic boolean removeRange(Address startAddr, Address endAddr) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (propertyTable == null) {return false;@@ -315,17 +308,13 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propertcatch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return false;}@Overridepublic boolean remove(Address addr) {- lock.acquire();boolean result = false;- try {+ try (Closeable c = lock.write()) {checkDeleted();if (propertyTable == null) {return false;@@ -338,9 +327,6 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propertcatch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return result;}@@ -612,13 +598,9 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert* Invalidates the cache.*/public void invalidate() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {setInvalid();}- finally {- lock.release();- }}@Override@@ -637,8 +619,7 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert@Overridepublic void moveRange(Address start, Address end, Address newStart) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();cache = new ObjectCache<>(DEFAULT_CACHE_SIZE);if (propertyTable != null) {@@ -655,8 +636,5 @@ public abstract class PropertyMapDB<T> extends DatabaseObject implements Propert}}- finally {- lock.release();- }}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/StringPropertyMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/StringPropertyMapDB.javaindex 1afa5a204f..1360c59304 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/StringPropertyMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/StringPropertyMapDB.java@@ -25,6 +25,7 @@ import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.util.StringPropertyMap;import ghidra.program.util.ChangeManager;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.exception.VersionException;import ghidra.util.task.TaskMonitor;@@ -76,8 +77,7 @@ public class StringPropertyMapDB extends PropertyMapDB<String> implements String@Overridepublic void add(Address addr, String value) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();long addrKey = addrMap.getKey(addr, true);@@ -102,9 +102,6 @@ public class StringPropertyMapDB extends PropertyMapDB<String> implements StringerrHandler.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/VoidPropertyMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/VoidPropertyMapDB.javaindex 018745d470..004e6e1d66 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/VoidPropertyMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/properties/VoidPropertyMapDB.java@@ -25,6 +25,7 @@ import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.util.VoidPropertyMap;import ghidra.program.util.ChangeManager;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.exception.VersionException;import ghidra.util.task.TaskMonitor;@@ -62,8 +63,7 @@ public class VoidPropertyMapDB extends PropertyMapDB<Boolean> implements VoidPro@Overridepublic void add(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();long addrKey = addrMap.getKey(addr, true);@@ -80,9 +80,6 @@ public class VoidPropertyMapDB extends PropertyMapDB<Boolean> implements VoidProcatch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/BigRefListV0.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/BigRefListV0.javaindex 4f8e5f2dda..85a7d0d152 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/BigRefListV0.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/BigRefListV0.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -25,7 +25,7 @@ import java.io.IOException;import java.util.Iterator;import db.*;-import ghidra.program.database.DBObjectCache;+import ghidra.program.database.DbCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;@@ -58,37 +58,50 @@ class BigRefListV0 extends RefList {private DBRecord record;/**- * Construct new empty reference list+ * Creates a new BigRefListV0 and adds it to the cache* @param address address associated with this list* @param adapter entry record storage adapter* @param addrMap address map for encoding/decoding addresses* @param program associated Program* @param cache RefList object cache* @param isFrom true for from-adapter use, false for to-adapter use+ * @return a new BigRefListV0 that has been added to the cache* @throws IOException if database IO error occurs*/- BigRefListV0(Address address, RecordAdapter adapter, AddressMap addrMap, ProgramDB program,- DBObjectCache<RefList> cache, boolean isFrom) throws IOException {- super(addrMap.getKey(address, true), address, adapter, addrMap, program, cache, isFrom);- record = ToAdapter.TO_REFS_SCHEMA.createRecord(key);- table = program.getDBHandle()- .createTable(getTableName(), BIG_REFS_SCHEMA, new int[] { ADDRESS_COL });+ static BigRefListV0 createNew(Address address, RecordAdapter adapter, AddressMap addrMap,+ ProgramDB program, boolean isFrom) throws IOException {+ return new BigRefListV0(address, adapter, addrMap, program, isFrom);}/**- * Construct reference list for existing record+ * Creates a new BigRefListV0 from an existing record and adds it to the cache* @param rec existing refList record* @param adapter entry record storage adapter* @param addrMap address map for encoding/decoding addresses* @param program associated Program* @param cache RefList object cache* @param isFrom true for from-adapter use, false for to-adapter use+ * @return a new BigRefListV0 that has been added to the cache* @throws IOException if database IO error occurs*/- BigRefListV0(DBRecord rec, RecordAdapter adapter, AddressMap addrMap, ProgramDB program,- DBObjectCache<RefList> cache, boolean isFrom) throws IOException {- super(rec.getKey(), addrMap.decodeAddress(rec.getKey()), adapter, addrMap, program, cache,- isFrom);+ static BigRefListV0 createExisting(DBRecord rec, RecordAdapter adapter, AddressMap addrMap,+ ProgramDB program, boolean isFrom) throws IOException {+ return new BigRefListV0(rec, adapter, addrMap, program, isFrom);+ }++ private BigRefListV0(Address address, RecordAdapter adapter, AddressMap addrMap,+ ProgramDB program, boolean isFrom) throws IOException {++ super(addrMap.getKey(address, true), address, adapter, addrMap, program, isFrom);+ record = ToAdapter.TO_REFS_SCHEMA.createRecord(key);+ table = program.getDBHandle()+ .createTable(getTableName(), BIG_REFS_SCHEMA, new int[] { ADDRESS_COL });+ }++ private BigRefListV0(DBRecord rec, RecordAdapter adapter, AddressMap addrMap, ProgramDB program,+ boolean isFrom) throws IOException {++ super(rec.getKey(), addrMap.decodeAddress(rec.getKey()), adapter, addrMap, program, isFrom);if (rec.getBinaryData(ToAdapter.REF_DATA_COL) != null) {throw new IllegalArgumentException("Invalid reference record");}@@ -109,7 +122,7 @@ class BigRefListV0 extends RefList {}@Override- public RefList checkRefListSize(DBObjectCache<RefList> cache, int newSpaceRequired) {+ public RefList checkRefListSize(DbCache<RefList> cache, int newSpaceRequired) {return this;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapter.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapter.javaindex c99e1899d1..75f991f92a 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapter.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapter.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -20,7 +20,6 @@ import java.io.IOException;import db.*;import db.util.ErrorHandler;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.*;@@ -98,10 +97,10 @@ abstract class FromAdapter implements RecordAdapter {throw new CancelledException();}Address from = addrIter.next();- RefListV0 refList = (RefListV0) oldAdapter.getRefList(null, null, from,- oldAddrMap.getKey(from, false));+ long key = oldAddrMap.getKey(from, false);+ RefListV0 refList = (RefListV0) oldAdapter.getRefList(null, from, key);Reference[] refs = refList.getAllRefs();- RefListV0 newRefList = new RefListV0(from, tmpAdapter, addrMap, null, null, true);+ RefListV0 newRefList = RefListV0.createNew(from, tmpAdapter, addrMap, null, true);newRefList.addRefs(refs);monitor.setProgress(++count);}@@ -116,7 +115,7 @@ abstract class FromAdapter implements RecordAdapter {}Address from = addrIter.next();long fromAddr = addrMap.getKey(from, true);- RefListV0 refList = (RefListV0) tmpAdapter.getRefList(null, null, from, fromAddr);+ RefListV0 refList = (RefListV0) tmpAdapter.getRefList(null, from, fromAddr);newAdapter.createRecord(fromAddr, refList != null ? refList.getNumRefs() : 0,(byte) -1, refList != null ? refList.getData() : null);monitor.setProgress(++count);@@ -130,11 +129,9 @@ abstract class FromAdapter implements RecordAdapter {abstract int getRecordCount();- abstract RefList createRefList(ProgramDB program, DBObjectCache<RefList> cache,- Address fromAddr) throws IOException;+ abstract RefList createRefList(ProgramDB program, Address fromAddr) throws IOException;- abstract RefList getRefList(ProgramDB program, DBObjectCache<RefList> cache, Address from,- long fromAddr) throws IOException;+ abstract RefList getRefList(ProgramDB program, Address from, long fromAddr) throws IOException;abstract boolean hasRefFrom(long fromAddr) throws IOException;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterSharedTable.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterSharedTable.javaindex fb55f7db87..8599af1f8f 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterSharedTable.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterSharedTable.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -19,7 +19,6 @@ import java.io.IOException;import db.*;import db.util.ErrorHandler;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.*;import ghidra.program.model.address.*;@@ -71,18 +70,16 @@ class FromAdapterSharedTable extends FromAdapter {}@Override- RefList createRefList(ProgramDB program, DBObjectCache<RefList> cache, Address fromAddr)- throws IOException {+ RefList createRefList(ProgramDB program, Address fromAddr) throws IOException {throw new UnsupportedOperationException();}@Override- RefList getRefList(ProgramDB program, DBObjectCache<RefList> cache, Address from, long fromAddr)- throws IOException {+ RefList getRefList(ProgramDB program, Address from, long fromAddr) throws IOException {LongField fromField = new LongField(fromAddr);- RefList fromRefs = new RefListV0(fromAddr, addrMap, program, cache, true);+ RefList fromRefs = RefListV0.createTemporary(fromAddr, addrMap, program, true);RecordIterator iter = table.indexIterator(OLD_FROM_ADDR_COL, fromField, fromField, true);while (iter.hasNext()) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterV0.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterV0.javaindex e05b0cf90f..7d04e306e2 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterV0.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/FromAdapterV0.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -19,7 +19,6 @@ import java.io.IOException;import db.*;import db.util.ErrorHandler;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.*;import ghidra.program.model.address.*;@@ -50,20 +49,19 @@ class FromAdapterV0 extends FromAdapter {}@Override- public RefList createRefList(ProgramDB program, DBObjectCache<RefList> cache, Address from)+ public RefList createRefList(ProgramDB program, Address from)throws IOException {- return new RefListV0(from, this, addrMap, program, cache, true);+ return RefListV0.createNew(from, this, addrMap, program, true);}@Override- public RefList getRefList(ProgramDB program, DBObjectCache<RefList> cache, Address from,- long fromAddr) throws IOException {+ public RefList getRefList(ProgramDB program, Address from, long fromAddr) throws IOException {DBRecord rec = table.getRecord(fromAddr);if (rec != null) {if (rec.getBinaryData(REF_DATA_COL) == null) {- return new BigRefListV0(rec, this, addrMap, program, cache, true);+ return BigRefListV0.createExisting(rec, this, addrMap, program, true);}- return new RefListV0(rec, this, addrMap, program, cache, true);+ return RefListV0.instantiateExisting(rec, this, addrMap, program, true);}return null;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefList.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefList.javaindex 166d7c5df6..7b860309a8 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefList.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefList.java@@ -24,7 +24,7 @@ import ghidra.program.model.symbol.*;import ghidra.util.BigEndianDataConverter;import ghidra.util.DataConverter;-abstract class RefList extends DatabaseObject {+abstract class RefList extends DbObject {static volatile int BIG_REFLIST_THRESHOLD = 1700;@@ -36,9 +36,9 @@ abstract class RefList extends DatabaseObject {protected ProgramDB program;protected boolean isFrom;- RefList(long key, Address address, RecordAdapter adapter, AddressMap addrMap, ProgramDB program,- DBObjectCache<RefList> cache, boolean isFrom) {- super(cache, key);+ protected RefList(long key, Address address, RecordAdapter adapter, AddressMap addrMap,+ ProgramDB program, boolean isFrom) {+ super(key);this.address = addrMap.decodeAddress(key);this.adapter = adapter;this.addrMap = addrMap;@@ -91,12 +91,13 @@ abstract class RefList extends DatabaseObject {* @return original or replacement RefList* @throws IOException*/- public RefList checkRefListSize(DBObjectCache<RefList> cache, int newSpaceRequired)+ public RefList checkRefListSize(DbCache<RefList> cache, int newSpaceRequired)throws IOException {if (adapter != null && (getNumRefs() + newSpaceRequired) >= BIG_REFLIST_THRESHOLD) {cache.delete(getKey()); // remove smaller list from cacheBigRefListV0 refList =- new BigRefListV0(address, adapter, addrMap, program, cache, isFrom);+ BigRefListV0.createNew(address, adapter, addrMap, program, isFrom);+ cache.add(refList);refList.addRefs(getRefs());return refList;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefListV0.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefListV0.javaindex 6da0e5b90a..9e7737a2ad 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefListV0.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/RefListV0.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -25,7 +25,6 @@ import java.io.IOException;import java.util.Iterator;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;@@ -50,61 +49,67 @@ class RefListV0 extends RefList {private DBRecord record;/**- * Construct new temporary empty reference list- * @param key address key corresponding to the address parameter- * @param adapter entry record storage adapter (may be null to prevent database update)+ * Create new temporary empty reference list and put it in the cache+ * @param addrKey address key corresponding to the address parameter* @param addrMap address map for encoding/decoding addresses* @param program may be null for upgrade in which* case resulting reference objects are not suitable for general use.- * @param cache RefList object cache* @param isFrom true for from-adapter use, false for to-adapter use+ * @return a new empty temporary (no adapter) reference list that has been added to the cache*/- RefListV0(long addrKey, AddressMap addrMap, ProgramDB program, DBObjectCache<RefList> cache,+ static RefListV0 createTemporary(long addrKey, AddressMap addrMap, ProgramDB program,boolean isFrom) {- super(addrKey, addrMap.decodeAddress(addrKey), null, addrMap, program, cache, isFrom);- this.refData = EMPTY_DATA;- numRefs = 0;- if (adapter != null) {- record = ToAdapter.TO_REFS_SCHEMA.createRecord(key);- record.setByteValue(ToAdapter.REF_LEVEL_COL, (byte) -1);- }++ Address address = addrMap.decodeAddress(addrKey);+ return new RefListV0(addrKey, address, null, addrMap, program, isFrom);}/**- * Construct new empty reference list+ * create new empty reference list and add it to the cache* @param address address associated with this list (a new key will be generated if required)* @param adapter entry record storage adapter (may be null to prevent database update)* @param addrMap address map for encoding/decoding addresses* @param program may be null for upgrade in which* case resulting reference objects are not suitable for general use.- * @param cache RefList object cache* @param isFrom true for from-adapter use, false for to-adapter use+ * @return a new empty reference list that has been added to the cache*/- RefListV0(Address address, RecordAdapter adapter, AddressMap addrMap, ProgramDB program,- DBObjectCache<RefList> cache, boolean isFrom) {- super(addrMap.getKey(address, true), address, adapter, addrMap, program, cache, isFrom);- this.refData = EMPTY_DATA;- numRefs = 0;- if (adapter != null) {- record = ToAdapter.TO_REFS_SCHEMA.createRecord(key);- record.setByteValue(ToAdapter.REF_LEVEL_COL, (byte) -1);- }+ static RefListV0 createNew(Address address, RecordAdapter adapter, AddressMap addrMap,+ ProgramDB program, boolean isFrom) {++ long addrKey = addrMap.getKey(address, true);+ return new RefListV0(addrKey, address, adapter, addrMap, program, isFrom);}/**- * Construct reference list for existing record- * @param rec existing refList record+ * Create reference list for existing record and add it to the cache+ * @param record existing refList record* @param adapter entry record storage adapter* @param addrMap address map for encoding/decoding addresses* @param program may be null for upgrade in which* case resulting reference objects are not suitable for general use.- * @param cache RefList object cache* @param isFrom true for from-adapter use, false for to-adapter use+ * @return a new reference list that has been added to the cache*/- RefListV0(DBRecord rec, RecordAdapter adapter, AddressMap addrMap, ProgramDB program,- DBObjectCache<RefList> cache, boolean isFrom) {- super(rec.getKey(), addrMap.decodeAddress(rec.getKey()), adapter, addrMap, program, cache,- isFrom);+ static RefListV0 instantiateExisting(DBRecord record, RecordAdapter adapter, AddressMap addrMap,+ ProgramDB program, boolean isFrom) {+ return new RefListV0(record, adapter, addrMap, program, isFrom);+ }++ private RefListV0(long addrKey, Address address, RecordAdapter adapter, AddressMap addrMap,+ ProgramDB program, boolean isFrom) {+ super(addrKey, address, adapter, addrMap, program, isFrom);+ this.refData = EMPTY_DATA;+ numRefs = 0;+ if (adapter != null) {+ record = ToAdapter.TO_REFS_SCHEMA.createRecord(key);+ record.setByteValue(ToAdapter.REF_LEVEL_COL, (byte) -1);+ }+ }++ private RefListV0(DBRecord rec, RecordAdapter adapter, AddressMap addrMap, ProgramDB program,+ boolean isFrom) {+ super(rec.getKey(), addrMap.decodeAddress(rec.getKey()), adapter, addrMap, program, isFrom);refData = rec.getBinaryData(ToAdapter.REF_DATA_COL);numRefs = rec.getIntValue(ToAdapter.REF_COUNT_COL);if (!isFrom) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ReferenceDBManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ReferenceDBManager.javaindex 65c0d5f390..2fc8121b0b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ReferenceDBManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ReferenceDBManager.java@@ -18,8 +18,7 @@ package ghidra.program.database.references;import java.io.IOException;import java.util.*;-import org.apache.commons.collections4.map.LazyMap;-import org.apache.commons.collections4.map.LazySortedMap;+import org.apache.commons.collections4.map.*;import db.*;import db.util.ErrorHandler;@@ -36,6 +35,7 @@ import ghidra.program.model.pcode.Varnode;import ghidra.program.model.symbol.*;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -46,7 +46,7 @@ import ghidra.util.task.TaskMonitor;public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHandler {private static final Reference[] NO_REFS = new Reference[0];- private FunctionVariableReferenceCacher functionCacher = new FunctionVariableReferenceCacher();+ private LRUMap<Function, VariableReferences> variablesCache = new LRUMap<>(10);private OldStackRefDBAdpater oldStackRefAdapter;private AddressMap addrMap;@@ -57,8 +57,8 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanprivate SymbolManager symbolMgr;private Lock lock;- private DBObjectCache<RefList> fromCache;- private DBObjectCache<RefList> toCache;+ private DbCache<RefList> fromCache;+ private DbCache<RefList> toCache;/***@@ -76,8 +76,8 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanTaskMonitor monitor) throws CancelledException, IOException, VersionException {this.addrMap = addrMap;this.lock = lock;- fromCache = new DBObjectCache<>(100);- toCache = new DBObjectCache<>(100);+ fromCache = new DbCache<>(new FromReferenceFactory(), lock, 100);+ toCache = new DbCache<>(new ToReferenceFactory(), lock, 100);VersionException versionExc = null;try {@@ -364,8 +364,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHantoAddr = ((OverlayAddressSpace) toAddr.getAddressSpace()).translateAddress(toAddr);}- lock.acquire();- try {+ try (Closeable c = lock.write()) {boolean isPrimary = false;ReferenceDB oldRef = (ReferenceDB) getReference(fromAddr, toAddr, opIndex);@@ -393,14 +392,16 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanfromRefs == null || (fromAddr.isMemoryAddress() && !fromRefs.hasReference(opIndex));if (fromRefs == null) {- fromRefs = fromAdapter.createRefList(program, fromCache, fromAddr);+ fromRefs = fromAdapter.createRefList(program, fromAddr);+ fromCache.add(fromRefs);}fromRefs = fromRefs.checkRefListSize(fromCache, 1);fromRefs.addRef(fromAddr, toAddr, type, opIndex, -1, isPrimary, sourceType, isOffset,isShifted, offsetOrShift);if (toRefs == null && !isStackRegisterRef) {- toRefs = toAdapter.createRefList(program, toCache, toAddr);+ toRefs = toAdapter.createRefList(program, toAddr);+ toCache.add(toRefs);}if (toRefs != null) {toRefs = toRefs.checkRefListSize(toCache, 1);@@ -415,9 +416,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanreferenceAdded(r);return r;}- finally {- lock.release();- }}@Override@@ -674,8 +672,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan@Overridepublic Reference[] getReferencesTo(Variable var) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Function function = var.getFunction();if (function.getProgram() != program || function.isDeleted()) {return NO_REFS;@@ -685,13 +682,12 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanif (varSymbol != null && varSymbol.isDeleted()) {return NO_REFS;}-- functionCacher.setFunction(function);+ VariableReferences varRefs = getVariablesReferences(function);VariableStorage storage = var.getVariableStorage();- Scope scope = findVariableScope(function, varSymbol, var);+ Scope scope = findVariableScope(varRefs, varSymbol, var);List<Reference> matchingReferences =- getScopedVariableReferences(storage, function, scope);+ getScopedVariableReferences(varRefs, storage, function, scope);if (matchingReferences.isEmpty()) {return NO_REFS;}@@ -700,12 +696,18 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanmatchingReferences.toArray(refs);return refs;}- finally {- lock.release();+ }++ private synchronized VariableReferences getVariablesReferences(Function function) {+ VariableReferences refs = variablesCache.get(function);+ if (refs == null) {+ refs = new VariableReferences(function);+ variablesCache.put(function, refs);}+ return refs;}- private Scope findVariableScope(Function function, Symbol varSymbol, Variable var) {+ private Scope findVariableScope(VariableReferences refs, Symbol varSymbol, Variable var) {VariableStorage storage = var.getVariableStorage();Address variableAddr = null;@@ -729,7 +731,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan// There could be more than one variable with the same address// Determine scope of variable within function- for (Variable v : functionCacher.getVariables(variableAddr)) {+ for (Variable v : refs.getVariables(variableAddr)) {int nextVarOffset = v.getFirstUseOffset();if (nextVarOffset < 0) {nextVarOffset = Integer.MAX_VALUE - nextVarOffset;@@ -742,11 +744,10 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanreturn new Scope(var.getFirstUseOffset(), outOfScopeOffset);}- private List<Reference> getScopedVariableReferences(VariableStorage storage, Function function,- Scope scope) {+ private List<Reference> getScopedVariableReferences(VariableReferences varRefs,+ VariableStorage storage, Function function, Scope scope) {- SortedMap<Address, List<Reference>> dataReferences =- functionCacher.getFunctionDataReferences();+ SortedMap<Address, List<Reference>> dataReferences = varRefs.getFunctionDataReferences();Address entry = function.getEntryPoint();List<Reference> references = new ArrayList<>();@@ -795,8 +796,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan@Overridepublic void setPrimary(Reference ref, boolean isPrimary) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address fromAddr = ref.getFromAddress();Address toAddr = ref.getToAddress();int opIndex = ref.getOperandIndex();@@ -824,15 +824,11 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }}@Overridepublic Reference[] getReferencesFrom(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RefList fromRefs = getFromRefs(addr);if (fromRefs == null) {return NO_REFS;@@ -842,9 +838,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -863,8 +856,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan@Overridepublic Reference getReference(Address fromAddr, Address toAddr, int opIndex) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (fromAddr.equals(Address.EXT_FROM_ADDRESS)) {RefList toRefs = getToRefs(toAddr);if (toRefs != null) {@@ -881,9 +873,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -926,8 +915,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan* @return the references*/Reference[] getReferences(Address fromAddr, int opIndex) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RefList fromRefs = getFromRefs(fromAddr);if (fromRefs == null) {return NO_REFS;@@ -946,16 +934,12 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic Reference getPrimaryReferenceFrom(Address addr, int opIndex) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RefList fromRefs = getFromRefs(addr);if (fromRefs != null) {return fromRefs.getPrimaryRef(opIndex);@@ -964,9 +948,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1028,8 +1009,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan@Overridepublic boolean hasFlowReferencesFrom(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RefList fromRefs = getFromRefs(addr);if (fromRefs == null) {return false;@@ -1045,43 +1025,35 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return false;}@Overridepublic boolean hasReferencesFrom(Address fromAddr) {- lock.acquire();+ long addr = addrMap.getKey(fromAddr, false);+ if (addr == AddressMap.INVALID_ADDRESS_KEY) {+ return false;+ }+ RefList refList = fromCache.getIfValid(addr);+ if (refList != null && !refList.isEmpty()) {+ return true;+ }try {- long addr = addrMap.getKey(fromAddr, false);- if (addr == AddressMap.INVALID_ADDRESS_KEY) {- return false;- }- RefList refList = fromCache.get(addr);- if (refList != null && !refList.isEmpty()) {- return true;- }return fromAdapter.hasRefFrom(addr);}catch (IOException e) {- program.dbError(e);- }- finally {- lock.release();+ dbError(e);}return false;}@Overridepublic boolean hasReferencesFrom(Address fromAddr, int opIndex) {- lock.acquire();+ RefList fromRefs = getFromRefs(fromAddr);+ if (fromRefs == null) {+ return false;+ }try {- RefList fromRefs = getFromRefs(fromAddr);- if (fromRefs == null) {- return false;- }ReferenceIterator it = fromRefs.getRefs();while (it.hasNext()) {Reference ref = it.next();@@ -1091,10 +1063,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan}}catch (IOException e) {- program.dbError(e);- }- finally {- lock.release();+ dbError(e);}return false;}@@ -1105,23 +1074,19 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanthrow new UnsupportedOperationException("hasReferencesTo not supported for stack/register addresses");}- lock.acquire();+ long addr = addrMap.getKey(toAddr, false);+ if (addr == AddressMap.INVALID_ADDRESS_KEY) {+ return false;+ }+ RefList refList = toCache.getIfValid(addr);+ if (refList != null && !refList.isEmpty()) {+ return true;+ }try {- long addr = addrMap.getKey(toAddr, false);- if (addr == AddressMap.INVALID_ADDRESS_KEY) {- return false;- }- RefList refList = toCache.get(addr);- if (refList != null && !refList.isEmpty()) {- return true;- }return toAdapter.hasRefTo(addr);}catch (IOException e) {- program.dbError(e);- }- finally {- lock.release();+ dbError(e);}return false;}@@ -1165,8 +1130,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan}void removeReference(Address fromAddr, Address toAddr, int opIndex) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Reference ref = null;RefList fromRefs = getFromRefs(fromAddr);if (fromRefs != null) {@@ -1195,9 +1159,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {dbError(e);}- finally {- lock.release();- }}/**@@ -1237,10 +1198,22 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan}private void checkFunctionChange(Symbol sym) {- SymbolType symbolType = sym.getSymbolType();- if (symbolType == SymbolType.FUNCTION || symbolType == SymbolType.PARAMETER ||- symbolType == SymbolType.LOCAL_VAR) {- functionCacher.clearCache();+ try (Closeable c = lock.write()) {+ SymbolType symbolType = sym.getSymbolType();+ if (symbolType == SymbolType.FUNCTION || symbolType == SymbolType.PARAMETER ||+ symbolType == SymbolType.LOCAL_VAR) {+ clearVariablesCache();+ }+ }+ }++ private void clearVariablesCache() {+ // Clear all variables cache. Perhaps it would be better to be more surgical and only+ // remove the affected function.+ try (Closeable c = lock.write()) {+ synchronized (this) {+ variablesCache.clear();+ }}}@@ -1250,8 +1223,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanif (s.getSymbolType() != SymbolType.LABEL || s.isDynamic() || s.isExternal()) {return;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address symAddr = s.getAddress();if (!symAddr.equals(ref.getToAddress())) {throw new IllegalArgumentException("Symbol address(" + symAddr +@@ -1266,15 +1238,11 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanprogram.setObjChanged(ProgramEvent.SYMBOL_ASSOCIATION_ADDED, ref.getFromAddress(), ref,null, s);}- finally {- lock.release();- }}@Overridepublic void removeAssociation(Reference ref) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {setSymbolID(ref, -1);program.setObjChanged(ProgramEvent.SYMBOL_ASSOCIATION_REMOVED, ref.getFromAddress(),ref, null, null);@@ -1282,15 +1250,11 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }}@Overridepublic Reference updateRefType(Reference ref, RefType refType) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (ref.getReferenceType() == refType) {return ref;}@@ -1315,16 +1279,12 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic ReferenceIterator getReferencesTo(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (addr.isStackAddress() || addr.isRegisterAddress()) {throw new UnsupportedOperationException("getReferencesTo not supported for stack/register addresses");@@ -1337,22 +1297,15 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return new EmptyMemReferenceIterator();}@Overridepublic void invalidateCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {fromCache.invalidate();toCache.invalidate();- functionCacher.clearCache();- }- finally {- lock.release();+ clearVariablesCache();}}@@ -1384,7 +1337,8 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanif (!newToAddr.isStackAddress() && !newToAddr.isRegisterAddress()) {newToRefs = getToRefs(newToAddr);if (newToRefs == null) {- newToRefs = toAdapter.createRefList(program, toCache, newToAddr);+ newToRefs = toAdapter.createRefList(program, newToAddr);+ toCache.add(newToRefs);}newToRefs = newToRefs.checkRefListSize(toCache, refs.length);}@@ -1423,8 +1377,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {// Move all non-default references from the moved range.// All default references from the range will be removed.@@ -1502,9 +1455,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }}/**@@ -1530,8 +1480,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan* Get address iterator over references that are external entry memory references*/public AddressIterator getExternalEntryIterator() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RefList refList = getFromRefs(Address.EXT_FROM_ADDRESS);if (refList != null) {return new ExtEntryAddressIterator(refList.getRefs());@@ -1540,9 +1489,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return new EmptyAddressIterator();}@@ -1555,8 +1501,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanif (!toAddr.isMemoryAddress()) {return false;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {RefList refList = getToRefs(toAddr);if (refList != null) {return refList.getRef(Address.EXT_FROM_ADDRESS, CodeUnit.MNEMONIC) != null;@@ -1565,9 +1510,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHancatch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return false;}@@ -1613,54 +1555,20 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan}private RefList getFromRefs(Address from) {- lock.acquire();- try {- long fromAddr = addrMap.getKey(from, false);- RefList refList = fromCache.get(fromAddr);- if (refList == null) {- try {- refList = fromAdapter.getRefList(program, fromCache, from, fromAddr);- }- catch (IOException e) {- dbError(e);- }- }- return refList;- }- finally {- lock.release();- }+ long key = addrMap.getKey(from, false);+ return fromCache.getCachedInstance(key);}private RefList getToRefs(Address to) {- lock.acquire();- try {- long toAddr = addrMap.getKey(to, false);- RefList refList = toCache.get(toAddr);- if (refList == null) {- try {- refList = toAdapter.getRefList(program, toCache, to, toAddr);- }- catch (ClosedException e) {- // TODO this seems wrong here; no other method handles closed exceptions- }- catch (IOException e) {- dbError(e);- }- }- return refList;- }- finally {- lock.release();- }+ long key = addrMap.getKey(to, false);+ return toCache.getCachedInstance(key);}/** Remove all references that have the "From" address as the given address.*/void removeAllFrom(Address fromAddr) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {RefList fromRefs = getFromRefs(fromAddr);if (fromRefs == null) {return;@@ -1679,9 +1587,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanfromRefs.removeAll();fromCache.delete(fromRefs.getKey());}- finally {- lock.release();- }}private void removeAllFrom(Address fromAddr, int opIndex) {@@ -1692,8 +1597,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan}void setSymbolID(Reference ref, long symbolID) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {RefList fromRefs = getFromRefs(ref.getFromAddress());if (fromRefs == null) {return;@@ -1709,9 +1613,6 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHantoRefs.setSymbolID(curRef, symbolID);}}- finally {- lock.release();- }}private void referenceAdded(Reference ref) {@@ -1719,7 +1620,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanif (addr == Address.EXT_FROM_ADDRESS) {addr = null;}- functionCacher.clearCache();+ clearVariablesCache();program.setObjChanged(ProgramEvent.REFERENCE_ADDED, addr, ref, null, ref);if (ref.getReferenceType() == RefType.FALL_THROUGH) {program.getCodeManager().fallThroughChanged(ref.getFromAddress(), ref);@@ -1727,7 +1628,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan}private void referenceRemoved(Reference ref) {- functionCacher.clearCache();+ clearVariablesCache();program.setObjChanged(ProgramEvent.REFERENCE_REMOVED, ref.getFromAddress(), ref, ref, null);if (ref.getReferenceType() == RefType.FALL_THROUGH) {program.getCodeManager().fallThroughChanged(ref.getFromAddress(), null);@@ -1735,7 +1636,7 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHan}private void referenceTypeChanged(Reference ref, RefType oldType, RefType newType) {- functionCacher.clearCache();+ clearVariablesCache();program.setObjChanged(ProgramEvent.REFERENCE_TYPE_CHANGED, ref.getFromAddress(), ref,oldType, newType);if (oldType == RefType.FALL_THROUGH) {@@ -2010,55 +1911,40 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanreturn program;}- private class FunctionVariableReferenceCacher {-- private Function cachedFunction;- private SortedMap<Address, List<Reference>> references;- private Map<Address, List<Variable>> variablesByAddress;-- synchronized void setFunction(Function function) {- if (cachedFunction == function) {- return;- }+ private class VariableReferences {- clearCache();- cachedFunction = function;- }+ private final Function function;+ private final SortedMap<Address, List<Reference>> references;+ private final Map<Address, List<Variable>> variablesByAddress;- synchronized void clearCache() {- cachedFunction = null;- references = null;- variablesByAddress = null;+ VariableReferences(Function function) {+ this.function = function;+ variablesByAddress = createVariablesMap();+ references = createSortedReferenceMap();}- synchronized SortedMap<Address, List<Reference>> getFunctionDataReferences() {- if (references == null) {- references = getSortedVariableReferences(cachedFunction);- }+ SortedMap<Address, List<Reference>> getFunctionDataReferences() {return references;}- synchronized List<Variable> getVariables(Address address) {+ List<Variable> getVariables(Address address) {+ return variablesByAddress.get(address);+ }- if (variablesByAddress != null) {- return variablesByAddress.get(address);- }+ private Map<Address, List<Variable>> createVariablesMap() {Map<Address, List<Variable>> map =LazyMap.lazyMap(new HashMap<>(), () -> new ArrayList<>());- for (Symbol s : symbolMgr.getSymbols(cachedFunction.getID())) {- Object object = s.getObject();- if (object instanceof Variable v) {- map.get(address).add(v);- }+ for (Symbol s : symbolMgr.getSymbols(function.getID())) {+ Address symbolAddress = s.getAddress();+ Variable v = (Variable) s.getObject();+ map.get(symbolAddress).add(v);}-- variablesByAddress = map;- return variablesByAddress.get(address);+ return map;}- private SortedMap<Address, List<Reference>> getSortedVariableReferences(Function function) {+ private SortedMap<Address, List<Reference>> createSortedReferenceMap() {SortedMap<Address, List<Reference>> newReferencesList =LazySortedMap.lazySortedMap(new TreeMap<>(), () -> new ArrayList<>());@@ -2095,4 +1981,51 @@ public class ReferenceDBManager implements ReferenceManager, ManagerDB, ErrorHanreturn outOfScopeOffset;}}++ private class FromReferenceFactory implements DbFactory<RefList> {++ @Override+ public RefList instantiate(long key) {+ Address fromAddress = addrMap.decodeAddress(key);+ try {+ return fromAdapter.getRefList(program, fromAddress, key);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public RefList instantiate(DBRecord record) {+ // Currently the reference manager doesn't deal in records, so this should never+ // be called+ throw new UnsupportedOperationException();+ }++ }++ private class ToReferenceFactory implements DbFactory<RefList> {++ @Override+ public RefList instantiate(long key) {+ Address toAddress = addrMap.decodeAddress(key);+ try {+ return toAdapter.getRefList(program, toAddress, key);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public RefList instantiate(DBRecord record) {+ // Currently the reference manager doesn't deal in records, so this should never+ // be called+ throw new UnsupportedOperationException();+ }++ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapter.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapter.javaindex 283e2c2e52..1ed131ace6 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapter.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapter.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -26,7 +26,6 @@ import java.io.IOException;import db.*;import db.util.ErrorHandler;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.*;@@ -111,10 +110,11 @@ abstract class ToAdapter implements RecordAdapter {throw new CancelledException();}Address to = addrIter.next();+ long key = addrMap.getKey(to, true);RefListV0 refList =- (RefListV0) oldAdapter.getRefList(null, null, to, oldAddrMap.getKey(to, false));+ (RefListV0) oldAdapter.getRefList(null, to, oldAddrMap.getKey(to, false));Reference[] refs = refList.getAllRefs();- RefListV0 newRefList = new RefListV0(to, tmpAdapter, addrMap, null, null, false);+ RefListV0 newRefList = RefListV0.createNew(to, tmpAdapter, addrMap, null, false);newRefList.addRefs(refs);monitor.setProgress(++count);}@@ -129,7 +129,7 @@ abstract class ToAdapter implements RecordAdapter {}Address to = addrIter.next();long toAddr = addrMap.getKey(to, true);- RefListV0 refList = (RefListV0) tmpAdapter.getRefList(null, null, to, toAddr);+ RefListV0 refList = (RefListV0) tmpAdapter.getRefList(null, to, toAddr);byte refLevel = -1;if (refList != null) {refLevel = refList.getReferenceLevel();@@ -147,11 +147,9 @@ abstract class ToAdapter implements RecordAdapter {abstract int getRecordCount();- abstract RefList createRefList(ProgramDB program, DBObjectCache<RefList> cache, Address toAddr)- throws IOException;+ abstract RefList createRefList(ProgramDB program, Address toAddr) throws IOException;- abstract RefList getRefList(ProgramDB program, DBObjectCache<RefList> cache, Address to,- long toAddr) throws IOException;+ abstract RefList getRefList(ProgramDB program, Address to, long toAddr) throws IOException;abstract boolean hasRefTo(long toAddr) throws IOException;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterSharedTable.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterSharedTable.javaindex 31215b2457..e0631dcf95 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterSharedTable.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterSharedTable.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -19,7 +19,6 @@ import java.io.IOException;import db.*;import db.util.ErrorHandler;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.*;import ghidra.program.model.address.*;@@ -71,17 +70,15 @@ class ToAdapterSharedTable extends ToAdapter {}@Override- RefList createRefList(ProgramDB program, DBObjectCache<RefList> cache, Address toAddr)- throws IOException {+ RefList createRefList(ProgramDB program, Address toAddr) throws IOException {throw new UnsupportedOperationException();}@Override- RefList getRefList(ProgramDB program, DBObjectCache<RefList> cache, Address to, long toAddr)- throws IOException {+ RefList getRefList(ProgramDB program, Address to, long toAddr) throws IOException {LongField toField = new LongField(toAddr);- RefList toRefs = new RefListV0(toAddr, addrMap, program, cache, false);+ RefList toRefs = RefListV0.createTemporary(toAddr, addrMap, program, false);RecordIterator iter = table.indexIterator(OLD_TO_ADDR_COL, toField, toField, true);while (iter.hasNext()) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV0.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV0.javaindex 21a5ad6cba..d8a7810666 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV0.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV0.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -25,7 +25,6 @@ import java.io.IOException;import db.*;import db.util.ErrorHandler;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.*;import ghidra.program.model.address.*;@@ -58,17 +57,15 @@ class ToAdapterV0 extends ToAdapter {}@Override- public RefList createRefList(ProgramDB program, DBObjectCache<RefList> cache, Address to)- throws IOException {+ public RefList createRefList(ProgramDB program, Address to) throws IOException {throw new UnsupportedOperationException();}@Override- public RefList getRefList(ProgramDB program, DBObjectCache<RefList> cache, Address to,- long toAddr) throws IOException {+ public RefList getRefList(ProgramDB program, Address to, long toAddr) throws IOException {DBRecord rec = translateRecord(table.getRecord(toAddr));if (rec != null) {- return new RefListV0(rec, this, addrMap, program, cache, false);+ return RefListV0.instantiateExisting(rec, this, addrMap, program, false);}return null;}@@ -140,7 +137,7 @@ class ToAdapterV0 extends ToAdapter {private byte getRefLevel(DBRecord newRec) {try {- RefList refList = new RefListV0(newRec, this, addrMap, null, null, false);+ RefList refList = RefListV0.instantiateExisting(newRec, this, addrMap, null, false);Reference[] refs = refList.getAllRefs();byte refLevel = (byte) -1;for (Reference ref : refs) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV1.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV1.javaindex 1c46e6093e..a8c0e8aa4d 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV1.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/references/ToAdapterV1.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -27,7 +27,6 @@ import java.util.NoSuchElementException;import db.*;import db.util.ErrorHandler;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.ProgramDB;import ghidra.program.database.map.*;import ghidra.program.model.address.*;@@ -65,20 +64,19 @@ class ToAdapterV1 extends ToAdapter {}@Override- public RefList createRefList(ProgramDB program, DBObjectCache<RefList> cache, Address to)+ public RefList createRefList(ProgramDB program, Address to)throws IOException {- return new RefListV0(to, this, addrMap, program, cache, false);+ return RefListV0.createNew(to, this, addrMap, program, false);}@Override- public RefList getRefList(ProgramDB program, DBObjectCache<RefList> cache, Address to,- long toAddr) throws IOException {+ public RefList getRefList(ProgramDB program, Address to, long toAddr) throws IOException {DBRecord rec = table.getRecord(toAddr);if (rec != null) {if (rec.getBinaryData(REF_DATA_COL) == null) {- return new BigRefListV0(rec, this, addrMap, program, cache, false);+ return BigRefListV0.createExisting(rec, this, addrMap, program, false);}- return new RefListV0(rec, this, addrMap, program, cache, false);+ return RefListV0.instantiateExisting(rec, this, addrMap, program, false);}return null;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/OldProgramContextDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/OldProgramContextDB.javaindex e2f388a4d5..324633519f 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/OldProgramContextDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/OldProgramContextDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -33,6 +33,7 @@ import ghidra.program.model.listing.ProgramContext;import ghidra.program.util.RangeMapAdapter;import ghidra.program.util.RegisterValueStore;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.task.TaskMonitor;@@ -311,13 +312,9 @@ public class OldProgramContextDB implements ProgramContext, DefaultProgramContex@Overridepublic void invalidateCache(boolean all) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {valueMaps.clear();}- finally {- lock.release();- }}@Override@@ -364,16 +361,12 @@ public class OldProgramContextDB implements ProgramContext, DefaultProgramContex}private AddressRangeMapDB createMap(int offset) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {AddressRangeMapDB map = new AddressRangeMapDB(dbHandle, addrMap, lock,"ProgContext" + offset, errHandler, ByteField.INSTANCE, false);valueMaps.put(offset, map);return map;}- finally {- lock.release();- }}private SortedSet<Address> getChangePoints(Address startAddr, Address endAddr,diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/ProgramRegisterContextDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/ProgramRegisterContextDB.javaindex a7b2bd9450..47d65b3090 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/ProgramRegisterContextDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/register/ProgramRegisterContextDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -33,6 +33,7 @@ import ghidra.program.model.lang.*;import ghidra.program.model.listing.ContextChangeException;import ghidra.program.util.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -176,8 +177,10 @@ public class ProgramRegisterContextDB extends AbstractStoredProgramContext imple@Overridepublic void invalidateCache(boolean all) throws IOException {- this.invalidateReadCache();- invalidateRegisterStores();+ try (Closeable c = lock.write()) {+ this.invalidateReadCache();+ invalidateRegisterStores();+ }}@Override@@ -213,24 +216,19 @@ public class ProgramRegisterContextDB extends AbstractStoredProgramContext imple@Overridepublic void deleteAddressRange(Address start, Address end, TaskMonitor monitor) {AddressRange.checkValidRange(start, end);- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.deleteAddressRange(start, end, monitor);if (program != null) {program.setRegisterValuesChanged(null, start, end);}}- finally {- lock.release();- }}@Overridepublic void remove(Address start, Address end, Register register)throws ContextChangeException {- lock.acquire();boolean restore = false;- try {+ try (Closeable c = lock.write()) {checkContextWrite(register, start, end);restore = !changing; // indicates that we just initiated a changechanging = true;@@ -243,16 +241,14 @@ public class ProgramRegisterContextDB extends AbstractStoredProgramContext impleif (restore) {changing = false;}- lock.release();}}@Overridepublic void setValue(Register register, Address start, Address end, BigInteger value)throws ContextChangeException {- lock.acquire();boolean restore = false;- try {+ try (Closeable c = lock.write()) {checkContextWrite(register, start, end);restore = !changing; // indicates that we just initiated a changechanging = true;@@ -265,7 +261,6 @@ public class ProgramRegisterContextDB extends AbstractStoredProgramContext impleif (restore) {changing = false;}- lock.release();}}@@ -273,9 +268,8 @@ public class ProgramRegisterContextDB extends AbstractStoredProgramContext imple@Overridepublic void setRegisterValue(Address start, Address end, RegisterValue value)throws ContextChangeException {- lock.acquire();boolean restore = false;- try {+ try (Closeable c = lock.write()) {checkContextWrite(value.getRegister(), start, end);restore = !changing; // indicates that we just initiated a changechanging = true;@@ -288,7 +282,6 @@ public class ProgramRegisterContextDB extends AbstractStoredProgramContext impleif (restore) {changing = false;}- lock.release();}}@@ -394,136 +387,88 @@ public class ProgramRegisterContextDB extends AbstractStoredProgramContext imple@Overridepublic void flushProcessorContextWriteCache() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.flushProcessorContextWriteCache();}- finally {- lock.release();- }}@Overridepublic void invalidateProcessorContextWriteCache() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.invalidateProcessorContextWriteCache();}- finally {- lock.release();- }}@Overridepublic AddressRangeIterator getRegisterValueAddressRanges(Register register) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getRegisterValueAddressRanges(register);}- finally {- lock.release();- }}@Overridepublic AddressRange getRegisterValueRangeContaining(Register register, Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getRegisterValueRangeContaining(register, addr);}- finally {- lock.release();- }}@Overridepublic AddressRangeIterator getRegisterValueAddressRanges(Register register, Address start,Address end) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getRegisterValueAddressRanges(register, start, end);}- finally {- lock.release();- }}@Overridepublic AddressRangeIterator getDefaultRegisterValueAddressRanges(Register register) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getDefaultRegisterValueAddressRanges(register);}- finally {- lock.release();- }}@Overridepublic AddressRangeIterator getDefaultRegisterValueAddressRanges(Register register,Address start, Address end) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getDefaultRegisterValueAddressRanges(register, start, end);}- finally {- lock.release();- }}@Overridepublic Register[] getRegistersWithValues() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getRegistersWithValues();}- finally {- lock.release();- }}@Overridepublic boolean hasValueOverRange(Register reg, BigInteger value, AddressSetView addrSet) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.hasValueOverRange(reg, value, addrSet);}- finally {- lock.release();- }}@Overridepublic void setDefaultValue(RegisterValue registerValue, Address start, Address end) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {super.setDefaultValue(registerValue, start, end);}- finally {- lock.release();- }}@Overridepublic RegisterValue getDefaultValue(Register register, Address address) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getDefaultValue(register, address);}- finally {- lock.release();- }}@Overridepublic RegisterValue getNonDefaultValue(Register register, Address address) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return super.getNonDefaultValue(register, address);}- finally {- lock.release();- }}private void invalidateRegisterStores() {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/reloc/RelocationManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/reloc/RelocationManager.javaindex 97ac8087d4..d80d2be06c 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/reloc/RelocationManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/reloc/RelocationManager.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -34,6 +34,7 @@ import ghidra.program.model.reloc.Relocation.Status;import ghidra.program.model.reloc.RelocationTable;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.exception.VersionException;import ghidra.util.task.TaskMonitor;@@ -145,8 +146,7 @@ public class RelocationManager implements RelocationTable, ManagerDB {@Overridepublic Relocation add(Address addr, Status status, int type, long[] values, byte[] bytes,String symbolName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {byte flags = RelocationDBAdapter.getFlags(status, 0);adapter.add(addr, flags, type, values, bytes, symbolName);Relocation reloc = new Relocation(addr, status, type, values,@@ -161,17 +161,13 @@ public class RelocationManager implements RelocationTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic Relocation add(Address addr, Status status, int type, long[] values, int byteLength,String symbolName) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {byte flags = RelocationDBAdapter.getFlags(status, byteLength);adapter.add(addr, flags, type, values, null, symbolName);Relocation reloc = new Relocation(addr, status, type, values,@@ -186,16 +182,12 @@ public class RelocationManager implements RelocationTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic boolean hasRelocation(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RecordIterator it = adapter.iterator(addr);if (!it.hasNext()) {return false;@@ -207,16 +199,12 @@ public class RelocationManager implements RelocationTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return false;}@Overridepublic List<Relocation> getRelocations(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {List<Relocation> list = null;RecordIterator it = adapter.iterator(addr);while (it.hasNext()) {@@ -235,9 +223,6 @@ public class RelocationManager implements RelocationTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -258,23 +243,18 @@ public class RelocationManager implements RelocationTable, ManagerDB {@Overridepublic Iterator<Relocation> getRelocations() {RecordIterator ri = null;- lock.acquire();- try {+ try (Closeable c = lock.read()) {ri = adapter.iterator();}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return new RelocationIterator(ri);}@Overridepublic Address getRelocationAddressAfter(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RecordIterator it = adapter.iterator(addr);while (it.hasNext()) {DBRecord rec = it.next();@@ -287,25 +267,18 @@ public class RelocationManager implements RelocationTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic Iterator<Relocation> getRelocations(AddressSetView set) {RecordIterator it = null;- lock.acquire();- try {+ try (Closeable c = lock.read()) {it = adapter.iterator(set);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return new RelocationIterator(it);}@@ -318,36 +291,30 @@ public class RelocationManager implements RelocationTable, ManagerDB {@Overridepublic boolean hasNext() {- if (it == null)+ if (it == null) {return false;- lock.acquire();- try {+ }+ try (Closeable c = lock.read()) {return it.hasNext();}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return false;}@Overridepublic Relocation next() {- if (it == null)+ if (it == null) {return null;- lock.acquire();- try {+ }+ try (Closeable c = lock.read()) {DBRecord r = it.next();return getRelocation(r);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceFileManagerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceFileManagerDB.javaindex 0c39f4f012..2ddf2f8a56 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceFileManagerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceFileManagerDB.java@@ -30,6 +30,7 @@ import ghidra.program.model.mem.MemoryBlock;import ghidra.program.model.sourcemap.*;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.exception.VersionException;import ghidra.util.task.TaskMonitor;@@ -100,8 +101,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHAddressRange.checkValidRange(start, end);AddressRange rangeToDelete = new AddressRangeImpl(start, end);- lock.acquire();- try {+ try (Closeable c = lock.write()) {RecordIterator recIter = sourceMapTableAdapter.getSourceMapRecordIterator(end, false);boolean sourceMapChanged = false;@@ -126,7 +126,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHcontinue;}- Address recEnd = getEndAddress(recStart,recLength);+ Address recEnd = getEndAddress(recStart, recLength);if (!rangeToDelete.intersects(recStart, recEnd)) {// we've found an entry that does not touch the range to delete@@ -168,9 +168,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHcatch (IOException e) {dbError(e);}- finally {- lock.release();- }}/**@@ -198,9 +195,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHreturn; // nothing to do}- lock.acquire();-- try {+ try (Closeable c = lock.write()) {Address rangeToMoveEnd = fromAddr.addNoWrap(length - 1);AddressRange rangeToMove = new AddressRangeImpl(fromAddr, rangeToMoveEnd);RecordIterator recIter =@@ -297,17 +292,13 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHcatch (IOException e) {dbError(e);}- finally {- lock.release();- }}@Overridepublic boolean addSourceFile(SourceFile sourceFile) throws LockException {Objects.requireNonNull(sourceFile, "sourceFile cannot be null");program.checkExclusiveAccess();- lock.acquire();- try {+ try (Closeable c = lock.write()) {Long key = getKeyForSourceFile(sourceFile);if (key != null) {return false;@@ -321,9 +312,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHdbError(e);return false;}- finally {- lock.release();- }}@Override@@ -331,8 +319,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHObjects.requireNonNull(sourceFile, "sourceFile cannot be null");boolean mapChanged = false;program.checkExclusiveAccess();- lock.acquire();- try {+ try (Closeable c = lock.write()) {Long key = getKeyForSourceFile(sourceFile);if (key == null) {return false;@@ -351,9 +338,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHcatch (IOException e) {dbError(e);}- finally {- lock.release();- }program.setObjChanged(ProgramEvent.SOURCE_FILE_REMOVED, null, sourceFile, null);if (mapChanged) {program.setChanged(ProgramEvent.SOURCE_MAP_CHANGED, sourceFile, null);@@ -366,8 +350,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHList<SourceMapEntry> sourceMapEntries = new ArrayList<>();- lock.acquire();- try {+ try (Closeable c = lock.read()) {RecordIterator recIter = sourceMapTableAdapter.getSourceMapRecordIterator(addr, false);boolean foundNonZeroLength = false;while (recIter.hasPrevious()) {@@ -395,9 +378,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHcatch (IOException e) {dbError(e);}- finally {- lock.release();- }Collections.sort(sourceMapEntries);return sourceMapEntries;}@@ -419,8 +399,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorH}program.checkExclusiveAccess();- lock.acquire();- try {+ try (Closeable c = lock.write()) {Long sourceFileId = getKeyForSourceFile(sourceFile);if (sourceFileId == null) {throw new IllegalArgumentException(@@ -481,9 +460,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHdbError(e);throw new AssertionError("addSourceMapEntry unsuccessful - possible database error");}- finally {- lock.release();- }}@Override@@ -491,8 +467,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHif (addrs == null || addrs.isEmpty()) {return false;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {for (AddressRangeIterator rangeIter = addrs.getAddressRanges(); rangeIter.hasNext();) {AddressRange r = rangeIter.next();RecordIterator recIter =@@ -519,9 +494,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHdbError(e);return false;}- finally {- lock.release();- }+}@Override@@ -532,8 +505,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorH@Overridepublic List<SourceFile> getMappedSourceFiles() {List<SourceFile> sourceFiles = new ArrayList<>();- lock.acquire();- try {+ try (Closeable c = lock.read()) {for (RecordIterator sourceFileRecordIter =sourceFileTableAdapter.getRecords(); sourceFileRecordIter.hasNext();) {@@ -550,17 +522,13 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHcatch (IOException e) {dbError(e);}- finally {- lock.release();- }return sourceFiles;}@Overridepublic List<SourceFile> getAllSourceFiles() {List<SourceFile> sourceFiles = new ArrayList<>();- lock.acquire();- try {+ try (Closeable c = lock.read()) {for (RecordIterator recordIter = sourceFileTableAdapter.getRecords(); recordIter.hasNext();) {updateLastSourceFileAndLastKey(recordIter.next());@@ -570,9 +538,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHcatch (IOException e) {dbError(e);}- finally {- lock.release();- }return sourceFiles;}@@ -581,8 +546,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHthrows LockException {program.checkExclusiveAccess();- lock.acquire();- try {+ try (Closeable c = lock.write()) {Long srcKey = getKeyForSourceFile(source);if (srcKey == null) {throw new IllegalArgumentException(@@ -606,9 +570,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorH// can't happen - entry ranges were validated upon insertthrow new AssertionError("bad address range in source map entry table");}- finally {- lock.release();- }}@Override@@ -628,13 +589,10 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHif (sourceFile == null) {return false;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {return getKeyForSourceFile(sourceFile) != null;}- finally {- lock.release();- }+}@Override@@ -650,8 +608,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHthrow new IllegalArgumentException("maxLine cannot be less than minLine");}List<SourceMapEntry> entries = new ArrayList<>();- lock.acquire();- try {+ try (Closeable c = lock.read()) {Long key = getKeyForSourceFile(sourceFile);if (key == null) {return entries;@@ -668,9 +625,6 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHdbError(e);}}- finally {- lock.release();- }Collections.sort(entries);return entries;}@@ -679,8 +633,7 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHpublic boolean removeSourceMapEntry(SourceMapEntry entry) throws LockException {Objects.requireNonNull(entry, "entry cannot be null");program.checkExclusiveAccess();- lock.acquire();- try {+ try (Closeable c = lock.write()) {RecordIterator recIter =sourceMapTableAdapter.getSourceMapRecordIterator(entry.getBaseAddress(), true);while (recIter.hasNext()) {@@ -706,20 +659,13 @@ public class SourceFileManagerDB implements SourceFileManager, ManagerDB, ErrorHdbError(e);return false;}- finally {- lock.release();- }return false;}SourceFile getSourceFile(long key) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return getSourceFileFromKey(key);}- finally {- lock.release();- }}SourceMapEntry getSourceMapEntry(DBRecord rec) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryDB.javaindex f4dc4d8e0d..4fcda95c4a 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryDB.java@@ -21,6 +21,7 @@ import db.DBRecord;import ghidra.program.database.map.AddressMapDB;import ghidra.program.model.address.*;import ghidra.program.model.sourcemap.SourceMapEntry;+import ghidra.util.Lock.Closeable;/*** Database implementation of {@link SourceMapEntry} interface.@@ -43,8 +44,7 @@ public class SourceMapEntryDB implements SourceMapEntry {* @param addrMap address map*/SourceMapEntryDB(SourceFileManagerDB manager, DBRecord record, AddressMapDB addrMap) {- manager.lock.acquire();- try {+ try (Closeable c = manager.lock.read()) {long fileAndLine = record.getLongValue(SourceMapAdapter.FILE_LINE_COL);lineNumber = (int) (fileAndLine & 0xffffffff);sourceFile = manager.getSourceFile(fileAndLine >> 32);@@ -63,9 +63,6 @@ public class SourceMapEntryDB implements SourceMapEntry {range = new AddressRangeImpl(baseAddress, max);}}- finally {- manager.lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryIteratorDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryIteratorDB.javaindex 9351cce628..73896ff62e 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryIteratorDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/sourcemap/SourceMapEntryIteratorDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -23,6 +23,7 @@ import db.DBRecord;import db.RecordIterator;import ghidra.program.model.sourcemap.SourceMapEntry;import ghidra.program.model.sourcemap.SourceMapEntryIterator;+import ghidra.util.Lock.Closeable;/*** Database implementation of {@link SourceMapEntryIterator}@@ -53,8 +54,7 @@ public class SourceMapEntryIteratorDB implements SourceMapEntryIterator {if (nextEntry != null) {return true;}- sourceManager.lock.acquire();- try {+ try (Closeable c = sourceManager.lock.read()) {boolean recIterNext = forward ? recIter.hasNext() : recIter.hasPrevious();if (!recIterNext) {return false;@@ -67,9 +67,6 @@ public class SourceMapEntryIteratorDB implements SourceMapEntryIterator {sourceManager.dbError(e);return false;}- finally {- sourceManager.lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/ClassSymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/ClassSymbol.javaindex 11a7732b4b..5a7af253e5 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/ClassSymbol.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/ClassSymbol.java@@ -16,10 +16,10 @@package ghidra.program.database.symbol;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.address.Address;import ghidra.program.model.listing.GhidraClass;import ghidra.program.model.symbol.*;+import ghidra.util.Lock.Closeable;/*** Symbols that represent classes@@ -31,11 +31,10 @@ public class ClassSymbol extends SymbolDB {/*** Construct a Ghidra Class symbol from an existing symbol record* @param symbolMgr the symbol manager- * @param cache symbol object cache* @param record the record associated with the symbol.*/- ClassSymbol(SymbolManager symbolMgr, DBObjectCache<SymbolDB> cache, DBRecord record) {- super(symbolMgr, cache, Address.NO_ADDRESS, record);+ ClassSymbol(SymbolManager symbolMgr, DBRecord record) {+ super(symbolMgr, Address.NO_ADDRESS, record, record.getKey());}@Override@@ -45,9 +44,8 @@ public class ClassSymbol extends SymbolDB {@Overridepublic GhidraClass getObject() {- lock.acquire();- try {- if (!checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (!refreshIfNeeded()) {return null;}if (ghidraClass == null) {@@ -55,9 +53,6 @@ public class ClassSymbol extends SymbolDB {}return ghidraClass;}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/CodeSymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/CodeSymbol.javaindex a2f0bf5966..7f34f90017 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/CodeSymbol.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/CodeSymbol.java@@ -16,7 +16,6 @@package ghidra.program.database.symbol;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.external.ExternalManagerDB;import ghidra.program.model.address.Address;import ghidra.program.model.listing.CodeUnit;@@ -24,6 +23,7 @@ import ghidra.program.model.listing.Data;import ghidra.program.model.symbol.*;import ghidra.program.util.LabelFieldLocation;import ghidra.program.util.ProgramLocation;+import ghidra.util.Lock.Closeable;/*** Symbols that represent "labels" or external data locations@@ -33,24 +33,12 @@ public class CodeSymbol extends MemorySymbol {/*** Constructs a new CodeSymbol* @param mgr the symbol manager- * @param cache symbol object cache* @param addr the address associated with the symbol* @param record the record for this symbol+ * @param key the database id to use as the object's database key*/- public CodeSymbol(SymbolManager mgr, DBObjectCache<SymbolDB> cache, Address addr,- DBRecord record) {- super(mgr, cache, addr, record);- }-- /**- * Constructs a new CodeSymbol for a default/dynamic label.- * @param mgr the symbol manager- * @param cache symbol object cache- * @param addr the address associated with the symbol- * @param key this must be the absolute encoding of addr- */- public CodeSymbol(SymbolManager mgr, DBObjectCache<SymbolDB> cache, Address addr, long key) {- super(mgr, cache, addr, key);+ CodeSymbol(SymbolManager mgr, Address addr, DBRecord record, long key) {+ super(mgr, addr, record, key);}@Override@@ -84,23 +72,18 @@ public class CodeSymbol extends MemorySymbol {* @return true if symbol successfully removed*/public boolean delete(boolean keepReferences) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (!keepReferences) {symbolMgr.getReferenceManager().removeAllReferencesTo(getAddress());}return super.delete();}- finally {- lock.release();- }}@Overridepublic Object getObject() {- lock.acquire();- try {- if (!checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (!refreshIfNeeded()) {return null;}if (isExternal()) {@@ -117,11 +100,8 @@ public class CodeSymbol extends MemorySymbol {return data != null ? data : cu;}}+ return null;}- finally {- lock.release();- }- return null;}@Override@@ -134,8 +114,7 @@ public class CodeSymbol extends MemorySymbol {@Overridepublic boolean setPrimary() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (address.isExternalAddress()) { // can't set primary on external locationsreturn false;}@@ -159,9 +138,6 @@ public class CodeSymbol extends MemorySymbol {symbolMgr.primarySymbolSet(this, oldPrimarySymbol);return true;}- finally {- lock.release();- }}void setPrimary(boolean primary) {@@ -201,4 +177,8 @@ public class CodeSymbol extends MemorySymbol {}return source;}++ void setIsValid() {+ super.setValid();+ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateDB.javaindex fdf823a467..21541eae94 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -20,8 +20,7 @@ import java.util.ArrayList;import java.util.List;import db.DBRecord;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.model.address.Address;import ghidra.program.model.data.DataTypeManager;import ghidra.program.model.data.Enum;@@ -31,6 +30,7 @@ import ghidra.program.model.scalar.Scalar;import ghidra.program.model.symbol.Equate;import ghidra.program.model.symbol.EquateReference;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.UniversalID;import ghidra.util.exception.*;@@ -39,7 +39,7 @@ import ghidra.util.exception.*;***/-public class EquateDB extends DatabaseObject implements Equate {+public class EquateDB extends DbObject implements Equate {private DBRecord record;private EquateManager equateMgr;@@ -47,11 +47,10 @@ public class EquateDB extends DatabaseObject implements Equate {/*** Constructor* @param equateMgr the equate manager- * @param cache EquateDB cache* @param record the record for this equate.*/- public EquateDB(EquateManager equateMgr, DBObjectCache<EquateDB> cache, DBRecord record) {- super(cache, record.getKey());+ EquateDB(EquateManager equateMgr, DBRecord record) {+ super(record.getKey());this.equateMgr = equateMgr;this.record = record;}@@ -68,8 +67,8 @@ public class EquateDB extends DatabaseObject implements Equate {@Overridepublic void addReference(Address refAddr, int opIndex) {- checkDeleted();- try {+ try (Closeable c = equateMgr.getLock().write()) {+ checkDeleted();Instruction instr = equateMgr.getProgram().getCodeManager().getInstructionAt(refAddr);long dynamicHash;if (instr == null) {@@ -90,12 +89,13 @@ public class EquateDB extends DatabaseObject implements Equate {catch (IOException e) {equateMgr.dbError(e);}+}@Overridepublic void addReference(long dynamicHash, Address refAddr) {- checkDeleted();- try {+ try (Closeable c = equateMgr.getLock().write()) {+ checkDeleted();short opIndex = findOpIndex(refAddr, dynamicHash);equateMgr.addReference(key, refAddr, opIndex, dynamicHash);}@@ -141,7 +141,7 @@ public class EquateDB extends DatabaseObject implements Equate {@Overridepublic String getName() {- checkIsValid();+ validate(equateMgr.getLock());return record.getString(EquateDBAdapter.NAME_COL);}@@ -171,20 +171,20 @@ public class EquateDB extends DatabaseObject implements Equate {@Overridepublic int getReferenceCount() {- checkIsValid();- try {- return equateMgr.getReferenceCount(key);- }- catch (IOException e) {- equateMgr.dbError(e);+ try (Closeable c = equateMgr.getLock().read()) {+ try {+ return equateMgr.getReferenceCount(key);+ }+ catch (IOException e) {+ equateMgr.dbError(e);+ }}return 0;}@Overridepublic EquateReference[] getReferences() {- checkIsValid();- try {+ try (Closeable c = equateMgr.getLock().read()) {return equateMgr.getReferences(key);}catch (IOException e) {@@ -196,24 +196,20 @@ public class EquateDB extends DatabaseObject implements Equate {@Overridepublic List<EquateReference> getReferences(Address refAddr) {Lock lock = equateMgr.getLock();- lock.acquire();- try {- if (checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (refreshIfNeeded()) {return equateMgr.getReferences(key, refAddr);}}catch (IOException e) {equateMgr.getProgram().dbError(e);}- finally {- lock.release();- }return new ArrayList<>();}@Overridepublic long getValue() {- checkIsValid();+ validate(equateMgr.getLock());return record.getLongValue(EquateDBAdapter.VALUE_COL);}@@ -248,8 +244,7 @@ public class EquateDB extends DatabaseObject implements Equate {@Overridepublic void renameEquate(String newName) throws DuplicateNameException, InvalidInputException {Lock lock = equateMgr.getLock();- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();String oldName = getName();@@ -273,9 +268,6 @@ public class EquateDB extends DatabaseObject implements Equate {updateRecord();equateMgr.equateNameChanged(oldName, newName);}- finally {- lock.release();- }}private void updateRecord() {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateManager.javaindex 9a1ad631ce..7c066cc646 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateManager.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -29,6 +29,7 @@ import ghidra.program.model.symbol.*;import ghidra.program.util.EquateInfo;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.UniversalID;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -39,8 +40,8 @@ import ghidra.util.task.TaskMonitor;public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {private AddressMap addrMap;- private DBObjectCache<EquateRefDB> refCache;- private DBObjectCache<EquateDB> equateCache;+ private DbCache<EquateRefDB> refCache;+ private DbCache<EquateDB> equateCache;private EquateDBAdapter equateAdapter;private EquateRefDBAdapter refAdapter;private ProgramDB program;@@ -65,8 +66,8 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {this.addrMap = addrMap;this.lock = lock;initializeAdapters(handle, openMode, monitor);- refCache = new DBObjectCache<>(100);- equateCache = new DBObjectCache<>(100);+ refCache = new DbCache<>(new EquateRefFactory(), lock, 10);+ equateCache = new DbCache<>(new EquateFactory(), lock, 10);}ProgramDB getProgram() {@@ -112,14 +113,14 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {@Overridepublic Equate createEquate(String name, long value)throws DuplicateNameException, InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (equateAdapter.hasRecord(name)) {throw new DuplicateNameException(name + " already exists for an equate.");}validateName(name);DBRecord record = equateAdapter.createEquate(name, value);- EquateDB equate = new EquateDB(this, equateCache, record);+ EquateDB equate = new EquateDB(this, record);+ equateCache.add(equate);program.setChanged(ProgramEvent.EQUATE_ADDED, new EquateInfo(name, value, null, 0, 0),null);return equate;@@ -128,25 +129,21 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic Equate getEquate(Address reference, int opIndex, long scalarValue) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {long refAddr = addrMap.getKey(reference, false);if (refAddr == AddressMap.INVALID_ADDRESS_KEY) {return null;}Field[] keys = refAdapter.getRecordKeysForAddr(refAddr);for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());if (ref.getOpIndex() == opIndex) {- EquateDB equate = getEquateDB(ref.getEquateID());+ EquateDB equate = equateCache.getCachedInstance(ref.getEquateID());if (equate.getValue() == scalarValue) {return equate;}@@ -156,68 +153,56 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic List<Equate> getEquates(Address reference, int opIndex) {List<Equate> ret = new LinkedList<>();- lock.acquire();- try {+ try (Closeable c = lock.read()) {long refAddr = addrMap.getKey(reference, false);if (refAddr == AddressMap.INVALID_ADDRESS_KEY) {return ret;}Field[] keys = refAdapter.getRecordKeysForAddr(refAddr);for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());if (ref.getOpIndex() == opIndex) {- ret.add(getEquateDB(ref.getEquateID()));+ ret.add(equateCache.getCachedInstance(ref.getEquateID()));}}}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return ret;}@Overridepublic List<Equate> getEquates(Address reference) {List<Equate> ret = new LinkedList<>();- lock.acquire();- try {+ try (Closeable c = lock.read()) {long refAddr = addrMap.getKey(reference, false);if (refAddr == AddressMap.INVALID_ADDRESS_KEY) {return ret;}Field[] keys = refAdapter.getRecordKeysForAddr(refAddr);for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());- ret.add(getEquateDB(ref.getEquateID()));+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());+ ret.add(equateCache.getCachedInstance(ref.getEquateID()));}}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return ret;}@Overridepublic Equate getEquate(String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {long equateID = equateAdapter.getRecordKey(name);- return getEquateDB(equateID);+ return equateCache.getCachedInstance(equateID);}catch (NotFoundException e) {// just return null below@@ -225,9 +210,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -293,14 +275,15 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {@Overridepublic List<Equate> getEquates(long value) {+ArrayList<Equate> list = new ArrayList<>();- try {+ try (Closeable c = lock.read()) {RecordIterator iter = equateAdapter.getRecords();while (iter.hasNext()) {DBRecord rec = iter.next();- EquateDB equateDB = getEquateDB(rec.getKey());- if (equateDB.getValue() == value) {- list.add(equateDB);+ long equateValue = rec.getLongValue(EquateDBAdapter.VALUE_COL);+ if (equateValue == value) {+ list.add(equateCache.getCachedInstance(rec));}}}@@ -314,8 +297,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {public void deleteAddressRange(Address startAddr, Address endAddr, TaskMonitor monitor)throws CancelledException {AddressRange.checkValidRange(startAddr, endAddr);- lock.acquire();- try {+ try (Closeable c = lock.write()) {ArrayList<EquateRefDB> list = new ArrayList<>();AddressIterator iter = getEquateAddresses(startAddr, endAddr);while (iter.hasNext()) {@@ -325,7 +307,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {Address addr = iter.next();Field[] keys = refAdapter.getRecordKeysForAddr(addrMap.getKey(addr, false));for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());list.add(ref);}}@@ -333,7 +315,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {if (monitor.isCancelled()) {throw new CancelledException();}- EquateDB equateDB = getEquateDB(ref.getEquateID());+ EquateDB equateDB = equateCache.getCachedInstance(ref.getEquateID());removeRef(equateDB, ref);if (getReferenceCount(equateDB.getKey()) == 0) {@@ -345,9 +327,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }}@Override@@ -355,9 +334,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {if (name == null) {return false;}- lock.acquire();- try {-+ try (Closeable c = lock.write()) {EquateDB equateDB = (EquateDB) getEquate(name);if (equateDB != null) {long equateID = equateDB.getKey();@@ -372,25 +349,16 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return false;}private void removeEquate(EquateDB equateDB) throws IOException {- lock.acquire();- try {- String name = equateDB.getName();- long equateID = equateDB.getKey();- equateAdapter.removeRecord(equateID);- equateCache.delete(equateID);- // fire event: oldValue = equate name, newValue=null- program.setChanged(ProgramEvent.EQUATE_REMOVED, name, null);- }- finally {- lock.release();- }+ String name = equateDB.getName();+ long equateID = equateDB.getKey();+ equateAdapter.removeRecord(equateID);+ equateCache.delete(equateID);+ // fire event: oldValue = equate name, newValue=null+ program.setChanged(ProgramEvent.EQUATE_REMOVED, name, null);}AddressMap getAddressMap() {@@ -407,45 +375,41 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {void addReference(long equateID, Address address, int opIndex, long dynamicHash)throws IOException {- lock.acquire();- try {- EquateDB equateDB = getEquateDB(equateID);- String name = equateDB.getName();- long value = equateDB.getValue();+ EquateDB equateDB = equateCache.getCachedInstance(equateID);+ String name = equateDB.getName();+ long value = equateDB.getValue();- long addr = addrMap.getKey(address, true);+ long addr = addrMap.getKey(address, true);- // first remove reference for address and opIndex- Field[] keys = refAdapter.getRecordKeysForAddr(addr);- for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());- if (dynamicHash != 0) {- if (ref.getDynamicHashValue() == dynamicHash) {- removeRef(equateDB, ref);- }- }- else if (ref.getDynamicHashValue() == 0 && ref.getOpIndex() == opIndex) {+ // first remove reference for address and opIndex+ Field[] keys = refAdapter.getRecordKeysForAddr(addr);+ for (Field key : keys) {+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());+ if (dynamicHash != 0) {+ if (ref.getDynamicHashValue() == dynamicHash) {removeRef(equateDB, ref);}}- DBRecord record =- refAdapter.createReference(addr, (short) opIndex, dynamicHash, equateID);- new EquateRefDB(this, refCache, record);-- // fire event: oldValue=EquateInfo, newValue = null- program.setChanged(ProgramEvent.EQUATE_REFERENCE_ADDED, address, address,- new EquateInfo(name, value, address, opIndex, dynamicHash), null);- }- finally {- lock.release();+ else if (ref.getDynamicHashValue() == 0 && ref.getOpIndex() == opIndex) {+ removeRef(equateDB, ref);+ }}+ DBRecord record =+ refAdapter.createReference(addr, (short) opIndex, dynamicHash, equateID);+ EquateRefDB eq = new EquateRefDB(this, record);+ refCache.add(eq);++ // fire event: oldValue=EquateInfo, newValue = null+ program.setChanged(ProgramEvent.EQUATE_REFERENCE_ADDED, address, address,+ new EquateInfo(name, value, address, opIndex, dynamicHash), null);+}EquateRefDB[] getReferences(long equateID) throws IOException {Field[] keys = refAdapter.getRecordKeysForEquateID(equateID);EquateRefDB[] refs = new EquateRefDB[keys.length];for (int i = 0; i < keys.length; i++) {- refs[i] = getEquateRefDB(keys[i].getLongValue());+ refs[i] = refCache.getCachedInstance(keys[i].getLongValue());}return refs;}@@ -462,7 +426,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {}Field[] keys = refAdapter.getRecordKeysForAddr(refAddr);for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());if (ref.getEquateID() == equateID) {refs.add(ref);}@@ -474,7 +438,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {Field[] keys = refAdapter.getRecordKeysForEquateID(equateDB.getKey());for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());if (ref.getOpIndex() == opIndex && ref.getAddress().equals(refAddr)) {removeRef(equateDB, ref);break;@@ -486,7 +450,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {Field[] keys = refAdapter.getRecordKeysForEquateID(equateDB.getKey());for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());if (ref.getDynamicHashValue() == dynamicHash && ref.getAddress().equals(refAddr)) {removeRef(equateDB, ref);break;@@ -534,57 +498,20 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {}private void removeRef(EquateDB equateDB, EquateRefDB ref) throws IOException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {long key = ref.getKey();refAdapter.removeRecord(key);refCache.delete(key);referenceRemoved(equateDB, ref.getAddress(), ref.getOpIndex(),ref.getDynamicHashValue());}- finally {- lock.release();- }-- }-- private EquateDB getEquateDB(long equateID) throws IOException {- lock.acquire();- try {- EquateDB equateDB = equateCache.get(equateID);- if (equateDB == null) {- DBRecord record = equateAdapter.getRecord(equateID);- if (record != null) {- equateDB = new EquateDB(this, equateCache, record);- }- }- return equateDB;- }- finally {- lock.release();- }- }-- private EquateRefDB getEquateRefDB(long key) throws IOException {- lock.acquire();- try {- EquateRefDB ref = refCache.get(key);- if (ref == null) {- DBRecord record = refAdapter.getRecord(key);- ref = new EquateRefDB(this, refCache, record);- }- return ref;- }- finally {- lock.release();- }}private void removeReferences(long equateID) throws IOException {- EquateDB equateDB = getEquateDB(equateID);+ EquateDB equateDB = equateCache.getCachedInstance(equateID);Field[] keys = refAdapter.getRecordKeysForEquateID(equateID);for (Field key : keys) {- EquateRefDB ref = getEquateRefDB(key.getLongValue());+ EquateRefDB ref = refCache.getCachedInstance(key.getLongValue());removeRef(equateDB, ref);}}@@ -602,30 +529,22 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {@Overridepublic void invalidateCache(boolean all) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {refCache.invalidate();equateCache.invalidate();}- finally {- lock.release();- }}@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {invalidateCache(true);refAdapter.moveAddressRange(fromAddr, toAddr, length, monitor);}catch (IOException e) {dbError(e);}- finally {- lock.release();- }}/**@@ -706,7 +625,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {try {DBRecord record = iter.next();if (record != null) {- return getEquateDB(record.getKey());+ return equateCache.getCachedInstance(record.getKey());}}catch (IOException e) {@@ -722,4 +641,45 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {}}++ class EquateFactory implements DbFactory<EquateDB> {++ @Override+ public EquateDB instantiate(long key) {+ try {+ DBRecord record = equateAdapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public EquateDB instantiate(DBRecord record) {+ return new EquateDB(EquateManager.this, record);+ }+ }++ class EquateRefFactory implements DbFactory<EquateRefDB> {++ @Override+ public EquateRefDB instantiate(long key) {+ try {+ DBRecord record = refAdapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ dbError(e);+ return null;+ }+ }++ @Override+ public EquateRefDB instantiate(DBRecord record) {+ return new EquateRefDB(EquateManager.this, record);+ }++ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateRefDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateRefDB.javaindex 10f4691c07..d02a80087f 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateRefDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/EquateRefDB.java@@ -16,31 +16,25 @@package ghidra.program.database.symbol;import db.DBRecord;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.DbObject;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.symbol.EquateReference;+import ghidra.util.Lock.Closeable;/*** Database object for the equate references.***/-class EquateRefDB extends DatabaseObject implements EquateReference {+class EquateRefDB extends DbObject implements EquateReference {private DBRecord record;private EquateManager equateMgr;private AddressMap addrMap;- /**- * Constructor- * @param equateMgr- * @param cache- * @param record- */- EquateRefDB(EquateManager equateMgr, DBObjectCache<EquateRefDB> cache, DBRecord record) {- super(cache, record.getKey());+ EquateRefDB(EquateManager equateMgr, DBRecord record) {+ super(record.getKey());addrMap = equateMgr.getAddressMap();this.equateMgr = equateMgr;this.record = record;@@ -62,21 +56,27 @@ class EquateRefDB extends DatabaseObject implements EquateReference {@Overridepublic Address getAddress() {- checkIsValid();- long addr = record.getLongValue(EquateRefDBAdapter.ADDR_COL);- return addrMap.decodeAddress(addr);+ try (Closeable c = equateMgr.getLock().read()) {+ refreshIfNeeded();+ long addr = record.getLongValue(EquateRefDBAdapter.ADDR_COL);+ return addrMap.decodeAddress(addr);+ }}@Overridepublic short getOpIndex() {- checkIsValid();- return record.getShortValue(EquateRefDBAdapter.OP_INDEX_COL);+ try (Closeable c = equateMgr.getLock().read()) {+ refreshIfNeeded();+ return record.getShortValue(EquateRefDBAdapter.OP_INDEX_COL);+ }}@Overridepublic long getDynamicHashValue() {- checkIsValid();- return record.getLongValue(EquateRefDBAdapter.HASH_COL);+ try (Closeable c = equateMgr.getLock().read()) {+ refreshIfNeeded();+ return record.getLongValue(EquateRefDBAdapter.HASH_COL);+ }}DBRecord getRecord() {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/FunctionSymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/FunctionSymbol.javaindex 301824f6b2..3d2d745012 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/FunctionSymbol.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/FunctionSymbol.java@@ -20,7 +20,6 @@ import java.util.ArrayList;import java.util.List;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.external.ExternalManagerDB;import ghidra.program.database.function.FunctionManagerDB;import ghidra.program.model.address.Address;@@ -29,6 +28,7 @@ import ghidra.program.model.listing.Function;import ghidra.program.model.symbol.*;import ghidra.program.util.FunctionReturnTypeFieldLocation;import ghidra.program.util.ProgramLocation;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -44,13 +44,11 @@ public class FunctionSymbol extends MemorySymbol {/*** Construct a new FunctionSymbol* @param symbolMgr the symbol manager.- * @param cache symbol object cache* @param address the address for this symbol.* @param record the record for this symbol.*/- public FunctionSymbol(SymbolManager symbolMgr, DBObjectCache<SymbolDB> cache, Address address,- DBRecord record) {- super(symbolMgr, cache, address, record);+ FunctionSymbol(SymbolManager symbolMgr, Address address, DBRecord record) {+ super(symbolMgr, address, record, record.getKey());this.functionMgr = symbolMgr.getFunctionManager();}@@ -66,23 +64,18 @@ public class FunctionSymbol extends MemorySymbol {@Overridepublic void setNameAndNamespace(String newName, Namespace newNamespace, SourceType source)throws DuplicateNameException, InvalidInputException, CircularDependencyException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {boolean namespaceChange = !getParentNamespace().equals(newNamespace);super.setNameAndNamespace(newName, newNamespace, source);if (namespaceChange) {functionMgr.functionNamespaceChanged(key);}}- finally {- lock.release();- }}@Overridepublic boolean delete() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {boolean restoreLabel = isExternal() || (getSource() != SourceType.DEFAULT);String symName = getName();Symbol parentSymbol = getParentSymbol();@@ -139,9 +132,6 @@ public class FunctionSymbol extends MemorySymbol {catch (IOException e) {symbolMgr.dbError(e);}- finally {- lock.release();- }return false;}@@ -157,9 +147,8 @@ public class FunctionSymbol extends MemorySymbol {@Overridepublic ProgramLocation getProgramLocation() {- lock.acquire();- try {- if (!checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (!refreshIfNeeded()) {return null;}Function f = getObject();@@ -167,9 +156,6 @@ public class FunctionSymbol extends MemorySymbol {return new FunctionReturnTypeFieldLocation(getProgram(), address, 0, signature,f.getReturnType().getName());}- finally {- lock.release();- }}@Override@@ -266,13 +252,9 @@ public class FunctionSymbol extends MemorySymbol {@Overridepublic Reference[] getReferences(TaskMonitor monitor) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();Reference[] refs = super.getReferences(monitor);- if (monitor == null) {- monitor = TaskMonitor.DUMMY;- }if (monitor.isCancelled()) {return refs;}@@ -293,16 +275,12 @@ public class FunctionSymbol extends MemorySymbol {}return newRefs;}- finally {- lock.release();- }}@Overridepublic int getReferenceCount() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();int count = super.getReferenceCount();List<Long> thunkIds = functionMgr.getThunkFunctionIds(key);if (thunkIds != null) {@@ -310,25 +288,18 @@ public class FunctionSymbol extends MemorySymbol {}return count;}- finally {- lock.release();- }}@Overridepublic boolean hasReferences() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (super.hasReferences()) {return true;}List<Long> thunkIds = functionMgr.getThunkFunctionIds(key);return thunkIds != null ? (thunkIds.size() != 0) : false;}- finally {- lock.release();- }}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/GlobalVariableSymbolDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/GlobalVariableSymbolDB.javaindex 9d152155f9..0b712dfc42 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/GlobalVariableSymbolDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/GlobalVariableSymbolDB.java@@ -16,7 +16,6 @@package ghidra.program.database.symbol;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.address.Address;import ghidra.program.model.lang.Register;import ghidra.program.model.listing.*;@@ -31,14 +30,13 @@ public class GlobalVariableSymbolDB extends VariableSymbolDB {/*** Constructs a new GlobalVariableSymbolDB which are restricted to the global namespace* @param symbolMgr the symbol manager- * @param cache symbol object cache* @param variableMgr variable storage manager* @param address the address of the symbol (stack address)* @param record the record for the symbol*/- public GlobalVariableSymbolDB(SymbolManager symbolMgr, DBObjectCache<SymbolDB> cache,- VariableStorageManagerDB variableMgr, Address address, DBRecord record) {- super(symbolMgr, cache, SymbolType.GLOBAL_VAR, variableMgr, address, record);+ public GlobalVariableSymbolDB(SymbolManager symbolMgr, VariableStorageManagerDB variableMgr,+ Address address, DBRecord record) {+ super(symbolMgr, SymbolType.GLOBAL_VAR, variableMgr, address, record);if (record.getLongValue(SymbolDatabaseAdapter.SYMBOL_PARENT_ID_COL) != Namespace.GLOBAL_NAMESPACE_ID) {throw new AssertException();@@ -63,7 +61,7 @@ public class GlobalVariableSymbolDB extends VariableSymbolDB {@Overrideprotected String doGetName() {- if (!checkIsValid()) {+ if (!refreshIfNeeded()) {// TODO: SCRreturn "[Invalid Global Variable Symbol - Deleted!]";}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/LibrarySymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/LibrarySymbol.javaindex 137a97818a..decb22c33c 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/LibrarySymbol.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/LibrarySymbol.java@@ -16,12 +16,12 @@package ghidra.program.database.symbol;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.address.Address;import ghidra.program.model.listing.CircularDependencyException;import ghidra.program.model.listing.Library;import ghidra.program.model.symbol.*;import ghidra.program.util.ProgramEvent;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -39,11 +39,10 @@ public class LibrarySymbol extends SymbolDB {/*** Constructs a new Library Symbol* @param symbolMgr the symbol manager- * @param cache symbol object cache* @param record the record for this symbol*/- public LibrarySymbol(SymbolManager symbolMgr, DBObjectCache<SymbolDB> cache, DBRecord record) {- super(symbolMgr, cache, Address.NO_ADDRESS, record);+ LibrarySymbol(SymbolManager symbolMgr, DBRecord record) {+ super(symbolMgr, Address.NO_ADDRESS, record, record.getKey());}@Override@@ -90,9 +89,8 @@ public class LibrarySymbol extends SymbolDB {@Overridepublic Library getObject() {- lock.acquire();- try {- if (!checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (!refreshIfNeeded()) {return null;}if (library == null) {@@ -100,9 +98,6 @@ public class LibrarySymbol extends SymbolDB {}return library;}- finally {- lock.release();- }}@Override@@ -132,16 +127,11 @@ public class LibrarySymbol extends SymbolDB {String oldPath = getExternalLibraryPath();- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();setRecordFields(record, libraryPath);updateRecord();}- finally {- lock.release();- }-symbolMgr.getProgram().setObjChanged(ProgramEvent.EXTERNAL_PATH_CHANGED, getName(), oldPath, libraryPath);}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/MemorySymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/MemorySymbol.javaindex 5e1601e091..06d49f04c2 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/MemorySymbol.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/MemorySymbol.java@@ -19,10 +19,10 @@ import java.util.ArrayList;import java.util.Objects;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.external.ExternalLocationDB;import ghidra.program.model.address.Address;import ghidra.program.model.symbol.*;+import ghidra.util.Lock.Closeable;import ghidra.util.task.TaskMonitor;import ghidra.util.task.UnknownProgressWrappingTaskMonitor;@@ -38,35 +38,17 @@ public abstract class MemorySymbol extends SymbolDB {/*** Constructs a new MemorySymbol which corresponds to the specified symbol record,* @param mgr the symbol manager- * @param cache symbol object cache* @param addr the address associated with the symbol* @param record the record for this symbol+ * @param key the database key for the symbol*/- protected MemorySymbol(SymbolManager mgr, DBObjectCache<SymbolDB> cache, Address addr,- DBRecord record) {- super(mgr, cache, addr, record);+ protected MemorySymbol(SymbolManager mgr, Address addr, DBRecord record, long key) {+ super(mgr, addr, record, key);if (!addr.isMemoryAddress() && !isExternal()) {throw new IllegalArgumentException("memory or external address required");}}- /**- * Constructs a new MemorySymbol which corresponds to the specified symbol key and has- * no record. This is intended to support dynamic label cases which do not have a record- * and do not support an external address.- * @param mgr the symbol manager- * @param cache symbol object cache- * @param addr the address associated with the symbol- * @param key this must be the absolute encoding of addr- */- protected MemorySymbol(SymbolManager mgr, DBObjectCache<SymbolDB> cache, Address addr,- long key) {- super(mgr, cache, addr, key);- if (!addr.isMemoryAddress()) {- throw new IllegalArgumentException("memory address required");- }- }-@Overridepublic final boolean isExternalEntryPoint() {validate(lock);@@ -94,23 +76,18 @@ public abstract class MemorySymbol extends SymbolDB {}private boolean doIsPinned() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return false;}byte flags = record.getByteValue(SymbolDatabaseAdapter.SYMBOL_FLAGS_COL);return ((flags & SymbolDatabaseAdapter.SYMBOL_PINNED_FLAG) != 0);}- finally {- lock.release();- }}private void doSetPinned(boolean pinned) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (pinned == isPinned()) {return;@@ -121,9 +98,6 @@ public abstract class MemorySymbol extends SymbolDB {symbolMgr.symbolAnchoredFlagChanged(this);}}- finally {- lock.release();- }}private void updatePinnedFlag(boolean pinned) {@@ -149,8 +123,7 @@ public abstract class MemorySymbol extends SymbolDB {*/protected void moveLowLevel(Address newAddress, String newName, Namespace newNamespace,SourceType newSource, boolean pinned) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();// update the address to the new location@@ -175,9 +148,6 @@ public abstract class MemorySymbol extends SymbolDB {updateRecord();setInvalid();}- finally {- lock.release();- }}private boolean hasExactlyOneSymbolAtAddress(Address addr) {@@ -191,9 +161,8 @@ public abstract class MemorySymbol extends SymbolDB {@Overridepublic int getReferenceCount() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();ReferenceManager rm = symbolMgr.getReferenceManager();// If there is only one symbol, then all the references to this address count@@ -216,16 +185,12 @@ public abstract class MemorySymbol extends SymbolDB {}return count;}- finally {- lock.release();- }}@Overridepublic boolean hasReferences() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();ReferenceManager rm = symbolMgr.getReferenceManager();ReferenceIterator iter = rm.getReferencesTo(address);boolean isPrimary = this.isPrimary();@@ -238,16 +203,12 @@ public abstract class MemorySymbol extends SymbolDB {}return false;}- finally {- lock.release();- }}@Overridepublic Reference[] getReferences(TaskMonitor monitor) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (monitor == null) {monitor = TaskMonitor.DUMMY;}@@ -279,9 +240,6 @@ public abstract class MemorySymbol extends SymbolDB {Reference[] refs = new Reference[list.size()];return list.toArray(refs);}- finally {- lock.release();- }}/**@@ -296,17 +254,13 @@ public abstract class MemorySymbol extends SymbolDB {* demangled or renamed.*/public final String getExternalOriginalImportedName() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return null;}return record.getString(SymbolDatabaseAdapter.SYMBOL_ORIGINAL_IMPORTED_NAME_COL);}- finally {- lock.release();- }}/**@@ -329,8 +283,7 @@ public abstract class MemorySymbol extends SymbolDB {throw new javax.help.UnsupportedOperationException("Symbol does not support: originalImportedName");}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (record == null) {return;@@ -346,9 +299,6 @@ public abstract class MemorySymbol extends SymbolDB {}}}- finally {- lock.release();- }}/**@@ -362,9 +312,8 @@ public abstract class MemorySymbol extends SymbolDB {* @return external Program address or null if not external or unknown*/public final Address getExternalProgramAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return null;}@@ -378,9 +327,6 @@ public abstract class MemorySymbol extends SymbolDB {}return symbolMgr.getAddressMap().getAddressFactory().getAddress(addrStr);}- finally {- lock.release();- }}/**@@ -405,8 +351,7 @@ public abstract class MemorySymbol extends SymbolDB {if (externalProgramAddress != null && !externalProgramAddress.isLoadedMemoryAddress()) {throw new IllegalArgumentException("Memory address required for external program");}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (record == null) {return;@@ -422,9 +367,6 @@ public abstract class MemorySymbol extends SymbolDB {}}}- finally {- lock.release();- }}static void setExternalFields(DBRecord record, String originalImportName,diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceManager.javaindex 51f52ae388..27bd5ff133 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceManager.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -18,6 +18,8 @@ package ghidra.program.database.symbol;import java.io.IOException;import java.util.*;+import org.apache.commons.collections4.map.LRUMap;+import db.*;import db.util.ErrorHandler;import ghidra.framework.data.OpenMode;@@ -28,6 +30,7 @@ import ghidra.program.database.util.AddressRangeMapDB;import ghidra.program.model.address.*;import ghidra.program.model.symbol.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.exception.VersionException;import ghidra.util.task.TaskMonitor;@@ -44,8 +47,7 @@ public class NamespaceManager implements ManagerDB {private SymbolManager symbolMgr;private Namespace globalNamespace;private Lock lock;- private Namespace lastBodyNamespace;- private AddressSet lastBody;+ private Map<Namespace, AddressSet> bodyCache = Collections.synchronizedMap(new LRUMap<>(10));/*** Construct a new namespace manager.@@ -76,19 +78,14 @@ public class NamespaceManager implements ManagerDB {@Overridepublic void deleteAddressRange(Address startAddr, Address endAddr, TaskMonitor monitor)throws CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {namespaceMap.clearRange(startAddr, endAddr);- }- finally {clearCache();- lock.release();}}private void clearCache() {- lastBodyNamespace = null;- lastBody = null;+ bodyCache.clear();}@Override@@ -133,8 +130,7 @@ public class NamespaceManager implements ManagerDB {throw new IllegalArgumentException("Namespace body size must be less than 0x7fffffff byte addresses");}- lock.acquire();- try {+ try (Closeable c = lock.write()) {AddressSetView oldBody = removeBody(namespace);AddressRange range = overlapsNamespace(set);if (range != null) {@@ -143,10 +139,7 @@ public class NamespaceManager implements ManagerDB {range.getMaxAddress());}doSetBody(namespace, set);- }- finally {clearCache();- lock.release();}}@@ -165,19 +158,15 @@ public class NamespaceManager implements ManagerDB {* @return old body*/public AddressSetView removeBody(Namespace namespace) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {AddressSetView set = getAddressSet(namespace);AddressRangeIterator iter = set.getAddressRanges();while (iter.hasNext()) {AddressRange range = iter.next();namespaceMap.clearRange(range.getMinAddress(), range.getMaxAddress());}- return set;- }- finally {clearCache();- lock.release();+ return set;}}@@ -228,8 +217,7 @@ public class NamespaceManager implements ManagerDB {* @return a LongField function key iterator.*/public Iterator<Namespace> getNamespacesOverlapping(AddressSetView set) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {LinkedHashSet<Long> idSet = new LinkedHashSet<Long>();AddressRangeIterator rangeIter = set.getAddressRanges();while (rangeIter.hasNext()) {@@ -256,13 +244,8 @@ public class NamespaceManager implements ManagerDB {}}}-- return list.iterator();-- }- finally {clearCache();- lock.release();+ return list.iterator();}}@@ -272,10 +255,10 @@ public class NamespaceManager implements ManagerDB {* @return body for the given namespace*/public AddressSetView getAddressSet(Namespace namespace) {- lock.acquire();- try {- if (namespace == lastBodyNamespace) {- return lastBody;+ try (Closeable c = lock.read()) {+ AddressSet addressSet = bodyCache.get(namespace);+ if (addressSet != null) {+ return addressSet;}symbolMgr.checkValidNamespaceArgument(namespace);@@ -290,30 +273,19 @@ public class NamespaceManager implements ManagerDB {set.add(((Namespace) obj).getBody());}}- lastBodyNamespace = namespace;- lastBody = set;+ bodyCache.put(namespace, set);return set;}- finally {- lock.release();- }}private AddressSetView getAddressSet(long namespaceID) {- lock.acquire();- try {- return namespaceMap.getAddressSet(new LongField(namespaceID));- }- finally {- lock.release();- }+ return lock.withRead(() -> namespaceMap.getAddressSet(new LongField(namespaceID)));}@Overridepublic void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws AddressOverflowException, CancelledException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {Address rangeEnd = fromAddr.addNoWrap(length - 1);AddressSet addrSet = new AddressSet(fromAddr, rangeEnd);@@ -353,10 +325,7 @@ public class NamespaceManager implements ManagerDB {namespaceMap.paintRange(h.range.getMinAddress(), h.range.getMaxAddress(),new LongField(h.namespaceID));}- }- finally {clearCache();- lock.release();}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceSymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceSymbol.javaindex 87616d48bc..8caf78f856 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceSymbol.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/NamespaceSymbol.java@@ -16,9 +16,9 @@package ghidra.program.database.symbol;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.model.address.Address;import ghidra.program.model.symbol.*;+import ghidra.util.Lock.Closeable;/*** Symbol class for namespaces.@@ -30,11 +30,10 @@ public class NamespaceSymbol extends SymbolDB {/*** Construct a new namespace symbol* @param mgr the symbol manager.- * @param cache symbol object cache* @param record the record for this symbol.*/- NamespaceSymbol(SymbolManager mgr, DBObjectCache<SymbolDB> cache, DBRecord record) {- super(mgr, cache, Address.NO_ADDRESS, record);+ NamespaceSymbol(SymbolManager mgr, DBRecord record) {+ super(mgr, Address.NO_ADDRESS, record, record.getKey());}@Override@@ -55,9 +54,8 @@ public class NamespaceSymbol extends SymbolDB {@Overridepublic Namespace getObject() {- lock.acquire();- try {- if (!checkIsValid()) {+ try (Closeable c = lock.read()) {+ if (!refreshIfNeeded()) {return null;}if (namespace == null) {@@ -65,9 +63,6 @@ public class NamespaceSymbol extends SymbolDB {}return namespace;}- finally {- lock.release();- }}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolDB.javaindex e5a9c53b20..e169382561 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolDB.java@@ -21,7 +21,8 @@ import java.util.List;import db.DBRecord;import db.Field;-import ghidra.program.database.*;+import ghidra.program.database.DbObject;+import ghidra.program.database.ProgramDB;import ghidra.program.database.external.ExternalLocationDB;import ghidra.program.database.external.ExternalManagerDB;import ghidra.program.model.address.Address;@@ -32,6 +33,7 @@ import ghidra.program.model.listing.Program;import ghidra.program.model.symbol.*;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.SystemUtilities;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -39,7 +41,7 @@ import ghidra.util.exception.InvalidInputException;/*** Base class for symbols*/-public abstract class SymbolDB extends DatabaseObject implements Symbol {+public abstract class SymbolDB extends DbObject implements Symbol {private boolean isDeleting = false;@@ -51,22 +53,14 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {private volatile String cachedName;private volatile long cachedNameModCount;- SymbolDB(SymbolManager symbolMgr, DBObjectCache<SymbolDB> cache, Address address,- DBRecord record) {- super(cache, record.getKey());+ SymbolDB(SymbolManager symbolMgr, Address address, DBRecord record, long key) {+ super(key);this.symbolMgr = symbolMgr;this.address = address;this.record = record;lock = symbolMgr.getLock();}- SymbolDB(SymbolManager symbolMgr, DBObjectCache<SymbolDB> cache, Address address, long key) {- super(cache, key);- this.symbolMgr = symbolMgr;- this.address = address;- lock = symbolMgr.getLock();- }-@Overridepublic boolean isDeleted() {return isDeleted(lock);@@ -108,14 +102,10 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {@Overridepublic Address getAddress() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return address;}- finally {- lock.release();- }}protected void setAddress(Address addr) {@@ -139,16 +129,12 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {return name;}- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();cachedName = doGetName();cachedNameModCount = symbolMgr.getProgram().getModificationNumber();return cachedName;}- finally {- lock.release();- }}private boolean hasValidCachedName(String name) {@@ -178,9 +164,8 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {@Overridepublic String getName(boolean includeNamespace) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();String symName = getName();if (includeNamespace) {Namespace ns = getParentNamespace();@@ -191,9 +176,6 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {}return symName;}- finally {- lock.release();- }}private void fillListWithNamespacePath(Namespace namespace, List<String> list) {@@ -210,18 +192,14 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {@Overridepublic String[] getPath() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();ArrayList<String> list = new ArrayList<>();fillListWithNamespacePath(getParentNamespace(), list);list.add(getName());String[] path = list.toArray(new String[list.size()]);return path;}- finally {- lock.release();- }}@Override@@ -239,8 +217,7 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {*/@Overridepublic void setSource(SourceType newSource) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();symbolMgr.validateSource(getName(), getAddress(), getSymbolType(), newSource);SourceType oldSource = getSource();@@ -258,9 +235,6 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {symbolMgr.symbolSourceChanged(this);}}- finally {- lock.release();- }}protected void setSourceFlagBits(SourceType newSource) {@@ -274,18 +248,14 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {@Overridepublic SourceType getSource() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return SourceType.DEFAULT;}byte flags = record.getByteValue(SymbolDatabaseAdapter.SYMBOL_FLAGS_COL);return SymbolDatabaseAdapter.decodeSourceTypeFromFlags(flags);}- finally {- lock.release();- }}@Override@@ -322,8 +292,7 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {boolean checkForDuplicates)throws DuplicateNameException, InvalidInputException, CircularDependencyException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();checkEditOK();@@ -418,9 +387,6 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {symbolMgr.convertDynamicSymbol(this, newName, newNamespace.getID(), source);}}- finally {- lock.release();- }}@Override@@ -530,48 +496,36 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {@Overridepublic Symbol getParentSymbol() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return null;}return symbolMgr.getSymbol(record.getLongValue(SymbolDatabaseAdapter.SYMBOL_PARENT_ID_COL));}- finally {- lock.release();- }}long getParentID() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return Namespace.GLOBAL_NAMESPACE_ID;}return record.getLongValue(SymbolDatabaseAdapter.SYMBOL_PARENT_ID_COL);}- finally {- lock.release();- }}@Overridepublic boolean isGlobal() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record == null) {return true;}return record.getLongValue(SymbolDatabaseAdapter.SYMBOL_PARENT_ID_COL) == Namespace.GLOBAL_NAMESPACE_ID;}- finally {- lock.release();- }}public long getDataTypeId() {@@ -589,8 +543,7 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {* @param value the value to set as symbol data 1.*/public void setDataTypeId(long value) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (record != null) {record.setLongValue(SymbolDatabaseAdapter.SYMBOL_DATATYPE_COL, value);@@ -598,14 +551,10 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {symbolMgr.symbolDataChanged(this);}}- finally {- lock.release();- }}protected void doSetPrimary(boolean primary) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (record != null) {if (primary) {@@ -619,38 +568,28 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {symbolMgr.symbolDataChanged(this);}}- finally {- lock.release();- }-}protected boolean doCheckIsPrimary() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record != null) {return !record.getFieldValue(SymbolDatabaseAdapter.SYMBOL_PRIMARY_COL).isNull();}return false;}- finally {- lock.release();- }}@Overridepublic boolean delete() {- lock.acquire();isDeleting = true;- try {- if (checkIsValid() && record != null) {+ try (Closeable c = lock.write()) {+ if (refreshIfNeeded() && record != null) {return symbolMgr.doRemoveSymbol(this);}}finally {isDeleting = false;- lock.release();}return false;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolManager.javaindex 6275670096..446c1440bf 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/SymbolManager.java@@ -41,6 +41,7 @@ import ghidra.program.model.symbol.*;import ghidra.program.util.LanguageTranslator;import ghidra.program.util.ProgramEvent;import ghidra.util.*;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -64,7 +65,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {private SymbolDatabaseAdapter adapter;private LabelHistoryAdapter historyAdapter;- private DBObjectCache<SymbolDB> cache;+ private DbCache<SymbolDB> cache;private ErrorHandler errHandler;private ProgramDB program;private ReferenceDBManager refManager;@@ -100,7 +101,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {this.lock = lock;dynamicSymbolAddressMap = new AddressMapImpl((byte) 0x40, addrMap.getAddressFactory());initializeAdapters(handle, openMode, monitor);- cache = new DBObjectCache<>(100);+ cache = new DbCache<>(new SymbolFactory(), lock, 100);variableStorageMgr =new VariableStorageManagerDB(handle, addrMap, openMode, errHandler, lock, monitor);@@ -165,9 +166,8 @@ public class SymbolManager implements SymbolTable, ManagerDB {// Migrate all other OldNamespaceAddresses used for variable symbolsprocessOldVariableAddresses(monitor);}-if (currentRevision < ProgramDB.EXTERNAL_FUNCTIONS_ADDED_VERSION) {- // SymbolType.EXTERNAL eliminated with program revision 17 (switch to LABEL)+ // SymbolType.EXTERNAL eliminated with program revision 17processOldExternalTypes(monitor);}@@ -522,8 +522,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {"' and set its new source to DEFAULT.";throw new IllegalArgumentException(msg);}- lock.acquire();- try {+ try (Closeable c = lock.write()) {long oldKey = symbol.getKey();Address address = symbol.getAddress();symbolRemoved(symbol, address, symbol.getName(), oldKey, Namespace.GLOBAL_NAMESPACE_ID,@@ -539,9 +538,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {dbError(e);}- finally {- lock.release();- }}SymbolDB getFunctionSymbol(Namespace namespace) {@@ -585,35 +581,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {adapter.updateSymbolRecord(rec);}- private SymbolDB makeSymbol(Address addr, DBRecord record, SymbolType type) {- if (addr == null) {- addr =- addrMap.decodeAddress(record.getLongValue(SymbolDatabaseAdapter.SYMBOL_ADDR_COL));- }- if (type == SymbolType.CLASS) {- return new ClassSymbol(this, cache, record); // uses NO_ADDRESS- }- else if (type == SymbolType.LABEL) {- return new CodeSymbol(this, cache, addr, record); // uses NO_ADDRESS- }- else if (type == SymbolType.NAMESPACE) {- return new NamespaceSymbol(this, cache, record);- }- else if (type == SymbolType.FUNCTION) {- return new FunctionSymbol(this, cache, addr, record);- }- else if (type == SymbolType.LIBRARY) {- return new LibrarySymbol(this, cache, record); // uses NO_ADDRESS- }- else if (type == SymbolType.PARAMETER || type == SymbolType.LOCAL_VAR) {- return new VariableSymbolDB(this, cache, type, variableStorageMgr, addr, record);- }- else if (type == SymbolType.GLOBAL_VAR) {- return new GlobalVariableSymbolDB(this, cache, variableStorageMgr, addr, record);- }- throw new IllegalArgumentException("No symbol type for " + type);- }-@Overridepublic int getNumSymbols() {return adapter.getSymbolCount();@@ -621,8 +588,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {@Overridepublic boolean removeSymbolSpecial(Symbol sym) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (sym.getSymbolType() == SymbolType.FUNCTION) {Address addr = sym.getAddress();Function f = (Function) sym.getObject();@@ -656,9 +622,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {}return sym.delete();}- finally {- lock.release();- }}private Symbol findFirstNonPrimarySymbol(Address address) {@@ -689,51 +652,45 @@ public class SymbolManager implements SymbolTable, ManagerDB {* @return true if the symbol was removed, false otherwise.*/boolean doRemoveSymbol(SymbolDB sym) {- lock.acquire();+ if (sym == null) {+ return false;+ }+ if (sym.getID() > 0) {+ removeChildren(sym);+ }+ long id = sym.getKey();+ long parentId = sym.getParentID();+ SymbolType symType = sym.getSymbolType();try {- if (sym == null) {- return false;- }- if (sym.getID() > 0) {- removeChildren(sym);- }- long id = sym.getKey();- long parentId = sym.getParentID();- SymbolType symType = sym.getSymbolType();- try {- Address address = sym.getAddress();+ Address address = sym.getAddress();// if (address.isVariableAddress()) {// variableStorageMgr.deleteVariableStorage(address);// }- String name = sym.getName();- boolean primary = sym.isPrimary();-- // Remove associated references- refManager.symbolRemoved(sym);-- adapter.removeSymbol(id);- cache.delete(id);- //sym.setInvalid(); // already invalidated by removeObj-- // if any symbols still exist here, then- // make one of these remaining symbols 'primary'- //- if (primary && address.isMemoryAddress()) {- Symbol[] remainingSyms = getSymbols(address);- if (remainingSyms.length > 0 &&- remainingSyms[0].getSource() != SourceType.DEFAULT) {- remainingSyms[remainingSyms.length - 1].setPrimary();- }+ String name = sym.getName();+ boolean primary = sym.isPrimary();++ // Remove associated references+ refManager.symbolRemoved(sym);++ adapter.removeSymbol(id);+ cache.delete(id);+ //sym.setInvalid(); // already invalidated by removeObj++ // if any symbols still exist here, then+ // make one of these remaining symbols 'primary'+ //+ if (primary && address.isMemoryAddress()) {+ Symbol[] remainingSyms = getSymbols(address);+ if (remainingSyms.length > 0 &&+ remainingSyms[0].getSource() != SourceType.DEFAULT) {+ remainingSyms[remainingSyms.length - 1].setPrimary();}- symbolRemoved(sym, address, name, id, parentId, symType);- return true;- }- catch (IOException e) {- program.dbError(e);}+ symbolRemoved(sym, address, name, id, parentId, symType);+ return true;}- finally {- lock.release();+ catch (IOException e) {+ program.dbError(e);}return false;}@@ -757,53 +714,38 @@ public class SymbolManager implements SymbolTable, ManagerDB {if (symbolID == Namespace.GLOBAL_NAMESPACE_ID) {return program.getGlobalNamespace().getSymbol();}- lock.acquire();- try {- SymbolDB s = cache.get(symbolID);- if (s != null) {- return s;- }- try {- DBRecord record = adapter.getSymbolRecord(symbolID);- if (record != null) {- return createCachedSymbol(record);- }- }- catch (IOException e) {- program.dbError(e);- }- try {- Address a = getDynamicAddress(symbolID);- if (a.getAddressSpace().isMemorySpace()) {- s = new CodeSymbol(this, cache, a, symbolID);- return s;- }- }- catch (Exception e) {- // handled below- }- return null;+ SymbolDB symbol = cache.getCachedInstance(symbolID);+ if (symbol != null) {+ return symbol;}- finally {- lock.release();+ // the assumption is that if asking for a symbol by a key that can be+ // interpreted as a dynamic symbol address, we should create a dynamic symbol+ // and add it to the cache.+ Address a = getDynamicAddress(symbolID);+ if (a.getAddressSpace().isMemorySpace()) {+ CodeSymbol s = new CodeSymbol(this, a, null, symbolID);+ cache.add(s);+ return s;}+ return null;}- private SymbolDB getDynamicSymbol(Address addr) {- lock.acquire();- try {- long symbolID = getDynamicSymbolID(addr);- SymbolDB s = cache.get(symbolID);- if (s != null) {- return s;- }- s = new CodeSymbol(this, cache, addr, symbolID);- return s;- }- finally {- lock.release();+ private SymbolDB getDynamicSymbol(Address memoryAddress) {+ long symbolID = getDynamicSymbolID(memoryAddress);++ // retrieve the symbol from the cache without validating or refreshing it. We know+ // if it exists is should+ CodeSymbol symbol = (CodeSymbol) cache.getRaw(symbolID);+ if (symbol != null) {+ // we know if we got here, the symbol is valid regardless of what it thinks+ symbol.setIsValid();+ return symbol;}++ CodeSymbol s = new CodeSymbol(SymbolManager.this, memoryAddress, null, symbolID);+ cache.add(s);+ return s;}boolean hasDynamicSymbol(Address address) {@@ -824,24 +766,19 @@ public class SymbolManager implements SymbolTable, ManagerDB {@Overridepublic SymbolIterator getSymbolsAsIterator(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {RecordIterator iterator = adapter.getSymbols(addr, addr, true);return new SymbolRecordIterator(iterator, true, true);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return new SymbolRecordIterator(new EmptyRecordIterator(), true, true);}@Overridepublic Symbol[] getSymbols(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Field[] symbolIDs = adapter.getSymbolIDs(addr);if (symbolIDs.length == 0) {if (addr.isMemoryAddress() && refManager.hasReferencesTo(addr)) {@@ -871,17 +808,12 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }-return NO_SYMBOLS;}@Overridepublic Symbol[] getUserSymbols(Address addr) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {Field[] symbolIDs = adapter.getSymbolIDs(addr);if (symbolIDs.length == 0) {return NO_SYMBOLS;@@ -896,9 +828,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return NO_SYMBOLS;}@@ -915,19 +844,15 @@ public class SymbolManager implements SymbolTable, ManagerDB {long namespaceId = namespace.getID();- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBRecord record = adapter.getSymbolRecord(address, name, namespaceId);if (record != null) {- return getSymbol(record);+ return cache.getCachedInstance(record);}}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }// check for default external symbolif (namespace.isExternal() && SymbolUtilities.isPossibleDefaultExternalName(name)) {@@ -975,17 +900,13 @@ public class SymbolManager implements SymbolTable, ManagerDB {@Overridepublic LibrarySymbol getLibrarySymbol(String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {for (Symbol s : getSymbols(name)) {if (s.getSymbolType() == SymbolType.LIBRARY) {return (LibrarySymbol) s;}}}- finally {- lock.release();- }return null;}@@ -1012,19 +933,15 @@ public class SymbolManager implements SymbolTable, ManagerDB {return searchNamespaceForSymbols(name, namespace);}- lock.acquire();- try {+ try (Closeable c = lock.read()) {RecordIterator it = adapter.getSymbolsByNameAndNamespace(name, namespace.getID());while (it.hasNext()) {- list.add(getSymbol(it.next()));+ list.add(cache.getCachedInstance(it.next()));}}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }// also check if the given name could be a default symbolSymbol symbol = getSymbolForDynamicName(name);@@ -1090,11 +1007,10 @@ public class SymbolManager implements SymbolTable, ManagerDB {return searchNamespaceForFirstSymbol(name, namespace, test);}- lock.acquire();- try {+ try (Closeable c = lock.read()) {RecordIterator it = adapter.getSymbolsByNameAndNamespace(name, namespace.getID());while (it.hasNext()) {- SymbolDB symbol = getSymbol(it.next());+ SymbolDB symbol = cache.getCachedInstance(it.next());if (test.test(symbol)) {return symbol;}@@ -1109,9 +1025,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1126,8 +1039,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {if (library != null) {checkValidNamespaceArgument(library);}- lock.acquire();- try {+ try (Closeable c = lock.read()) {long matchLibraryId = library != null ? library.getID() : -1;RecordIterator recordIter = adapter.getExternalSymbolsByOriginalImportName(extLabel);return new SymbolRecordConstraintIterator(recordIter, rec -> {@@ -1147,9 +1059,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {program.dbError(e);return null; // will not occur}- finally {- lock.release();- }}/**@@ -1166,8 +1075,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {if (library != null) {checkValidNamespaceArgument(library);}- lock.acquire();- try {+ try (Closeable c = lock.read()) {long matchLibraryId = library != null ? library.getID() : -1;RecordIterator recordIter = adapter.getExternalSymbolsByMemoryAddress(extProgAddr);return new SymbolRecordConstraintIterator(recordIter, rec -> {@@ -1187,9 +1095,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {program.dbError(e);return null; // will not occur}- finally {- lock.release();- }}@Override@@ -1227,8 +1132,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {@Overridepublic SymbolIterator getSymbols(String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {SymbolIterator symIter = new SymbolNameRecordIterator(name);if (!symIter.hasNext()) {Symbol symbol = getSymbolForDynamicName(name);@@ -1239,24 +1143,17 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@Overridepublic SymbolIterator scanSymbolsByName(String startName) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {return new SymbolNameScanningIterator(startName);}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1270,11 +1167,10 @@ public class SymbolManager implements SymbolTable, ManagerDB {return symbols.length > 0 ? symbols[0] : null;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBRecord record = adapter.getPrimarySymbol(addr);if (record != null) {- return getSymbol(record);+ return cache.getCachedInstance(record);}if (addr.isMemoryAddress() && refManager.hasReferencesTo(addr)) {return getDynamicSymbol(addr);@@ -1283,9 +1179,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -1509,14 +1402,10 @@ public class SymbolManager implements SymbolTable, ManagerDB {@Overridepublic void invalidateCache(boolean all) {variableStorageMgr.invalidateCache(all);- lock.acquire();- try {+ try (Closeable c = lock.write()) {cache.invalidate();dynamicSymbolAddressMap.reconcile();}- finally {- lock.release();- }}/**@@ -1528,8 +1417,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {* @param newAddr the new symbol memory address*/public void moveSymbolsAt(Address oldAddr, Address newAddr) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {long oldAddrKey = addrMap.getKey(oldAddr, false);if (oldAddrKey != AddressMap.INVALID_ADDRESS_KEY) {invalidateCache(true);@@ -1540,9 +1428,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }}@Override@@ -1595,14 +1480,13 @@ public class SymbolManager implements SymbolTable, ManagerDB {* @param namespaceID ID of namespace being removed*/public void namespaceRemoved(long namespaceID) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {try {ArrayList<SymbolDB> symbols = new ArrayList<>();RecordIterator iter = adapter.getSymbolsByNamespace(namespaceID);while (iter.hasNext()) {DBRecord rec = iter.next();- symbols.add(getSymbol(rec));+ symbols.add(cache.getCachedInstance(rec));}for (SymbolDB s : symbols) {s.delete();@@ -1612,9 +1496,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {dbError(e);}}- finally {- lock.release();- }}void symbolRenamed(Symbol symbol, String oldName) {@@ -1726,28 +1607,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {}}- private SymbolDB createCachedSymbol(DBRecord record) {- long addr = record.getLongValue(SymbolDatabaseAdapter.SYMBOL_ADDR_COL);- byte typeID = record.getByteValue(SymbolDatabaseAdapter.SYMBOL_TYPE_COL);- SymbolType type = SymbolType.getSymbolType(typeID);- SymbolDB s = makeSymbol(addrMap.decodeAddress(addr), record, type);- return s;- }-- SymbolDB getSymbol(DBRecord record) {- lock.acquire();- try {- SymbolDB s = cache.get(record);- if (s != null) {- return s;- }- return createCachedSymbol(record);- }- finally {- lock.release();- }- }-private class SingleSymbolIterator implements SymbolIterator {Symbol sym;@@ -1885,11 +1744,10 @@ public class SymbolManager implements SymbolTable, ManagerDB {return true;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {while (nextSymbol == null && (forward ? it.hasNext() : it.hasPrevious())) {DBRecord rec = forward ? it.next() : it.previous();- Symbol sym = getSymbol(rec);+ Symbol sym = cache.getCachedInstance(rec);if (includeDefaultThunks || !isDefaultThunk(sym)) {nextSymbol = sym;}@@ -1899,9 +1757,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return false;}@@ -1946,12 +1801,11 @@ public class SymbolManager implements SymbolTable, ManagerDB {return true;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {while (nextSymbol == null && (forward ? it.hasNext() : it.hasPrevious())) {DBRecord rec = forward ? it.next() : it.previous();if (predicate == null || predicate.test(rec)) {- nextSymbol = getSymbol(rec);+ nextSymbol = cache.getCachedInstance(rec);}}return nextSymbol != null;@@ -1959,9 +1813,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return false;}@@ -2059,7 +1910,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {public Symbol next() {if (hasNext()) {try {- return getSymbol(it.next());+ return cache.getCachedInstance(it.next());}catch (IOException e) {dbError(e);@@ -2121,7 +1972,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {private void findNextMatch() {try {while (it.hasNext()) {- Symbol sym = getSymbol(it.next());+ Symbol sym = cache.getCachedInstance(it.next());if (sym.isExternal()) {nextMatch = sym;return;@@ -2248,7 +2099,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {public GhidraClass next() {try {if (iter.hasNext()) {- Symbol s = getSymbol(iter.next());+ Symbol s = cache.getCachedInstance(iter.next());return (GhidraClass) s.getObject();}}@@ -2279,32 +2130,24 @@ public class SymbolManager implements SymbolTable, ManagerDB {@Overridepublic Symbol getExternalSymbol(String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {SymbolIterator it = getExternalSymbols(name);if (it.hasNext()) {return it.next();}return null;}- finally {- lock.release();- }}@Overridepublic SymbolIterator getExternalSymbols(String name) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {SymbolIterator symIter = new ExternalSymbolNameRecordIterator(name);return symIter;}catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }return null;}@@ -2387,8 +2230,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {}public void replaceDataTypes(Map<Long, Long> dataTypeReplacementMap) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {RecordIterator it = adapter.getSymbols();while (it.hasNext()) {DBRecord rec = it.next();@@ -2419,17 +2261,14 @@ public class SymbolManager implements SymbolTable, ManagerDB {if (replacementId != null) {rec.setLongValue(SymbolDatabaseAdapter.SYMBOL_DATATYPE_COL, replacementId);adapter.updateSymbolRecord(rec);- symbolDataChanged(getSymbol(rec));+ symbolDataChanged(cache.getCachedInstance(rec));}}+ cache.invalidate();}catch (IOException e) {dbError(e);}- finally {- cache.invalidate();- lock.release();- }}@Override@@ -2443,8 +2282,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {return;}Set<Address> primaryFixups = new HashSet<>();- lock.acquire();- try {+ try (Closeable c = lock.write()) {invalidateCache(true);Address lastAddress = fromAddr.add(length - 1);AddressRange range = new AddressRangeImpl(fromAddr, lastAddress);@@ -2474,9 +2312,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {// go back and make sure there is a valid primary symbol at touched addressesfixupPrimarySymbols(primaryFixups);}- finally {- lock.release();- }}// This method is specifically for moving symbols in the context of a memory block move@@ -2617,8 +2452,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {public void deleteAddressRange(Address startAddr, Address endAddr, TaskMonitor monitor)throws CancelledException {AddressRange.checkValidRange(startAddr, endAddr);- lock.acquire();- try {+ try (Closeable c = lock.write()) {invalidateCache(true);Set<Address> notDeletedSet = adapter.deleteAddressRange(startAddr, endAddr, monitor);historyAdapter.deleteAddressRange(startAddr, endAddr, addrMap, notDeletedSet, monitor);@@ -2626,9 +2460,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {catch (IOException e) {program.dbError(e);}- finally {- lock.release();- }}public void imageBaseChanged(Address oldBase, Address newBase) {@@ -2780,8 +2611,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {checkValidNamespaceArgument(function);- lock.acquire();- try {+ try (Closeable c = lock.write()) {source = adjustSourceTypeIfNecessary(name, type, source, storage);Address varAddr = variableStorageMgr.getVariableStorageAddress(storage, true);DBRecord record =@@ -2790,8 +2620,9 @@ public class SymbolManager implements SymbolTable, ManagerDB {adapter.updateSymbolRecord(record);VariableSymbolDB newSymbol =- new VariableSymbolDB(this, cache, type, variableStorageMgr, varAddr, record);+ new VariableSymbolDB(this, type, variableStorageMgr, varAddr, record);+ cache.add(newSymbol);symbolAdded(newSymbol);return newSymbol;}@@ -2799,9 +2630,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {program.dbError(e); // will not returnreturn null;}- finally {- lock.release();- }}private SourceType adjustSourceTypeIfNecessary(String name, SymbolType type, SourceType source,@@ -2844,8 +2672,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {return (GhidraClass) namespace;}- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkValidNamespaceArgument(namespace);@@ -2883,17 +2710,13 @@ public class SymbolManager implements SymbolTable, ManagerDB {throw new AssertException("Unexpected exception creating class from namespace: " + e.getMessage(), e);}- finally {- lock.release();- }}@Overridepublic Namespace getOrCreateNameSpace(Namespace parent, String name, SourceType source)throws DuplicateNameException, InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkValidNamespaceArgument(parent);@@ -2913,9 +2736,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {return new NamespaceDB(s, namespaceMgr);}- finally {- lock.release();- }}/**@@ -2934,14 +2754,14 @@ public class SymbolManager implements SymbolTable, ManagerDB {*/public LibrarySymbol createLibrarySymbol(String name, String pathname, SourceType source)throws DuplicateNameException, InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {DBRecord record = doCreateBasicSymbolRecord(name, null, Address.NO_ADDRESS,SymbolType.LIBRARY, false, source, true);LibrarySymbol.setRecordFields(record, pathname);adapter.updateSymbolRecord(record);- LibrarySymbol newSymbol = new LibrarySymbol(this, cache, record);+ LibrarySymbol newSymbol = new LibrarySymbol(this, record);+ cache.add(newSymbol);symbolAdded(newSymbol);return newSymbol;@@ -2950,9 +2770,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {program.dbError(e); // will not returnreturn null;}- finally {- lock.release();- }}/**@@ -2972,13 +2789,13 @@ public class SymbolManager implements SymbolTable, ManagerDB {*/ClassSymbol createClassSymbol(String name, Namespace parent, SourceType source,boolean checkForDuplicates) throws DuplicateNameException, InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {DBRecord record = doCreateBasicSymbolRecord(name, parent, Address.NO_ADDRESS,SymbolType.CLASS, false, source, checkForDuplicates);adapter.updateSymbolRecord(record);- ClassSymbol newSymbol = new ClassSymbol(this, cache, record);+ ClassSymbol newSymbol = new ClassSymbol(this, record);+ cache.add(newSymbol);symbolAdded(newSymbol);return newSymbol;@@ -2987,9 +2804,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {program.dbError(e); // will not returnreturn null;}- finally {- lock.release();- }}/**@@ -3009,13 +2823,13 @@ public class SymbolManager implements SymbolTable, ManagerDB {*/NamespaceSymbol createNamespaceSymbol(String name, Namespace parent, SourceType source,boolean checkForDuplicates) throws DuplicateNameException, InvalidInputException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {DBRecord record = doCreateBasicSymbolRecord(name, parent, Address.NO_ADDRESS,SymbolType.NAMESPACE, false, source, checkForDuplicates);adapter.updateSymbolRecord(record);- NamespaceSymbol newSymbol = new NamespaceSymbol(this, cache, record);+ NamespaceSymbol newSymbol = new NamespaceSymbol(this, record);+ cache.add(newSymbol);symbolAdded(newSymbol);return newSymbol;@@ -3024,9 +2838,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {program.dbError(e); // will not returnreturn null;}- finally {- lock.release();- }}/**@@ -3103,7 +2914,8 @@ public class SymbolManager implements SymbolTable, ManagerDB {MemorySymbol.setExternalFields(record, originalImportName, externalProgramAddress);adapter.updateSymbolRecord(record);- CodeSymbol newSymbol = new CodeSymbol(this, cache, addr, record);+ CodeSymbol newSymbol = new CodeSymbol(this, addr, record, record.getKey());+ cache.add(newSymbol);symbolAdded(newSymbol);return newSymbol;@@ -3144,8 +2956,8 @@ public class SymbolManager implements SymbolTable, ManagerDB {MemorySymbol.setExternalFields(record, originalImportName, externalProgramAddress);adapter.updateSymbolRecord(record);- FunctionSymbol newSymbol = new FunctionSymbol(this, cache, addr, record);-+ FunctionSymbol newSymbol = new FunctionSymbol(this, addr, record);+ cache.add(newSymbol);symbolAdded(newSymbol);return newSymbol;}@@ -3168,8 +2980,7 @@ public class SymbolManager implements SymbolTable, ManagerDB {throw new IllegalArgumentException("Invalid memory address: " + addr);}- lock.acquire();- try {+ try (Closeable c = lock.write()) {namespace = validateNamespace(namespace, addr, SymbolType.LABEL);source = validateSource(source, name, addr, SymbolType.LABEL);name = validateName(name, source);@@ -3197,7 +3008,8 @@ public class SymbolManager implements SymbolTable, ManagerDB {makePrimary, source, false);adapter.updateSymbolRecord(record);- CodeSymbol newSymbol = new CodeSymbol(this, cache, addr, record);+ CodeSymbol newSymbol = new CodeSymbol(this, addr, record, record.getKey());+ cache.add(newSymbol);symbolAdded(newSymbol);return newSymbol;@@ -3209,9 +3021,6 @@ public class SymbolManager implements SymbolTable, ManagerDB {program.dbError(e);return null; // will not occur}- finally {- lock.release();- }}public FunctionSymbol createMemoryFunctionSymbol(Address addr, String name, Namespace namespace,@@ -3261,8 +3070,8 @@ public class SymbolManager implements SymbolTable, ManagerDB {throw new RuntimeException("Unexpected", e); // duplicate was avoided above}- FunctionSymbol newSymbol = new FunctionSymbol(this, cache, addr, record);-+ FunctionSymbol newSymbol = new FunctionSymbol(this, addr, record);+ cache.add(newSymbol);symbolAdded(newSymbol);if (needsPinning) {@@ -3483,4 +3292,62 @@ public class SymbolManager implements SymbolTable, ManagerDB {Symbol newNamespaceSymbol = namespace.getSymbol();return (newNamespaceSymbol == null) || newNamespaceSymbol.isDeleted();}++ Symbol getSymbol(DBRecord rec) {+ return cache.getCachedInstance(rec);+ }++ private class SymbolFactory implements DbFactory<SymbolDB> {++ @Override+ public SymbolDB instantiate(long key) {+ try {+ DBRecord record = adapter.getSymbolRecord(key);+ if (record != null) {+ return instantiate(record);+ }+ }+ catch (IOException e) {+ program.dbError(e);+ }+ return null;+ }++ @Override+ public SymbolDB instantiate(DBRecord record) {+ long addr = record.getLongValue(SymbolDatabaseAdapter.SYMBOL_ADDR_COL);+ byte typeID = record.getByteValue(SymbolDatabaseAdapter.SYMBOL_TYPE_COL);+ SymbolType type = SymbolType.getSymbolType(typeID);+ SymbolDB s = instantiateSymbol(addrMap.decodeAddress(addr), record, type);+ return s;+ }++ private SymbolDB instantiateSymbol(Address addr, DBRecord record, SymbolType type) {+ if (type == SymbolType.CLASS) {+ return new ClassSymbol(SymbolManager.this, record);+ }+ else if (type == SymbolType.LABEL) {+ return new CodeSymbol(SymbolManager.this, addr, record, record.getKey());+ }+ else if (type == SymbolType.NAMESPACE) {+ return new NamespaceSymbol(SymbolManager.this, record);+ }+ else if (type == SymbolType.FUNCTION) {+ return new FunctionSymbol(SymbolManager.this, addr, record);+ }+ else if (type == SymbolType.LIBRARY) {+ return new LibrarySymbol(SymbolManager.this, record);+ }+ else if (type == SymbolType.PARAMETER || type == SymbolType.LOCAL_VAR) {+ return new VariableSymbolDB(SymbolManager.this, type, variableStorageMgr, addr,+ record);+ }+ else if (type == SymbolType.GLOBAL_VAR) {+ return new GlobalVariableSymbolDB(SymbolManager.this, variableStorageMgr, addr,+ record);+ }+ throw new IllegalArgumentException("No symbol type for " + type);+ }+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableStorageManagerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableStorageManagerDB.javaindex 02c52404ae..3963178dbf 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableStorageManagerDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableStorageManagerDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -16,22 +16,19 @@package ghidra.program.database.symbol;import java.io.IOException;-import java.util.List;import db.*;import db.util.ErrorHandler;import ghidra.framework.data.OpenMode;-import ghidra.program.database.DBObjectCache;-import ghidra.program.database.DatabaseObject;+import ghidra.program.database.*;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressSpace;import ghidra.program.model.lang.ProgramArchitecture;import ghidra.program.model.listing.VariableStorage;-import ghidra.program.model.pcode.Varnode;import ghidra.program.util.LanguageTranslator;import ghidra.util.Lock;-import ghidra.util.Msg;+import ghidra.util.Lock.Closeable;import ghidra.util.datastruct.WeakValueHashMap;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -44,9 +41,8 @@ public class VariableStorageManagerDB implements VariableStorageManager {private VariableStorageDBAdapter adapter;- private DBObjectCache<MyVariableStorage> cache = new DBObjectCache<>(256);- private WeakValueHashMap<Long, MyVariableStorage> cacheMap =- new WeakValueHashMap<Long, MyVariableStorage>(256);+ private DbCache<MyVariableStorage> cache;+ private ByHashCache byHashCache = new ByHashCache();/*** Construct a new variable manager.@@ -67,6 +63,7 @@ public class VariableStorageManagerDB implements VariableStorageManager {this.errorHandler = errorHandler;this.lock = lock;adapter = VariableStorageDBAdapter.getAdapter(handle, openMode, addrMap, monitor);+ cache = new DbCache<MyVariableStorage>(new VariableStorageFactory(), lock, 256);}/**@@ -97,50 +94,14 @@ public class VariableStorageManagerDB implements VariableStorageManager {void invalidateCache(boolean all) {cache.invalidate();- cacheMap.clear();+ byHashCache.clear();}- private MyVariableStorage getMyVariableStorage(Address variableAddr) throws IOException {+ private MyVariableStorage getMyVariableStorage(Address variableAddr) {if (!variableAddr.isVariableAddress()) {throw new IllegalArgumentException("Address is not a VariableAddress: " + variableAddr);}- MyVariableStorage varStore = cache.get(variableAddr.getOffset());- if (varStore != null) {- return varStore;- }- DBRecord rec = adapter.getRecord(variableAddr.getOffset());- if (rec == null) {- return null;- }- varStore = new MyVariableStorage(cache, rec);- cacheMap.put(varStore.getLongHash(), varStore);- return varStore;- }-- /**- * Get the list of varnodes associated with the specified variable storage address.- * NOTE: The program architecture and error handler must be set appropriately prior to- * invocation of this method (see {@link #setProgramArchitecture(ProgramArchitecture)}.- * @param variableAddr variable storage address- * @return storage varnode list or null if address unknown- * @throws IOException if a database IO error occurs- */- List<Varnode> getStorageVarnodes(Address variableAddr) throws IOException {- if (!variableAddr.isVariableAddress()) {- throw new IllegalArgumentException();- }- DBRecord rec = adapter.getRecord(variableAddr.getOffset());- if (rec == null) {- return null;- }- try {- return VariableStorage.getVarnodes(arch.getAddressFactory(),- rec.getString(VariableStorageDBAdapter.STORAGE_COL));- }- catch (InvalidInputException e) {- Msg.error(this, "Invalid variable storage: " + e.getMessage());- }- return null;+ return cache.getCachedInstance(variableAddr.getOffset());}/**@@ -172,88 +133,61 @@ public class VariableStorageManagerDB implements VariableStorageManager {@Overridepublic Address getVariableStorageAddress(VariableStorage storage, boolean create)throws IOException {- long hash = storage.getLongHash();- MyVariableStorage myStorage = cacheMap.get(hash);- long key;- if (myStorage != null) {- key = myStorage.getKey();- // verify that cache entry is still valid- if (cache.get(key) == null) {- key = -1;- }- }- else {- key = adapter.findRecordKey(hash);- }-- if (key == -1) {- if (!create) {- return null;- }+ long key = getExistingKey(storage);+ if (key == -1 && create) {// create new storage recordkey = adapter.getNextStorageID();DBRecord rec = VariableStorageDBAdapter.VARIABLE_STORAGE_SCHEMA.createRecord(key);rec.setLongValue(VariableStorageDBAdapter.HASH_COL, storage.getLongHash());rec.setString(VariableStorageDBAdapter.STORAGE_COL, storage.getSerializationString());adapter.updateRecord(rec);+ MyVariableStorage myStorage = new MyVariableStorage(rec);+ cache.add(myStorage);+ byHashCache.add(myStorage);+ }+ return key == -1 ? null : AddressSpace.VARIABLE_SPACE.getAddress(key);+ }- // add to cache- myStorage = new MyVariableStorage(cache, rec);- cacheMap.put(hash, myStorage);+ private long getExistingKey(VariableStorage storage) throws IOException {+ long hash = storage.getLongHash();+ MyVariableStorage myStorage = byHashCache.get(hash);+ if (myStorage != null && myStorage.isValid()) {+ return myStorage.getKey();+ }+ return adapter.findRecordKey(hash);+ }++ private class VariableStorageFactory implements DbFactory<MyVariableStorage> {++ @Override+ public MyVariableStorage instantiate(long key) {+ DBRecord record;+ try {+ record = adapter.getRecord(key);+ return record == null ? null : instantiate(record);+ }+ catch (IOException e) {+ errorHandler.dbError(e);+ return null;+ }+ }++ @Override+ public MyVariableStorage instantiate(DBRecord record) {+ MyVariableStorage storage = new MyVariableStorage(record);+ byHashCache.add(storage);+ return storage;}- return AddressSpace.VARIABLE_SPACE.getAddress(key);}-// void deleteVariableStorage(Address variableAddr) throws IOException {-// cache.removeObj(variableAddr.getOffset());-// adapter.deleteRecord(variableAddr.getOffset());-// }-//-// void setVariableStorage(Address variableAddr, VariableStorage storage) throws IOException {-// if (storage == null || storage.isUnassignedStorage()) {-// deleteVariableStorage(variableAddr);-// return;-// }-// cache.invalidate(variableAddr.getOffset());-// Record rec = adapter.getRecord(variableAddr.getOffset());-// if (rec == null) {-// rec =-// VariableStorageDBAdapter.VARIABLE_STORAGE_SCHEMA.createRecord(variableAddr.getOffset());-// }-// rec.setString(VariableStorageDBAdapter.STORAGE_COL, storage.getSerializationString());-// adapter.updateRecord(rec);-// }-//-// /**-// * Set variable storage based upon unrestricted varnodes (useful for upgrades)-// * @param variableAddr-// * @param varnodes-// * @throws IOException-// */-// void setVariableStorage(Address variableAddr, Varnode... varnodes) throws IOException {-// if (varnodes == null || varnodes.length == 0) {-// deleteVariableStorage(variableAddr);-// return;-// }-// cache.invalidate(variableAddr.getOffset());-// Record rec = adapter.getRecord(variableAddr.getOffset());-// if (rec == null) {-// rec =-// VariableStorageDBAdapter.VARIABLE_STORAGE_SCHEMA.createRecord(variableAddr.getOffset());-// }-// rec.setString(VariableStorageDBAdapter.STORAGE_COL,-// VariableStorage.getSerializationString(varnodes));-// adapter.updateRecord(rec);-// }-- private class MyVariableStorage extends DatabaseObject {+ private class MyVariableStorage extends DbObject {private VariableStorage storage;private DBRecord record;- private MyVariableStorage(DBObjectCache<MyVariableStorage> cache, DBRecord record) {- super(cache, record.getKey());+ private MyVariableStorage(DBRecord record) {+ super(record.getKey());this.record = record;try {storage = VariableStorage.deserialize(arch,@@ -265,14 +199,10 @@ public class VariableStorageManagerDB implements VariableStorageManager {}VariableStorage getVariableStorage() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();return storage;}- finally {- lock.release();- }}long getLongHash() {@@ -281,7 +211,6 @@ public class VariableStorageManagerDB implements VariableStorageManager {@Overrideprotected boolean refresh() {- lock.acquire();try {storage = VariableStorage.BAD_STORAGE;if (record != null) {@@ -303,9 +232,6 @@ public class VariableStorageManagerDB implements VariableStorageManager {catch (IOException e) {errorHandler.dbError(e);}- finally {- lock.release();- }return false;}@@ -327,8 +253,7 @@ public class VariableStorageManagerDB implements VariableStorageManager {monitor.initialize(adapter.getRecordCount());int cnt = 0;- lock.acquire();- try {+ try (Closeable c = lock.write()) {RecordIterator recIter = adapter.getRecords();while (recIter.hasNext()) {monitor.checkCancelled();@@ -349,14 +274,32 @@ public class VariableStorageManagerDB implements VariableStorageManager {continue;}monitor.setProgress(++cnt);+ invalidateCache(true);}}catch (IOException e) {errorHandler.dbError(e);}- finally {- invalidateCache(true);- lock.release();+ }++ /**+ * Cache of MyVariableStorage by their long hash value+ */+ private static class ByHashCache {+ private WeakValueHashMap<Long, MyVariableStorage> cacheMap =+ new WeakValueHashMap<Long, MyVariableStorage>();++ synchronized void add(MyVariableStorage storage) {+ cacheMap.put(storage.getLongHash(), storage);+ }++ synchronized void clear() {+ cacheMap.clear();+ }++ synchronized MyVariableStorage get(long hash) {+ return cacheMap.get(hash);}}+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableSymbolDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableSymbolDB.javaindex 2844e7ddd9..8f69b64309 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableSymbolDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/symbol/VariableSymbolDB.java@@ -18,7 +18,6 @@ package ghidra.program.database.symbol;import java.io.IOException;import db.DBRecord;-import ghidra.program.database.DBObjectCache;import ghidra.program.database.function.FunctionDB;import ghidra.program.model.address.Address;import ghidra.program.model.address.OldGenericNamespaceAddress;@@ -28,7 +27,7 @@ import ghidra.program.model.listing.*;import ghidra.program.model.symbol.*;import ghidra.program.util.ProgramLocation;import ghidra.program.util.VariableNameFieldLocation;-import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.InvalidInputException;import ghidra.util.task.TaskMonitor;@@ -47,15 +46,14 @@ public class VariableSymbolDB extends SymbolDB {/*** Constructs a new VariableSymbol* @param symbolMgr the symbol manager- * @param cache symbol object cache* @param type the symbol type.* @param variableMgr variable storage manager* @param address the address of the symbol (stack address)* @param record the record for the symbol*/- public VariableSymbolDB(SymbolManager symbolMgr, DBObjectCache<SymbolDB> cache, SymbolType type,+ VariableSymbolDB(SymbolManager symbolMgr, SymbolType type,VariableStorageManagerDB variableMgr, Address address, DBRecord record) {- super(symbolMgr, cache, address, record);+ super(symbolMgr, address, record, record.getKey());this.type = type;this.variableMgr = variableMgr;}@@ -67,15 +65,26 @@ public class VariableSymbolDB extends SymbolDB {}public VariableStorage getVariableStorage() {- lock.acquire();- try {- if (!checkIsValid() || variableStorage != null) {- return variableStorage;+ try (Closeable c = lock.read()) {+ refreshIfNeeded();+ VariableStorage local = variableStorage;+ if (local == null) {+ local = computeVariableStorage();}+ return local;+ }+ }++ private synchronized VariableStorage computeVariableStorage() {+ VariableStorage local = variableStorage;+ if (local != null) {+ return local;+ }+ try {if (address instanceof OldGenericNamespaceAddress) {// old use case for upgradetry {- variableStorage = new VariableStorage(symbolMgr.getProgram(),+ local = new VariableStorage(symbolMgr.getProgram(),((OldGenericNamespaceAddress) address).getGlobalAddress(),getDataType().getLength());}@@ -84,9 +93,9 @@ public class VariableSymbolDB extends SymbolDB {}}else {- variableStorage = variableMgr.getVariableStorage(address);- if (variableStorage == null) {- variableStorage = (type != SymbolType.PARAMETER) ? VariableStorage.BAD_STORAGE+ local = variableMgr.getVariableStorage(address);+ if (local == null) {+ local = (type != SymbolType.PARAMETER) ? VariableStorage.BAD_STORAGE: VariableStorage.UNASSIGNED_STORAGE;}}@@ -94,10 +103,8 @@ public class VariableSymbolDB extends SymbolDB {catch (IOException e) {symbolMgr.dbError(e);}- finally {- lock.release();- }- return variableStorage;+ variableStorage = local;+ return local;}@Override@@ -120,9 +127,8 @@ public class VariableSymbolDB extends SymbolDB {@Overridepublic boolean delete() {- lock.acquire();- try {- if (checkIsValid()) {+ try (Closeable c = lock.write()) {+ if (refreshIfNeeded()) {FunctionDB fun = getFunction();if (fun != null) {fun.doDeleteVariable(this);@@ -132,9 +138,6 @@ public class VariableSymbolDB extends SymbolDB {}return false;}- finally {- lock.release();- }}@Override@@ -183,7 +186,7 @@ public class VariableSymbolDB extends SymbolDB {@Overrideprotected String doGetName() {- if (!checkIsValid()) {+ if (!refreshIfNeeded()) {// TODO: SCRreturn "[Invalid VariableSymbol - Deleted!]";}@@ -253,9 +256,7 @@ public class VariableSymbolDB extends SymbolDB {* @param dt data-type*/public void setStorageAndDataType(VariableStorage newStorage, DataType dt) {- Lock myLock = symbolMgr.getLock();- myLock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();long dataTypeID = symbolMgr.getProgram().getDataTypeManager().getResolvedID(dt);@@ -274,9 +275,6 @@ public class VariableSymbolDB extends SymbolDB {catch (IOException e) {symbolMgr.dbError(e);}- finally {- myLock.release();- }}public int getFirstUseOffset() {@@ -306,15 +304,11 @@ public class VariableSymbolDB extends SymbolDB {@Overridepublic Reference[] getReferences(TaskMonitor monitor) {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();ReferenceManager rm = symbolMgr.getReferenceManager();return rm.getReferencesTo(getObject());}- finally {- lock.release();- }}@Override@@ -327,17 +321,14 @@ public class VariableSymbolDB extends SymbolDB {* @return the symbol data*/protected int getVariableOffset() {- lock.acquire();- try {- checkIsValid();+ try (Closeable c = lock.read()) {+ refreshIfNeeded();if (record != null) {return record.getIntValue(SymbolDatabaseAdapter.SYMBOL_VAROFFSET_COL);}return 0;}- finally {- lock.release();- }+}/**@@ -346,8 +337,7 @@ public class VariableSymbolDB extends SymbolDB {* @param offset the value to set as the symbols variable offset.*/public void setVariableOffset(int offset) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();if (record != null) {record.setIntValue(SymbolDatabaseAdapter.SYMBOL_VAROFFSET_COL, offset);@@ -355,17 +345,13 @@ public class VariableSymbolDB extends SymbolDB {symbolMgr.symbolDataChanged(this);}}- finally {- lock.release();- }}/*** {@return variable symbol comment}*/public String getSymbolComment() {- validate(lock);- return record.getString(SymbolDatabaseAdapter.SYMBOL_COMMENT_COL);+ return lock.withRead(() -> record.getString(SymbolDatabaseAdapter.SYMBOL_COMMENT_COL));}/**@@ -373,15 +359,11 @@ public class VariableSymbolDB extends SymbolDB {* @param comment variable comment*/public void setSymbolComment(String comment) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {checkDeleted();record.setString(SymbolDatabaseAdapter.SYMBOL_COMMENT_COL, comment);updateRecord();}- finally {- lock.release();- }}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapDB.javaindex 9bcf0a3dd1..a174e7a55c 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapDB.java@@ -26,6 +26,7 @@ import ghidra.program.database.map.AddressKeyRecordIterator;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.Msg;import ghidra.util.exception.*;import ghidra.util.task.TaskMonitor;@@ -137,18 +138,14 @@ public class AddressRangeMapDB implements DBListener {* @throws DuplicateNameException if there is already range map with that name*/public boolean setName(String newName) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.write()) {String newTableName = RANGE_MAP_TABLE_PREFIX + newName;if (rangeMapTable == null || rangeMapTable.setName(newTableName)) {tableName = newTableName;return true;}+ return false;}- finally {- lock.release();- }- return false;}/**@@ -185,8 +182,7 @@ public class AddressRangeMapDB implements DBListener {if (localValue != null && localValue.first.contains(address)) {return localValue.second;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (rangeMapTable == null) {return null;}@@ -203,9 +199,6 @@ public class AddressRangeMapDB implements DBListener {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }return null;}@@ -224,8 +217,7 @@ public class AddressRangeMapDB implements DBListener {return;}AddressRange.checkValidRange(startAddress, endAddress);- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (value == null && rangeMapTable == null) {return;}@@ -240,9 +232,6 @@ public class AddressRangeMapDB implements DBListener {catch (IOException e) {errHandler.dbError(e);}- finally {- lock.release();- }}/**@@ -261,8 +250,7 @@ public class AddressRangeMapDB implements DBListener {DBHandle tmpDb = null;AddressRangeMapDB tmpMap = null;- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (rangeMapTable == null) {return;}@@ -304,7 +292,6 @@ public class AddressRangeMapDB implements DBListener {// ignore}}- lock.release();}}@@ -326,8 +313,7 @@ public class AddressRangeMapDB implements DBListener {if (isEmpty()) {return set;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (rangeMapTable == null) {return set;}@@ -337,9 +323,6 @@ public class AddressRangeMapDB implements DBListener {}return set;}- finally {- lock.release();- }}/**@@ -353,12 +336,11 @@ public class AddressRangeMapDB implements DBListener {if (isEmpty()) {return set;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (rangeMapTable == null) {return set;}-+RecordIterator it = rangeMapTable.indexIterator(VALUE_COL, value, value, true);while (it.hasNext()) {DBRecord record = it.next();@@ -369,9 +351,6 @@ public class AddressRangeMapDB implements DBListener {catch (IOException e) {dbError(e);}- finally {- lock.release();- }return set;}@@ -435,14 +414,10 @@ public class AddressRangeMapDB implements DBListener {@Overridepublic void dbRestored(DBHandle dbh) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {clearCache();findTable();}- finally {- lock.release();- }}@Override@@ -453,14 +428,10 @@ public class AddressRangeMapDB implements DBListener {@Overridepublic void tableDeleted(DBHandle dbh, Table table) {if (table == rangeMapTable) {- lock.acquire();- try {+ try (Closeable c = lock.write()) {clearCache();rangeMapTable = null;}- finally {- lock.release();- }}}@@ -475,8 +446,7 @@ public class AddressRangeMapDB implements DBListener {* Deletes the database table used to store this range map.*/public void dispose() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {if (rangeMapTable != null) {try {dbHandle.deleteTable(tableName);@@ -488,9 +458,6 @@ public class AddressRangeMapDB implements DBListener {rangeMapTable = null;}}- finally {- lock.release();- }}/**@@ -498,14 +465,10 @@ public class AddressRangeMapDB implements DBListener {* to invalidate our cache and possibly have a wrapping record again.*/public void invalidate() {- lock.acquire();- try {+ try (Closeable c = lock.write()) {clearCache();alreadyCheckedForWrappingRecord = false;}- finally {- lock.release();- }}/**@@ -520,8 +483,7 @@ public class AddressRangeMapDB implements DBListener {if (localValue != null && localValue.first.contains(address)) {return localValue.first;}- lock.acquire();- try {+ try (Closeable c = lock.read()) {// look for a stored value range that contains that addressAddressRange range = findValueRangeContainingAddress(address);if (range == null) {@@ -534,9 +496,6 @@ public class AddressRangeMapDB implements DBListener {dbError(e);return null;}- finally {- lock.release();- }}private AddressRange findValueRangeContainingAddress(Address address) throws IOException {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapIterator.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapIterator.javaindex c33982f770..7c15c651ef 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapIterator.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressRangeMapIterator.java@@ -24,6 +24,7 @@ import ghidra.program.database.map.AddressKeyRecordIterator;import ghidra.program.database.map.AddressMap;import ghidra.program.model.address.*;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;/*** An iterator over ranges that have a defined values in the AddressRangeMapDB@@ -119,8 +120,7 @@ class AddressRangeMapIterator implements AddressRangeIterator {@Overridepublic boolean hasNext() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (expectedModCount != rangeMap.getModCount()) {throw new ConcurrentModificationException();}@@ -140,15 +140,11 @@ class AddressRangeMapIterator implements AddressRangeIterator {}return false;}- finally {- lock.release();- }}@Overridepublic AddressRange next() {- lock.acquire();- try {+ try (Closeable c = lock.read()) {if (expectedModCount != rangeMap.getModCount()) {throw new ConcurrentModificationException();}@@ -173,9 +169,6 @@ class AddressRangeMapIterator implements AddressRangeIterator {rangeMap.dbError(e);return null;}- finally {- lock.release();- }}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressSetPropertyMapDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressSetPropertyMapDB.javaindex 6e77f9b31d..820788587b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressSetPropertyMapDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/util/AddressSetPropertyMapDB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -25,6 +25,7 @@ import ghidra.program.model.address.*;import ghidra.program.model.util.AddressSetPropertyMap;import ghidra.program.util.ProgramEvent;import ghidra.util.Lock;+import ghidra.util.Lock.Closeable;import ghidra.util.exception.CancelledException;import ghidra.util.exception.DuplicateNameException;import ghidra.util.task.TaskMonitor;@@ -50,8 +51,7 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {public static AddressSetPropertyMapDB getPropertyMap(ProgramDB program, String mapName,ErrorHandler errHandler, AddressMap addrMap, Lock lock) {- lock.acquire();- try {+ try (Closeable c = lock.read()) {String tableName = AddressSetPropertyMapDB.TABLE_PREFIX + mapName;DBHandle dbh = program.getDBHandle();@@ -59,16 +59,12 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {return new AddressSetPropertyMapDB(program, mapName, program, addrMap, lock);}}- finally {- lock.release();- }return null;}public static AddressSetPropertyMapDB createPropertyMap(ProgramDB program, String mapName,ErrorHandler errHandler, AddressMap addrMap, Lock lock) throws DuplicateNameException {- lock.acquire();- try {+ try (Closeable c = lock.read()) {DBHandle dbh = program.getDBHandle();String tableName = AddressSetPropertyMapDB.TABLE_PREFIX + mapName;if (dbh.getTable(tableName) != null) {@@ -78,9 +74,6 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {return new AddressSetPropertyMapDB(program, mapName, program, addrMap, lock);}- finally {- lock.release();- }}private AddressSetPropertyMapDB(ProgramDB program, String mapName, ErrorHandler errHandler,@@ -95,27 +88,18 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {@Overridepublic void add(Address startAddr, Address endAddr) {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ checkDeleted();propertyMap.paintRange(startAddr, endAddr, FIELD);program.setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }-}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#add(ghidra.program.model.address.AddressSet)- */@Overridepublic void add(AddressSetView addressSet) {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ checkDeleted();AddressRangeIterator iter = addressSet.getAddressRanges();while (iter.hasNext()) {AddressRange range = iter.next();@@ -123,53 +107,31 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {}program.setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#set(ghidra.program.model.address.AddressSet)- */@Overridepublic void set(AddressSetView addressSet) {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ checkDeleted();clear();add(addressSet);program.setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#remove(ghidra.program.model.address.Address, ghidra.program.model.address.Address)- */@Overridepublic void remove(Address startAddr, Address endAddr) {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ checkDeleted();propertyMap.clearRange(startAddr, endAddr);program.setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }-}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#remove(ghidra.program.model.address.AddressSet)- */@Overridepublic void remove(AddressSetView addressSet) {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ checkDeleted();AddressRangeIterator iter = addressSet.getAddressRanges();while (iter.hasNext()) {AddressRange range = iter.next();@@ -177,95 +139,58 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {}program.setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#getAddressSet()- */@Overridepublic AddressSet getAddressSet() {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.read()) {+ checkDeleted();return propertyMap.getAddressSet();}- finally {- lock.release();- }}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#getAddresses()- */@Overridepublic AddressIterator getAddresses() {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.read()) {+ checkDeleted();if (propertyMap.isEmpty()) {return new EmptyAddressIterator();}AddressSet set = getAddressSet();return set.getAddresses(true);}- finally {- lock.release();- }}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#getAddressRanges()- */@Overridepublic AddressRangeIterator getAddressRanges() {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.read()) {+ checkDeleted();return propertyMap.getAddressRanges();}- finally {- lock.release();- }}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#clear()- */@Overridepublic void clear() {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.write()) {+ checkDeleted();propertyMap.dispose();program.setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_CHANGED, null, mapName);}- finally {- lock.release();- }-}- /* (non-Javadoc)- * @see ghidra.program.model.util.AddressSetPropertyMap#contains(ghidra.program.model.address.Address)- */@Overridepublic boolean contains(Address addr) {- checkDeleted();- lock.acquire();- try {+ try (Closeable c = lock.read()) {+ checkDeleted();return propertyMap.getValue(addr) != null;}- finally {- lock.release();- }}public void delete() {- propertyMap.dispose();- invalid = true;+ try (Closeable c = lock.write()) {+ invalid = true;+ propertyMap.dispose();+ }}/**@@ -273,14 +198,14 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {* @param fromAddr move from address* @param toAddr move to address* @param length number of address to move- * @param monitor- * @throws CancelledException+ * @param monitor task monitor+ * @throws AddressOverflowException address out of bounds+ * @throws CancelledException if cancelled*/public void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)throws AddressOverflowException, CancelledException {- lock.acquire();- try {-+ try (Closeable c = lock.write()) {+ checkDeleted();Address rangeEnd = fromAddr.addNoWrap(length - 1);AddressSet currentSet = new AddressSet();@@ -305,9 +230,6 @@ public class AddressSetPropertyMapDB implements AddressSetPropertyMap {propertyMap.paintRange(startAddr, endAddr, FIELD);}}- finally {- lock.release();- }}private void checkDeleted() {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StandAloneDataTypeManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StandAloneDataTypeManager.javaindex 1326596935..4d3ce8fda8 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StandAloneDataTypeManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StandAloneDataTypeManager.java@@ -578,8 +578,7 @@ public class StandAloneDataTypeManager extends DataTypeManagerDB implements Clos*/public void clearProgramArchitecture(TaskMonitor monitor)throws CancelledException, IOException, LockException {- lock.acquire();- try {+ try (Lock.Closeable c = lock.write()) {if (!isArchitectureChangeAllowed()) {throw new UnsupportedOperationException(@@ -612,10 +611,7 @@ public class StandAloneDataTypeManager extends DataTypeManagerDB implements Clos}defaultListener.programArchitectureChanged(this);- }- finally {invalidateCache();- lock.release();}}@@ -662,8 +658,7 @@ public class StandAloneDataTypeManager extends DataTypeManagerDB implements ClosCancelledException, LockException, UnsupportedOperationException,IncompatibleLanguageException {- lock.acquire();- try {+ try (Lock.Closeable c = lock.write()) {if (!isArchitectureChangeAllowed()) {throw new UnsupportedOperationException(@@ -766,7 +761,6 @@ public class StandAloneDataTypeManager extends DataTypeManagerDB implements Clos}finally {invalidateCache();- lock.release();}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/BookmarkManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/BookmarkManager.javaindex 2908d8a653..298a914986 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/BookmarkManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/BookmarkManager.java@@ -66,15 +66,6 @@ public interface BookmarkManager {*/BookmarkType defineType(String type, Icon icon, Color color, int priority);- /**- * Returns true if the given bookmark type has been defined via a call to- * {@link #defineType(String, Icon, Color, int)}. This allows clients to know whether the- * given type should be displayed.- * @param type the type to check- * @return true if defined- */- boolean isDefinedType(String type);-/*** Returns list of known bookmark types* @return list of known bookmark typesdiff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AbstractStoredProgramContext.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AbstractStoredProgramContext.javaindex 607641a4ee..2c03400dd5 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AbstractStoredProgramContext.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AbstractStoredProgramContext.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -202,22 +202,34 @@ abstract public class AbstractStoredProgramContext extends AbstractProgramContex@Overridepublic Register[] getRegistersWithValues() {- if (registersWithValues == null) {- registersWithValues = new HashSet<>();- for (Register register : language.getRegisters()) {- RegisterValueStore store = registerValueMap.get(register.getBaseRegister());- if (store != null && !store.isEmpty()) {- registersWithValues.add(register);- continue;- }- store = defaultRegisterValueMap.get(register.getBaseRegister());- if (store != null && !store.isEmpty()) {- registersWithValues.add(register);- }+ Set<Register> localRegisterWithValues = registersWithValues;+ if (localRegisterWithValues == null) {+ localRegisterWithValues = computeRegistersWithValues();+ }+ Register[] regs = new Register[localRegisterWithValues.size()];+ return localRegisterWithValues.toArray(regs);+ }++ private synchronized Set<Register> computeRegistersWithValues() {+ Set<Register> localRegisterWithValues = registersWithValues;+ if (localRegisterWithValues != null) {+ return localRegisterWithValues;+ }++ Set<Register> set = new HashSet<>();+ for (Register register : language.getRegisters()) {+ RegisterValueStore store = registerValueMap.get(register.getBaseRegister());+ if (store != null && !store.isEmpty()) {+ set.add(register);+ continue;+ }+ store = defaultRegisterValueMap.get(register.getBaseRegister());+ if (store != null && !store.isEmpty()) {+ set.add(register);}}- Register[] regs = new Register[registersWithValues.size()];- return registersWithValues.toArray(regs);+ registersWithValues = set;+ return set;}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.javaindex 5463a55946..4fb4b4d999 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.java@@ -15,7 +15,8 @@*/package ghidra.program.util;-import java.util.*;+import java.util.ArrayList;+import java.util.List;import ghidra.program.model.address.*;import ghidra.program.model.lang.Register;@@ -124,9 +125,10 @@ public class RegisterValueStore {rangeMap.checkWritableState();}synchronized (this) {- rangeWriteCacheValue = newValue;- rangeWriteCacheMin = start;- rangeWriteCacheMax = end;+ doSetValue(start, end, newValue);+// rangeWriteCacheValue = newValue;+// rangeWriteCacheMin = start;+// rangeWriteCacheMax = end;}return;}