bug-gnulib
[Top][All Lists]
Advanced

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

[Bug-gnulib] obstack.c, obstack.h port to AS/400


From: Paul Eggert
Subject: [Bug-gnulib] obstack.c, obstack.h port to AS/400
Date: Mon, 17 May 2004 00:54:23 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed the following:

2004-05-17  Paul Eggert  <address@hidden>

        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
        <http://sources.redhat.com/ml/libc-alpha/2004-05/msg00073.html>.

        * 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 <stddef.h>, 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.

Index: lib/obstack.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/obstack.c,v
retrieving revision 1.24
diff -p -u -r1.24 obstack.c
--- lib/obstack.c       7 May 2004 04:22:58 -0000       1.24
+++ lib/obstack.c       17 May 2004 07:51:57 -0000
@@ -54,18 +54,26 @@
 # include <wchar.h>
 #endif
 
+#include <stddef.h>
+
 #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
   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, in
 
   /* 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
Index: lib/obstack.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/obstack.h,v
retrieving revision 1.26
diff -p -u -r1.26 obstack.h
--- lib/obstack.h       7 May 2004 04:22:59 -0000       1.26
+++ lib/obstack.h       17 May 2004 07:51:57 -0000
@@ -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 <stddef.h>'s symbols.  Otherwise, include <stddef.h>
    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 <string.h>
 
 struct _obstack_chunk          /* Lives at front of each chunk. */
@@ -153,7 +158,11 @@ struct obstack             /* control current objec
   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__ */
 




reply via email to

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