Commit 9409178c 9409178c04f8e3b631a5f623fbc12751944d99bf by Jeff Bailey

Portability fixes

1 parent df58081a
2000-04-19 Jeff Bailey <jbailey@nisa.net>
* configure.in: Test for limits.h and inttypes.h
* configure.in: Test for limits.h and inttypes.h, add malloc
and realloc checks.
* m4/: New directory
* m4/Makefile.am: New file to support directory
* m4/malloc.m4: File to support lib/ directory
* m4/realloc.m4: File to support lib/ directory
* lib/xalloc.h: New file to support xmalloc.c
* lib/xmalloc.c: New version from sh-utils-2.0g
2000-03-28 Jakob 'sparky' Kaivo <jkaivo@elijah.nodomainname.net>
......
AUTOMAKE_OPTIONS = gnu 1.4
ACLOCAL_AMFLAGS = -I m4
SUBDIRS = lib libmailbox mail pop3d doc m4
SUBDIRS = lib libmailbox mail pop3d doc
......
......@@ -26,6 +26,11 @@ AC_CHECK_HEADERS(malloc.h stdlib.h stdio.h errno.h unistd.h\
inttypes.h)
AC_CHECK_HEADERS(string.h strings.h, break)
dnl Check for working functions
AC_REQUIRE([jm_FUNC_MALLOC])
AC_REQUIRE([jm_FUNC_REALLOC])
dnl Check for libraries
dnl Use either PAM or CRYPT, not both.
......
/* xalloc.h -- malloc with out-of-memory checking
Copyright (C) 1990-1998, 1999 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 2, 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef XALLOC_H_
# define XALLOC_H_
# ifndef PARAMS
# if defined PROTOTYPES || (defined __STDC__ && __STDC__)
# define PARAMS(Args) Args
# else
# define PARAMS(Args) ()
# endif
# endif
# ifndef __attribute__
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
# define __attribute__(x)
# endif
# endif
# ifndef ATTRIBUTE_NORETURN
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
# endif
/* Exit value when the requested amount of memory is not available.
It is initialized to EXIT_FAILURE, but the caller may set it to
some other value. */
extern int xalloc_exit_failure;
/* If this pointer is non-zero, run the specified function upon each
allocation failure. It is initialized to zero. */
extern void (*xalloc_fail_func) PARAMS ((void));
/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
message must be non-NULL. It is translated via gettext.
The default value is "Memory exhausted". */
extern char *const xalloc_msg_memory_exhausted;
/* 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. */
extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN;
void *xmalloc PARAMS ((size_t n));
void *xcalloc PARAMS ((size_t n, size_t s));
void *xrealloc PARAMS ((void *p, size_t n));
char *xstrdup PARAMS ((const char *str));
# define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items)))
# define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items)))
# define XREALLOC(Ptr, Type, N_items) \
((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items)))
/* Declare and alloc memory for VAR of type TYPE. */
# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1)
/* Free VAR only if non NULL. */
# define XFREE(Var) \
do { \
if (Var) \
free (Var); \
} while (0)
/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
# define CCLONE(Src, Num) \
(memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num)))
/* Return a malloc'ed copy of SRC. */
# define CLONE(Src) CCLONE (Src, 1)
#endif /* !XALLOC_H_ */
......@@ -59,18 +59,22 @@ you must run the autoconf test for a properly working realloc -- see realloc.m4
int xalloc_exit_failure = EXIT_FAILURE;
/* If non NULL, call this function when memory is exhausted. */
void (*xalloc_fail_func) () = 0;
void (*xalloc_fail_func) PARAMS ((void)) = 0;
/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
before exiting when memory is exhausted. Goes through gettext. */
char *const xalloc_msg_memory_exhausted = N_("Memory exhausted");
static void
xalloc_fail (void)
void
xalloc_die (void)
{
if (xalloc_fail_func)
(*xalloc_fail_func) ();
error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
/* The `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);
}
/* Allocate N bytes of memory dynamically, with error checking. */
......@@ -82,7 +86,7 @@ xmalloc (size_t n)
p = malloc (n);
if (p == 0)
xalloc_fail ();
xalloc_die ();
return p;
}
......@@ -95,7 +99,7 @@ xrealloc (void *p, size_t n)
{
p = realloc (p, n);
if (p == 0)
xalloc_fail ();
xalloc_die ();
return p;
}
......@@ -108,6 +112,6 @@ xcalloc (size_t n, size_t s)
p = calloc (n, s);
if (p == 0)
xalloc_fail ();
xalloc_die ();
return p;
}
......
EXTRA_DIST = \
malloc.m4
realloc.m4
#serial 3
dnl From Jim Meyering.
dnl Determine whether malloc accepts 0 as its argument.
dnl If it doesn't, arrange to use the replacement function.
dnl
AC_DEFUN(jm_FUNC_MALLOC,
[
dnl xmalloc.c requires that this symbol be defined so it doesn't
dnl mistakenly use a broken malloc -- as it might if this test were omitted.
AC_DEFINE_UNQUOTED(HAVE_DONE_WORKING_MALLOC_CHECK, 1,
[Define if the malloc check has been performed. ])
AC_CACHE_CHECK([for working malloc], jm_cv_func_working_malloc,
[AC_TRY_RUN([
char *malloc ();
int
main ()
{
exit (malloc (0) ? 0 : 1);
}
],
jm_cv_func_working_malloc=yes,
jm_cv_func_working_malloc=no,
dnl When crosscompiling, assume malloc is broken.
jm_cv_func_working_malloc=no)
])
if test $jm_cv_func_working_malloc = no; then
AC_SUBST(LIBOBJS)
LIBOBJS="$LIBOBJS malloc.$ac_objext"
AC_DEFINE_UNQUOTED(malloc, rpl_malloc,
[Define to rpl_malloc if the replacement function should be used.])
fi
])
#serial 3
dnl From Jim Meyering.
dnl Determine whether realloc works when both arguments are 0.
dnl If it doesn't, arrange to use the replacement function.
dnl
AC_DEFUN(jm_FUNC_REALLOC,
[
dnl xmalloc.c requires that this symbol be defined so it doesn't
dnl mistakenly use a broken realloc -- as it might if this test were omitted.
AC_DEFINE_UNQUOTED(HAVE_DONE_WORKING_REALLOC_CHECK, 1,
[Define if the realloc check has been performed. ])
AC_CACHE_CHECK([for working realloc], jm_cv_func_working_realloc,
[AC_TRY_RUN([
char *realloc ();
int
main ()
{
exit (realloc (0, 0) ? 0 : 1);
}
],
jm_cv_func_working_realloc=yes,
jm_cv_func_working_realloc=no,
dnl When crosscompiling, assume realloc is broken.
jm_cv_func_working_realloc=no)
])
if test $jm_cv_func_working_realloc = no; then
AC_SUBST(LIBOBJS)
LIBOBJS="$LIBOBJS realloc.$ac_objext"
AC_DEFINE_UNQUOTED(realloc, rpl_realloc,
[Define to rpl_realloc if the replacement function should be used.])
fi
])