List of usage examples for com.google.common.primitives Ints MAX_POWER_OF_TWO
int MAX_POWER_OF_TWO
To view the source code for com.google.common.primitives Ints MAX_POWER_OF_TWO.
Click Source Link
From source file:org.summer.dsl.xbase.typesystem.util.Maps2.java
/** * Copied from {@link Maps#capacity(int)}. *//*from w w w. j a v a 2 s . c o m*/ private static int capacity(int expectedSize) { if (expectedSize < 3) { Preconditions.checkArgument(expectedSize >= 0); return expectedSize + 1; } if (expectedSize < Ints.MAX_POWER_OF_TWO) { return expectedSize + expectedSize / 3; } return Integer.MAX_VALUE; // any large value }
From source file:com.facebook.buck.cxx.Machos.java
static void relativizeOsoSymbols(FileChannel file, ImmutableCollection<Path> cellRoots) throws IOException, MachoException { for (Path root : cellRoots) { Preconditions.checkState(root.isAbsolute()); }// w ww. j av a 2 s . c o m long size = file.size(); MappedByteBuffer map = file.map(FileChannel.MapMode.READ_WRITE, 0, size); MachoHeader header = getHeader(map); int symbolTableOffset = 0; int symbolTableCount = 0; int stringTableOffset = 0; int stringTableSizePosition = 0; int stringTableSize = 0; boolean symbolTableSegmentFound = false; int segmentSizePosition = 0; int segmentSize = 0; int commandsCount = header.getCommandsCount(); for (int i = 0; i < commandsCount; i++) { int commandStart = map.position(); // NOPMD int command = ObjectFileScrubbers.getLittleEndianInt(map); int commandSize = ObjectFileScrubbers.getLittleEndianInt(map); // NOPMD switch (command) { case LC_SYMTAB: symbolTableOffset = ObjectFileScrubbers.getLittleEndianInt(map); symbolTableCount = ObjectFileScrubbers.getLittleEndianInt(map); stringTableOffset = ObjectFileScrubbers.getLittleEndianInt(map); stringTableSizePosition = map.position(); stringTableSize = ObjectFileScrubbers.getLittleEndianInt(map); symbolTableSegmentFound = true; break; case LC_SEGMENT: /* segment name */ ObjectFileScrubbers.getBytes(map, 16); /* vm address */ ObjectFileScrubbers.getLittleEndianInt(map); /* vm size */ ObjectFileScrubbers.getLittleEndianInt(map); int segmentFileOffset = ObjectFileScrubbers.getLittleEndianInt(map); int segmentFileSizePosition = map.position(); int segmentFileSize = ObjectFileScrubbers.getLittleEndianInt(map); /* maximum vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* initial vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* number of sections */ ObjectFileScrubbers.getLittleEndianInt(map); /* flags */ ObjectFileScrubbers.getLittleEndianInt(map); if (segmentFileOffset + segmentFileSize == size) { if (segmentSizePosition != 0) { throw new MachoException("multiple map segment commands map string table"); } segmentSizePosition = segmentFileSizePosition; segmentSize = segmentFileSize; } break; case LC_SEGMENT_64: /* segment name */ ObjectFileScrubbers.getBytes(map, 16); /* vm address */ ObjectFileScrubbers.getLittleEndianLong(map); /* vm size */ ObjectFileScrubbers.getLittleEndianLong(map); long segment64FileOffset = ObjectFileScrubbers.getLittleEndianLong(map); int segment64FileSizePosition = map.position(); long segment64FileSize = ObjectFileScrubbers.getLittleEndianLong(map); /* maximum vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* initial vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* number of sections */ ObjectFileScrubbers.getLittleEndianInt(map); /* flags */ ObjectFileScrubbers.getLittleEndianInt(map); if (segment64FileOffset + segment64FileSize == size) { if (segmentSizePosition != 0) { throw new MachoException("multiple map segment commands map string table"); } segmentSizePosition = segment64FileSizePosition; if (segment64FileSize > Ints.MAX_POWER_OF_TWO) { throw new MachoException("map segment file size too big"); } segmentSize = (int) segment64FileSize; } break; } map.position(commandStart + commandSize); } if (!symbolTableSegmentFound) { throw new MachoException("LC_SYMTAB command not found"); } if (stringTableOffset + stringTableSize != size) { throw new MachoException("String table does not end at end of file"); } if (stringTableSize == 0) { return; } if (segmentSizePosition == 0 || segmentSize == 0) { throw new MachoException("LC_SEGMENT or LC_SEGMENT_64 command for string table not found"); } map.position(stringTableOffset); if (map.get() != 0x20) { throw new MachoException("First character in the string table is not a space"); } if (map.get() != 0x00) { throw new MachoException("Second character in the string table is not a NUL"); } int currentStringTableOffset = map.position(); byte[] stringTableBytes = new byte[stringTableSize]; map.position(stringTableOffset); map.get(stringTableBytes); ByteBuffer stringTable = ByteBuffer.wrap(stringTableBytes); map.position(symbolTableOffset); Map<Integer, Integer> strings = new HashMap<>(); for (int i = 0; i < symbolTableCount; i++) { int stringTableIndexPosition = map.position(); int stringTableIndex = ObjectFileScrubbers.getLittleEndianInt(map); byte type = map.get(); /* section */ map.get(); /* description */ ObjectFileScrubbers.getLittleEndianShort(map); int valuePosition = map.position(); if (header.getIs64Bit()) { /* value */ ObjectFileScrubbers.getLittleEndianLong(map); } else { /* value */ ObjectFileScrubbers.getLittleEndianInt(map); } if (stringTableIndex < 2) { continue; } int position = map.position(); try { int newStringTableIndex; if (strings.containsKey(stringTableIndex)) { newStringTableIndex = strings.get(stringTableIndex); } else { stringTable.position(stringTableIndex); String string = ObjectFileScrubbers.getAsciiString(stringTable); if (type == N_OSO) { for (Path root : cellRoots) { String rootPrefix = root + "/"; Optional<String> fixed = MoreStrings.stripPrefix(string, rootPrefix) .map(input -> "./" + input); if (fixed.isPresent()) { string = fixed.get(); break; } } map.position(valuePosition); int lastModifiedValue = ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP; if (header.getIs64Bit()) { ObjectFileScrubbers.putLittleEndianLong(map, lastModifiedValue); } else { ObjectFileScrubbers.putLittleEndianInt(map, lastModifiedValue); } } map.position(currentStringTableOffset); ObjectFileScrubbers.putAsciiString(map, string); newStringTableIndex = currentStringTableOffset - stringTableOffset; currentStringTableOffset = map.position(); strings.put(stringTableIndex, newStringTableIndex); } map.position(stringTableIndexPosition); ObjectFileScrubbers.putLittleEndianInt(map, newStringTableIndex); } finally { map.position(position); } } map.position(stringTableSizePosition); int newStringTableSize = currentStringTableOffset - stringTableOffset; ObjectFileScrubbers.putLittleEndianInt(map, newStringTableSize); map.position(segmentSizePosition); ObjectFileScrubbers.putLittleEndianInt(map, segmentSize + (newStringTableSize - stringTableSize)); file.truncate(currentStringTableOffset); }
From source file:com.facebook.buck.cxx.toolchain.objectfile.Machos.java
/** * Relativize paths in OSO entries./* www . ja v a 2 s . c o m*/ * * <p>OSO entries point to other files containing debug information. These are generated by the * linker as absolute paths. */ static void relativizeOsoSymbols(FileChannel file, ImmutableMap<Path, Path> cellRoots) throws IOException, MachoException { cellRoots.forEach((from, to) -> { Preconditions.checkArgument(from.isAbsolute()); Preconditions.checkArgument(!to.isAbsolute()); }); long size = file.size(); MappedByteBuffer map = file.map(FileChannel.MapMode.READ_WRITE, 0, size); MachoHeader header = getHeader(map); int symbolTableOffset = 0; int symbolTableCount = 0; int stringTableOffset = 0; int stringTableSizePosition = 0; int stringTableSize = 0; boolean symbolTableSegmentFound = false; int segmentSizePosition = 0; int segmentSize = 0; boolean linkEditSegmentFound = false; int segmentFileSizePosition = 0; int segmentFileSize = 0; int segment64FileSizePosition = 0; long segment64FileSize = 0; int commandsCount = header.getCommandsCount(); for (int i = 0; i < commandsCount; i++) { int commandStart = map.position(); // NOPMD int command = ObjectFileScrubbers.getLittleEndianInt(map); int commandSize = ObjectFileScrubbers.getLittleEndianInt(map); // NOPMD switch (command) { case LC_SYMTAB: symbolTableOffset = ObjectFileScrubbers.getLittleEndianInt(map); symbolTableCount = ObjectFileScrubbers.getLittleEndianInt(map); stringTableOffset = ObjectFileScrubbers.getLittleEndianInt(map); stringTableSizePosition = map.position(); stringTableSize = ObjectFileScrubbers.getLittleEndianInt(map); symbolTableSegmentFound = true; break; case LC_SEGMENT: byte[] segmentNameBytes = ObjectFileScrubbers.getBytes(map, 16); String segmentName = new String(segmentNameBytes, Charsets.US_ASCII); if (segmentName.startsWith(LINKEDIT)) { linkEditSegmentFound = true; /* vm address */ ObjectFileScrubbers.getLittleEndianInt(map); /* vm size */ ObjectFileScrubbers.getLittleEndianInt(map); /* segment file offset */ ObjectFileScrubbers.getLittleEndianInt(map); segmentFileSizePosition = map.position(); segmentFileSize = ObjectFileScrubbers.getLittleEndianInt(map); /* maximum vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* initial vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* number of sections */ ObjectFileScrubbers.getLittleEndianInt(map); /* flags */ ObjectFileScrubbers.getLittleEndianInt(map); if (segmentSizePosition != 0) { throw new MachoException("multiple map segment commands map string table"); } segmentSizePosition = segmentFileSizePosition; segmentSize = segmentFileSize; } break; case LC_SEGMENT_64: byte[] segment64NameBytes = ObjectFileScrubbers.getBytes(map, 16); String segment64Name = new String(segment64NameBytes, Charsets.US_ASCII); if (segment64Name.startsWith(LINKEDIT)) { linkEditSegmentFound = true; /* vm address */ ObjectFileScrubbers.getLittleEndianLong(map); /* vm size */ ObjectFileScrubbers.getLittleEndianLong(map); /* segment file offset */ ObjectFileScrubbers.getLittleEndianLong(map); segment64FileSizePosition = map.position(); segment64FileSize = ObjectFileScrubbers.getLittleEndianLong(map); /* maximum vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* initial vm protection */ ObjectFileScrubbers.getLittleEndianInt(map); /* number of sections */ ObjectFileScrubbers.getLittleEndianInt(map); /* flags */ ObjectFileScrubbers.getLittleEndianInt(map); if (segmentSizePosition != 0) { throw new MachoException("multiple map segment commands map string table"); } segmentSizePosition = segment64FileSizePosition; if (segment64FileSize > Ints.MAX_POWER_OF_TWO) { throw new MachoException("map segment file size too big"); } segmentSize = (int) segment64FileSize; } break; } map.position(commandStart + commandSize); } if (!linkEditSegmentFound) { /*The OSO entries are identified in segments named __LINKEDIT. If no segment is found with that name, there is nothing to scrub.*/ return; } if (!symbolTableSegmentFound) { throw new MachoException("LC_SYMTAB command not found"); } if (stringTableOffset + stringTableSize != size) { throw new MachoException("String table does not end at end of file"); } if (stringTableSize == 0) { return; } if (segmentSizePosition == 0 || segmentSize == 0) { throw new MachoException("LC_SEGMENT or LC_SEGMENT_64 command for string table not found"); } map.position(stringTableOffset); if (map.get() != 0x20) { throw new MachoException("First character in the string table is not a space"); } if (map.get() != 0x00) { throw new MachoException("Second character in the string table is not a NUL"); } int currentStringTableOffset = map.position(); byte[] stringTableBytes = new byte[stringTableSize]; map.position(stringTableOffset); map.get(stringTableBytes); ByteBuffer stringTable = ByteBuffer.wrap(stringTableBytes); map.position(symbolTableOffset); Map<Integer, Integer> strings = new HashMap<>(); for (int i = 0; i < symbolTableCount; i++) { int stringTableIndexPosition = map.position(); int stringTableIndex = ObjectFileScrubbers.getLittleEndianInt(map); byte type = map.get(); /* section */ map.get(); /* description */ ObjectFileScrubbers.getLittleEndianShort(map); int valuePosition = map.position(); if (header.getIs64Bit()) { /* value */ ObjectFileScrubbers.getLittleEndianLong(map); } else { /* value */ ObjectFileScrubbers.getLittleEndianInt(map); } if (stringTableIndex < 2) { continue; } int position = map.position(); try { int newStringTableIndex; if (strings.containsKey(stringTableIndex)) { newStringTableIndex = strings.get(stringTableIndex); } else { stringTable.position(stringTableIndex); String string = ObjectFileScrubbers.getAsciiString(stringTable); if (type == N_OSO) { for (Map.Entry<Path, Path> root : cellRoots.entrySet()) { String rootPrefix = root.getKey() + "/"; if (string.startsWith(rootPrefix)) { String replacementPrefix = root.getValue().toString(); if (replacementPrefix.equals("")) { replacementPrefix = "."; } string = replacementPrefix + "/" + string.substring(rootPrefix.length()); } } map.position(valuePosition); int lastModifiedValue = ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP; if (header.getIs64Bit()) { ObjectFileScrubbers.putLittleEndianLong(map, lastModifiedValue); } else { ObjectFileScrubbers.putLittleEndianInt(map, lastModifiedValue); } } map.position(currentStringTableOffset); ObjectFileScrubbers.putAsciiString(map, string); newStringTableIndex = currentStringTableOffset - stringTableOffset; currentStringTableOffset = map.position(); strings.put(stringTableIndex, newStringTableIndex); } map.position(stringTableIndexPosition); ObjectFileScrubbers.putLittleEndianInt(map, newStringTableIndex); } finally { map.position(position); } } map.position(stringTableSizePosition); int newStringTableSize = currentStringTableOffset - stringTableOffset; ObjectFileScrubbers.putLittleEndianInt(map, newStringTableSize); map.position(segmentSizePosition); ObjectFileScrubbers.putLittleEndianInt(map, segmentSize + (newStringTableSize - stringTableSize)); file.truncate(currentStringTableOffset); }
From source file:com.google.template.soy.jbcsrc.restricted.BytecodeUtils.java
private static int hashMapCapacity(int expectedSize) { if (expectedSize < 3) { return expectedSize + 1; }/* w ww . j av a2 s. c o m*/ if (expectedSize < Ints.MAX_POWER_OF_TWO) { // This is the calculation used in JDK8 to resize when a putAll // happens; it seems to be the most conservative calculation we // can make. 0.75 is the default load factor. return (int) (expectedSize / 0.75F + 1.0F); } return Integer.MAX_VALUE; // any large value }