aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2011-04-17 21:41:29 -0700
committerPaul Eggert2011-04-17 21:41:29 -0700
commit36372bf93fc75b5f85d04007268e98840d1699c5 (patch)
treebd15d9623db1c58cbac99a5b3b40590fee8703c9 /src
parentc7bda33cad5112de8c093dce0eaf62c84fb32063 (diff)
downloademacs-36372bf93fc75b5f85d04007268e98840d1699c5.tar.gz
emacs-36372bf93fc75b5f85d04007268e98840d1699c5.zip
* alloc.c: Remove unportable assumptions about struct layout.
(SDATA_SELECTOR, SDATA_DATA_OFFSET): New macros. (SDATA_OF_STRING, SDATA_SIZE, allocate_string_data): (allocate_vectorlike, make_pure_vector): Use the new macros, plus offsetof, to remove unportable assumptions about struct layout. These assumptions hold on all porting targets that I know of, but they are not guaranteed, they're easy to remove, and removing them makes further changes easier.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog9
-rw-r--r--src/alloc.c32
2 files changed, 22 insertions, 19 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 81053d74480..b7a4a6701e0 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,14 @@
12011-04-18 Paul Eggert <eggert@cs.ucla.edu> 12011-04-18 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 * alloc.c: Remove unportable assumptions about struct layout.
4 (SDATA_SELECTOR, SDATA_DATA_OFFSET): New macros.
5 (SDATA_OF_STRING, SDATA_SIZE, allocate_string_data):
6 (allocate_vectorlike, make_pure_vector): Use the new macros,
7 plus offsetof, to remove unportable assumptions about struct layout.
8 These assumptions hold on all porting targets that I know of, but
9 they are not guaranteed, they're easy to remove, and removing them
10 makes further changes easier.
11
3 * alloc.c (BLOCK BYTES): Fix typo by changing "ablock" to "ablocks". 12 * alloc.c (BLOCK BYTES): Fix typo by changing "ablock" to "ablocks".
4 This doesn't fix a bug but makes the code clearer. 13 This doesn't fix a bug but makes the code clearer.
5 (string_overrun_cookie): Now const. Use initializers that 14 (string_overrun_cookie): Now const. Use initializers that
diff --git a/src/alloc.c b/src/alloc.c
index 2d1c8ffe70b..fbc075be3be 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -1517,6 +1517,7 @@ struct sdata
1517 1517
1518#define SDATA_NBYTES(S) (S)->nbytes 1518#define SDATA_NBYTES(S) (S)->nbytes
1519#define SDATA_DATA(S) (S)->data 1519#define SDATA_DATA(S) (S)->data
1520#define SDATA_SELECTOR(member) member
1520 1521
1521#else /* not GC_CHECK_STRING_BYTES */ 1522#else /* not GC_CHECK_STRING_BYTES */
1522 1523
@@ -1531,8 +1532,11 @@ struct sdata
1531 1532
1532#define SDATA_NBYTES(S) (S)->u.nbytes 1533#define SDATA_NBYTES(S) (S)->u.nbytes
1533#define SDATA_DATA(S) (S)->u.data 1534#define SDATA_DATA(S) (S)->u.data
1535#define SDATA_SELECTOR(member) u.member
1534 1536
1535#endif /* not GC_CHECK_STRING_BYTES */ 1537#endif /* not GC_CHECK_STRING_BYTES */
1538
1539#define SDATA_DATA_OFFSET offsetof (struct sdata, SDATA_SELECTOR (data))
1536}; 1540};
1537 1541
1538 1542
@@ -1608,18 +1612,7 @@ static EMACS_INT total_string_size;
1608 a pointer to the `u.data' member of its sdata structure; the 1612 a pointer to the `u.data' member of its sdata structure; the
1609 structure starts at a constant offset in front of that. */ 1613 structure starts at a constant offset in front of that. */
1610 1614
1611#ifdef GC_CHECK_STRING_BYTES 1615#define SDATA_OF_STRING(S) ((struct sdata *) ((S)->data - SDATA_DATA_OFFSET))
1612
1613#define SDATA_OF_STRING(S) \
1614 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1615 - sizeof (EMACS_INT)))
1616
1617#else /* not GC_CHECK_STRING_BYTES */
1618
1619#define SDATA_OF_STRING(S) \
1620 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1621
1622#endif /* not GC_CHECK_STRING_BYTES */
1623 1616
1624 1617
1625#ifdef GC_CHECK_STRING_OVERRUN 1618#ifdef GC_CHECK_STRING_OVERRUN
@@ -1643,17 +1636,16 @@ static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] =
1643#ifdef GC_CHECK_STRING_BYTES 1636#ifdef GC_CHECK_STRING_BYTES
1644 1637
1645#define SDATA_SIZE(NBYTES) \ 1638#define SDATA_SIZE(NBYTES) \
1646 ((sizeof (struct Lisp_String *) \ 1639 ((SDATA_DATA_OFFSET \
1647 + (NBYTES) + 1 \ 1640 + (NBYTES) + 1 \
1648 + sizeof (EMACS_INT) \
1649 + sizeof (EMACS_INT) - 1) \ 1641 + sizeof (EMACS_INT) - 1) \
1650 & ~(sizeof (EMACS_INT) - 1)) 1642 & ~(sizeof (EMACS_INT) - 1))
1651 1643
1652#else /* not GC_CHECK_STRING_BYTES */ 1644#else /* not GC_CHECK_STRING_BYTES */
1653 1645
1654#define SDATA_SIZE(NBYTES) \ 1646#define SDATA_SIZE(NBYTES) \
1655 ((sizeof (struct Lisp_String *) \ 1647 ((SDATA_DATA_OFFSET \
1656 + (NBYTES) + 1 \ 1648 + max (NBYTES, sizeof (EMACS_INT) - 1) + 1 \
1657 + sizeof (EMACS_INT) - 1) \ 1649 + sizeof (EMACS_INT) - 1) \
1658 & ~(sizeof (EMACS_INT) - 1)) 1650 & ~(sizeof (EMACS_INT) - 1))
1659 1651
@@ -1877,7 +1869,7 @@ allocate_string_data (struct Lisp_String *s,
1877 1869
1878 if (nbytes > LARGE_STRING_BYTES) 1870 if (nbytes > LARGE_STRING_BYTES)
1879 { 1871 {
1880 size_t size = sizeof *b - sizeof (struct sdata) + needed; 1872 size_t size = offsetof (struct sblock, first_data) + needed;
1881 1873
1882#ifdef DOUG_LEA_MALLOC 1874#ifdef DOUG_LEA_MALLOC
1883 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed 1875 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
@@ -2807,7 +2799,8 @@ allocate_vectorlike (EMACS_INT len)
2807 /* This gets triggered by code which I haven't bothered to fix. --Stef */ 2799 /* This gets triggered by code which I haven't bothered to fix. --Stef */
2808 /* eassert (!handling_signal); */ 2800 /* eassert (!handling_signal); */
2809 2801
2810 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0]; 2802 nbytes = (offsetof (struct Lisp_Vector, contents)
2803 + len * sizeof p->contents[0]);
2811 p = (struct Lisp_Vector *) lisp_malloc (nbytes, MEM_TYPE_VECTORLIKE); 2804 p = (struct Lisp_Vector *) lisp_malloc (nbytes, MEM_TYPE_VECTORLIKE);
2812 2805
2813#ifdef DOUG_LEA_MALLOC 2806#ifdef DOUG_LEA_MALLOC
@@ -4735,7 +4728,8 @@ make_pure_vector (EMACS_INT len)
4735{ 4728{
4736 Lisp_Object new; 4729 Lisp_Object new;
4737 struct Lisp_Vector *p; 4730 struct Lisp_Vector *p;
4738 size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object); 4731 size_t size = (offsetof (struct Lisp_Vector, contents)
4732 + len * sizeof (Lisp_Object));
4739 4733
4740 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike); 4734 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike);
4741 XSETVECTOR (new, p); 4735 XSETVECTOR (new, p);