Tamas Somogyi wrote:
My conclusion is that there's no standard way to directly access typed
values in byte-stream.
Not guaranteed, no. However so far we've been able to rely on
implementation-specific properties of individual compilers. Even though
there's no standard way to pack, just about every sane compiler for
embedded use has some way to do it.
So the solution is writing get/set code for byte-copying data from/to
the stream to the variable. It can be done either on (A) member-level or
(B) on struct-level, like this (using Alain's sample):
(A)
#define OFS_DNS_ANSWER_TTL 4
#define get_dns_answer_ttl(p,a) memcpy(&a,
((char*)p)+OFS_DNS_ANSWER_TTL, sizeof(a));
#define set_dns_answer_ttl(p,a) memcpy(((char*)p)+OFS_DNS_ANSWER_TTL,
&a, sizeof(a));
- where 'a' is an aligned (local) variable of the corresponding type.
- supposing that alignment req for char is 1 on all supported platform
according to Jifl [1].
(B)
void dns_answer_load_from_stream(char* p, struct dns_answer* s)
{
memcpy(&(s->type), p, sizeof(s->type));
p+=sizeof(s->type);
memcpy(&(s->class), p, sizeof(s->class));
p+=sizeof(s->class);
...
}
As I said in a mail last night: "A compromise might be to define offsets
of members, and SMEMCPY from the correct location. An optimising compiler
can more easily eliminate that and convert to an assignment where it knows
it can. The situation for those using poor compilers could be bad though -
function calls every time; and they may have been unaffected by the
portability issue in the first place. "
In other words, the price of complete portability would be poor
performance for the vast majority of existing users. And more complex code.
Jifl
--
eCosCentric Limited http://www.eCosCentric.com/ The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK. Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
------["The best things in life aren't things."]------ Opinions==mine
_______________________________________________
lwip-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/lwip-users