diff options
| author | Paul Eggert | 2016-09-07 18:08:45 -0700 |
|---|---|---|
| committer | Paul Eggert | 2016-09-07 18:10:11 -0700 |
| commit | d2f1971dd570439da4198fa76603b53b072060f8 (patch) | |
| tree | 38b1ddbeda27b6ed6ac52205169624608cc597fd /src/alloc.c | |
| parent | 12a7e0f88eaa68aabe7e32589e2d5c8f776f6346 (diff) | |
| download | emacs-d2f1971dd570439da4198fa76603b53b072060f8.tar.gz emacs-d2f1971dd570439da4198fa76603b53b072060f8.zip | |
Port flexible array members to GCC + valgrind
These changes are needed to conform to the C standard's rule for
allocating structs containing flexible array members. C11 says
that malloc (offsetof (struct s, m) + n) does not suffice to
allocate a struct with an n-byte tail; instead, malloc’s arg
should be rounded up to the nearest multiple of alignof (struct s).
Although this is arguably a defect in C11, gcc -O2 + valgrind
sometimes complains when this rule is violated, and when debugging
it’s better to keep valgrind happy.
For details please see the thread containing the message at:
https://gcc.gnu.org/ml/gcc-patches/2016-09/msg00416.html
* lib-src/ebrowse.c, src/alloc.c, src/image.c, src/process.c:
Include flexmember.h.
* lib-src/ebrowse.c (add_sym, add_member, make_namespace)
(register_namespace_alias):
* src/alloc.c (SDATA_SIZE, allocate_string_data):
* src/image.c (xpm_cache_color, imagemagick_create_cache):
* src/process.c (Fmake_network_process):
Use FLEXSIZEOF instead of offsetof and addition.
* src/alloc.c (SDATA_SIZE, vector_alignment):
Use FLEXALIGNOF instead of sizeof (ptrdiff_t).
* src/lisp.h (ALIGNOF_STRUCT_LISP_VECTOR):
Remove, as alloc.c can now calculate this on its own.
Diffstat (limited to 'src/alloc.c')
| -rw-r--r-- | src/alloc.c | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/alloc.c b/src/alloc.c index 67187f12ea6..5bbd5e55c42 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -46,6 +46,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 46 | #include TERM_HEADER | 46 | #include TERM_HEADER |
| 47 | #endif /* HAVE_WINDOW_SYSTEM */ | 47 | #endif /* HAVE_WINDOW_SYSTEM */ |
| 48 | 48 | ||
| 49 | #include <flexmember.h> | ||
| 49 | #include <verify.h> | 50 | #include <verify.h> |
| 50 | #include <execinfo.h> /* For backtrace. */ | 51 | #include <execinfo.h> /* For backtrace. */ |
| 51 | 52 | ||
| @@ -1757,27 +1758,23 @@ static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] = | |||
| 1757 | 1758 | ||
| 1758 | #ifdef GC_CHECK_STRING_BYTES | 1759 | #ifdef GC_CHECK_STRING_BYTES |
| 1759 | 1760 | ||
| 1760 | #define SDATA_SIZE(NBYTES) \ | 1761 | #define SDATA_SIZE(NBYTES) FLEXSIZEOF (struct sdata, data, NBYTES) |
| 1761 | ((SDATA_DATA_OFFSET \ | ||
| 1762 | + (NBYTES) + 1 \ | ||
| 1763 | + sizeof (ptrdiff_t) - 1) \ | ||
| 1764 | & ~(sizeof (ptrdiff_t) - 1)) | ||
| 1765 | 1762 | ||
| 1766 | #else /* not GC_CHECK_STRING_BYTES */ | 1763 | #else /* not GC_CHECK_STRING_BYTES */ |
| 1767 | 1764 | ||
| 1768 | /* The 'max' reserves space for the nbytes union member even when NBYTES + 1 is | 1765 | /* The 'max' reserves space for the nbytes union member even when NBYTES + 1 is |
| 1769 | less than the size of that member. The 'max' is not needed when | 1766 | less than the size of that member. The 'max' is not needed when |
| 1770 | SDATA_DATA_OFFSET is a multiple of sizeof (ptrdiff_t), because then the | 1767 | SDATA_DATA_OFFSET is a multiple of FLEXALIGNOF (struct sdata), |
| 1771 | alignment code reserves enough space. */ | 1768 | because then the alignment code reserves enough space. */ |
| 1772 | 1769 | ||
| 1773 | #define SDATA_SIZE(NBYTES) \ | 1770 | #define SDATA_SIZE(NBYTES) \ |
| 1774 | ((SDATA_DATA_OFFSET \ | 1771 | ((SDATA_DATA_OFFSET \ |
| 1775 | + (SDATA_DATA_OFFSET % sizeof (ptrdiff_t) == 0 \ | 1772 | + (SDATA_DATA_OFFSET % FLEXALIGNOF (struct sdata) == 0 \ |
| 1776 | ? NBYTES \ | 1773 | ? NBYTES \ |
| 1777 | : max (NBYTES, sizeof (ptrdiff_t) - 1)) \ | 1774 | : max (NBYTES, FLEXALIGNOF (struct sdata) - 1)) \ |
| 1778 | + 1 \ | 1775 | + 1 \ |
| 1779 | + sizeof (ptrdiff_t) - 1) \ | 1776 | + FLEXALIGNOF (struct sdata) - 1) \ |
| 1780 | & ~(sizeof (ptrdiff_t) - 1)) | 1777 | & ~(FLEXALIGNOF (struct sdata) - 1)) |
| 1781 | 1778 | ||
| 1782 | #endif /* not GC_CHECK_STRING_BYTES */ | 1779 | #endif /* not GC_CHECK_STRING_BYTES */ |
| 1783 | 1780 | ||
| @@ -1997,7 +1994,7 @@ allocate_string_data (struct Lisp_String *s, | |||
| 1997 | 1994 | ||
| 1998 | if (nbytes > LARGE_STRING_BYTES) | 1995 | if (nbytes > LARGE_STRING_BYTES) |
| 1999 | { | 1996 | { |
| 2000 | size_t size = offsetof (struct sblock, data) + needed; | 1997 | size_t size = FLEXSIZEOF (struct sblock, data, needed); |
| 2001 | 1998 | ||
| 2002 | #ifdef DOUG_LEA_MALLOC | 1999 | #ifdef DOUG_LEA_MALLOC |
| 2003 | if (!mmap_lisp_allowed_p ()) | 2000 | if (!mmap_lisp_allowed_p ()) |
| @@ -2953,15 +2950,15 @@ set_next_vector (struct Lisp_Vector *v, struct Lisp_Vector *p) | |||
| 2953 | enum | 2950 | enum |
| 2954 | { | 2951 | { |
| 2955 | /* Alignment of struct Lisp_Vector objects. */ | 2952 | /* Alignment of struct Lisp_Vector objects. */ |
| 2956 | vector_alignment = COMMON_MULTIPLE (ALIGNOF_STRUCT_LISP_VECTOR, | 2953 | vector_alignment = COMMON_MULTIPLE (FLEXALIGNOF (struct Lisp_Vector), |
| 2957 | GCALIGNMENT), | 2954 | GCALIGNMENT), |
| 2958 | 2955 | ||
| 2959 | /* Vector size requests are a multiple of this. */ | 2956 | /* Vector size requests are a multiple of this. */ |
| 2960 | roundup_size = COMMON_MULTIPLE (vector_alignment, word_size) | 2957 | roundup_size = COMMON_MULTIPLE (vector_alignment, word_size) |
| 2961 | }; | 2958 | }; |
| 2962 | 2959 | ||
| 2963 | /* Verify assumptions described above. */ | 2960 | /* Verify assumptions described above. */ |
| 2964 | verify ((VECTOR_BLOCK_SIZE % roundup_size) == 0); | 2961 | verify (VECTOR_BLOCK_SIZE % roundup_size == 0); |
| 2965 | verify (VECTOR_BLOCK_SIZE <= (1 << PSEUDOVECTOR_SIZE_BITS)); | 2962 | verify (VECTOR_BLOCK_SIZE <= (1 << PSEUDOVECTOR_SIZE_BITS)); |
| 2966 | 2963 | ||
| 2967 | /* Round up X to nearest mult-of-ROUNDUP_SIZE --- use at compile time. */ | 2964 | /* Round up X to nearest mult-of-ROUNDUP_SIZE --- use at compile time. */ |