Annotation of embedaddon/libiconv/srclib/xalloc.h, revision 1.1

1.1     ! misho       1: /* malloc with out of memory checking.
        !             2:    Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
        !             3:    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
        !             4: 
        !             5:    This program is free software: you can redistribute it and/or modify
        !             6:    it under the terms of the GNU General Public License as published by
        !             7:    the Free Software Foundation; either version 3 of the License, or
        !             8:    (at your option) any later version.
        !             9: 
        !            10:    This program is distributed in the hope that it will be useful,
        !            11:    but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            12:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            13:    GNU General Public License for more details.
        !            14: 
        !            15:    You should have received a copy of the GNU General Public License
        !            16:    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
        !            17: 
        !            18: #ifndef _XALLOC_H
        !            19: #define _XALLOC_H
        !            20: 
        !            21: #include <stddef.h>
        !            22: 
        !            23: 
        !            24: #ifdef __cplusplus
        !            25: extern "C" {
        !            26: #endif
        !            27: 
        !            28: 
        !            29: /* Defined in xmalloc.c.  */
        !            30: 
        !            31: /* Allocate SIZE bytes of memory dynamically, with error checking.  */
        !            32: extern void *xmalloc (size_t size);
        !            33: 
        !            34: /* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
        !            35:    SIZE must be > 0.  */
        !            36: extern void *xnmalloc (size_t nmemb, size_t size);
        !            37: 
        !            38: /* Allocate SIZE bytes of memory dynamically, with error checking,
        !            39:    and zero it.  */
        !            40: extern void *xzalloc (size_t size);
        !            41: 
        !            42: /* Allocate memory for NMEMB elements of SIZE bytes, with error checking,
        !            43:    and zero it.  */
        !            44: extern void *xcalloc (size_t nmemb, size_t size);
        !            45: 
        !            46: /* Change the size of an allocated block of memory PTR to SIZE bytes,
        !            47:    with error checking.  If PTR is NULL, run xmalloc.  */
        !            48: extern void *xrealloc (void *ptr, size_t size);
        !            49: #ifdef __cplusplus
        !            50: }
        !            51: template <typename T>
        !            52:   inline T * xrealloc (T * ptr, size_t size)
        !            53:   {
        !            54:     return (T *) xrealloc ((void *) ptr, size);
        !            55:   }
        !            56: extern "C" {
        !            57: #endif
        !            58: 
        !            59: /* This function is always triggered when memory is exhausted.  It is
        !            60:    in charge of honoring the three previous items.  This is the
        !            61:    function to call when one wants the program to die because of a
        !            62:    memory allocation failure.  */
        !            63: extern void xalloc_die (void)
        !            64: #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) && !__STRICT_ANSI__
        !            65:      __attribute__ ((__noreturn__))
        !            66: #endif
        !            67:      ;
        !            68: 
        !            69: /* In the following macros, T must be an elementary or structure/union or
        !            70:    typedef'ed type, or a pointer to such a type.  To apply one of the
        !            71:    following macros to a function pointer or array type, you need to typedef
        !            72:    it first and use the typedef name.  */
        !            73: 
        !            74: /* Allocate an object of type T dynamically, with error checking.  */
        !            75: /* extern T *XMALLOC (typename T); */
        !            76: #define XMALLOC(T) \
        !            77:   ((T *) xmalloc (sizeof (T)))
        !            78: 
        !            79: /* Allocate memory for NMEMB elements of type T, with error checking.  */
        !            80: /* extern T *XNMALLOC (size_t nmemb, typename T); */
        !            81: #if HAVE_INLINE
        !            82: /* xnmalloc performs a division and multiplication by sizeof (T).  Arrange to
        !            83:    perform the division at compile-time and the multiplication with a factor
        !            84:    known at compile-time.  */
        !            85: # define XNMALLOC(N,T) \
        !            86:    ((T *) (sizeof (T) == 1 \
        !            87:           ? xmalloc (N) \
        !            88:           : xnboundedmalloc(N, (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / sizeof (T), sizeof (T))))
        !            89: static inline void *
        !            90: xnboundedmalloc (size_t n, size_t bound, size_t s)
        !            91: {
        !            92:   if (n > bound)
        !            93:     xalloc_die ();
        !            94:   return xmalloc (n * s);
        !            95: }
        !            96: #else
        !            97: # define XNMALLOC(N,T) \
        !            98:    ((T *) (sizeof (T) == 1 ? xmalloc (N) : xnmalloc (N, sizeof (T))))
        !            99: #endif
        !           100: 
        !           101: /* Allocate an object of type T dynamically, with error checking,
        !           102:    and zero it.  */
        !           103: /* extern T *XZALLOC (typename T); */
        !           104: #define XZALLOC(T) \
        !           105:   ((T *) xzalloc (sizeof (T)))
        !           106: 
        !           107: /* Allocate memory for NMEMB elements of type T, with error checking,
        !           108:    and zero it.  */
        !           109: /* extern T *XCALLOC (size_t nmemb, typename T); */
        !           110: #define XCALLOC(N,T) \
        !           111:   ((T *) xcalloc (N, sizeof (T)))
        !           112: 
        !           113: /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
        !           114:    except it returns char *.  */
        !           115: #define xcharalloc(N) \
        !           116:   XNMALLOC (N, char)
        !           117: 
        !           118: 
        !           119: /* Defined in xstrdup.c.  */
        !           120: 
        !           121: /* Return a newly allocated copy of the N bytes of memory starting at P.  */
        !           122: extern void *xmemdup (const void *p, size_t n);
        !           123: #ifdef __cplusplus
        !           124: }
        !           125: template <typename T>
        !           126:   inline T * xmemdup (const T * p, size_t n)
        !           127:   {
        !           128:     return (T *) xmemdup ((const void *) p, n);
        !           129:   }
        !           130: extern "C" {
        !           131: #endif
        !           132: 
        !           133: /* Return a newly allocated copy of STRING.  */
        !           134: extern char *xstrdup (const char *string);
        !           135: 
        !           136: 
        !           137: /* Return 1 if an array of N objects, each of size S, cannot exist due
        !           138:    to size arithmetic overflow.  S must be positive and N must be
        !           139:    nonnegative.  This is a macro, not an inline function, so that it
        !           140:    works correctly even when SIZE_MAX < N.
        !           141: 
        !           142:    By gnulib convention, SIZE_MAX represents overflow in size
        !           143:    calculations, so the conservative dividend to use here is
        !           144:    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
        !           145:    However, malloc (SIZE_MAX) fails on all known hosts where
        !           146:    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
        !           147:    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
        !           148:    branch when S is known to be 1.  */
        !           149: # define xalloc_oversized(n, s) \
        !           150:     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
        !           151: 
        !           152: 
        !           153: #ifdef __cplusplus
        !           154: }
        !           155: #endif
        !           156: 
        !           157: 
        !           158: #endif /* _XALLOC_H */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>