From: alvin <alvin@iplink.net>
To: axp-list@redhat.com
Subject: Re: unaligned traps question
Date: Wed, 16 Oct 2002 17:30:43 -0400 [thread overview]
Message-ID: <3DADDA83.6040900@iplink.net> (raw)
In-Reply-To: 20021016142302.A25749@ellpspace.math.ualberta.ca
Michal Jaegermann wrote:
> On Wed, Oct 16, 2002 at 02:15:55PM -0400, alvin wrote:
>
>>So I have a chunk of code like this:
>>
>>int add_message_type_avp(struct buffer *buf, _u16 type) {
>> _u16 *raw = (_u16 *)(buf->start + buf->len);
>> raw[0] = htons(0x8 | MBIT);
>> raw[1] = htons(VENDOR_ID);
>> raw[2] = 0;
>> raw[3] = htons(type);
>> buf->len += 8;
>> return 0;
>>}
>
>
> Regardless of all other issues this is a really costly method for
> doing this. You may cause four kernel traps.
>
>
>>I end up converting it to this:
>>
>>int add_message_type_avp(struct buffer *buf, _u16 type) {
>> _u16 raw[32];
>> raw[0] = htons(0x8 | MBIT);
>> raw[1] = htons(VENDOR_ID);
>> raw[2] = 0;
>> raw[3] = htons(type);
>> memcpy((char *)(buf->start + buf->len), (char *)&raw,8);
>>buf->len += 8;
>> return 0;
>>}
>>
>>
>>This is not pretty but it does work.
>
>
> Save some code questions this is actually a correct way of coding
> something of that sort. Should compile to something reasonably fast
> too.
>
> Here are questions.
> - Why you are pushing on stack raw[32] while only four positions
> are used?
I just did a global search and replace. It seemed easier to make the
array larger than the largest item and not worry about it
> - Are MBIT and VENDOR_ID really constants as suggested by a case?
> If yes then how about making a static initializer for first three
> positions in raw[4] and filling up only one cell with htons(type)?
This is true. I was looking to make the fewest changes to the way the
code operated. As the number of changes increse so does the likelyhood
of new bugs creeping in.
> - With a correct size a "magic constant" of 8 can and should be
> replaced by sizeof(raw).
Hmmm. This is true. Possibly time to look at changing this code to clean
it up a bit.
I did not write this code originaly. Personaly I think I would have
created a structure and tried to work with that.
This is only one of about a hundred routines or so that do just about
the same thing.
>
>>I spent a lot of time tracking this one down and could only come to the
>>conclusion that the unaligned trap handler was not working correctly.
>
>
> Seems like there is a potential for few other mess-ups. You really
> eliminated them all?
Well the software seems to work quite reliably. But on the other hand
the system wedged on the weekend.
--
Alvin Starr || voice: (416)785-4051
Interlink Connectivity || fax: (416)785-3668
alvin@iplink.net ||
next prev parent reply other threads:[~2002-10-16 21:32 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-10-15 0:50 unaligned traps question Ram Viswanadha
2002-10-15 1:38 ` Michal Jaegermann
2002-10-15 2:03 ` Uncle George
2002-10-15 15:39 ` George France
2002-10-15 16:46 ` Michal Jaegermann
2002-10-15 18:24 ` alvin
2002-10-15 19:33 ` George France
2002-10-15 19:40 ` Falk Hueffner
[not found] ` <3DAC6EF2.BC962839@voicenet.com>
2002-10-16 18:15 ` alvin
2002-10-16 19:18 ` George France
2002-10-16 19:45 ` Falk Hueffner
2002-10-16 20:08 ` George France
2002-10-17 18:37 ` Falk Hueffner
2002-10-16 20:23 ` Michal Jaegermann
2002-10-16 21:30 ` alvin [this message]
2002-10-17 1:47 ` Uncle George
2002-10-15 18:08 ` Uncle George
2002-10-15 19:29 ` George France
2002-10-15 20:10 ` Uncle George
2002-10-15 20:46 ` George France
2002-10-15 15:33 ` George France
2002-10-17 22:17 ` Ram Viswanadha
2002-10-18 15:36 ` Michal Jaegermann
2002-10-18 17:17 ` Ram Viswanadha
2002-10-18 18:47 ` Michal Jaegermann
[not found] <673E0A4C-E1DF-11D6-AC80-000393768D2C@mcgillsociety.org>
2002-10-17 15:50 ` George France
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox