performance improvement when fetching memory (using System.arraycopy instead of earlier for-loop)

+ code formatting (causing a lot of cvs diffs)
This commit is contained in:
fros4943 2006-08-23 15:48:15 +00:00
parent 5d6358b602
commit 52c14b8a4e

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: SectionMoteMemory.java,v 1.1 2006/08/21 12:12:56 fros4943 Exp $ * $Id: SectionMoteMemory.java,v 1.2 2006/08/23 15:48:15 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -39,13 +39,13 @@ import org.apache.log4j.Logger;
import se.sics.cooja.MoteMemory; import se.sics.cooja.MoteMemory;
/** /**
* Represents a mote memory consisting of non-overlapping * Represents a mote memory consisting of non-overlapping memory sections. This
* memory sections. This memory also contains information * memory also contains information about variable starts addresses.
* about variable starts addresses.
* <p> * <p>
* When an unhandled memory segment is set a new section is * When an unhandled memory segment is set a new section is automatically
* automatically created for this segment. * created for this segment.
* <p> * <p>
*
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public class SectionMoteMemory implements MoteMemory { public class SectionMoteMemory implements MoteMemory {
@ -55,10 +55,11 @@ public class SectionMoteMemory implements MoteMemory {
private final Properties variableAddresses; private final Properties variableAddresses;
/** /**
* Create a new mote memory with information about which * Create a new mote memory with information about which variables exist and
* variables exist and their relative memory addresses. * their relative memory addresses.
* *
* @param variableAddresses Variable addresses * @param variableAddresses
* Variable addresses
*/ */
public SectionMoteMemory(Properties variableAddresses) { public SectionMoteMemory(Properties variableAddresses) {
this.variableAddresses = variableAddresses; this.variableAddresses = variableAddresses;
@ -81,9 +82,10 @@ public class SectionMoteMemory implements MoteMemory {
} }
public byte[] getMemorySegment(int address, int size) { public byte[] getMemorySegment(int address, int size) {
for (int i=0; i < sections.size(); i++) { for (MoteMemorySection section : sections) {
if (sections.elementAt(i).includesAddr(address) && sections.elementAt(i).includesAddr(address + size - 1)) { if (section.includesAddr(address)
return sections.elementAt(i).getMemorySegment(address, size); && section.includesAddr(address + size - 1)) {
return section.getMemorySegment(address, size);
} }
} }
return null; return null;
@ -91,9 +93,10 @@ public class SectionMoteMemory implements MoteMemory {
public void setMemorySegment(int address, byte[] data) { public void setMemorySegment(int address, byte[] data) {
// TODO Creating overlapping memory sections possible // TODO Creating overlapping memory sections possible
for (int i=0; i < sections.size(); i++) { for (MoteMemorySection section : sections) {
if (sections.elementAt(i).includesAddr(address) && sections.elementAt(i).includesAddr(address + data.length - 1)) { if (section.includesAddr(address)
sections.elementAt(i).setMemorySegment(address, data); && section.includesAddr(address + data.length - 1)) {
section.setMemorySegment(address, data);
return; return;
} }
} }
@ -117,29 +120,31 @@ public class SectionMoteMemory implements MoteMemory {
} }
/** /**
* Removes a memory segment from this memory. * Removes a memory segment from this memory. The section containing the
* The section containing the segment may be split into two sections. * segment may be split into two sections.
* *
* @param startAddr Start address * @param startAddr
* @param size Length * Start address
* @param size
* Length
*/ */
public void removeSegmentFromMemory(int startAddr, int size) { public void removeSegmentFromMemory(int startAddr, int size) {
for (MoteMemorySection section : sections) for (MoteMemorySection section : sections)
// Find section containing segment to remove // Find section containing segment to remove
if (section.includesAddr(startAddr) && if (section.includesAddr(startAddr)
section.includesAddr(startAddr + size - 1)) { && section.includesAddr(startAddr + size - 1)) {
MoteMemorySection oldSection = section; MoteMemorySection oldSection = section;
byte[] dataFirstPart = oldSection.getMemorySegment( byte[] dataFirstPart = oldSection.getMemorySegment(
oldSection.startAddr, oldSection.startAddr, (int) (startAddr - oldSection.startAddr));
(int) (startAddr - oldSection.startAddr) byte[] dataSecondPart = oldSection
); .getMemorySegment(startAddr + size, (int) (oldSection.startAddr
byte[] dataSecondPart = oldSection.getMemorySegment( + oldSection.getSize() - (startAddr + size)));
startAddr + size,
(int) (oldSection.startAddr + oldSection.getSize() - (startAddr + size)));
MoteMemorySection newSectionFirstPart = new MoteMemorySection(oldSection.startAddr, dataFirstPart); MoteMemorySection newSectionFirstPart = new MoteMemorySection(
MoteMemorySection newSectionSecondPart = new MoteMemorySection(startAddr + size, dataSecondPart); oldSection.startAddr, dataFirstPart);
MoteMemorySection newSectionSecondPart = new MoteMemorySection(
startAddr + size, dataSecondPart);
// Remove old section, add new sections // Remove old section, add new sections
sections.remove(oldSection); sections.remove(oldSection);
@ -150,11 +155,11 @@ public class SectionMoteMemory implements MoteMemory {
} }
} }
/** /**
* Get start address of section at given position. * Get start address of section at given position.
* *
* @param sectionNr Section position * @param sectionNr
* Section position
* @return Start address of section * @return Start address of section
*/ */
public int getStartAddrOfSection(int sectionNr) { public int getStartAddrOfSection(int sectionNr) {
@ -167,7 +172,8 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Get size of section at given position. * Get size of section at given position.
* *
* @param sectionNr Section position * @param sectionNr
* Section position
* @return Size of section * @return Size of section
*/ */
public int getSizeOfSection(int sectionNr) { public int getSizeOfSection(int sectionNr) {
@ -180,7 +186,8 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Get data of section at given position. * Get data of section at given position.
* *
* @param sectionNr Section position * @param sectionNr
* Section position
* @return Data at section * @return Data at section
*/ */
public byte[] getDataOfSection(int sectionNr) { public byte[] getDataOfSection(int sectionNr) {
@ -193,7 +200,8 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Returns a value of the integer variable with the given name. * Returns a value of the integer variable with the given name.
* *
* @param varName Name of integer variable * @param varName
* Name of integer variable
* @return Value of integer variable * @return Value of integer variable
*/ */
public int getIntValueOf(String varName) { public int getIntValueOf(String varName) {
@ -220,8 +228,10 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Set integer value of variable with given name. * Set integer value of variable with given name.
* *
* @param varName Name of integer variable * @param varName
* @param newVal New value to set * Name of integer variable
* @param newVal
* New value to set
*/ */
public void setIntValueOf(String varName, int newVal) { public void setIntValueOf(String varName, int newVal) {
// Get start address of variable // Get start address of variable
@ -247,7 +257,8 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Returns a value of the byte variable with the given name. * Returns a value of the byte variable with the given name.
* *
* @param varName Name of byte variable * @param varName
* Name of byte variable
* @return Value of byte variable * @return Value of byte variable
*/ */
public byte getByteValueOf(String varName) { public byte getByteValueOf(String varName) {
@ -264,8 +275,10 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Set byte value of variable with given name. * Set byte value of variable with given name.
* *
* @param varName Name of byte variable * @param varName
* @param newVal New value to set * Name of byte variable
* @param newVal
* New value to set
*/ */
public void setByteValueOf(String varName, byte newVal) { public void setByteValueOf(String varName, byte newVal) {
// Get start address of variable // Get start address of variable
@ -283,8 +296,10 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Returns byte array of given length and with the given name. * Returns byte array of given length and with the given name.
* *
* @param varName Name of array * @param varName
* @param length Length of array * Name of array
* @param length
* Length of array
* @return Data of array * @return Data of array
*/ */
public byte[] getByteArray(String varName, int length) { public byte[] getByteArray(String varName, int length) {
@ -300,8 +315,10 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Set byte array of the variable with the given name. * Set byte array of the variable with the given name.
* *
* @param varName Name of array * @param varName
* @param data New data of array * Name of array
* @param data
* New data of array
*/ */
public void setByteArray(String varName, byte[] data) { public void setByteArray(String varName, byte[] data) {
// Get start address of variable // Get start address of variable
@ -325,8 +342,10 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Create a new memory section. * Create a new memory section.
* *
* @param startAddr Start address of section * @param startAddr
* @param data Data of section * Start address of section
* @param data
* Data of section
*/ */
public MoteMemorySection(int startAddr, byte[] data) { public MoteMemorySection(int startAddr, byte[] data) {
this.startAddr = startAddr; this.startAddr = startAddr;
@ -363,8 +382,10 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* True if given address is part of this memory section. * True if given address is part of this memory section.
* *
* @param addr Address * @param addr
* @return True if given address is part of this memory section, false otherwise * Address
* @return True if given address is part of this memory section, false
* otherwise
*/ */
public boolean includesAddr(int addr) { public boolean includesAddr(int addr) {
if (data == null) if (data == null)
@ -376,34 +397,33 @@ public class SectionMoteMemory implements MoteMemory {
/** /**
* Returns memory segment. * Returns memory segment.
* *
* @param addr Start address of memory segment * @param addr
* @param size Size of memory segment * Start address of memory segment
* @param size
* Size of memory segment
* @return Memory segment * @return Memory segment
*/ */
public byte[] getMemorySegment(int addr, int size) { public byte[] getMemorySegment(int addr, int size) {
byte[] ret = new byte[size]; byte[] ret = new byte[size];
for (int i = 0; i < size; i++) System.arraycopy(data, addr - startAddr, ret, 0, size);
ret[i] = data[(int) (addr - startAddr + i)];
return ret; return ret;
} }
/** /**
* Sets a memory segment. * Sets a memory segment.
* @param addr Start of memory segment *
* @param data Data of memory segment * @param addr
* Start of memory segment
* @param data
* Data of memory segment
*/ */
public void setMemorySegment(int addr, byte[] data) { public void setMemorySegment(int addr, byte[] data) {
int nonnull=0; System.arraycopy(data, 0, this.data, addr - startAddr, data.length);
for (int i = 0; i < data.length; i++) {
if (data[i] != 0) nonnull++;
this.data[(int) (addr - startAddr + i)] = data[i];
}
} }
public MoteMemorySection clone() { public MoteMemorySection clone() {
byte[] dataClone = new byte[data.length]; byte[] dataClone = new byte[data.length];
for (int i=0; i < data.length; i++) System.arraycopy(data, 0, dataClone, 0, data.length);
dataClone[i] = data[i];
MoteMemorySection clone = new MoteMemorySection(startAddr, dataClone); MoteMemorySection clone = new MoteMemorySection(startAddr, dataClone);
return clone; return clone;
@ -411,12 +431,11 @@ public class SectionMoteMemory implements MoteMemory {
} }
// EXPERIMENTAL AND DEBUG METHODS // EXPERIMENTAL AND DEBUG METHODS
public SectionMoteMemory clone() { public SectionMoteMemory clone() {
Vector<MoteMemorySection> clonedSections = new Vector<MoteMemorySection>(); Vector<MoteMemorySection> clonedSections = new Vector<MoteMemorySection>();
for (int i=0; i < sections.size(); i++) { for (MoteMemorySection section : sections) {
clonedSections.add((MoteMemorySection) sections.elementAt(i).clone()); clonedSections.add(section.clone());
} }
SectionMoteMemory clone = new SectionMoteMemory(variableAddresses); SectionMoteMemory clone = new SectionMoteMemory(variableAddresses);
@ -430,8 +449,8 @@ public class SectionMoteMemory implements MoteMemory {
try { try {
messageDigest = MessageDigest.getInstance("MD5"); messageDigest = MessageDigest.getInstance("MD5");
for (int i=0; i < sections.size(); i++) { for (MoteMemorySection section : sections) {
messageDigest.update(sections.get(i).data, 0, sections.get(i).getSize()); messageDigest.update(section.data, 0, section.getSize());
} }
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
return null; return null;
@ -439,7 +458,8 @@ public class SectionMoteMemory implements MoteMemory {
return messageDigest.digest(); return messageDigest.digest();
} }
protected Vector<Integer> getDifferenceAddressesOf(SectionMoteMemory anotherMem) { protected Vector<Integer> getDifferenceAddressesOf(
SectionMoteMemory anotherMem) {
Vector<Integer> differences = new Vector<Integer>(); Vector<Integer> differences = new Vector<Integer>();
if (this.getNumberOfSections() != anotherMem.getNumberOfSections()) { if (this.getNumberOfSections() != anotherMem.getNumberOfSections()) {
@ -507,6 +527,4 @@ public class SectionMoteMemory implements MoteMemory {
System.err.println(); System.err.println();
} }
} }