X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/acfa068f4a1a4652b784af1d7aaac92929399249..564eb533f9f36bbc1a18ea17522d97fa21d1b4bc:/src/gmalloc.c diff --git a/src/gmalloc.c b/src/gmalloc.c index 78d6934755..c50df25cd4 100644 --- a/src/gmalloc.c +++ b/src/gmalloc.c @@ -1,6 +1,6 @@ /* Declarations for `malloc' and friends. - Copyright (C) 1990, 1991, 1992, 1993, 1995, 1996, 1999, 2002, 2003, 2004, - 2005, 2006, 2007 Free Software Foundation, Inc. + Copyright (C) 1990-1993, 1995-1996, 1999, 2002-2007, 2013 Free + Software Foundation, Inc. Written May 1989 by Mike Haertel. This library is free software; you can redistribute it and/or @@ -14,9 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. +License along with this library. If not, see . The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. */ @@ -36,6 +34,10 @@ Fifth Floor, Boston, MA 02110-1301, USA. #include #endif +#ifdef WINDOWSNT +#include /* for sbrk */ +#endif + #ifdef __cplusplus extern "C" { @@ -56,6 +58,7 @@ extern void free (void *ptr); /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */ #ifdef MSDOS +extern void *aligned_alloc (size_t, size_t); extern void *memalign (size_t, size_t); extern int posix_memalign (void **, size_t, size_t); #endif @@ -141,11 +144,11 @@ struct list /* Free list headers for each fragment size. */ extern struct list _fraghead[]; -/* List of blocks allocated with `memalign' (or `valloc'). */ +/* List of blocks allocated with aligned_alloc and friends. */ struct alignlist { struct alignlist *next; - void *aligned; /* The address that memaligned returned. */ + void *aligned; /* The address that aligned_alloc returned. */ void *exact; /* The address that malloc returned. */ }; extern struct alignlist *_aligned_blocks; @@ -288,9 +291,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. +License along with this library. If not, see . The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. */ @@ -460,7 +461,7 @@ get_contiguous_space (ptrdiff_t size, void *position) /* This is called when `_heapinfo' and `heapsize' have just been set to describe a new info table. Set up the table to describe itself and account for it in the statistics. */ -static inline void +static void register_heapinfo (void) { size_t block, blocks; @@ -968,9 +969,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. +License along with this library. If not, see . The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. */ @@ -979,7 +978,7 @@ Fifth Floor, Boston, MA 02110-1301, USA. /* Debugging hook for free. */ void (*__free_hook) (void *__ptr); -/* List of blocks allocated by memalign. */ +/* List of blocks allocated by aligned_alloc. */ struct alignlist *_aligned_blocks = NULL; /* Return memory to the heap. @@ -1282,14 +1281,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. +License along with this library. If not, see . The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. */ +#ifndef min #define min(A, B) ((A) < (B) ? (A) : (B)) +#endif /* On Cygwin the dumped emacs may try to realloc storage allocated in the static heap. We just malloc space in the new heap and copy the @@ -1308,8 +1307,8 @@ special_realloc (void *ptr, size_t size) type == 0 ? bss_sbrk_heapinfo[block].busy.info.size * BLOCKSIZE : (size_t) 1 << type; result = _malloc_internal_nolock (size); - if (result != NULL) - memcpy (result, ptr, min (oldsize, size)); + if (result) + return memcpy (result, ptr, min (oldsize, size)); return result; } #endif @@ -1481,9 +1480,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. +License along with this library. If not, see . The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. */ @@ -1491,13 +1488,20 @@ Fifth Floor, Boston, MA 02110-1301, USA. /* Allocate an array of NMEMB elements each SIZE bytes long. The entire array is initialized to zeros. */ void * -calloc (register size_t nmemb, register size_t size) +calloc (size_t nmemb, size_t size) { - register void *result = malloc (nmemb * size); + void *result; + size_t bytes = nmemb * size; - if (result != NULL) - (void) memset (result, 0, nmemb * size); + if (size != 0 && bytes / size != nmemb) + { + errno = ENOMEM; + return NULL; + } + result = malloc (bytes); + if (result) + return memset (result, 0, bytes); return result; } /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. @@ -1514,9 +1518,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License -along with the GNU C Library; see the file COPYING. If not, write to -the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ +along with the GNU C Library. If not, see . */ /* uClibc defines __GNU_LIBRARY__, but it is not completely compatible. */ @@ -1560,14 +1562,12 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. */ +License along with this library. If not, see . */ void *(*__memalign_hook) (size_t size, size_t alignment); void * -memalign (size_t alignment, size_t size) +aligned_alloc (size_t alignment, size_t size) { void *result; size_t adj, lastadj; @@ -1578,6 +1578,11 @@ memalign (size_t alignment, size_t size) /* Allocate a block with enough extra space to pad the block with up to (ALIGNMENT - 1) bytes if necessary. */ + if (- size < alignment) + { + errno = ENOMEM; + return NULL; + } result = malloc (size + alignment - 1); if (result == NULL) return NULL; @@ -1639,13 +1644,14 @@ memalign (size_t alignment, size_t size) return result; } -#ifndef ENOMEM -#define ENOMEM 12 -#endif +/* An obsolete alias for aligned_alloc, for any old libraries that use + this alias. */ -#ifndef EINVAL -#define EINVAL 22 -#endif +void * +memalign (size_t alignment, size_t size) +{ + return aligned_alloc (alignment, size); +} int posix_memalign (void **memptr, size_t alignment, size_t size) @@ -1657,7 +1663,7 @@ posix_memalign (void **memptr, size_t alignment, size_t size) || (alignment & (alignment - 1)) != 0) return EINVAL; - mem = memalign (alignment, size); + mem = aligned_alloc (alignment, size); if (mem == NULL) return ENOMEM; @@ -1680,9 +1686,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. +License along with this library. If not, see . The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. */ @@ -1704,7 +1708,7 @@ valloc (size_t size) if (pagesize == 0) pagesize = getpagesize (); - return memalign (pagesize, size); + return aligned_alloc (pagesize, size); } #ifdef GC_MCHECK @@ -1724,9 +1728,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public -License along with this library; see the file COPYING. If -not, write to the Free Software Foundation, Inc., 51 Franklin Street, -Fifth Floor, Boston, MA 02110-1301, USA. +License along with this library. If not, see . The author may be reached (Email) at the address mike@ai.mit.edu, or (US mail) as Mike Haertel c/o Free Software Foundation. */ @@ -1812,8 +1814,7 @@ mallochook (size_t size) hdr->size = size; hdr->magic = MAGICWORD; ((char *) &hdr[1])[size] = MAGICBYTE; - memset (hdr + 1, MALLOCFLOOD, size); - return hdr + 1; + return memset (hdr + 1, MALLOCFLOOD, size); } static void *