diff options
author | Bruno Haible <bruno@clisp.org> | 2021-04-04 16:01:02 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2021-04-04 20:59:16 +0200 |
commit | 14b07598f1fa770873ab8aa5ad8f2445b163fab1 (patch) | |
tree | 4991a6480790bf1204c265e1f7afda0dcf28aa02 | |
parent | 0d606b37f90316b51a59ec5fefea7383bf3a0882 (diff) | |
download | gettext-14b07598f1fa770873ab8aa5ad8f2445b163fab1.tar.gz |
Use xalloc module from gnulib.
* gnulib-local/lib/xalloc.h: Remove file.
* gnulib-local/lib/xmalloc.c: Remove file.
* gnulib-local/lib/xstrdup.c: Remove file.
* gnulib-local/modules/xalloc: Remove file.
* gnulib-local/modules/xalloc-die: Remove file.
* gnulib-local/Makefile.am (EXTRA_DIST): Remove them.
* libtextstyle/lib/misc.c (xmalloc_exit_failure): Remove declaration.
(libtextstyle_set_failure_exit_code): Don't set xmalloc_exit_failure.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | gnulib-local/Makefile.am | 5 | ||||
-rw-r--r-- | gnulib-local/lib/xalloc.h | 157 | ||||
-rw-r--r-- | gnulib-local/lib/xmalloc.c | 232 | ||||
-rw-r--r-- | gnulib-local/lib/xstrdup.c | 41 | ||||
-rw-r--r-- | gnulib-local/modules/xalloc | 29 | ||||
-rw-r--r-- | gnulib-local/modules/xalloc-die | 21 | ||||
-rw-r--r-- | libtextstyle/.gitignore | 1 | ||||
-rw-r--r-- | libtextstyle/lib/misc.c | 5 |
9 files changed, 3 insertions, 489 deletions
diff --git a/.gitignore b/.gitignore index 4110c15b1..b194c84d8 100644 --- a/.gitignore +++ b/.gitignore @@ -267,6 +267,7 @@ /gettext-tools/libgettextpo/windows-rwlock.h /gettext-tools/libgettextpo/windows-tls.c /gettext-tools/libgettextpo/windows-tls.h +/gettext-tools/libgettextpo/xalloc-die.c /gettext-tools/libgettextpo/xalloc-oversized.h /gettext-tools/libgettextpo/xalloc.h /gettext-tools/libgettextpo/xasprintf.c diff --git a/gnulib-local/Makefile.am b/gnulib-local/Makefile.am index babd8b246..8045a8068 100644 --- a/gnulib-local/Makefile.am +++ b/gnulib-local/Makefile.am @@ -144,13 +144,10 @@ lib/obstack.h.diff \ lib/progname.h.diff \ lib/regex_internal.h.diff \ lib/unistd.in.h.diff \ -lib/xalloc.h \ lib/xerror.c \ lib/xerror.h \ lib/xgetcwd.c \ lib/xgetcwd.h \ -lib/xmalloc.c \ -lib/xstrdup.c \ m4/backupfile.m4 \ m4/java.m4 \ m4/libxml.m4 \ @@ -164,8 +161,6 @@ modules/libtextstyle-dummy \ modules/libxml \ modules/markup \ modules/mem-hash-map \ -modules/xalloc \ -modules/xalloc-die \ modules/xerror \ modules/xgetcwd diff --git a/gnulib-local/lib/xalloc.h b/gnulib-local/lib/xalloc.h deleted file mode 100644 index 460d06c69..000000000 --- a/gnulib-local/lib/xalloc.h +++ /dev/null @@ -1,157 +0,0 @@ -/* malloc with out of memory checking. - Copyright (C) 2001-2004, 2006, 2019-2020 Free Software Foundation, Inc. - Written by Bruno Haible <haible@clisp.cons.org>, 2001. - - 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 of the License, 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/>. */ - -#ifndef _XALLOC_H -#define _XALLOC_H - -#include <stddef.h> - -#include "noreturn.h" -#include "xalloc-oversized.h" - - -#ifdef __cplusplus -extern "C" { -#endif - - -/* Defined in xmalloc.c. */ - -/* Allocate SIZE bytes of memory dynamically, with error checking. */ -extern void *xmalloc (size_t size); - -/* Allocate memory for NMEMB elements of SIZE bytes, with error checking. - SIZE must be > 0. */ -extern void *xnmalloc (size_t nmemb, size_t size); - -/* Allocate SIZE bytes of memory dynamically, with error checking, - and zero it. */ -extern void *xzalloc (size_t size); - -/* Allocate memory for NMEMB elements of SIZE bytes, with error checking, - and zero it. */ -extern void *xcalloc (size_t nmemb, size_t size); - -/* Change the size of an allocated block of memory PTR to SIZE bytes, - with error checking. If PTR is NULL, run xmalloc. */ -extern void *xrealloc (void *ptr, size_t size); -#ifdef __cplusplus -} -template <typename T> - inline T * xrealloc (T * ptr, size_t size) - { - return (T *) xrealloc ((void *) ptr, size); - } -extern "C" { -#endif - -/* If P is null, allocate a block of at least *PN bytes; otherwise, - reallocate P so that it contains more than *PN bytes. *PN must be - nonzero unless P is null. Set *PN to the new block's size, and - return the pointer to the new block. *PN is never set to zero, and - the returned pointer is never null. */ -extern void *x2realloc (void *ptr, size_t *pn); -#ifdef __cplusplus -} -template <typename T> - inline T * x2realloc (T * ptr, size_t *pn) - { - return (T *) x2realloc ((void *) ptr, pn); - } -extern "C" { -#endif - -/* This function is always triggered when memory is exhausted. It is - in charge of honoring the three previous items. This is the - function to call when one wants the program to die because of a - memory allocation failure. */ -_GL_NORETURN_FUNC extern void xalloc_die (void); - -/* In the following macros, T must be an elementary or structure/union or - typedef'ed type, or a pointer to such a type. To apply one of the - following macros to a function pointer or array type, you need to typedef - it first and use the typedef name. */ - -/* Allocate an object of type T dynamically, with error checking. */ -/* extern T *XMALLOC (typename T); */ -#define XMALLOC(T) \ - ((T *) xmalloc (sizeof (T))) - -/* Allocate memory for NMEMB elements of type T, with error checking. */ -/* extern T *XNMALLOC (size_t nmemb, typename T); */ -#if HAVE_INLINE -/* xnmalloc performs a division and multiplication by sizeof (T). Arrange to - perform the division at compile-time and the multiplication with a factor - known at compile-time. */ -# define XNMALLOC(N,T) \ - ((T *) (sizeof (T) == 1 \ - ? xmalloc (N) \ - : xnboundedmalloc(N, (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / sizeof (T), sizeof (T)))) -static inline void * -xnboundedmalloc (size_t n, size_t bound, size_t s) -{ - if (n > bound) - xalloc_die (); - return xmalloc (n * s); -} -#else -# define XNMALLOC(N,T) \ - ((T *) (sizeof (T) == 1 ? xmalloc (N) : xnmalloc (N, sizeof (T)))) -#endif - -/* Allocate an object of type T dynamically, with error checking, - and zero it. */ -/* extern T *XZALLOC (typename T); */ -#define XZALLOC(T) \ - ((T *) xzalloc (sizeof (T))) - -/* Allocate memory for NMEMB elements of type T, with error checking, - and zero it. */ -/* extern T *XCALLOC (size_t nmemb, typename T); */ -#define XCALLOC(N,T) \ - ((T *) xcalloc (N, sizeof (T))) - -/* Return a pointer to a new buffer of N bytes. This is like xmalloc, - except it returns char *. */ -#define xcharalloc(N) \ - XNMALLOC (N, char) - - -/* Defined in xstrdup.c. */ - -/* Return a newly allocated copy of the N bytes of memory starting at P. */ -extern void *xmemdup (const void *p, size_t n); -#ifdef __cplusplus -} -template <typename T> - inline T * xmemdup (const T * p, size_t n) - { - return (T *) xmemdup ((const void *) p, n); - } -extern "C" { -#endif - -/* Return a newly allocated copy of STRING. */ -extern char *xstrdup (const char *string); - - -#ifdef __cplusplus -} -#endif - - -#endif /* _XALLOC_H */ diff --git a/gnulib-local/lib/xmalloc.c b/gnulib-local/lib/xmalloc.c deleted file mode 100644 index a1456c520..000000000 --- a/gnulib-local/lib/xmalloc.c +++ /dev/null @@ -1,232 +0,0 @@ -/* xmalloc.c -- malloc with out of memory checking - Copyright (C) 1990-1996, 2000-2003, 2005-2007, 2012 Free Software - Foundation, Inc. - - 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 of the License, 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/>. */ - -#include <config.h> - -/* Specification. */ -#include "xalloc.h" - -#include <stdlib.h> - -#include "error.h" -#include "gettext.h" - -#define _(str) gettext (str) - - -/* Exit value when the requested amount of memory is not available. - The caller may set it to some other value. */ -int xmalloc_exit_failure = EXIT_FAILURE; - -void -xalloc_die () -{ - error (xmalloc_exit_failure, 0, _("memory exhausted")); - /* _Noreturn cannot be given to error, since it may return if - its first argument is 0. To help compilers understand the - xalloc_die does terminate, call exit. */ - exit (EXIT_FAILURE); -} - -static void * -fixup_null_alloc (size_t n) -{ - void *p; - - p = NULL; - if (n == 0) - p = malloc ((size_t) 1); - if (p == NULL) - xalloc_die (); - return p; -} - -/* Allocate N bytes of memory dynamically, with error checking. */ - -void * -xmalloc (size_t n) -{ - void *p; - - p = malloc (n); - if (p == NULL) - p = fixup_null_alloc (n); - return p; -} - -/* Allocate memory for NMEMB elements of SIZE bytes, with error checking. - SIZE must be > 0. */ - -void * -xnmalloc (size_t nmemb, size_t size) -{ - size_t n; - void *p; - - if (xalloc_oversized (nmemb, size)) - xalloc_die (); - n = nmemb * size; - p = malloc (n); - if (p == NULL) - p = fixup_null_alloc (n); - return p; -} - -/* Allocate SIZE bytes of memory dynamically, with error checking, - and zero it. */ - -void * -xzalloc (size_t size) -{ - void *p; - - p = xmalloc (size); - memset (p, 0, size); - return p; -} - -/* Allocate memory for N elements of S bytes, with error checking, - and zero it. */ - -void * -xcalloc (size_t n, size_t s) -{ - void *p; - - p = calloc (n, s); - if (p == NULL) - p = fixup_null_alloc (n); - return p; -} - -/* Change the size of an allocated block of memory P to N bytes, - with error checking. - If P is NULL, run xmalloc. */ - -void * -xrealloc (void *p, size_t n) -{ - if (p == NULL) - return xmalloc (n); - p = realloc (p, n); - if (p == NULL) - p = fixup_null_alloc (n); - return p; -} - -/* If P is null, allocate a block of at least *PN such objects; - otherwise, reallocate P so that it contains more than *PN objects - each of S bytes. S must be nonzero. Set *PN to the new number of - objects, and return the pointer to the new block. *PN is never set - to zero, and the returned pointer is never null. - - Repeated reallocations are guaranteed to make progress, either by - allocating an initial block with a nonzero size, or by allocating a - larger block. - - In the following implementation, nonzero sizes are increased by a - factor of approximately 1.5 so that repeated reallocations have - O(N) overall cost rather than O(N**2) cost, but the - specification for this function does not guarantee that rate. - - Here is an example of use: - - int *p = NULL; - size_t used = 0; - size_t allocated = 0; - - void - append_int (int value) - { - if (used == allocated) - p = x2nrealloc (p, &allocated, sizeof *p); - p[used++] = value; - } - - This causes x2nrealloc to allocate a block of some nonzero size the - first time it is called. - - To have finer-grained control over the initial size, set *PN to a - nonzero value before calling this function with P == NULL. For - example: - - int *p = NULL; - size_t used = 0; - size_t allocated = 0; - size_t allocated1 = 1000; - - void - append_int (int value) - { - if (used == allocated) - { - p = x2nrealloc (p, &allocated1, sizeof *p); - allocated = allocated1; - } - p[used++] = value; - } - - */ - -static inline void * -x2nrealloc (void *p, size_t *pn, size_t s) -{ - size_t n = *pn; - - if (! p) - { - if (! n) - { - /* The approximate size to use for initial small allocation - requests, when the invoking code specifies an old size of - zero. This is the largest "small" request for the GNU C - library malloc. */ - enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; - - n = DEFAULT_MXFAST / s; - n += !n; - } - if (xalloc_oversized (n, s)) - xalloc_die (); - } - else - { - /* Set N = floor (1.5 * N) + 1 so that progress is made even if N == 0. - Check for overflow, so that N * S stays in both ptrdiff_t and - size_t range. The check may be slightly conservative, but an - exact check isn't worth the trouble. */ - if ((PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX) / 3 * 2 / s - <= n) - xalloc_die (); - n += n / 2 + 1; - } - - *pn = n; - return xrealloc (p, n * s); -} - -/* If P is null, allocate a block of at least *PN bytes; otherwise, - reallocate P so that it contains more than *PN bytes. *PN must be - nonzero unless P is null. Set *PN to the new block's size, and - return the pointer to the new block. *PN is never set to zero, and - the returned pointer is never null. */ - -void * -x2realloc (void *p, size_t *pn) -{ - return x2nrealloc (p, pn, 1); -} diff --git a/gnulib-local/lib/xstrdup.c b/gnulib-local/lib/xstrdup.c deleted file mode 100644 index 1b369e91a..000000000 --- a/gnulib-local/lib/xstrdup.c +++ /dev/null @@ -1,41 +0,0 @@ -/* xstrdup.c -- copy a string with out of memory checking - Copyright (C) 1990, 1996, 2000-2003, 2005-2006 Free Software - Foundation, Inc. - - 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 of the License, 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/>. */ - -#include <config.h> - -/* Specification. */ -#include "xalloc.h" - -#include <string.h> - -/* Return a newly allocated copy of the N bytes of memory starting at P. */ - -void * -xmemdup (const void *p, size_t n) -{ - void *q = xmalloc (n); - memcpy (q, p, n); - return q; -} - -/* Return a newly allocated copy of STRING. */ - -char * -xstrdup (const char *string) -{ - return strcpy (XNMALLOC (strlen (string) + 1, char), string); -} diff --git a/gnulib-local/modules/xalloc b/gnulib-local/modules/xalloc deleted file mode 100644 index deec4d451..000000000 --- a/gnulib-local/modules/xalloc +++ /dev/null @@ -1,29 +0,0 @@ -Description: -Memory allocation with out-of-memory checking. - -Files: -lib/xalloc.h -lib/xmalloc.c -lib/xstrdup.c - -Depends-on: -error -gettext-h -noreturn -stdlib -xalloc-oversized - -configure.ac: -AC_REQUIRE([AC_C_INLINE]) - -Makefile.am: -lib_SOURCES += xalloc.h xmalloc.c xstrdup.c - -Include: -"xalloc.h" - -License: -GPL - -Maintainer: -all diff --git a/gnulib-local/modules/xalloc-die b/gnulib-local/modules/xalloc-die deleted file mode 100644 index 4fe557c26..000000000 --- a/gnulib-local/modules/xalloc-die +++ /dev/null @@ -1,21 +0,0 @@ -Description: -Report a memory allocation failure and exit. - -Files: - -Depends-on: -xalloc - -configure.ac: - -Makefile.am: - -Include: -"xalloc.h" - -License: -GPL - -Maintainer: -Bruno Haible - diff --git a/libtextstyle/.gitignore b/libtextstyle/.gitignore index 8acc5a615..a5c6de8d6 100644 --- a/libtextstyle/.gitignore +++ b/libtextstyle/.gitignore @@ -225,6 +225,7 @@ /lib/windows-rwlock.c /lib/windows-rwlock.h /lib/write.c +/lib/xalloc-die.c /lib/xalloc-oversized.h /lib/xalloc.h /lib/xasprintf.c diff --git a/libtextstyle/lib/misc.c b/libtextstyle/lib/misc.c index 866fd212f..6b1f0eb38 100644 --- a/libtextstyle/lib/misc.c +++ b/libtextstyle/lib/misc.c @@ -1,5 +1,5 @@ /* Miscellaneous public API. - Copyright (C) 2019 Free Software Foundation, Inc. + Copyright (C) 2019, 2021 Free Software Foundation, Inc. Written by Bruno Haible <bruno@clisp.org>, 2019. This program is free software: you can redistribute it and/or modify @@ -25,8 +25,6 @@ #include "fd-ostream.h" #include "exitfail.h" -extern int xmalloc_exit_failure; - styled_ostream_t styled_ostream_create (int fd, const char *filename, ttyctl_t tty_control, @@ -47,5 +45,4 @@ void libtextstyle_set_failure_exit_code (int exit_code) { exit_failure = exit_code; - xmalloc_exit_failure = exit_code; } |