private final static int MAX_PROBES = 5;
/**
- * A cycling index (0 to MAX_PROBES-1) used to replace elements in the cache.
+ * A cycling index (0 to MAX_PROBES-1) used to replace elements in
+ * the cache.
*/
private static int replacementIndex = 0;
* Array of PlatformAddress references kept from garbage collection.
*/
private static PlatformAddress[] cache = new PlatformAddress[CACHE_SIZE];
- // END android-added
+ /**
+ * Constructs a {@code PlatformAddress} or returns
+ * {@link PlatformAddress#NULL} if given a {@code null} address.
+ *
+ * @param address the start address for the memory; {@code 0} means
+ * {@code null}
+ * @param size the size of the memory in bytes
+ * @return an appropriately-constructed {@code PlatformAddress}
+ */
+ private static PlatformAddress make(int value, long size) {
+ if (value == 0) {
+ return PlatformAddress.NULL;
+ }
+
+ return new PlatformAddress(value, size);
+ }
+ // END android-added
// BEGIN android-changed
public synchronized static PlatformAddress on(int value, long size) {
*/
public static PlatformAddress alloc(int size) {
int osAddress = PlatformAddress.osMemory.malloc(size);
- PlatformAddress newMemory = on(osAddress, size);
+ // BEGIN android-changed
+ /*
+ * We use make() and not on() here, for a couple reasons:
+ * First and foremost, doing so means that if the client uses
+ * address.autoFree() (to enable auto-free on gc) the cache
+ * won't prevent the freeing behavior. Second, this avoids
+ * polluting the cache with addresses that aren't likely to be
+ * reused anyway.
+ */
+ PlatformAddress newMemory = make(osAddress, size);
+ // END android-changed
PlatformAddress.memorySpy.alloc(newMemory);
return newMemory;
}
public static PlatformAddress alloc(int size, byte init) {
int osAddress = PlatformAddress.osMemory.malloc(size);
PlatformAddress.osMemory.memset(osAddress, init, size);
- PlatformAddress newMemory = on(osAddress, size);
+ // BEGIN android-changed
+ // See above for the make() vs. on() rationale.
+ PlatformAddress newMemory = make(osAddress, size);
+ // END android-changed
PlatformAddress.memorySpy.alloc(newMemory);
return newMemory;
}