GP-2040: Remove garbage from AsyncUtils.

This commit is contained in:
Dan 2025-03-04 17:09:06 +00:00
parent 3d7089d391
commit 922c4d0186
40 changed files with 271 additions and 5277 deletions

View file

@ -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,15 @@
*/
package ghidra.program.model.address;
import java.util.Iterator;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* A class to break a range of addresses into 'chunks' of a give size. This is useful to
* break-up processing of large swaths of addresses, such as when performing work in a
* background thread. Doing this allows the client to iterator over the range, pausing
* enough to allow the UI to update.
* A class to break a range of addresses into 'chunks' of a give size. This is useful to break-up
* processing of large swaths of addresses, such as when performing work in a background thread.
* Doing this allows the client to iterator over the range, pausing enough to allow the UI to
* update.
*/
public class AddressRangeChunker implements Iterable<AddressRange> {
@ -106,4 +108,22 @@ public class AddressRangeChunker implements Iterable<AddressRange> {
};
}
@Override
public Spliterator<AddressRange> spliterator() {
long countAddrs = end.subtract(nextStartAddress) + 1;
long size = Long.divideUnsigned(countAddrs + chunkSize - 1, chunkSize);
return Spliterators.spliterator(iterator(), size,
Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SORTED |
Spliterator.SIZED);
}
/**
* Stream the chunks
*
* @return the stream
*/
public Stream<AddressRange> stream() {
return StreamSupport.stream(spliterator(), false);
}
}

View file

@ -17,7 +17,9 @@
package ghidra.program.model.address;
import java.util.Iterator;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* Defines a read-only interface for an address set.
@ -33,13 +35,13 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Test if the given address range is contained in this set.
* The specified start and end addresses must form a valid range within
* a single {@link AddressSpace}.
* <p>
* The specified start and end addresses must form a valid range within a single
* {@link AddressSpace}.
*
* @param start the first address in the range.
* @param end the last address in the range.
* @return true if entire range is contained within the set,
* false otherwise.
* @return true if entire range is contained within the set, false otherwise.
*/
public boolean contains(Address start, Address end);
@ -47,8 +49,7 @@ public interface AddressSetView extends Iterable<AddressRange> {
* Test if the given address set is a subset of this set.
*
* @param rangeSet the set to test.
* @return true if the entire set is contained within this set,
* false otherwise.
* @return true if the entire set is contained within this set, false otherwise.
*/
public boolean contains(AddressSetView rangeSet);
@ -59,8 +60,9 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Get the minimum address for this address set.
* NOTE: An {@link AddressRange} should generally not be formed using this address
* and {@link #getMaxAddress()} since it may span multiple {@link AddressSpace}s.
* <p>
* NOTE: An {@link AddressRange} should generally not be formed using this address and
* {@link #getMaxAddress()} since it may span multiple {@link AddressSpace}s.
*
* @return the minimum address for this set. Returns null if the set is empty.
*/
@ -68,8 +70,9 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Get the maximum address for this address set.
* NOTE: An {@link AddressRange} should generally not be formed using this address
* and {@link #getMaxAddress()} since it may span multiple {@link AddressSpace}s.
* <p>
* NOTE: An {@link AddressRange} should generally not be formed using this address and
* {@link #getMaxAddress()} since it may span multiple {@link AddressSpace}s.
*
* @return the maximum address for this set. Returns null if the set is empty.
*/
@ -87,17 +90,21 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Returns an iterator over the ranges in the specified order
* @param forward the ranges are returned from lowest to highest, otherwise from
* highest to lowest
*
* @param forward the ranges are returned from lowest to highest, otherwise from highest to
* lowest
* @return an iterator over all the addresse ranges in the set.
*/
public AddressRangeIterator getAddressRanges(boolean forward);
/**
* Returns an iterator of address ranges starting with the range that contains the given address.
* If there is no range containing the start address, then the first range will be
* the next range greater than the start address if going forward, otherwise the range less than
* the start address
* Returns an iterator of address ranges starting with the range that contains the given
* address.
* <p>
* If there is no range containing the start address, then the first range will be the next
* range greater than the start address if going forward, otherwise the range less than the
* start address
*
* @param start the address the first range should contain.
* @param forward true iterators forward, false backwards
* @return the AddressRange iterator
@ -110,25 +117,91 @@ public interface AddressSetView extends Iterable<AddressRange> {
@Override
public Iterator<AddressRange> iterator();
@Override
default Spliterator<AddressRange> spliterator() {
return Spliterators.spliterator(iterator(), getNumAddressRanges(),
Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SORTED |
Spliterator.SIZED);
}
/**
* Stream the ranges in this set
*
* @return the stream
*/
default Stream<AddressRange> stream() {
return StreamSupport.stream(spliterator(), false);
}
/**
* Returns an iterator over the ranges in the specified order
* @param forward the ranges are returned from lowest to highest, otherwise from
* highest to lowest
* @return an iterator over all the addresse ranges in the set.
*
* @param forward the ranges are returned from lowest to highest, otherwise from highest to
* lowest
* @return an iterator over all the address ranges in the set.
*/
public Iterator<AddressRange> iterator(boolean forward);
/**
* Returns an iterator of address ranges starting with the range that contains the given address.
* If there is no range containing the start address, then the first range will be
* the next range greater than the start address if going forward, otherwise the range less than
* the start address
* Create a spliterator over the ranges, as in {@link #iterator(boolean)}
*
* @param forward true to traverse lowest to highest, false for reverse
* @return a spliterator over all the address ranges in the set.
*/
default Spliterator<AddressRange> spliterator(boolean forward) {
return Spliterators.spliterator(iterator(forward), getNumAddressRanges(),
Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SORTED |
Spliterator.SIZED);
}
/**
* Stream the ranges in the set forward or backward
*
* @param forward true to stream lowest to highest, false for reverse
* @return a stream over all the address ranges in the set.
*/
default Stream<AddressRange> stream(boolean forward) {
return StreamSupport.stream(spliterator(forward), false);
}
/**
* Returns an iterator of address ranges starting with the range that contains the given
* address.
* <p>
* If there is no range containing the start address, then the first range will be the next
* range greater than the start address if going forward, otherwise the range less than the
* start address
*
* @param start the address that the first range should contain.
* @param forward true iterators forward, false backwards
* @return the AddressRange iterator
*/
public Iterator<AddressRange> iterator(Address start, boolean forward);
/**
* Create a spliterator over the ranges, as in {@link #iterator(boolean)}
*
* @param start the address that the first range should contain.
* @param forward true to traverse lowest to highest, false for reverse
* @return a spliterator over the address ranges.
*/
default Spliterator<AddressRange> spliterator(Address start, boolean forward) {
return Spliterators.spliterator(iterator(start, forward), getNumAddressRanges(),
Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SORTED |
Spliterator.SIZED);
}
/**
* Stream the ranges in the set as in {@link #iterator(Address, boolean)}
*
* @param start the address that the first range should contain.
* @param forward true to stream lowest to highest, false for reverse
* @return a stream over the address ranges.
*/
default Stream<AddressRange> stream(Address start, boolean forward) {
return StreamSupport.stream(spliterator(start, forward), false);
}
/**
* @return the number of addresses in this set.
*/
@ -136,19 +209,19 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Returns an iterator over all addresses in this set.
* @param forward if true the address are return in increasing order, otherwise in
* decreasing order.
*
* @param forward if true the address are return in increasing order, otherwise in decreasing
* order.
* @return an iterator over all addresses in this set.
*/
public AddressIterator getAddresses(boolean forward);
/**
* Returns an iterator over the addresses in this address set
* starting at the start address
* Returns an iterator over the addresses in this address set starting at the start address
*
* @param start address to start iterating at in the address set
* @param forward if true address are return from lowest to highest, else from highest to lowest
* @return an iterator over the addresses in this address set
* starting at the start address
* @return an iterator over the addresses in this address set starting at the start address
*/
public AddressIterator getAddresses(Address start, boolean forward);
@ -162,8 +235,10 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Determine if the start and end range intersects with the specified address set.
* The specified start and end addresses must form a valid range within
* a single {@link AddressSpace}.
* <p>
* The specified start and end addresses must form a valid range within a single
* {@link AddressSpace}.
*
* @param start start of range
* @param end end of range
* @return true if the given range intersects this address set.
@ -172,75 +247,86 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Computes the intersection of this address set with the given address set.
* <p>
* This method does not modify this address set.
*
* @param view the address set to intersect with.
* @return AddressSet a new address set that contains all addresses that are
* contained in both this set and the given set.
* @return AddressSet a new address set that contains all addresses that are contained in both
* this set and the given set.
*/
public AddressSet intersect(AddressSetView view);
/**
* Computes the intersection of this address set with the given address range.
* This method does not modify this address set.
* The specified start and end addresses must form a valid range within
* a single {@link AddressSpace}.
* <p>
* This method does not modify this address set. The specified start and end addresses must form
* a valid range within a single {@link AddressSpace}.
*
* @param start start of range
* @param end end of range
* @return AddressSet a new address set that contains all addresses that are
* contained in both this set and the given range.
* @return AddressSet a new address set that contains all addresses that are contained in both
* this set and the given range.
*/
public AddressSet intersectRange(Address start, Address end);
/**
* Computes the union of this address set with the given address set. This
* method does not change this address set.
* Computes the union of this address set with the given address set.
* <p>
* This method does not change this address set.
*
* @param addrSet The address set to be unioned with this address set.
* @return AddressSet A new address set which contains all the addresses
* from both this set and the given set.
* @return AddressSet A new address set which contains all the addresses from both this set and
* the given set.
*/
public AddressSet union(AddressSetView addrSet);
/**
* Computes the difference of this address set with the given address set
* (this - set). Note that this is not the same as (set - this). This
* method does not change this address set.
* Computes the difference of this address set with the given address set (this - set).
* <p>
* Note that this is not the same as (set - this). This method does not change this address set.
*
* @param addrSet the set to subtract from this set.
* @return AddressSet a new address set which contains all the addresses
* that are in this set, but not in the given set.
* @return AddressSet a new address set which contains all the addresses that are in this set,
* but not in the given set.
*/
public AddressSet subtract(AddressSetView addrSet);
/**
* Computes the exclusive-or of this address set with the given set. This
* method does not modify this address set.
* Computes the exclusive-or of this address set with the given set.
* <p>
* This method does not modify this address set.
*
* @param addrSet address set to exclusive-or with.
* @return AddressSet a new address set containing all addresses that are in
* either this set or the given set, but not in both sets
* @return AddressSet a new address set containing all addresses that are in either this set or
* the given set, but not in both sets
*/
public AddressSet xor(AddressSetView addrSet);
/**
* Returns true if the given address set contains the same set of addresses
* as this set.
* Returns true if the given address set contains the same set of addresses as this set.
*
* @param view the address set to compare.
* @return true if the given set contains the same addresses as this set.
*/
public boolean hasSameAddresses(AddressSetView view);
/**
* Returns the first range in this set or null if the set is empty;
* @return the first range in this set or null if the set is empty;
* Returns the first range in this set or null if the set is empty
*
* @return the first range in this set or null if the set is empty
*/
public AddressRange getFirstRange();
/**
* Returns the last range in this set or null if the set is empty;
* @return the last range in this set or null if the set is empty;
* Returns the last range in this set or null if the set is empty
*
* @return the last range in this set or null if the set is empty
*/
public AddressRange getLastRange();
/**
* Returns the range that contains the given address
*
* @param address the address for which to find a range.
* @return the range that contains the given address.
*/
@ -248,6 +334,7 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Finds the first address in this collection that is also in the given addressSet.
*
* @param set the addressSet to search for the first (lowest) common address.
* @return the first address that is contained in this set and the given set.
*/
@ -255,6 +342,7 @@ public interface AddressSetView extends Iterable<AddressRange> {
/**
* Returns the number of address in this address set before the given address.
*
* @param address the address after the last address to be counted
* @return the number of address in this address set before the given address
*/
@ -274,12 +362,13 @@ public interface AddressSetView extends Iterable<AddressRange> {
}
/**
* Trim address set removing all addresses less-than-or-equal to specified
* address based upon {@link Address} comparison.
* The address set may contain address ranges from multiple
* address spaces.
* Trim address set removing all addresses less-than-or-equal to specified address based upon
* {@link Address} comparison.
* <p>
* The address set may contain address ranges from multiple address spaces.
*
* @param set address set to be trimmed
* @param addr trim point. Only addresses greater than this address will be returned.
* @param addr trim point. Only addresses greater than this address will be returned.
* @return trimmed address set view
*/
public static AddressSetView trimStart(AddressSetView set, Address addr) {
@ -301,12 +390,13 @@ public interface AddressSetView extends Iterable<AddressRange> {
}
/**
* Trim address set removing all addresses greater-than-or-equal to specified
* address based upon {@link Address} comparison.
* The address set may contain address ranges from multiple
* address spaces.
* Trim address set removing all addresses greater-than-or-equal to specified address based upon
* {@link Address} comparison.
* <p>
* The address set may contain address ranges from multiple address spaces.
*
* @param set address set to be trimmed
* @param addr trim point. Only addresses less than this address will be returned.
* @param addr trim point. Only addresses less than this address will be returned.
* @return trimmed address set view
*/
public static AddressSetView trimEnd(AddressSetView set, Address addr) {