I have a very large one structin the existing program. This structure includes a large number of bitfields.
I want to save part of it (say, 10 fields out of 150).
The sample code that I would use to save the subclass is as follows:
typedef struct {int a;int b;char c} bigstruct;
typedef struct {int a;char c;} smallstruct;
void substruct(smallstruct *s,bigstruct *b) {
s->a = b->a;
s->c = b->c;
}
int save_struct(bigstruct *bs) {
smallstruct s;
substruct(&s,bs);
save_struct(s);
}
I also want to choose which part of it will not be too complicated, since I want to change it from time to time. The naive approach that I presented earlier is very fragile and unattainable. When scaling to 20 different fields, you must change the fields in both the smallstructfunction and the function substruct.
I thought of two best approaches. Unfortunately, both require me to use an external CIL as a tool to analyze my structures.
substruct. smallstruct , substruct smallstruct.
( C parser) bigstruct, , . ad-hoc Java.
, , struct
struct st {
int a;
char c1:5;
char c2:3;
long d;
}
:
int field2distance[] = {0,sizeof(int),sizeof(int),sizeof(int)+sizeof(char)}
int field2size[] = {sizeof(int),1,1,sizeof(long)}
int field2bitmask[] = {0,0x1F,0xE0,0};
char *fieldNames[] = {"a","c1","c2","d"};
i th :
long getFieldData(void *strct,int i) {
int distance = field2distance[i];
int size = field2size[i];
int bitmask = field2bitmask[i];
void *ptr = ((char *)strct + distance);
long result;
switch (size) {
case 1:
result = *(char*)ptr;
break;
case 2:
result = *(short*)ptr;
...
}
if (bitmask == 0) return result;
return (result & bitmask) >> num_of_trailing_zeros(bitmask);
}
, makefile - - .
- .
- ? , - ?