Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaprasad-basavaraj committed Aug 29, 2022
1 parent 250b875 commit caa1b0f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/cluster_copier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ namespace grpc_labview {

for (int x = 0; x < count; ++x)
{
auto data = (LVCluster**)(*array)->bytes(nestedMetadata->clusterSize * x, nestedMetadata->alignmentRequirement);
auto data = (LVCluster*)(*array)->bytes(nestedMetadata->clusterSize * x, nestedMetadata->alignmentRequirement);
auto nested = std::make_shared<LVMessage>(nestedMetadata);
repeatedValue->_value.push_back(nested);
CopyFromCluster(*nested, (int8_t*)data);
Expand Down
6 changes: 4 additions & 2 deletions src/grpc_interop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ namespace grpc_labview
int clusterOffset = 0;
if (lvMetadata->elements != nullptr)
{
auto lvElement = (*lvMetadata->elements)->bytes<LVMesageElementMetadata>();
// byteAlignment for LVMesageElementMetadata would be the size of its largest element which is a LStrHandle
auto lvElement = (LVMesageElementMetadata*)(*lvMetadata->elements)->bytes(0, sizeof(LStrHandle));
for (int x = 0; x < (*lvMetadata->elements)->cnt; ++x, ++lvElement)
{
auto element = std::make_shared<MessageElementMetadata>(metadataOwner);
Expand Down Expand Up @@ -75,7 +76,8 @@ namespace grpc_labview
int clusterOffset = 0;
if (lvMetadata->elements != nullptr)
{
auto lvElement = (*lvMetadata->elements)->bytes<LVMesageElementMetadata>();
// byteAlignment for LVMesageElementMetadata would be the size of its largest element which is a LStrHandle
auto lvElement = (LVMesageElementMetadata*)(*lvMetadata->elements)->bytes(0, sizeof(LStrHandle));
for (int x = 0; x < (*lvMetadata->elements)->cnt; ++x, ++lvElement)
{
auto element = std::make_shared<MessageElementMetadata>(metadataOwner);
Expand Down
14 changes: 14 additions & 0 deletions src/lv_interop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,18 @@ namespace grpc_labview
{
return RTSetCleanupProc(cleanUpProc, id, kCleanOnRemove);
}

int AlignClusterOffset(int clusterOffset, int alignmentRequirement)
{
#ifndef _PS_4
int remainder = abs(clusterOffset) % alignmentRequirement;
if (remainder == 0)
{
return clusterOffset;
}
return clusterOffset + alignmentRequirement - remainder;
#else
return clusterOffset;
#endif
}
}
35 changes: 7 additions & 28 deletions src/lv_interop.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace grpc_labview
virtual ~gRPCid() { }
};

int AlignClusterOffset(int clusterOffset, int alignmentRequirement);

//---------------------------------------------------------------------
// LabVIEW definitions
//---------------------------------------------------------------------
Expand All @@ -69,43 +71,20 @@ namespace grpc_labview
template<typename T>
T* bytes()
{
#ifndef _PS_4
if (sizeof(T) < 8)
{
return (T*)rawBytes;
}
return (T*)(rawBytes + 4); // 8-byte aligned data
#else
return (T*)rawBytes;
#endif
static_assert(!std::is_class<T>::value, "T must not be a struct/class type.");
return (T*)(bytes(0, sizeof(T)));
}

template<typename T>
T* bytes(int byteOffset)
{
#ifndef _PS_4
if (sizeof(T) < 8)
{
return (T*)(rawBytes + byteOffset);
}
return (T*)(rawBytes + 4 + byteOffset); // 8-byte aligned data
#else
return (T*)(rawBytes + byteOffset);
#endif
static_assert(!std::is_class<T>::value, "T must not be a struct/class type.");
return (T*)(bytes(byteOffset, sizeof(T)));
}

void* bytes(int byteOffset, int byteAlignment)
{
#ifndef _PS_4
int reminder = 4 % byteAlignment;
if (reminder == 0)
{
return (void*)(rawBytes + byteOffset);
}
return (void*)(rawBytes + byteAlignment - reminder + byteOffset); // 8-byte aligned data
#else
return (void*)(rawBytes + byteOffset);
#endif
return (void*)(rawBytes + AlignClusterOffset(4, byteAlignment) - 4 + byteOffset);
}
};

Expand Down
44 changes: 3 additions & 41 deletions src/message_element_metadata_owner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,41 @@ namespace grpc_labview {
//---------------------------------------------------------------------
int ClusterElementSize(LVMessageMetadataType type, bool repeated)
{
#ifndef _PS_4
if (repeated)
{
return 8;
return sizeof(void*);
}
switch (type)
{
case LVMessageMetadataType::BoolValue:
return 1;
case LVMessageMetadataType::EnumValue:
case LVMessageMetadataType::Int32Value:
case LVMessageMetadataType::UInt32Value:
case LVMessageMetadataType::EnumValue:
case LVMessageMetadataType::FloatValue:
return 4;
case LVMessageMetadataType::Int64Value:
case LVMessageMetadataType::UInt64Value:
case LVMessageMetadataType::DoubleValue:
case LVMessageMetadataType::StringValue:
case LVMessageMetadataType::BytesValue:
case LVMessageMetadataType::MessageValue:
return 8;
}
#else
if (repeated)
{
return 4;
}
switch (type)
{
case LVMessageMetadataType::BoolValue:
return 1;
case LVMessageMetadataType::Int32Value:
case LVMessageMetadataType::UInt32Value:
case LVMessageMetadataType::EnumValue:
case LVMessageMetadataType::FloatValue:
return 4;
case LVMessageMetadataType::StringValue:
case LVMessageMetadataType::BytesValue:
case LVMessageMetadataType::MessageValue:
return 4;
case LVMessageMetadataType::Int64Value:
case LVMessageMetadataType::UInt64Value:
case LVMessageMetadataType::DoubleValue:
return 8;
return sizeof(void*);
}
#endif
return 0;
}

int AlignClusterOffset(int clusterOffset, int alignmentRequirement)
{
int remainder = abs(clusterOffset) % alignmentRequirement;
if (remainder == 0)
{
return clusterOffset;
}
return clusterOffset + alignmentRequirement - remainder;
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
int AlignClusterOffset(int clusterOffset, LVMessageMetadataType type, bool repeated)
{
#ifndef _PS_4
if (clusterOffset == 0)
{
return 0;
}
auto multiple = ClusterElementSize(type, repeated);
return AlignClusterOffset(clusterOffset, multiple);
#else
return clusterOffset;
#endif
}

//---------------------------------------------------------------------
Expand Down

0 comments on commit caa1b0f

Please sign in to comment.