Name: Anonymous 2011-07-06 19:04
I'm making a library that's to be compiled by the user to both static and shared files (.a and .dll/.so). Padding in structs can vary if the compiler linking with the library is different than the one that built the library, and you can't portably make just some structs have 0 padding, so I'm leaving all struct member setting/retrieving and memory allocation for the structs as functions in the library—just as libpng does it.
I have a few functions:
The first does the same as the second, but doesn't require 15 function calls to set 15 different variables in the struct.
And if you're wondering, here's the code to making a new DERPFile:
So I need a neat way to match a given Option argument with the offset and size of a DERPFile struct member. The best way to do this is with a const uint8_t DERPFile_offsets[] and a const uint8_t DERPFile_sizes[]. But am I really supposed to do *this*?
I'd be repeating the struct definition twice... Well, with the second table, I can simply supply the numerical constants instead of using sizeof, but the first requires offsetof() to take the struct padding into account.
Just wondering if there's any better way to do this so the people I'm working with don't laugh at my code.
I have a few functions:
void SetDERPFile(DERPFile * DERPFileInfo, uint32_t Options, uint32_t Param, uint32_t Value, ...);
void SetDERPFileOption(DERPFile * DERPFileInfo, uint32_t Param, uint32_t Value);
uint32_t GetDERPFileOption(const DERPFile * DERPFileInfo, uint32_t Param);The first does the same as the second, but doesn't require 15 function calls to set 15 different variables in the struct.
And if you're wondering, here's the code to making a new DERPFile:
DERPFile * NewDERPFile(uint32_t Type)
{
DERPFile *ptr = (DERPFile*) malloc(sizeof(DERPFile));
if(ptr == NULL) return NULL;
ptr->Type = Type;
memset(ptr+4, 0, sizeof(DERPFile)-4);
return ptr;
}So I need a neat way to match a given Option argument with the offset and size of a DERPFile struct member. The best way to do this is with a const uint8_t DERPFile_offsets[] and a const uint8_t DERPFile_sizes[]. But am I really supposed to do *this*?
const uint8_t DERPFile_struct_offsets[] = {
offsetof(DERPFile, FirstMember),
offsetof(DERPFile, SecondMember),
...etc...
}
const uint8_t DERPFile_struct_sizes[] = {
sizeof(DERPFile.FirstMember),
sizeof(DERPFile.SecondMember),
...etc...
}I'd be repeating the struct definition twice... Well, with the second table, I can simply supply the numerical constants instead of using sizeof, but the first requires offsetof() to take the struct padding into account.
Just wondering if there's any better way to do this so the people I'm working with don't laugh at my code.