lwip-users
[Top][All Lists]
Advanced

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

Re: [lwip-users] NIOS II port: Request for memory alignement macro to be


From: Derek Guerdon
Subject: Re: [lwip-users] NIOS II port: Request for memory alignement macro to be included in lwip source
Date: Wed, 07 Dec 2005 00:46:17 -0500

Jan Ulvesten wrote:
>In Alteras uCOS II port of lwIP for NIOS II, 3 source files have been
>changed.
>
>__attribute__ ((aligned (MEM_ALIGNMENT))) is added to a few defs (5) to
>control the memory alignment (gnu C syntax).
>
>Patching the standard code base is by no mean the preferred method of
>targeting alignment. I suppose that an array is always aligned at an
>even boundary, but the 4-byte alignment is only achieved using
>__attribute__ ((aligned (4))). 
>
>It could easily be avoided adding a macro the defs below, e.g.:
>
>#define MEM_ALIGMENT_ATTR __attribute__ ((aligned (MEM_ALIGNMENT)))
>
>- or -
>
>#define MEM_ALIGMENT_ATTR
>
>The definition could then be:
>
>static u8_t ram[MEM_SIZE + sizeof(struct mem) + MEM_ALIGNMENT]
>MEM_ALIGMENT_ATTR;

That works well for those who have the problem and use gcc, but it
doesn't help those with the same problem who are not using gcc. I
don't think the solution requires the use of extensions, anyhow. If
the user can determine a data type with the greatest alignment
requirements, the compiler can do the dirty work -- in an ANSI-C
manner to boot.

For the code below, assume that in "cc.h" there is a typedef named
'max_align_t' that defines a data type that has the maximum alignment
requirements of the data types in the system.

>
>
>Altera changes:
>
>------------------------------------------------------------------------
>----
>mem.c                 
>static u8_t ram[MEM_SIZE + sizeof(struct mem) + MEM_ALIGNMENT]
>__attribute__ ((aligned (MEM_ALIGNMENT)));
>
Use max_align_t to ensure the RAM buffer is maximally aligned:

static union {
    max_align_t m; /* Guarantees this union has maximum alignment */
    u8_t ram[MEM_SIZE + sizeof(struct mem) + MEM_ALIGNMENT];
} rambuf;

static u8_t * const ram = rambuf.ram;


>------------------------------------------------------------------------
>----ipfrag.c:
>static u8_t ip_reassbuf[IP_HLEN + IP_REASS_BUFSIZE] __attribute__
>((aligned (MEM_ALIGNMENT)));
>
>static u8_t ip_reassbitmap[IP_REASS_BUFSIZE / (8 * 8)] __attribute__
>((aligned (MEM_ALIGNMENT)));
>
>static u8_t ip_reassbuf[IP_HLEN + IP_REASS_BUFSIZE] __attribute__
>((aligned (MEM_ALIGNMENT)));
>
Use struct ip_hdr to ensure that &ip_reassbuf[0] is a validly aligned
address for a struct ip_hdr object.

static union {
    struct ip_hdr h; /* Guarantees this union has proper alignment */
    u8_t buf[IP_HLEN + IP_REASS_BUFSIZE];
} ip_reass;

static u8_t * const ip_reassbuf = ip_reass.buf;


I don't think reassbitmap needs to be aligned since it appears to be
always accessed as a character array (although I may have missed some
code where it isn't). 

>------------------------------------------------------------------------
>----pbuf.c
>static u8_t pbuf_pool_memory[(PBUF_POOL_SIZE *
>MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE + sizeof(struct pbuf)))] __attribute__
>((aligned (MEM_ALIGNMENT)));
>
>------------------------------------------------------------------------
>----

As above, this object could be created as a union containing
max_align_t to ensure the buffer is properly aligned. This module
would probably benefit more from a replacing the u8_t array with just
an array of structures. They do not appear to be used in any other
way.


Just my suggestion for trying to keep the solution within hailing
distance of ANSI-C.

-- 
Derek Guerdon





reply via email to

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