aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c42
1 files changed, 32 insertions, 10 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index bc1d85ac5fb..fc728eeea7e 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -58,6 +58,7 @@ extern void free (void *ptr);
58 58
59/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */ 59/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
60#ifdef MSDOS 60#ifdef MSDOS
61extern void *aligned_alloc (size_t, size_t);
61extern void *memalign (size_t, size_t); 62extern void *memalign (size_t, size_t);
62extern int posix_memalign (void **, size_t, size_t); 63extern int posix_memalign (void **, size_t, size_t);
63#endif 64#endif
@@ -143,11 +144,11 @@ struct list
143/* Free list headers for each fragment size. */ 144/* Free list headers for each fragment size. */
144extern struct list _fraghead[]; 145extern struct list _fraghead[];
145 146
146/* List of blocks allocated with `memalign' (or `valloc'). */ 147/* List of blocks allocated with aligned_alloc and friends. */
147struct alignlist 148struct alignlist
148 { 149 {
149 struct alignlist *next; 150 struct alignlist *next;
150 void *aligned; /* The address that memaligned returned. */ 151 void *aligned; /* The address that aligned_alloc returned. */
151 void *exact; /* The address that malloc returned. */ 152 void *exact; /* The address that malloc returned. */
152 }; 153 };
153extern struct alignlist *_aligned_blocks; 154extern struct alignlist *_aligned_blocks;
@@ -977,7 +978,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
977/* Debugging hook for free. */ 978/* Debugging hook for free. */
978void (*__free_hook) (void *__ptr); 979void (*__free_hook) (void *__ptr);
979 980
980/* List of blocks allocated by memalign. */ 981/* List of blocks allocated by aligned_alloc. */
981struct alignlist *_aligned_blocks = NULL; 982struct alignlist *_aligned_blocks = NULL;
982 983
983/* Return memory to the heap. 984/* Return memory to the heap.
@@ -1487,13 +1488,20 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
1487/* Allocate an array of NMEMB elements each SIZE bytes long. 1488/* Allocate an array of NMEMB elements each SIZE bytes long.
1488 The entire array is initialized to zeros. */ 1489 The entire array is initialized to zeros. */
1489void * 1490void *
1490calloc (register size_t nmemb, register size_t size) 1491calloc (size_t nmemb, size_t size)
1491{ 1492{
1492 register void *result = malloc (nmemb * size); 1493 void *result;
1494 size_t bytes = nmemb * size;
1493 1495
1494 if (result != NULL) 1496 if (size != 0 && bytes / size != nmemb)
1495 (void) memset (result, 0, nmemb * size); 1497 {
1498 errno = ENOMEM;
1499 return NULL;
1500 }
1496 1501
1502 result = malloc (bytes);
1503 if (result)
1504 memset (result, 0, bytes);
1497 return result; 1505 return result;
1498} 1506}
1499/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. 1507/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
@@ -1559,7 +1567,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. *
1559void *(*__memalign_hook) (size_t size, size_t alignment); 1567void *(*__memalign_hook) (size_t size, size_t alignment);
1560 1568
1561void * 1569void *
1562memalign (size_t alignment, size_t size) 1570aligned_alloc (size_t alignment, size_t size)
1563{ 1571{
1564 void *result; 1572 void *result;
1565 size_t adj, lastadj; 1573 size_t adj, lastadj;
@@ -1570,6 +1578,11 @@ memalign (size_t alignment, size_t size)
1570 1578
1571 /* Allocate a block with enough extra space to pad the block with up to 1579 /* Allocate a block with enough extra space to pad the block with up to
1572 (ALIGNMENT - 1) bytes if necessary. */ 1580 (ALIGNMENT - 1) bytes if necessary. */
1581 if (- size < alignment)
1582 {
1583 errno = ENOMEM;
1584 return NULL;
1585 }
1573 result = malloc (size + alignment - 1); 1586 result = malloc (size + alignment - 1);
1574 if (result == NULL) 1587 if (result == NULL)
1575 return NULL; 1588 return NULL;
@@ -1631,6 +1644,15 @@ memalign (size_t alignment, size_t size)
1631 return result; 1644 return result;
1632} 1645}
1633 1646
1647/* An obsolete alias for aligned_alloc, for any old libraries that use
1648 this alias. */
1649
1650void *
1651memalign (size_t alignment, size_t size)
1652{
1653 return aligned_alloc (alignment, size);
1654}
1655
1634int 1656int
1635posix_memalign (void **memptr, size_t alignment, size_t size) 1657posix_memalign (void **memptr, size_t alignment, size_t size)
1636{ 1658{
@@ -1641,7 +1663,7 @@ posix_memalign (void **memptr, size_t alignment, size_t size)
1641 || (alignment & (alignment - 1)) != 0) 1663 || (alignment & (alignment - 1)) != 0)
1642 return EINVAL; 1664 return EINVAL;
1643 1665
1644 mem = memalign (alignment, size); 1666 mem = aligned_alloc (alignment, size);
1645 if (mem == NULL) 1667 if (mem == NULL)
1646 return ENOMEM; 1668 return ENOMEM;
1647 1669
@@ -1686,7 +1708,7 @@ valloc (size_t size)
1686 if (pagesize == 0) 1708 if (pagesize == 0)
1687 pagesize = getpagesize (); 1709 pagesize = getpagesize ();
1688 1710
1689 return memalign (pagesize, size); 1711 return aligned_alloc (pagesize, size);
1690} 1712}
1691 1713
1692#ifdef GC_MCHECK 1714#ifdef GC_MCHECK