[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] about alignment issues.

From: Pedro Alves
Subject: Re: [lwip-users] about alignment issues.
Date: Mon, 17 Apr 2006 13:26:07 +0100
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

Derek Guerdon wrote:

On Fri, 14 Apr 2006 09:30:46 -0700, Curt McDowell wrote:
Still, a union is not called for:

struct {
struct pbuf pbuf;
u64_t payload[MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) / sizeof(u64_t)];
} pbuf_pool_memory[PBUF_POOL_SIZE];

Or, if it's more pleasing, we could define a "maximal" type somewhere
and use that for payload[].  "struct mem" does that in mem.c, except it
should be defined in a header file to clean it up.

I think the latter is preferable for three reasons. First, even u64_t
isn't required to be maximally aligned under ANSI-C (although in
practice I've never seen a case where it wasn't). Second, there are
some compilers without native unsigned 64-bit integer. Third,
allocating an array of 'memory aligned types' allows targets with
limited memory to avoid allocating unused memory.

The declaration needs to be reworked a bit as the one given won't
allocate enough space when MEM_ALIGNMENT is less than 8 (e.g. if
MEM_ALIGNMENT is 4 and PBUF_POOL_SIZE is 1540, only 1536 bytes are
allocated in payload).

Something along these lines should work:

I'd like to offer my thanks to you for addressing these issues. I
think they consumed the majority of the time I spent porting lwIP.
Getting the alignment issues fixed will be a major improvement to the
code base.

Although I don't belong to the "unions are ugly school", I'm ok with a solution like this.
Actually, I think that flattening the pool some more would be even better.

static struct pbuf mem[PBUF_POOL_SIZE];
typedef u32_t MAX_ALIGN_T;
typedef MAX_ALIGN_T pbuf_pool_payload_t[(PBUF_POOL_BUFSIZE+sizeof(MAX_ALIGN_T)-1)/sizeof(MAX_ALIGN_T)];
pbuf_pool_payload_t pbuf_pool_payload[PBUF_POOL_SIZE];

 int i;
 for(i = 0; i < PBUF_POOL_SIZE; ++i) {
   mem[i].next = mem[i+1].next;
   mem[i].len = mem[i].tot_len = sizeof(pbuf_pool_payload_t);
   mem[i].payload = pbuf_pool_payload[i];
   mem[i].flags = PBUF_FLAG_POOL;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]