diff options
| author | Paul Eggert | 2013-11-06 21:31:04 -0800 |
|---|---|---|
| committer | Paul Eggert | 2013-11-06 21:31:04 -0800 |
| commit | aea07e2c6e54733804d0be54e97d44fcb3df63dd (patch) | |
| tree | 1f84e37d28aa136603322428a81c2f0046bf9844 /src/alloc.c | |
| parent | e6e4db3cac4630fc83e4bc520f99823572c3e592 (diff) | |
| download | emacs-aea07e2c6e54733804d0be54e97d44fcb3df63dd.tar.gz emacs-aea07e2c6e54733804d0be54e97d44fcb3df63dd.zip | |
Port to C11 aligned_alloc, and fix some integer overflows.
* configure.ac (GMALLOC_OBJ): Initialize to empty if !system_malloc
and doug_lea_malloc.
(aligned_alloc): Test for existence if !GMALLOC_OBJ and not darwin.
(posix_memalign): Test for existence only if !GMALLOC_OBJ and
not darwin and !aligned_alloc.
* src/alloc.c (USE_ALIGNED_ALLOC): New symbol.
(USE_POSIX_MEMALIGN): Remove. All uses replaced with USE_ALIGNED_ALLOC,
and use of posix_memalign replaced with aligned_alloc.
(aligned_alloc): New function, defined or declared as needed.
* src/conf_post.h (HAVE_POSIX_MEMALIGN) [DARWIN_OS]:
Don't undef; configure.ac now does this.
* src/gmalloc.c (aligned_alloc) [MSDOS]: New decl.
(calloc, aligned_alloc): Check for integer overflow.
(aligned_alloc): Rename from memalign. All uses changed.
(memalign): New function, an alias for aligned_alloc.
Diffstat (limited to 'src/alloc.c')
| -rw-r--r-- | src/alloc.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/alloc.c b/src/alloc.c index 7054083acba..bc5ed6d94bb 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -920,8 +920,20 @@ lisp_free (void *block) | |||
| 920 | /* The entry point is lisp_align_malloc which returns blocks of at most | 920 | /* The entry point is lisp_align_malloc which returns blocks of at most |
| 921 | BLOCK_BYTES and guarantees they are aligned on a BLOCK_ALIGN boundary. */ | 921 | BLOCK_BYTES and guarantees they are aligned on a BLOCK_ALIGN boundary. */ |
| 922 | 922 | ||
| 923 | #if defined (HAVE_POSIX_MEMALIGN) && defined (SYSTEM_MALLOC) | 923 | #if !defined SYSTEM_MALLOC && !defined DOUG_LEA_MALLOC |
| 924 | #define USE_POSIX_MEMALIGN 1 | 924 | # define USE_ALIGNED_ALLOC 1 |
| 925 | /* Defined in gmalloc.c. */ | ||
| 926 | void *aligned_alloc (size_t, size_t); | ||
| 927 | #elif defined HAVE_ALIGNED_ALLOC | ||
| 928 | # define USE_ALIGNED_ALLOC 1 | ||
| 929 | #elif defined HAVE_POSIX_MEMALIGN | ||
| 930 | # define USE_ALIGNED_ALLOC 1 | ||
| 931 | static void * | ||
| 932 | aligned_alloc (size_t alignment, size_t size) | ||
| 933 | { | ||
| 934 | void *p; | ||
| 935 | return posix_memalign (&p, alignment, size) == 0 ? p : 0; | ||
| 936 | } | ||
| 925 | #endif | 937 | #endif |
| 926 | 938 | ||
| 927 | /* BLOCK_ALIGN has to be a power of 2. */ | 939 | /* BLOCK_ALIGN has to be a power of 2. */ |
| @@ -931,7 +943,7 @@ lisp_free (void *block) | |||
| 931 | malloc a chance to minimize the amount of memory wasted to alignment. | 943 | malloc a chance to minimize the amount of memory wasted to alignment. |
| 932 | It should be tuned to the particular malloc library used. | 944 | It should be tuned to the particular malloc library used. |
| 933 | On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best. | 945 | On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best. |
| 934 | posix_memalign on the other hand would ideally prefer a value of 4 | 946 | aligned_alloc on the other hand would ideally prefer a value of 4 |
| 935 | because otherwise, there's 1020 bytes wasted between each ablocks. | 947 | because otherwise, there's 1020 bytes wasted between each ablocks. |
| 936 | In Emacs, testing shows that those 1020 can most of the time be | 948 | In Emacs, testing shows that those 1020 can most of the time be |
| 937 | efficiently used by malloc to place other objects, so a value of 0 can | 949 | efficiently used by malloc to place other objects, so a value of 0 can |
| @@ -976,7 +988,7 @@ struct ablocks | |||
| 976 | struct ablock blocks[ABLOCKS_SIZE]; | 988 | struct ablock blocks[ABLOCKS_SIZE]; |
| 977 | }; | 989 | }; |
| 978 | 990 | ||
| 979 | /* Size of the block requested from malloc or posix_memalign. */ | 991 | /* Size of the block requested from malloc or aligned_alloc. */ |
| 980 | #define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING) | 992 | #define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING) |
| 981 | 993 | ||
| 982 | #define ABLOCK_ABASE(block) \ | 994 | #define ABLOCK_ABASE(block) \ |
| @@ -988,7 +1000,7 @@ struct ablocks | |||
| 988 | #define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase) | 1000 | #define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase) |
| 989 | 1001 | ||
| 990 | /* Pointer to the (not necessarily aligned) malloc block. */ | 1002 | /* Pointer to the (not necessarily aligned) malloc block. */ |
| 991 | #ifdef USE_POSIX_MEMALIGN | 1003 | #ifdef USE_ALIGNED_ALLOC |
| 992 | #define ABLOCKS_BASE(abase) (abase) | 1004 | #define ABLOCKS_BASE(abase) (abase) |
| 993 | #else | 1005 | #else |
| 994 | #define ABLOCKS_BASE(abase) \ | 1006 | #define ABLOCKS_BASE(abase) \ |
| @@ -1027,13 +1039,8 @@ lisp_align_malloc (size_t nbytes, enum mem_type type) | |||
| 1027 | mallopt (M_MMAP_MAX, 0); | 1039 | mallopt (M_MMAP_MAX, 0); |
| 1028 | #endif | 1040 | #endif |
| 1029 | 1041 | ||
| 1030 | #ifdef USE_POSIX_MEMALIGN | 1042 | #ifdef USE_ALIGNED_ALLOC |
| 1031 | { | 1043 | abase = base = aligned_alloc (BLOCK_ALIGN, ABLOCKS_BYTES); |
| 1032 | int err = posix_memalign (&base, BLOCK_ALIGN, ABLOCKS_BYTES); | ||
| 1033 | if (err) | ||
| 1034 | base = NULL; | ||
| 1035 | abase = base; | ||
| 1036 | } | ||
| 1037 | #else | 1044 | #else |
| 1038 | base = malloc (ABLOCKS_BYTES); | 1045 | base = malloc (ABLOCKS_BYTES); |
| 1039 | abase = ALIGN (base, BLOCK_ALIGN); | 1046 | abase = ALIGN (base, BLOCK_ALIGN); |