#define LARGE_STRING_BYTES 1024
-/* The SDATA typedef is a struct or union describing string memory
- sub-allocated from an sblock. This is where the contents of Lisp
- strings are stored. */
+/* The layout of a nonnull string. */
struct sdata
{
unsigned char data[FLEXIBLE_ARRAY_MEMBER];
};
-#ifdef GC_CHECK_STRING_BYTES
-
-typedef struct sdata sdata;
-#define SDATA_NBYTES(S) (S)->nbytes
-#define SDATA_DATA(S) (S)->data
-
-#else
+/* A union describing string memory sub-allocated from an sblock.
+ This is where the contents of Lisp strings are stored. */
typedef union
{
#define SDATA_NBYTES(S) (S)->n.nbytes
#define SDATA_DATA(S) ((struct sdata *) (S))->data
-#endif /* not GC_CHECK_STRING_BYTES */
-
enum { SDATA_DATA_OFFSET = offsetof (struct sdata, data) };
/* Structure describing a block of memory which is sub-allocated to
#define GC_STRING_OVERRUN_COOKIE_SIZE 0
#endif
-/* Value is the size of an sdata structure large enough to hold NBYTES
- bytes of string data. The value returned includes a terminating
- NUL byte, the size of the sdata structure, and padding. */
-
-#ifdef GC_CHECK_STRING_BYTES
-
-#define SDATA_SIZE(NBYTES) FLEXSIZEOF (struct sdata, data, (NBYTES) + 1)
-
-#else /* not GC_CHECK_STRING_BYTES */
-
-/* The 'max' reserves space for the nbytes union member even when NBYTES + 1 is
- less than the size of that member. The 'max' is not needed when
- SDATA_DATA_OFFSET is a multiple of FLEXALIGNOF (struct sdata),
- because then the alignment code reserves enough space. */
-
-#define SDATA_SIZE(NBYTES) \
- ((SDATA_DATA_OFFSET \
- + (SDATA_DATA_OFFSET % FLEXALIGNOF (struct sdata) == 0 \
- ? NBYTES \
- : max (NBYTES, FLEXALIGNOF (struct sdata) - 1)) \
- + 1 \
- + FLEXALIGNOF (struct sdata) - 1) \
- & ~(FLEXALIGNOF (struct sdata) - 1))
+/* Return the size of an sdata structure large enough to hold N bytes
+ of string data. This counts the sdata structure, the N bytes, a
+ terminating NUL byte, and alignment padding. */
-#endif /* not GC_CHECK_STRING_BYTES */
+static ptrdiff_t
+sdata_size (ptrdiff_t n)
+{
+ /* Reserve space for the nbytes union member even when N + 1 is less
+ than the size of that member. */
+ ptrdiff_t unaligned_size = max (SDATA_DATA_OFFSET + n + 1,
+ sizeof (sdata));
+ int sdata_align = max (FLEXALIGNOF (struct sdata), alignof (sdata));
+ return (unaligned_size + sdata_align - 1) & ~(sdata_align - 1);
+}
/* Extra bytes to allocate for each string. */
static void
check_sblock (struct sblock *b)
{
- sdata *from, *end, *from_end;
-
- end = b->next_free;
+ sdata *end = b->next_free;
- for (from = b->data; from < end; from = from_end)
+ for (sdata *from = b->data; from < end; )
{
- /* Compute the next FROM here because copying below may
- overwrite data we need to compute it. */
- ptrdiff_t nbytes;
-
- /* Check that the string size recorded in the string is the
- same as the one recorded in the sdata structure. */
- nbytes = SDATA_SIZE (from->string ? string_bytes (from->string)
- : SDATA_NBYTES (from));
- from_end = (sdata *) ((char *) from + nbytes + GC_STRING_EXTRA);
+ ptrdiff_t nbytes = sdata_size (from->string
+ ? string_bytes (from->string)
+ : SDATA_NBYTES (from));
+ from = (sdata *) ((char *) from + nbytes + GC_STRING_EXTRA);
}
}
{
sdata *data, *old_data;
struct sblock *b;
- ptrdiff_t needed, old_nbytes;
+ ptrdiff_t old_nbytes;
if (STRING_BYTES_MAX < nbytes)
string_overflow ();
/* Determine the number of bytes needed to store NBYTES bytes
of string data. */
- needed = SDATA_SIZE (nbytes);
+ ptrdiff_t needed = sdata_size (nbytes);
if (s->u.s.data)
{
old_data = SDATA_OF_STRING (s);
nbytes = s ? STRING_BYTES (s) : SDATA_NBYTES (from);
eassert (nbytes <= LARGE_STRING_BYTES);
- nbytes = SDATA_SIZE (nbytes);
+ nbytes = sdata_size (nbytes);
sdata *from_end = (sdata *) ((char *) from
+ nbytes + GC_STRING_EXTRA);