(root)/
tar-1.35/
gnu/
ialloc.h
       1  /* ialloc.h -- malloc with idx_t rather than size_t
       2  
       3     Copyright 2021-2023 Free Software Foundation, Inc.
       4  
       5     This file is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU Lesser General Public License as
       7     published by the Free Software Foundation; either version 2.1 of the
       8     License, or (at your option) any later version.
       9  
      10     This file 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 Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  #ifndef IALLOC_H_
      19  #define IALLOC_H_
      20  
      21  /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_ATTRIBUTE_COLD,
      22     _GL_ATTRIBUTE_MALLOC.  */
      23  #if !_GL_CONFIG_H_INCLUDED
      24   #error "Please include config.h first."
      25  #endif
      26  
      27  #include "idx.h"
      28  
      29  #include <errno.h>
      30  #include <stdint.h>
      31  #include <stdlib.h>
      32  
      33  _GL_INLINE_HEADER_BEGIN
      34  #ifndef IALLOC_INLINE
      35  # define IALLOC_INLINE _GL_INLINE
      36  #endif
      37  
      38  #ifdef __cplusplus
      39  extern "C" {
      40  #endif
      41  
      42  IALLOC_INLINE void * _GL_ATTRIBUTE_COLD
      43  _gl_alloc_nomem (void)
      44  {
      45    errno = ENOMEM;
      46    return NULL;
      47  }
      48  
      49  /* imalloc (size) is like malloc (size).
      50     It returns a non-NULL pointer to size bytes of memory.
      51     Upon failure, it returns NULL with errno set.  */
      52  IALLOC_INLINE
      53  _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/
      54  void *
      55  imalloc (idx_t s)
      56  {
      57    return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem ();
      58  }
      59  
      60  /* irealloc (ptr, size) is like realloc (ptr, size).
      61     It returns a non-NULL pointer to size bytes of memory.
      62     Upon failure, it returns NULL with errno set.  */
      63  IALLOC_INLINE
      64  /*_GL_ATTRIBUTE_DEALLOC_FREE*/
      65  void *
      66  irealloc (void *p, idx_t s)
      67  {
      68    /* Work around GNU realloc glitch by treating a zero size as if it
      69       were 1, so that returning NULL is equivalent to failing.  */
      70    return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem ();
      71  }
      72  
      73  /* icalloc (num, size) is like calloc (num, size).
      74     It returns a non-NULL pointer to num * size bytes of memory.
      75     Upon failure, it returns NULL with errno set.  */
      76  IALLOC_INLINE
      77  _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/
      78  void *
      79  icalloc (idx_t n, idx_t s)
      80  {
      81    if (SIZE_MAX < n)
      82      {
      83        if (s != 0)
      84          return _gl_alloc_nomem ();
      85        n = 0;
      86      }
      87    if (SIZE_MAX < s)
      88      {
      89        if (n != 0)
      90          return _gl_alloc_nomem ();
      91        s = 0;
      92      }
      93    return calloc (n, s);
      94  }
      95  
      96  /* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size).
      97     It returns a non-NULL pointer to num * size bytes of memory.
      98     Upon failure, it returns NULL with errno set.  */
      99  IALLOC_INLINE void *
     100  ireallocarray (void *p, idx_t n, idx_t s)
     101  {
     102    /* Work around GNU reallocarray glitch by treating a zero size as if
     103       it were 1, so that returning NULL is equivalent to failing.  */
     104    if (n == 0 || s == 0)
     105      n = s = 1;
     106    return (n <= SIZE_MAX && s <= SIZE_MAX
     107            ? reallocarray (p, n, s)
     108            : _gl_alloc_nomem ());
     109  }
     110  
     111  #ifdef __cplusplus
     112  }
     113  #endif
     114  
     115  _GL_INLINE_HEADER_END
     116  
     117  #endif