will introduce flexible structures, which allow both the struct and the flexible portions to be defined, for reference as needed in code.
In this example, the SCdxNode contains information that is a fixed/rigid portion which is always 12 bytes, along with a flexible structure portion which is accessed based on information obtained in the header.
// Create a standard struct
struct SCdxNode
{
u16 type;
u16 keyCount;
u32 nodeLeft;
u32 nodeRight;
u32 mask_RN;
u8 mask_TC;
u8 mask_DC;
u8 bits_RN;
u8 bits_TC;
u8 bits_DC;
// Total: 21 bytes
};
The above structure contains on the rigidly defined header portion, but the actual use of the SCdxNode structure is always in 512-byte chunks on the disk. So we need a way to define the 512 byte section in a way that can be referenced in code through a built-in function without having to hard-code the size:
// Create the same structure with the flexible portion
flexstruct SCdxNode
{
u16 type;
u16 keyCount;
u32 nodeLeft;
u32 nodeRight;
u32 mask_RN;
u8 mask_TC;
u8 mask_DC;
u8 bits_RN;
u8 bits_TC;
u8 bits_DC;
// Total: 21 bytes
// Add the flex space which extends up to 512 bytes
flex keyspace#512;
};
The flex portions can be sequential in a file, with each one proceeding from the end of the prior up to the address size specified. The traditional sizeof(SCdxNode) will return 21, while flexsizeof(SCdxNode) will return the full 512 bytes.
When defining the structure, you define it as needed:
// Create the same structure with the flexible portion
SCdxNode* node; // Defines a pointer to the 21-byte block
flex SCdxNode* fnode; // Defines a pointer to a 512-byte block
// Both are accessed identically, except flex entries are visible in the flex struct:
// fnode->keyspace is visible
// node->keyspace is not visible
s32 length = sizeof(*node); // 21 bytes
s32 flength = sizeof(*fnode); // 512 bytes
--
Rick C. Hodgin