Skip to content

Commit

Permalink
Remove ENDIAN macros
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier committed Feb 29, 2024
1 parent cd44abc commit 5c6cd76
Show file tree
Hide file tree
Showing 14 changed files with 940 additions and 797 deletions.
50 changes: 50 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
# See https://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowShortIfStatementsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: TopLevel
BinPackParameters: false
BreakBeforeBraces: Allman
ColumnLimit: 120
DerivePointerAlignment: false
FixNamespaceComments: false
IndentCaseLabels: true
IndentPPDirectives: AfterHash
NamespaceIndentation: All
PackConstructorInitializers: CurrentLine
PointerAlignment: Left
SortIncludes: Never
SpaceAfterTemplateKeyword: false
TabWidth: 4
UseTab: Never
---
Language: ObjC
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowShortIfStatementsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: TopLevel
BinPackParameters: false
BreakBeforeBraces: Allman
ColumnLimit: 120
DerivePointerAlignment: false
FixNamespaceComments: false
IndentCaseLabels: true
IndentPPDirectives: AfterHash
NamespaceIndentation: All
PackConstructorInitializers: CurrentLine
PointerAlignment: Left
SortIncludes: Never
SpaceAfterTemplateKeyword: false
TabWidth: 4
UseTab: Never
23 changes: 1 addition & 22 deletions cpp/include/Ice/InputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -824,28 +824,7 @@ class ICE_API InputStream : public IceInternal::Buffer
* Reads an int from the stream.
* @param v The extracted int.
*/
void read(std::int32_t& v) // Inlined for performance reasons.
{
if(b.end() - i < static_cast<int>(sizeof(std::int32_t)))
{
throwUnmarshalOutOfBoundsException(__FILE__, __LINE__);
}
const std::uint8_t* src = &(*i);
i += sizeof(std::int32_t);
#ifdef ICE_BIG_ENDIAN
std::uint8_t* dest = reinterpret_cast<std::uint8_t*>(&v) + sizeof(std::int32_t) - 1;
*dest-- = *src++;
*dest-- = *src++;
*dest-- = *src++;
*dest = *src;
#else
std::uint8_t* dest = reinterpret_cast<std::uint8_t*>(&v);
*dest++ = *src++;
*dest++ = *src++;
*dest++ = *src++;
*dest = *src;
#endif
}
void read(std::int32_t& v);

/**
* Reads a sequence of ints from the stream.
Expand Down
17 changes: 1 addition & 16 deletions cpp/include/Ice/OutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,22 +615,7 @@ class ICE_API OutputStream : public IceInternal::Buffer
* @param v The integer value to be written.
* @param dest The buffer destination for the integer value.
*/
void write(std::int32_t v, Container::iterator dest)
{
#ifdef ICE_BIG_ENDIAN
const std::uint8_t* src = reinterpret_cast<const std::uint8_t*>(&v) + sizeof(std::) - 1;
*dest++ = *src--;
*dest++ = *src--;
*dest++ = *src--;
*dest = *src;
#else
const std::uint8_t* src = reinterpret_cast<const std::uint8_t*>(&v);
*dest++ = *src++;
*dest++ = *src++;
*dest++ = *src++;
*dest = *src;
#endif
}
void write(std::int32_t v, Container::iterator dest);

/**
* Writes an int sequence to the stream.
Expand Down
50 changes: 0 additions & 50 deletions cpp/include/IceUtil/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,6 @@

#include <stdlib.h>

#if defined(__GLIBC__)
# include <endian.h>
#elif defined(__APPLE__)
# include <machine/endian.h>
#elif defined(__FreeBSD__)
# include <sys/endian.h>
#endif

#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN)) || \
(defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && (_BYTE_ORDER == _LITTLE_ENDIAN)) || \
(defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))

# define ICE_LITTLE_ENDIAN

#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)) || \
(defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && (_BYTE_ORDER == _BIG_ENDIAN)) || \
(defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))

# define ICE_BIG_ENDIAN

#elif defined(__i386) || \
defined(_M_IX86) || \
defined(__x86_64) || \
defined(_M_X64) || \
defined(_M_IA64) || \
defined(__alpha__) || \
defined(__ARMEL__) || \
defined(_M_ARM64) || \
defined(_M_ARM_FP) || \
defined(__arm64) || \
defined(__MIPSEL__)

# define ICE_LITTLE_ENDIAN

#elif defined(__sparc) || \
defined(__sparc__) || \
defined(__hppa) || \
defined(__ppc__) || \
defined(__powerpc) || \
defined(_ARCH_COM) || \
defined(__MIPSEB__)

# define ICE_BIG_ENDIAN

#else

# error "Unknown architecture"

#endif

#ifdef _MSC_VER

# ifdef _WIN64
Expand Down
15 changes: 9 additions & 6 deletions cpp/src/Ice/CollocatedRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <Ice/Instance.h>
#include <Ice/TraceLevels.h>
#include "Ice/OutgoingAsync.h"
#include "Endian.h"

#include <Ice/TraceUtil.h>

Expand Down Expand Up @@ -53,13 +54,15 @@ void
fillInValue(OutputStream* os, int pos, int32_t value)
{
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
#ifdef ICE_BIG_ENDIAN
reverse_copy(p, p + sizeof(std::int32_t), os->b.begin() + pos);
#else
copy(p, p + sizeof(std::int32_t), os->b.begin() + pos);
#endif
if (endian::native == endian::big)
{
reverse_copy(p, p + sizeof(std::int32_t), os->b.begin() + pos);
}
else
{
copy(p, p + sizeof(std::int32_t), os->b.begin() + pos);
}
}

}

CollocatedRequestHandler::CollocatedRequestHandler(const ReferencePtr& ref, const ObjectAdapterPtr& adapter) :
Expand Down
79 changes: 49 additions & 30 deletions cpp/src/Ice/ConnectionI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <Ice/ProxyFactory.h> // For createProxy().
#include <Ice/BatchRequestQueue.h>
#include "CheckIdentity.h"
#include "Endian.h"

#ifdef ICE_HAS_BZIP2
# include <bzlib.h>
Expand Down Expand Up @@ -723,20 +724,26 @@ Ice::ConnectionI::sendAsyncRequest(const OutgoingAsyncBasePtr& out, bool compres
// Fill in the request ID.
//
const uint8_t* p = reinterpret_cast<const uint8_t*>(&requestId);
#ifdef ICE_BIG_ENDIAN
reverse_copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
#else
copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
#endif
if (endian::native == endian::big)
{
reverse_copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
}
else
{
copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
}
}
else if(batchRequestNum > 0)
{
const uint8_t* p = reinterpret_cast<const uint8_t*>(&batchRequestNum);
#ifdef ICE_BIG_ENDIAN
reverse_copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
#else
copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
#endif
if (endian::native == endian::big)
{
reverse_copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
}
else
{
copy(p, p + sizeof(int32_t), os->b.begin() + headerSize);
}
}

out->attachRemoteObserver(initConnectionInfo(), _endpoint, requestId);
Expand Down Expand Up @@ -2825,11 +2832,14 @@ Ice::ConnectionI::sendNextMessage(vector<OutgoingMessage>& callbacks)
//
int32_t sz = static_cast<int32_t>(message->stream->b.size());
const uint8_t* p = reinterpret_cast<const uint8_t*>(&sz);
#ifdef ICE_BIG_ENDIAN
reverse_copy(p, p + sizeof(int32_t), message->stream->b.begin() + 10);
#else
copy(p, p + sizeof(int32_t), message->stream->b.begin() + 10);
#endif
if (endian::native == endian::big)
{
reverse_copy(p, p + sizeof(int32_t), message->stream->b.begin() + 10);
}
else
{
copy(p, p + sizeof(int32_t), message->stream->b.begin() + 10);
}
message->stream->i = message->stream->b.begin();
traceSend(*message->stream, _logger, _traceLevels);

Expand Down Expand Up @@ -2965,11 +2975,14 @@ Ice::ConnectionI::sendMessage(OutgoingMessage& message)
//
int32_t sz = static_cast<int32_t>(message.stream->b.size());
const uint8_t* p = reinterpret_cast<const uint8_t*>(&sz);
#ifdef ICE_BIG_ENDIAN
reverse_copy(p, p + sizeof(int32_t), message.stream->b.begin() + 10);
#else
copy(p, p + sizeof(int32_t), message.stream->b.begin() + 10);
#endif
if (endian::native == endian::big)
{
reverse_copy(p, p + sizeof(int32_t), message.stream->b.begin() + 10);
}
else
{
copy(p, p + sizeof(int32_t), message.stream->b.begin() + 10);
}
message.stream->i = message.stream->b.begin();

traceSend(*message.stream, _logger, _traceLevels);
Expand Down Expand Up @@ -3103,23 +3116,29 @@ Ice::ConnectionI::doCompress(OutputStream& uncompressed, OutputStream& compresse
//
int32_t compressedSize = static_cast<int32_t>(compressed.b.size());
p = reinterpret_cast<const uint8_t*>(&compressedSize);
#ifdef ICE_BIG_ENDIAN
reverse_copy(p, p + sizeof(int32_t), uncompressed.b.begin() + 10);
#else
copy(p, p + sizeof(int32_t), uncompressed.b.begin() + 10);
#endif
if (endian::native == endian::big)
{
reverse_copy(p, p + sizeof(int32_t), uncompressed.b.begin() + 10);
}
else
{
copy(p, p + sizeof(int32_t), uncompressed.b.begin() + 10);
}

//
// Add the size of the uncompressed stream before the message body
// of the compressed stream.
//
int32_t uncompressedSize = static_cast<int32_t>(uncompressed.b.size());
p = reinterpret_cast<const uint8_t*>(&uncompressedSize);
#ifdef ICE_BIG_ENDIAN
reverse_copy(p, p + sizeof(int32_t), compressed.b.begin() + headerSize);
#else
copy(p, p + sizeof(int32_t), compressed.b.begin() + headerSize);
#endif
if (endian::native == endian::big)
{
reverse_copy(p, p + sizeof(int32_t), compressed.b.begin() + headerSize);
}
else
{
copy(p, p + sizeof(int32_t), compressed.b.begin() + headerSize);
}

//
// Copy the header from the uncompressed stream to the compressed one.
Expand Down
36 changes: 36 additions & 0 deletions cpp/src/Ice/Endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#ifndef ICE_ENDIAN_H
#define ICE_ENDIAN_H

#ifdef __cpp_lib_endian
# include <bit>
#else

// Provide a minimal implementation of std::endian for C++17.
namespace std
{
#ifdef _MSC_VER
// Always little-endian on Windows.
enum class endian
{
little = 0,
big = 1,
native = little
};
#else
// Use macros predefined by gcc/clang to determine the endianness.
enum class endian
{
little = __ORDER_LITTLE_ENDIAN__,
big = __ORDER_BIG_ENDIAN__,
native = __BYTE_ORDER__
};
#endif
}

#endif // __cpp_lib_endian

#endif
Loading

0 comments on commit 5c6cd76

Please sign in to comment.