mirror of
https://git.savannah.gnu.org/git/emacs.git
synced 2024-11-26 07:33:47 +00:00
64d1b9fd8a
This incorporates: 2019-12-23 mktime, nstrftime: tweak division performance 2019-12-22 count-leading-zeros: assume 'long long' 2019-12-22 count-one-bits: assume 'long long' 2019-12-22 count-trailing-zeros: assume 'long long' 2019-12-12 inttypes-incomplete: assume 'long long' 2019-12-22 malloca: assume 'long long' 2019-12-22 stdint: assume 'long long' 2019-12-22 strtoll, strtoimax, strtoumax: assume 'long long' 2019-12-22 prefer lib_SOURCES to unconditional AC_LIBOBJ 2019-12-19 nstrftime: avoid a shadowing warning 2019-12-18 improve port of AC_C_RESTRICT to Oracle C++ 2019-12-12 stdalign: port to xlclang 16.01 2019-12-11 stddef, unistd: fix compilation error in C++ mode on MSVC 2019-12-08 fix compilation errors in C++ mode on Haiku 2019-12-08 fix compilation errors in 32-bit C++ mode on HP-UX 11/ia64 2019-12-08 fix compilation error in C++ mode on OpenBSD * build-aux/config.guess, doc/misc/texinfo.tex: * lib/count-leading-zeros.h, lib/count-one-bits.h: * lib/count-trailing-zeros.h, lib/inttypes.in.h, lib/malloca.h: * lib/mktime.c, lib/nstrftime.c, lib/signal.in.h, lib/stdalign.in.h: * lib/stddef.in.h, lib/stdint.in.h, lib/stdio.in.h, lib/stdlib.in.h: * lib/strtoimax.c, lib/unistd.in.h, m4/gnulib-common.m4: * m4/inttypes.m4, m4/largefile.m4, m4/malloca.m4, m4/strtoimax.m4: * m4/strtoll.m4: Copy from Gnulib. Also, change copyright notices in some other Gnulib-copied files to exactly match Gnulib, as Gnulib updated them in a trivially different way. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
106 lines
3.3 KiB
C
106 lines
3.3 KiB
C
/* Safe automatic memory allocation.
|
|
Copyright (C) 2003, 2006-2007, 2009-2020 Free Software Foundation, Inc.
|
|
Written by Bruno Haible <bruno@clisp.org>, 2003, 2018.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3, or (at your option)
|
|
any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
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 program; if not, see <https://www.gnu.org/licenses/>. */
|
|
|
|
#define _GL_USE_STDLIB_ALLOC 1
|
|
#include <config.h>
|
|
|
|
/* Specification. */
|
|
#include "malloca.h"
|
|
|
|
#include "verify.h"
|
|
|
|
/* The speed critical point in this file is freea() applied to an alloca()
|
|
result: it must be fast, to match the speed of alloca(). The speed of
|
|
mmalloca() and freea() in the other case are not critical, because they
|
|
are only invoked for big memory sizes.
|
|
Here we use a bit in the address as an indicator, an idea by Ondřej Bílka.
|
|
malloca() can return three types of pointers:
|
|
- Pointers ≡ 0 mod 2*sa_alignment_max come from stack allocation.
|
|
- Pointers ≡ sa_alignment_max mod 2*sa_alignment_max come from heap
|
|
allocation.
|
|
- NULL comes from a failed heap allocation. */
|
|
|
|
/* Type for holding very small pointer differences. */
|
|
typedef unsigned char small_t;
|
|
/* Verify that it is wide enough. */
|
|
verify (2 * sa_alignment_max - 1 <= (small_t) -1);
|
|
|
|
void *
|
|
mmalloca (size_t n)
|
|
{
|
|
#if HAVE_ALLOCA
|
|
/* Allocate one more word, used to determine the address to pass to freea(),
|
|
and room for the alignment ≡ sa_alignment_max mod 2*sa_alignment_max. */
|
|
size_t nplus = n + sizeof (small_t) + 2 * sa_alignment_max - 1;
|
|
|
|
if (nplus >= n)
|
|
{
|
|
char *mem = (char *) malloc (nplus);
|
|
|
|
if (mem != NULL)
|
|
{
|
|
char *p =
|
|
(char *)((((uintptr_t)mem + sizeof (small_t) + sa_alignment_max - 1)
|
|
& ~(uintptr_t)(2 * sa_alignment_max - 1))
|
|
+ sa_alignment_max);
|
|
/* Here p >= mem + sizeof (small_t),
|
|
and p <= mem + sizeof (small_t) + 2 * sa_alignment_max - 1
|
|
hence p + n <= mem + nplus.
|
|
So, the memory range [p, p+n) lies in the allocated memory range
|
|
[mem, mem + nplus). */
|
|
((small_t *) p)[-1] = p - mem;
|
|
/* p ≡ sa_alignment_max mod 2*sa_alignment_max. */
|
|
return p;
|
|
}
|
|
}
|
|
/* Out of memory. */
|
|
return NULL;
|
|
#else
|
|
# if !MALLOC_0_IS_NONNULL
|
|
if (n == 0)
|
|
n = 1;
|
|
# endif
|
|
return malloc (n);
|
|
#endif
|
|
}
|
|
|
|
#if HAVE_ALLOCA
|
|
void
|
|
freea (void *p)
|
|
{
|
|
/* Check argument. */
|
|
if ((uintptr_t) p & (sa_alignment_max - 1))
|
|
{
|
|
/* p was not the result of a malloca() call. Invalid argument. */
|
|
abort ();
|
|
}
|
|
/* Determine whether p was a non-NULL pointer returned by mmalloca(). */
|
|
if ((uintptr_t) p & sa_alignment_max)
|
|
{
|
|
void *mem = (char *) p - ((small_t *) p)[-1];
|
|
free (mem);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* Hey Emacs!
|
|
* Local Variables:
|
|
* coding: utf-8
|
|
* End:
|
|
*/
|