Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement object packer for consistent hashing #6489

Merged
merged 1 commit into from Jul 27, 2018

Conversation

Al2Klimov
Copy link
Member

No description provided.

@Al2Klimov Al2Klimov requested a review from dnsmichi July 26, 2018 15:00
@dnsmichi dnsmichi added this to the 2.10.0 milestone Jul 26, 2018
@dnsmichi dnsmichi added enhancement New feature or request core/quality Improve code, libraries, algorithms, inline docs labels Jul 26, 2018
@Al2Klimov Al2Klimov removed the request for review from dnsmichi July 26, 2018 15:09
@Al2Klimov Al2Klimov force-pushed the feature/object-packer branch 2 times, most recently from 8150c9c to 44dc128 Compare July 26, 2018 15:32
@Al2Klimov
Copy link
Member Author

Have I already complained about MSVC?

diff --git a/lib/base/object-packer.cpp b/lib/base/object-packer.cpp
index 2ff08e5da..988504fe7 100644
--- a/lib/base/object-packer.cpp
+++ b/lib/base/object-packer.cpp
@@ -33,15 +33,21 @@ using namespace icinga;
 // Just for the sake of code readability
 typedef std::vector<char> StringBuilder;

-static const union {
+union EndiannessDetector
+{
+       EndiannessDetector()
+       {
+               i = 1;
+       }
+
        int i;
        char buf[sizeof(int)];
-} endianness = {
-       .i = 1
 };

+static const EndiannessDetector l_EndiannessDetector;
+
 // Assumption: The compiler will optimize (away) if/else statements using this.
-#define MACHINE_LITTLE_ENDIAN (endianness.buf[0])
+#define MACHINE_LITTLE_ENDIAN (l_EndiannessDetector.buf[0])

 static void PackAny(const Value& value, StringBuilder& builder);

@@ -55,6 +61,19 @@ static inline void SwapBytes(char& a, char& b)
        b = c;
 }

+#if CHAR_MIN != 0
+union CharU2SConverter
+{
+       CharU2SConverter()
+       {
+               s = 0;
+       }
+
+       unsigned char u;
+       signed char s;
+};
+#endif
+
 /**
  * Avoid implementation-defined overflows during unsigned to signed casts
  */
@@ -63,12 +82,7 @@ static inline char UIntToByte(unsigned i)
 #if CHAR_MIN == 0
        return i;
 #else
-       union {
-               unsigned char u;
-               signed char s;
-       } converter = {
-                       .s = 0
-       };
+       CharU2SConverter converter;

        converter.u = i;
        return converter.s;
@@ -94,17 +108,30 @@ static inline void PackUInt64BE(uint_least64_t i, StringBuilder& builder)
        builder.insert(builder.end(), (char*)buf, (char*)buf + 8);
 }

+union Double2BytesConverter
+{
+       Double2BytesConverter()
+       {
+               buf[0] = 0;
+               buf[1] = 0;
+               buf[2] = 0;
+               buf[3] = 0;
+               buf[4] = 0;
+               buf[5] = 0;
+               buf[6] = 0;
+               buf[7] = 0;
+       }
+
+       double f;
+       char buf[8];
+};
+
 /**
  * Append the given double as big-endian IEEE 754 binary64
  */
 static inline void PackFloat64BE(double f, StringBuilder& builder)
 {
-       union {
-               double f;
-               char buf[8];
-       } converter = {
-                       .buf = {0, 0, 0, 0, 0, 0, 0, 0}
-       };
+       Double2BytesConverter converter;

        converter.f = f;

@Al2Klimov Al2Klimov requested a review from dnsmichi July 26, 2018 15:54
@dnsmichi dnsmichi merged commit b16d96d into master Jul 27, 2018
@dnsmichi dnsmichi deleted the feature/object-packer branch July 27, 2018 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core/quality Improve code, libraries, algorithms, inline docs enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants