Skip to content

Commit

Permalink
Code cleanup and updated version
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatino committed Jul 20, 2016
1 parent 9684541 commit fa0dddf
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'maven'

group = 'com.beaudoin.jmm'
version = '1.5'
version = '1.6'

repositories {
mavenCentral()
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/beaudoin/jmm/misc/Cacheable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.beaudoin.jmm.misc;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.ptr.IntByReference;

import java.util.HashMap;
Expand All @@ -34,16 +35,17 @@
public final class Cacheable {

private static final Map<Integer, MemoryBuffer> bufferCache = new HashMap<>();
private static final Function<Integer, MemoryBuffer> butterCreate = MemoryBuffer::new;
private static final Function<Integer, MemoryBuffer> bufferCreate = MemoryBuffer::new;

private static final Map<Integer, byte[]> arrayCache = new HashMap<>();
private static final Function<Integer, byte[]> arrayCreate = byte[]::new;

private static final Pointer cachedPointer = new Pointer(0);
public static final IntByReference INT_BY_REF = new IntByReference();
public static final WinDef.DWORD DWORD_ZERO = new WinDef.DWORD();

public static MemoryBuffer buffer(int size) {
return bufferCache.computeIfAbsent(size, butterCreate);
return bufferCache.computeIfAbsent(size, bufferCreate);
}

public static byte[] array(int size) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/beaudoin/jmm/misc/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@
*/
public final class Strings {

private static Map<Long, String> map = new HashMap<>(16_982);
private static Map<Long, String> stringCache = new HashMap<>(16_982);

public static String transform(byte[] bytes) {
long hash = LongHashFunction.xx_r39().hashBytes(bytes);
if (map.containsKey(hash)) {
return map.get(hash);
if (stringCache.containsKey(hash)) {
return stringCache.get(hash);
}
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == 0) {
bytes[i] = 32;
}
}
String string = new String(bytes).split(" ")[0].trim().intern();
map.put(hash, string);
stringCache.put(hash, string);
return string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public int id() {

@Override
public Module findModule(String moduleName) {
Module module = modules.get(moduleName);
Module module = modules.isEmpty() ? null : modules.get(moduleName);
if (module == null) {
initModules();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/beaudoin/jmm/process/DataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ default double readDouble(long address) {
}

default String readString(long address, int length) {
byte[] bytes = Cacheable.array(length);
byte[] bytes = new byte[length];
read(address, bytes.length).get(bytes);
return Strings.transform(bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public final class Win32Process extends AbstractProcess {
public Win32Process(int id, Pointer handle) {
super(id);
this.handle = handle;
initModules();
}

public Pointer pointer() {
Expand All @@ -59,7 +58,7 @@ public void initModules() {
try {
while (Kernel32.Module32NextW(snapshot, entry)) {
String name = entry.szModule();
modules.putIfAbsent(name, new Module(this, name, entry.getPointer(), entry.modBaseSize.intValue()));
modules.putIfAbsent(name, new Module(this, name, entry.hModule.getPointer(), entry.modBaseSize.intValue()));
}
} finally {
Kernel32.CloseHandle(snapshot);
Expand Down

0 comments on commit fa0dddf

Please sign in to comment.