switch BpfBitmap key from U32 to S32
Since this is an array from 32 bit int to 64 bit int,
the S32/U32 distinction doesn't matter before we hit
a bitmap with 2 billion entries, ie. 128 billion bits,
taking up 16GiB of RAM...
But S32 is a native java int, while U32 requires long.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5b06e80e801e007bafb18693647d968cba905c36
diff --git a/staticlibs/device/com/android/net/module/util/BpfBitmap.java b/staticlibs/device/com/android/net/module/util/BpfBitmap.java
index 0c8bb37..d2a5b65 100644
--- a/staticlibs/device/com/android/net/module/util/BpfBitmap.java
+++ b/staticlibs/device/com/android/net/module/util/BpfBitmap.java
@@ -27,7 +27,7 @@
*
*/
public class BpfBitmap {
- private BpfMap<Struct.U32, Struct.S64> mBpfMap;
+ private BpfMap<Struct.S32, Struct.S64> mBpfMap;
/**
* Create a BpfBitmap map wrapper with "path" of filesystem.
@@ -35,8 +35,8 @@
* @param path The path of the BPF map.
*/
public BpfBitmap(@NonNull String path) throws ErrnoException {
- mBpfMap = new BpfMap<Struct.U32, Struct.S64>(path, BpfMap.BPF_F_RDWR,
- Struct.U32.class, Struct.S64.class);
+ mBpfMap = new BpfMap<Struct.S32, Struct.S64>(path, BpfMap.BPF_F_RDWR,
+ Struct.S32.class, Struct.S64.class);
}
/**
@@ -44,7 +44,7 @@
*
* @param key The key in the map corresponding to the value to return.
*/
- private long getBpfMapValue(Struct.U32 key) throws ErrnoException {
+ private long getBpfMapValue(Struct.S32 key) throws ErrnoException {
Struct.S64 curVal = mBpfMap.getValue(key);
if (curVal != null) {
return curVal.val;
@@ -61,7 +61,7 @@
public boolean get(int index) throws ErrnoException {
if (index < 0) return false;
- Struct.U32 key = new Struct.U32(index >> 6);
+ Struct.S32 key = new Struct.S32(index >> 6);
return ((getBpfMapValue(key) >>> (index & 63)) & 1L) != 0;
}
@@ -92,7 +92,7 @@
public void set(int index, boolean set) throws ErrnoException {
if (index < 0) throw new IllegalArgumentException("Index out of bounds.");
- Struct.U32 key = new Struct.U32(index >> 6);
+ Struct.S32 key = new Struct.S32(index >> 6);
long mask = (1L << (index & 63));
long val = getBpfMapValue(key);
if (set) val |= mask; else val &= ~mask;
@@ -114,7 +114,7 @@
* Checks if all bitmap values are 0.
*/
public boolean isEmpty() throws ErrnoException {
- Struct.U32 key = mBpfMap.getFirstKey();
+ Struct.S32 key = mBpfMap.getFirstKey();
while (key != null) {
if (getBpfMapValue(key) != 0) {
return false;