From: Paul Eggert Date: Mon, 17 May 2004 07:52:52 +0000 (+0000) Subject: Port obstack to the AS/400. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e868156834d09d1a0b2046c3defac1691fcfd953;p=pspp Port obstack to the AS/400. --- diff --git a/lib/ChangeLog b/lib/ChangeLog index 15e3e1efdb..23d45e39b2 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,29 @@ +2004-05-17 Paul Eggert + + Port obstack to the AS/400, where pointers are 16 bytes wide and + you cannot cast an integer to a valid pointer. This patch is + currently waiting to be integrated into glibc; see + . + + * obstack.h (__PTR_TO_INT, __INT_TO_PTR): Remove. + All uses of __INT_TO_PTR (PTR_TO_INT ...) replaced by __PTR_ALIGN. + (__BPTR_ALIGN, __PTR_ALIGN): New macros. + (struct obstack): temp member is now a union of a pointer and + an integer, instead of an integer. All integer uses changed. + This does not affect the physical layout of struct obstack, + except on hosts (like the AS/400) where the size or alignment of + void * is greater than that of ptrdiff_t. + (obstack_finish) [! (defined __GNUC__ && defined __STDC__ && + __STDC__)]: Store temporary in pointer member of union, not + integer member. + * obstack.c: Include , for offsetof. + (struct fooalign): Remove; it doesn't need a name. + (union fooround): Change double to long double, and add void *. + (DEFAULT_ALIGNMENT): Use offsetof to compute. + (DEFAULT_ALIGNMENT, DEFAULT_ROUNDING): Now an enum constant, + not a macro. Hence the values are always int; so remove all + casts-to-int in uses. + 2004-05-16 Paul Eggert Derek Price @@ -17,7 +43,7 @@ getline cleanup. This changes the getndelim2 API: both order of arguments, and meaning of delim2 (now uses EOF, not 0, to indicate no delimiter). - + * getline.c: Don't include stddef.h or stdio.h, since our interface does that. (getline): Always use getdelim, so that we don't have two diff --git a/lib/obstack.c b/lib/obstack.c index 6a1ca0086c..9bbc3d4ca1 100644 --- a/lib/obstack.c +++ b/lib/obstack.c @@ -54,18 +54,26 @@ # include #endif +#include + #ifndef ELIDE_CODE /* Determine default alignment. */ -struct fooalign {char x; double d;}; -# define DEFAULT_ALIGNMENT \ - ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0)) +union fooround +{ + long int i; + long double d; + void *p; +}; /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. But in fact it might be less smart and round addresses to as much as DEFAULT_ROUNDING. So we prepare for it to do that. */ -union fooround {long x; double d;}; -# define DEFAULT_ROUNDING (sizeof (union fooround)) +enum + { + DEFAULT_ALIGNMENT = offsetof (struct { char c; union fooround u; }, u), + DEFAULT_ROUNDING = sizeof (union fooround) + }; /* When we copy a long block of data, this is the unit to do it with. On some machines, copying successive ints does not work; @@ -136,7 +144,7 @@ _obstack_begin (struct obstack *h, register struct _obstack_chunk *chunk; /* points to new chunk */ if (alignment == 0) - alignment = (int) DEFAULT_ALIGNMENT; + alignment = DEFAULT_ALIGNMENT; if (size == 0) /* Default size is what GNU malloc can fit in a 4096-byte block. */ { @@ -182,7 +190,7 @@ _obstack_begin_1 (struct obstack *h, int size, int alignment, register struct _obstack_chunk *chunk; /* points to new chunk */ if (alignment == 0) - alignment = (int) DEFAULT_ALIGNMENT; + alignment = DEFAULT_ALIGNMENT; if (size == 0) /* Default size is what GNU malloc can fit in a 4096-byte block. */ { @@ -252,8 +260,7 @@ _obstack_newchunk (struct obstack *h, int length) /* Compute an aligned object_base in the new chunk */ object_base = - __INT_TO_PTR ((__PTR_TO_INT (new_chunk->contents) + h->alignment_mask) - & ~ (h->alignment_mask)); + __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask); /* Move the existing object to the new chunk. Word at a time is fast and is safe if the object diff --git a/lib/obstack.h b/lib/obstack.h index 028dc166f8..8170bf2a63 100644 --- a/lib/obstack.h +++ b/lib/obstack.h @@ -113,19 +113,7 @@ Summary: extern "C" { #endif -/* We use subtraction of (char *) 0 instead of casting to int - because on word-addressable machines a simple cast to int - may ignore the byte-within-word field of the pointer. */ - -#ifndef __PTR_TO_INT -# define __PTR_TO_INT(P) ((P) - (char *) 0) -#endif - -#ifndef __INT_TO_PTR -# define __INT_TO_PTR(P) ((P) + (char *) 0) -#endif - -/* We need the type of the resulting object. If __PTRDIFF_TYPE__ is +/* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is defined, as with GNU C, use that; that way we don't pollute the namespace with 's symbols. Otherwise, include and use ptrdiff_t. */ @@ -137,6 +125,23 @@ extern "C" { # define PTR_INT_TYPE ptrdiff_t #endif +/* If B is the base of an object addressed by P, return the result of + aligning P to the next multiple of A + 1. B and P must be of type + char *. A + 1 must be a power of 2. */ + +#define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A))) + +/* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case + where pointers can be converted to integers, aligned as integers, + and converted back again. If PTR_INT_TYPE is narrower than a + pointer (e.g., the AS/400), play it safe and compute the alignment + relative to B. Otherwise, use the faster strategy of computing the + alignment relative to 0. */ + +#define __PTR_ALIGN(B, P, A) \ + __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \ + P, A) + #include struct _obstack_chunk /* Lives at front of each chunk. */ @@ -153,7 +158,11 @@ struct obstack /* control current object in current chunk */ char *object_base; /* address of object we are building */ char *next_free; /* where to add next char to current object */ char *chunk_limit; /* address of char after current chunk */ - PTR_INT_TYPE temp; /* Temporary for some macros. */ + union + { + PTR_INT_TYPE tempint; + void *tempptr; + } temp; /* Temporary for some macros. */ int alignment_mask; /* Mask of alignment for each object. */ /* These prototypes vary based on `use_extra_arg', and we use casts to the prototypeless function type in all assignments, @@ -413,8 +422,8 @@ __extension__ \ if (__o1->next_free == __value) \ __o1->maybe_empty_object = 1; \ __o1->next_free \ - = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\ - & ~ (__o1->alignment_mask)); \ + = __PTR_ALIGN (__o1->object_base, __o1->next_free, \ + __o1->alignment_mask); \ if (__o1->next_free - (char *)__o1->chunk \ > __o1->chunk_limit - (char *)__o1->chunk) \ __o1->next_free = __o1->chunk_limit; \ @@ -447,23 +456,23 @@ __extension__ \ but some compilers won't accept it. */ # define obstack_make_room(h,length) \ -( (h)->temp = (length), \ - (((h)->next_free + (h)->temp > (h)->chunk_limit) \ - ? (_obstack_newchunk ((h), (h)->temp), 0) : 0)) +( (h)->temp.tempint = (length), \ + (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \ + ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0)) # define obstack_grow(h,where,length) \ -( (h)->temp = (length), \ - (((h)->next_free + (h)->temp > (h)->chunk_limit) \ - ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \ - memcpy ((h)->next_free, where, (h)->temp), \ - (h)->next_free += (h)->temp) +( (h)->temp.tempint = (length), \ + (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \ + ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \ + memcpy ((h)->next_free, where, (h)->temp.tempint), \ + (h)->next_free += (h)->temp.tempint) # define obstack_grow0(h,where,length) \ -( (h)->temp = (length), \ - (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \ - ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0), \ - memcpy ((h)->next_free, where, (h)->temp), \ - (h)->next_free += (h)->temp, \ +( (h)->temp.tempint = (length), \ + (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \ + ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \ + memcpy ((h)->next_free, where, (h)->temp.tempint), \ + (h)->next_free += (h)->temp.tempint, \ *((h)->next_free)++ = 0) # define obstack_1grow(h,datum) \ @@ -488,10 +497,10 @@ __extension__ \ (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr)) # define obstack_blank(h,length) \ -( (h)->temp = (length), \ - (((h)->chunk_limit - (h)->next_free < (h)->temp) \ - ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \ - obstack_blank_fast (h, (h)->temp)) +( (h)->temp.tempint = (length), \ + (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \ + ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \ + obstack_blank_fast (h, (h)->temp.tempint)) # define obstack_alloc(h,length) \ (obstack_blank ((h), (length)), obstack_finish ((h))) @@ -506,22 +515,23 @@ __extension__ \ ( ((h)->next_free == (h)->object_base \ ? (((h)->maybe_empty_object = 1), 0) \ : 0), \ - (h)->temp = __PTR_TO_INT ((h)->object_base), \ + (h)->temp.tempptr = (h)->object_base, \ (h)->next_free \ - = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \ - & ~ ((h)->alignment_mask)), \ + = __PTR_ALIGN ((h)->object_base, (h)->next_free, \ + (h)->alignment_mask), \ (((h)->next_free - (char *) (h)->chunk \ > (h)->chunk_limit - (char *) (h)->chunk) \ ? ((h)->next_free = (h)->chunk_limit) : 0), \ (h)->object_base = (h)->next_free, \ - (void *) __INT_TO_PTR ((h)->temp)) + (h)->temp.tempptr) # define obstack_free(h,obj) \ -( (h)->temp = (char *) (obj) - (char *) (h)->chunk, \ - (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\ +( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \ + ((((h)->temp.tempint > 0 \ + && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \ ? (int) ((h)->next_free = (h)->object_base \ - = (h)->temp + (char *) (h)->chunk) \ - : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0))) + = (h)->temp.tempint + (char *) (h)->chunk) \ + : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0))) #endif /* not __GNUC__ or not __STDC__ */