aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
authorEli Zaretskii2021-05-20 11:44:54 +0300
committerEli Zaretskii2021-05-20 11:44:54 +0300
commit328efb47d04e3aa996bb8cd387d01c1a66ec29f5 (patch)
treeaf1c147167f2f02896209d3a439bb6589387a16c /src/gmalloc.c
parentb2eed2ae568b53ac910f4a3b8458eedf8d8c67ec (diff)
downloademacs-328efb47d04e3aa996bb8cd387d01c1a66ec29f5.tar.gz
emacs-328efb47d04e3aa996bb8cd387d01c1a66ec29f5.zip
Make sure gmalloc's hybrid_free preserves errno
* src/gmalloc.c (hybrid_free_1): New function, with the body of the previous 'hybrid_free'. (hybrid_free): Call 'hybrid_free_1' while preserving the value of 'errno'. Suggested by Paul Eggert <eggert@cs.ucla.edu>.
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index dedd25fa22f..55ae7365d99 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -1726,8 +1726,8 @@ hybrid_calloc (size_t nmemb, size_t size)
1726 return gcalloc (nmemb, size); 1726 return gcalloc (nmemb, size);
1727} 1727}
1728 1728
1729void 1729static void
1730hybrid_free (void *ptr) 1730hybrid_free_1 (void *ptr)
1731{ 1731{
1732 if (allocated_via_gmalloc (ptr)) 1732 if (allocated_via_gmalloc (ptr))
1733 gfree (ptr); 1733 gfree (ptr);
@@ -1735,6 +1735,24 @@ hybrid_free (void *ptr)
1735 free (ptr); 1735 free (ptr);
1736} 1736}
1737 1737
1738void
1739hybrid_free (void *ptr)
1740{
1741 /* Stolen from Gnulib, to make sure we preserve errno. */
1742#if defined __GNUC__ && !defined __clang__
1743 int err[2];
1744 err[0] = errno;
1745 err[1] = errno;
1746 errno = 0;
1747 hybrid_free_1 (ptr);
1748 errno = err[errno == 0];
1749#else
1750 int err = errno;
1751 hybrid_free_1 (ptr);
1752 errno = err;
1753#endif
1754}
1755
1738#if defined HAVE_ALIGNED_ALLOC || defined HAVE_POSIX_MEMALIGN 1756#if defined HAVE_ALIGNED_ALLOC || defined HAVE_POSIX_MEMALIGN
1739void * 1757void *
1740hybrid_aligned_alloc (size_t alignment, size_t size) 1758hybrid_aligned_alloc (size_t alignment, size_t size)