|
Using Bit Fields
Submitted by |
A way to save memory is to use flag, here i little tips to make cleaner code
To store flag data like this way is to AVOID
// Structure that content most important cpu informations
// (avoid)
typedef struct CPUINFO2
{
char* pszProcessor; // CPU Processor string
char* pszVendor; // CPU vendor name string
bool bHasCPUID; // Supports CPUID instruction
bool bHasFPU; // FPU present
bool bHasVME; // Virtual Mode Extensions
bool bHasDebug; // Debug extensions
bool bHasPSE; // Page Size Extensions
bool bHasTSC; // Time Stamp Counter
bool bHasMSR; // Model Specific Registers
bool bHasPAE; // Page Address Extensions
bool bHasMCE; // Machine Check Extensions
bool bHasCMPXCHG8; // CMPXCHG8 instruction
bool bHasAPIC; // APIC
bool bHasSysCenter; // SYSENTER/SYSEXIT instruction
bool bHasMTRR; // Memory Type Range Registers
bool bHasGPE; // Global Paging Extensions
bool bHasMCA; // Machine Check Architecture
bool bHasCMOV; // CMOV instruction
bool bHasPAT; // Page Attribue Table
bool bHasPSE36; // PSE36 (Page Size Extensions)
bool bHasMMXExt; // MMX Extensions
bool bHasMMX; // MMX support
bool bHasFXSave; // FXSAVE/FXRSTOR instruction
bool bHas3DNowExt; // Extended 3DNow! support
bool bHas3DNow; // 3DNow! support
bool bHasSSEMMX; // SSE MMX support (same as MMXEXT)
bool bHasSSE; // SSE
bool bHasSSEFP; // SSE FP support
} CPUINFO2, *LPCPUINFO2; |
Why? using a byte for each flag make the struct consume memory as well
The solution is to use flag stored on a single data like byte , word or dword but the old way
like that is not the prefered one:
remove flag:
dwFlags &= ~MYFLAG
Set flag:
dwFlags |= MYFLAG
Is flag:
if( dwFlags & MYFLAG )
By using bit field we can make cleaner code and using same memory as old flag way....
example:
// Structure that content most important cpu informations
// (prefered)
typedef struct CPUINFO
{
char* pszProcessor; // CPU Processor string
char* pszVendor; // CPU vendor name string
struct
{
DWORD bHasCPUID : 1; // Supports CPUID instruction
DWORD bHasFPU : 1; // FPU present
DWORD bHasVME : 1; // Virtual Mode Extensions
DWORD bHasDebug : 1; // Debug extensions
DWORD bHasPSE : 1; // Page Size Extensions
DWORD bHasTSC : 1; // Time Stamp Counter
DWORD bHasMSR : 1; // Model Specific Registers
DWORD bHasPAE : 1; // Page Address Extensions
DWORD bHasMCE : 1; // Machine Check Extensions
DWORD bHasCMPXCHG8 : 1; // CMPXCHG8 instruction
DWORD bHasAPIC : 1; // APIC
DWORD bHasSysCenter : 1; // SYSENTER/SYSEXIT instruction
DWORD bHasMTRR : 1; // Memory Type Range Registers
DWORD bHasGPE : 1; // Global Paging Extensions
DWORD bHasMCA : 1; // Machine Check Architecture
DWORD bHasCMOV : 1; // CMOV instruction
DWORD bHasPAT : 1; // Page Attribue Table
DWORD bHasPSE36 : 1; // PSE36 (Page Size Extensions)
DWORD bHasMMXExt : 1; // MMX Extensions
DWORD bHasMMX : 1; // MMX support
DWORD bHasFXSave : 1; // FXSAVE/FXRSTOR instruction
DWORD bHas3DNowExt : 1; // Extended 3DNow! support
DWORD bHas3DNow : 1; // 3DNow! support
DWORD bHasSSEMMX : 1; // SSE MMX support (same as MMXEXT)
DWORD bHasSSE : 1; // SSE
DWORD bHasSSEFP : 1; // SSE FP support
};
} CPUINFO, *LPCPUINFO; |
All states using bHas... is a single bit, not a byte or doubleword only bit
Note you can change for any bit as you want but the maximum of the struct must be 32 (dword) as well..
// Size of the struct CPUINFO is 12
// Size of the struct CPUINFO2 is 36
Hope you like it!
|
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
|