Skip to content

Commit

Permalink
ros2: extract HostProcessValue from HostProcessPointer
Browse files Browse the repository at this point in the history
Move the generic parts to a new HostProcessValue class. Add
pointer-specific methods to HostProcessPointer, such as #isNullptr().

Signed-off-by: Christophe Bedard <bedard.christophe@gmail.com>
Signed-off-by: Christophe Bedard <christophe.bedard@apex.ai>
Change-Id: I18e5c092da9863c4d276f8295b423774f73ac619
Reviewed-on: https://git.eclipse.org/r/c/tracecompass.incubator/org.eclipse.tracecompass.incubator/+/202311
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
  • Loading branch information
christophebedard committed Jun 2, 2023
1 parent caf0f1e commit ed82369
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 63 deletions.
Expand Up @@ -15,8 +15,6 @@
import org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader;
import org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter;

import com.google.common.base.Objects;

/**
* Unique identifier for a pointer or memory address value on a host. Combines
* the {@link HostProcess} and the pointer to avoid collisions when analyzing
Expand All @@ -26,13 +24,10 @@
*
* @author Christophe Bedard
*/
public class HostProcessPointer {
public class HostProcessPointer extends HostProcessValue<@NonNull Long> {

private static final @NonNull String STRING_ID_SEP = "|"; //$NON-NLS-1$
private static final @NonNull String HEX_PREFIX = "0x"; //$NON-NLS-1$

private final @NonNull HostProcess fHostProcess;
private final @NonNull Long fPointer;
private final int fSerializedValueSize;

/**
Expand All @@ -44,81 +39,54 @@ public class HostProcessPointer {
* the pointer/memory address value
*/
public HostProcessPointer(@NonNull HostProcess hostProcess, @NonNull Long pointer) {
fHostProcess = hostProcess;
fPointer = pointer;
super(hostProcess, pointer);

int size = 0;
size += fHostProcess.getSerializedValueSize();
size += super.getSerializedValueSize();
size += Long.BYTES;
fSerializedValueSize = size;
}

/**
* @return the host process
*/
public HostProcess getHostProcess() {
return fHostProcess;
}

/**
* @return the PID
* @return the pointer value
*/
public @NonNull Long getPid() {
return fHostProcess.getPid();
public @NonNull Long getPointer() {
return super.getValue();
}

/**
* @return the pointer value
* @return whether the pointer is <code>nullptr</code>, i.e., 0
*/
public @NonNull Long getPointer() {
return fPointer;
public boolean isNullptr() {
return 0 == getPointer();
}

@Override
public int hashCode() {
return Objects.hashCode(fHostProcess, fPointer);
protected @NonNull String valueToString() {
return HEX_PREFIX + Long.toHexString(getPointer());
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
HostProcessPointer o = (HostProcessPointer) obj;
return o.fHostProcess.equals(fHostProcess) && o.fPointer.equals(fPointer);
public @NonNull String toString() {
return String.format("HostProcessPointer: pointer=%s", super.toString()); //$NON-NLS-1$
}

/**
* @return the string ID to uniquely represent this pointer
* @param buffer
* the buffer
*/
public @NonNull String getStringId() {
return String.format(
"%s%s%s%d%s%s", //$NON-NLS-1$
HEX_PREFIX, Long.toHexString(getPointer()), STRING_ID_SEP, getPid(), STRING_ID_SEP, getHostProcess().getHostId().getId());
}

@Override
public @NonNull String toString() {
return String.format(
"HostProcessPointer: pointer=0x%s, pid=%d, hostId=[%s]", //$NON-NLS-1$
Long.toHexString(getPointer()), getPid(), getHostProcess().getHostId().toString());
public void serializeValue(@NonNull ISafeByteBufferWriter buffer) {
super.serializeValue(buffer);
buffer.putLong(getPointer());
}

/**
* Serialize this state value into the byte buffer.
*
* @param buffer
* the buffer
* @return the serialized size
*/
public void serializeValue(@NonNull ISafeByteBufferWriter buffer) {
fHostProcess.serializeValue(buffer);
buffer.putLong(fPointer);
@Override
public int getSerializedValueSize() {
return fSerializedValueSize;
}

/**
Expand All @@ -131,11 +99,4 @@ public void serializeValue(@NonNull ISafeByteBufferWriter buffer) {
Long pointer = buffer.getLong();
return new HostProcessPointer(hostProcess, pointer);
}

/**
* @return the serialized size
*/
public int getSerializedValueSize() {
return fSerializedValueSize;
}
}
@@ -0,0 +1,133 @@
/**********************************************************************
* Copyright (c) 2023 École Polytechnique de Montréal, Apex.AI, Inc.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

package org.eclipse.tracecompass.incubator.internal.ros2.core.model;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter;

import com.google.common.base.Objects;

/**
* Unique identifier that combines a value and a {@link HostProcess}, i.e., some
* value + process ID + host ID.
*
* @author Christophe Bedard
* @param <T>
* the type of the wrapped value
*/
public abstract class HostProcessValue<@NonNull T> {

private static final @NonNull String STRING_ID_SEP = "|"; //$NON-NLS-1$

private final @NonNull HostProcess fHostProcess;
private final @NonNull T fValue;
private final int fSerializedValueSize;

/**
* Constructor
*
* @param hostProcess
* the host process object
* @param value
* the value
*/
public HostProcessValue(@NonNull HostProcess hostProcess, @NonNull T value) {
fHostProcess = hostProcess;
fValue = value;

// Concrete classes will compute the value's size properly
fSerializedValueSize = fHostProcess.getSerializedValueSize();
}

/**
* @return the host process
*/
public @NonNull HostProcess getHostProcess() {
return fHostProcess;
}

/**
* @return the PID
*/
public @NonNull Long getPid() {
return fHostProcess.getPid();
}

/**
* Concrete classes will provide their own appropriately-named getter
* method.
*
* @return the value
*/
protected @NonNull T getValue() {
return fValue;
}

/**
* @return the value as a string
*/
protected abstract @NonNull String valueToString();

@Override
public int hashCode() {
return Objects.hashCode(fHostProcess, fValue);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
/**
* Compare attributes directly; do not compare object classes. We want
* two objects of different classes that extend HostProcessValue to be
* equal if they have the same HostProcess and value.
*/
HostProcessValue<?> o = (HostProcessValue<?>) obj;
return o.fHostProcess.equals(fHostProcess) && o.fValue.equals(fValue);
}

/**
* @return the string ID to uniquely represent this pointer
*/
public @NonNull String getStringId() {
return String.format(
"%s%s%d%s%s", //$NON-NLS-1$
valueToString(), STRING_ID_SEP, getPid(), STRING_ID_SEP, getHostProcess().getHostId().getId());
}

@Override
public @NonNull String toString() {
return String.format(
"%s, pid=%d, hostId=[%s]", //$NON-NLS-1$
valueToString(), getPid(), getHostProcess().getHostId().toString());
}

/**
* @param buffer
* the buffer
*/
public void serializeValue(@NonNull ISafeByteBufferWriter buffer) {
fHostProcess.serializeValue(buffer);
// Concrete classes will serialize the value properly
}

/**
* @return the serialized size
*/
public int getSerializedValueSize() {
return fSerializedValueSize;
}
}
Expand Up @@ -13,7 +13,7 @@

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter;
import org.eclipse.tracecompass.incubator.internal.ros2.core.model.HostProcessPointer;
import org.eclipse.tracecompass.incubator.internal.ros2.core.model.HostProcessValue;
import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.CustomStateValue;
import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;

Expand All @@ -27,7 +27,7 @@
* the type of the object's handle object
*/
@SuppressWarnings("restriction")
public abstract class Ros2Object<@NonNull T extends HostProcessPointer> extends CustomStateValue {
public abstract class Ros2Object<@NonNull T extends HostProcessValue<?>> extends CustomStateValue {

private final @NonNull T fHandle;

Expand Down

0 comments on commit ed82369

Please sign in to comment.