lundi 2 mars 2015

Packing a 1 byte value and 3 byte value into a single 32bit structure

While it admittedly doesn't add much to my question, let me start by saying that my background is MUCH heavier in languages like Java and C# than C/C++ and much of my confusion undoubtedly comes from those gaps.


I have a C++ application which can load objects from up to 255 different data sources at any given time. Each of these shared data sources are assigned a GUID. Each object within one of these data sources is keyed with a 24bit index. I refer to these data objects as Resources.


Because each data source indexes it's resources sequentially, a reference to any resource includes the 24bit index and an additional byte to determine the data source within a given context. Thus eliminating ambiguity in the runtime environment.


If a data object contains a reference to a resource within the same data source, this byte will be loaded with a 0 value. If it references an object in another data source, the byte serves as an index into the data source's "dependency table" of up to 128 GUIDs for other data sources.


I'm inclined to define the C structure of these resource keys as:



typedef struct ResourceID {
char DataSourceIndex;
char[3] ResourceIndex;
} ResourceID;


The problem is that before the factory classes in my application can build a complete resource object, it must resolve each DataSourceIndex from the data source's dependency context to the runtime application's own context table (with up to 255 GUIDs). Not only for the ID of the resource it's building, but also for all references the resource may contain.


While the structure above is extremely easy to resolve in that manner, I don't know if it's as friendly or as efficient as a typical UINT32 would be for comparisons within a performance critical application. Though I am a bit worried of how endianness could become a sneaky problem in such a case.


Would this structure be implemented better with a single UINT32 member and methods which use bit operators to read/write the data source index byte?


Would a definition involving unions be a better strategy?


Is there an obvious common alternative which I'm missing?


Aucun commentaire:

Enregistrer un commentaire