From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 30309 invoked from network); 8 Jul 1999 12:39:48 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 8 Jul 1999 12:39:48 -0000 Received: from yalta.NL.net (yalta.NL.net [193.67.79.154]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id FAA15124 for ; Tue, 29 Jun 1999 05:13:46 -0400 Received: from [195.193.204.42] ([195.193.204.42]:1547 "HELO msmail.energie-delfland.nl" ident: "NO-IDENT-SERVICE[2]") by yalta.NL.net with SMTP id <15457-22853>; Tue, 29 Jun 1999 11:13:22 +0200 Received: by msmail.energie-delfland.nl with SMTP (Microsoft Exchange Server Internet Mail Connector Version 4.0.996.62) id <01BEC220.52C3AC90@msmail.energie-delfland.nl>; Tue, 29 Jun 1999 11:12:49 +0200 Message-ID: From: "Wessels, Hans" To: "'axp-list@redhat.com'" Subject: RE: problem with unaligned access on Alpha Date: Tue, 29 Jun 1999 11:12:48 +0200 X-Mailer: Microsoft Exchange Server Internet Mail Connector Version 4.0.996.62 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit On 25 juni 1999 Maciej W. Rozycki wrote: >It may even be completely ignored for archs that support unaligned >accesses in hardware, which is a true win -- no arch dependent >conditionals are needed (I do not even know how to fetch information about >alignment needed for a particular target -- "alignof" is useless here and >it's not portable, anyway). > > Using unions is good when you are sure all data is aligned. If that's >not the case, the problem remains. The "packed" approach is reasonable >but not necessarily the best. As C is relatively low-level language (some >people call it "a portable assembler") I wouldn't mind if some way of >alignment hinting became standard one day. I am using the following macro to get alignment of elements in an malloced memory block: /* * ALIGN is a macro to align elements of a certain size in a malloced * block. The start of the block is guaranteed aligned with every possible * object. So an object is aligned when it's position relative to the start * of the array is a multiple of it's size. Instead of the modulo operator * the & operator is used. This assumes that the alignment of an object * is the smalest power of two of its size; ie: sizeof(object)==3 -> alignment * is 1. sizeof(object)==6 ->alignment is 2. */ #define ALIGN(start,ptr,size) ptr+=((size)-((ptr)-(start)))&((size)-1) some code.... { ... uint8 *base = malloc(memneed); if (base == NULL) { return GUP_OUT_OF_MEM; } else { uint8 *cp = base; com->dictionary = (void *)(cp + 4); /* vier extra om terug te kunnen kijken */ cp += (DIC_SIZE + MAX_MATCH*4 + 6UL + 4UL) * sizeof (uint8); /* sliding dictionary + 4 voor terugkijken */ ALIGN(base, cp, sizeof(node_type)); /* allign buffer on node_type allignment */ com->root.big = (void *)cp; /* cp += sizeof (node_type) * HASH_SIZE; /* normal root */ ALIGN(base, cp, sizeof(unsigned long)); /* align on unsigned long for output buffer */ com->buffer_start=(void*)cp; cp += com->buffer_size; ALIGN(base, cp, sizeof(node_struct)); com->tree.big = (void *)cp; cp += sizeof (node_struct) * TREE_SIZE; /* sld tree */ ALIGN(base, cp, sizeof(uint8)); com->charlen = (void *)cp; cp += sizeof (uint8) * (NC + NC); /* karakter lengte */ ALIGN(base, cp, sizeof(uint16)); com->ptr2huffman = (void *)cp; cp += sizeof (uint16) * (MAX_NPT); /* huffman codes van de pointers */ ALIGN(base, cp, sizeof(pointer_type)); com->pointers = (void *)(cp); cp += buf * sizeof (pointer_type); ... } This code works fine from MC68K and a compiler using 68bit int's, going through GCC on MC68K and i386 to GCC on the Alpha. I know it isn't perfect but it works fine for me. Bye, Hans ---------------------------------------------------------------------- /| /\/\/\ |\ ir. Hans Wessels / | / /\/\ \ | \ H.Wessels@Energie-Delfland.nl / | / / /\ \ \ | \ tel.: 015 251 4397 / |/ / / \ \ \| \ \ |\ \ \ / / /| / prive: \ | \ \ \/ / / | / mr_ni@mbh.org \ | \ \/\/ / | / Fidelus Bittus et Fuckus Clockcyclus! \| \/\/\/ |/