Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ll-xn!cit-vax!oberon!castor.usc.edu!blarson From: blarson@castor.usc.edu (Bob Larson) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: pointer alignment when int != char * Message-ID: <3195@oberon.USC.EDU> Date: Sat, 4-Jul-87 19:24:15 EDT Article-I.D.: oberon.3195 Posted: Sat Jul 4 19:24:15 1987 Date-Received: Sun, 5-Jul-87 01:31:33 EDT References: <493@its63b.ed.ac.uk> Sender: nobody@oberon.USC.EDU Reply-To: blarson@castor.usc.edu.UUCP (Bob Larson) Organization: USC AIS, Los Angeles Lines: 45 Xref: mnetor comp.lang.c:2818 comp.unix.wizards:3105 In article <493@its63b.ed.ac.uk> simon@its63b.ed.ac.uk (Simon Brown) writes: >If I were to want to implement malloc (or some such) on a machine where >sizeof(int) != sizeof(char *), how do I ensure that the pointer-values I >return are maximally aligned (eg, quad-aligned)? The same way as you would on any other machine: non-portably. (What is your definition of quad-aligned? 4 * sizeof(char)? There are quit a few machines where this is not maximally aligned.) For example, prime 64v mode: char *alignpointer(p) char *p; { union { char *up; struct { unsigned fault:1; unsigned ring:2; /* I may have the ring and extend bits exchanged */ unsigned extend:1; /* check before you try this on a real prime */ unsigned segment:12; unsigned offset:16; unsigned bit:4; unsigned unused:12; } point; } un; int zerooffset; un.up = p; if(un.point.fault) return p; /* faulted pointer, not a valid address */ zerooffset = un.point.offset == 0; un.point.offset = (un.point.offset | (un.point.extend & (un.point.bit!=0)) + 1) & ~1; /* round offset up to next 4 byte boundry */ un.point.extend = 0;/* and say it is at a 2-byte boundry */ un.point.bit = 0; /* unneded, but leave it clean */ if(un.point.offset == 0 && !zerooffset) un.point.segment++; return un.up; } Obviously, it would be easier to make sure to generate aligned pointers in the first place. Also I did not make all the assumptions that the C compiler does, assuming you could have gotten the pointer via another language. Bob Larson Arpa: Blarson@Ecla.Usc.Edu Uucp: {sdcrdcf,seismo!cit-vax}!oberon!castor!blarson "How well do we use our freedom to choose the illusions we create?" -- Timbuk3