2015年10月13日 星期二

C++ Data Structure Alignment Example

#include <typeinfo>
#include <iostream>
#include <vector>
#include <stdint.h>
#include "SortedPointerVector.h"
using namespace std;

typedef uint32_t PartPosition;
typedef uint32_t PartDataLength;
typedef uint16_t PartNumber;
typedef uint32_t PartSize;

typedef uint16_t FieldPosition;
typedef uint16_t FieldDataLength;
typedef uint32_t FieldListLength;
typedef uint32_t FieldListPosition;

typedef int16_t Fid;
typedef uint8_t FieldType;

struct Struct1 // 8
{
       double d; // 8
};
struct Struct2 // 4
{
       int i; // 4
};
struct Struct3 // 2
{
       short s; // 2
};
struct Struct4 // 1
{
       bool b; // 1
};
struct Struct5 // 8
{
       char * c; // 8
};

struct Struct11      // 16
{
       bool b; // 1
       short s; // 2
       int i; // 4
       double d; // 8
};
struct Struct12      // 16
{
       double d; // 8
       int i; // 4
       short s; // 2
       bool b; // 1
};
struct Struct13      // 24
{
       int i; // 4
       double d; // 8
       short s; // 2
       bool b; // 1
};
struct Struct14      // 24
{
       bool b; // 1
       double d; // 8
       short s; // 2
       int i; // 4
};

struct Struct21      // 8
{
       char c[8]; // 8
};
struct Struct22      // 9
{
       char c[9]; // 9
};
struct Struct23 // 17
{
       char c1[9];
       char c2[8];
};
struct Struct24 // 24, because 8,1,8
{
       char c1[9];
       double d;
};
struct Struct25 // 16, because 8,2
{
       double d; // 8
       char c[2]; // 2
};
struct Struct26 // 16, because 8,2
{
       char c[2]; // 2
       double d; // 8
};
struct Struct27      // 8
{
       int i; // 4
       char c[2]; // 2
};
struct Struct28      // 8
{
       char c[2]; // 2
       int i; // 4
};

struct Struct31 // 34, because 17+17
{
       Struct23 s1; // 17
       Struct23 s2; // 17
};
struct Struct33  // 48
{
       Struct24 s1; // 24 (8,1,8)
       Struct24 s2; // 24 (8,1,8)
};
struct Struct34      // 24, because 8,2,2,8
{
       Struct25 s1; // 16 (8,2)
       Struct26 s2; // 16 (2,8)
};
struct Struct35 // 24, because (16,1),4, align 4
{
       Struct23 s1; // 17
       int i; // 4
};
struct Struct36 // 28, because (16,1),4,1+1+1, align 4
{
       Struct23 s1; // 17
       int i; // 4
       bool b1; // 1
       bool b2; // 1
       bool b3; // 1
};
struct Struct37 // 28, because (16,1),4,1+1+1+1, align 4
{
       Struct23 s1; // 17
       int i; // 4
       bool b1; // 1
       bool b2; // 1
       bool b3; // 1
       bool b4; // 1
};
struct Struct38 // 32, because (16,1),4,1+1+1+1,1, align 4
{
       Struct23 s1; // 17
       int i; // 4
       bool b1; // 1
       bool b2; // 1
       bool b3; // 1
       bool b4; // 1
       bool b5; // 1
};
struct Struct39 // 40, because (8,8,1),8,1+1+1+1+1, align 8
{
       Struct23 s1; // 17
       double d; // 8
       bool b1; // 1
       bool b2; // 1
       bool b3; // 1
       bool b4; // 1
       bool b5; // 1
};
struct Struct40 // 16, because 4,2,2,4, align 4
{
       Struct27 s1; // 4,2
       Struct28 s2; // 2,4
};

struct InstrumentName // 65
{
       static const uint64_t INSTRUMENT_NAME_LENGTH = 64;
       char name[INSTRUMENT_NAME_LENGTH]; // 64
       uint8_t size; // 1
};

struct InstrumentNameWrapper1 // 65
{
       InstrumentName n; // 65
};

struct InstrumentNameWrapper2 // 66, align 1
{
       InstrumentName n; // 65
       bool b; // 1
};
struct InstrumentNameWrapper3 // 72, because 64,1,4, align 4
{
       InstrumentName n; // 65
       int i; // 4
};

struct InstrumentKey // 72, because 64,1,2,2,1+1, align 2
{
       InstrumentName name; // 65 (64+1)
       uint16_t domain; // 2
       uint16_t serviceID; // 2
       uint8_t nameType; // 1
       uint8_t qos; // 1
};

struct InstrumentKey2 // 70, 2,2,66, align 2
{
       uint16_t domain; // 2
       uint16_t serviceID; // 2
       InstrumentName name; // 65 (64+1)
       uint8_t nameTypeQos; // 1
};

struct ConstituentKey // 1
{
       uint8_t constituentNumber;
};

struct PartSizes // 32
{
       std::vector<std::pair<PartNumber, PartSize>> contents;
};

struct PartDatas // 48, because 8,4,32, align 8
{
       char * data; // 8
       PartDataLength dataLength; // 4
    PartSizes partSizes; // 32
};

struct ConstituentData // 72, because 48,4+4,1+2+2+1+1+1,1+1, align 8
{
       PartDatas partDatas; // 48, align 8
       uint32_t pe; // 4
       uint32_t mask; // 4
       ConstituentKey key; // 1
       PartNumber lastPartNumber; // 2
       int16_t fieldListNumber; // 2
       uint8_t instrumentType; // 1
       bool isLastPartNumberKnown; // 1
       bool hasMultipleParts; // 1
       bool hasPE; // 1
       bool isContainShellMetaData; // 1
};
struct ConstituentData2 // 64, because 48,4+2+2,2+1+1+1+1+1+1, align 8
{
       PartDatas partDatas; // 48, align 8
       uint32_t mask; // 4
       uint16_t pe; // 2
       PartNumber lastPartNumber; // 2
       int16_t fieldListNumber; // 2
       ConstituentKey key; // 1
       uint8_t instrumentType; // 1
       bool isLastPartNumberKnown; // 1
       bool hasMultipleParts; // 1
       bool hasPE; // 1
       bool isContainShellMetaData; // 1
};

typedef SortedPointerVector<ConstituentKey, ConstituentData> ConstituentDataList; // 40

struct InstrumentContentData // 72, because 1+1+4,8,8,40,8
{
       uint8_t ll2Type; // 1
       bool isEverInstrumentAvailable; // 1
       uint32_t instrumentId; // 4
       int64_t lastUpdateTickCountInNanosec; // 8
       InstrumentKey * underlyingKey; // 8
       ConstituentDataList constituentDataList; // 40
       uint32_t mcLabel; // 4
};

struct InstrumentContentData2 // 64, because 40,8,8,4+2+1+1
{
       ConstituentDataList constituentDataList; // 40
       int64_t lastUpdateTickCountInNanosec; // 8
       InstrumentKey * underlyingKey; // 8
       uint32_t instrumentId; // 4
       uint16_t mcLabel; // 2
       uint8_t ll2Type; // 1
       bool isEverInstrumentAvailable; // 1
};

//-----------------------------------------------------------

template <class T>
void printSize()
{
       cout << typeid(T).name() << " : " << sizeof(T) << endl;
}

int main()
{
       cout << "primitive 1" << endl;
       printSize<Struct1>();
       printSize<Struct2>();
       printSize<Struct3>();
       printSize<Struct4>();
       printSize<Struct5>();

       cout << "struct 11" << endl;
       printSize<Struct11>();
       printSize<Struct12>();
       printSize<Struct13>();
       printSize<Struct14>();

       cout << "array 21" << endl;
       printSize<Struct21>();
       printSize<Struct22>();
       printSize<Struct23>();
       printSize<Struct24>();
       printSize<Struct25>();
       printSize<Struct26>();
       printSize<Struct27>();
       printSize<Struct28>();

       cout << "compund 31" << endl;
       printSize<Struct31>();
       printSize<Struct33>();
       printSize<Struct34>();
       printSize<Struct35>();
       printSize<Struct36>();
       printSize<Struct37>();
       printSize<Struct38>();
       printSize<Struct39>();
       printSize<Struct40>();

       cout << "real" << endl;
       printSize<InstrumentName>();
       printSize<InstrumentKey>();
       printSize<ConstituentDataList>();
       printSize<InstrumentContentData>();

       cout << "real 2" << endl;
       printSize<ConstituentData>();
       printSize<PartDatas>();
       printSize<PartSizes>();

       cout << "real modified" << endl;
       printSize<InstrumentNameWrapper1>();
       printSize<InstrumentNameWrapper2>();
       printSize<InstrumentNameWrapper3>();
       printSize<InstrumentContentData2>(); // saved 8 bytes
       printSize<ConstituentData2>();           // saved 8 bytes
       printSize<InstrumentKey2>();                    // saved 2 bytes

       system("PAUSE");

}

沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...