Quick little structure organization problem. Basically revolves around how do you define a set of structures with optimal memory layout for networking purposes; set as in, dynamic fields in a struct.
Constructing a transaction structure which holds a header and optional TLV (Type/Length/Value (networking term)) structs. Though, say, there are 6 different TLV structs and they are only to be included if necessary. So, how do I create a dynamic transaction struct that holds different combinations of TLVs that are contiguous in memory?
The following is an example if I want to include all the TLVs.
**NOTE: tlv_artist has a array ptr field. So when malloc'ing the transaction structure I'd do a "sizeof(transaction_t) + artist_name_length)", then memcpy into artist_name[0] so that the memory is all contiguous (no pointers to more data, etc...).
**Also note that tlv_artist is at the bottom of the transaction struct. This is so that you can properly index after malloc'ing. Otherwise, you couldn't index to fields defined below tlv_artist.
Code:
typedef struct tlv_song_size_ {
int type;
int length;
int song_size;
} tlv_song_size_t;
typedef struct tlv_song_len_ {
int type;
int length;
int song_length;
} tlv_song_len_t;
typedef struct tlv_artist {
int type;
int length;
char artist_name[0];
} tlv_artist_t;
typedef struct header_ {
int version;
int blah;
} header_t;
typedef struct transaction_ {
header_t header;
tlv_song_size;
tlv_song_len;
tlv_artist_t artist;
} transaction_t;
Note this works fine. Though, what if I don't need to include a tlv_song_size? What if I don't want to include a tlv_song_len? I could always malloc that structure and fill it, but the memory wouldn't be contiguous then...
So how do I avoid defining multiple transaction structures that holds different combinations of header + TLVs?
I sincerely thank you for your advice.