Home | History | Annotate | Line # | Download | only in import
      1      1.1  christos /* malloc() function that is glibc compatible.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 1997-1998, 2006-2007, 2009-2022 Free Software Foundation, Inc.
      4      1.1  christos 
      5  1.1.1.2  christos    This file is free software: you can redistribute it and/or modify
      6  1.1.1.2  christos    it under the terms of the GNU Lesser General Public License as
      7  1.1.1.2  christos    published by the Free Software Foundation; either version 2.1 of the
      8  1.1.1.2  christos    License, or (at your option) any later version.
      9      1.1  christos 
     10  1.1.1.2  christos    This file is distributed in the hope that it will be useful,
     11      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  1.1.1.2  christos    GNU Lesser General Public License for more details.
     14      1.1  christos 
     15  1.1.1.2  christos    You should have received a copy of the GNU Lesser General Public License
     16  1.1.1.2  christos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
     17      1.1  christos 
     18      1.1  christos /* written by Jim Meyering and Bruno Haible */
     19      1.1  christos 
     20      1.1  christos #define _GL_USE_STDLIB_ALLOC 1
     21      1.1  christos #include <config.h>
     22      1.1  christos 
     23      1.1  christos #include <stdlib.h>
     24      1.1  christos 
     25      1.1  christos #include <errno.h>
     26      1.1  christos 
     27  1.1.1.2  christos #include "xalloc-oversized.h"
     28  1.1.1.2  christos 
     29  1.1.1.2  christos /* Allocate an N-byte block of memory from the heap, even if N is 0.  */
     30      1.1  christos 
     31      1.1  christos void *
     32      1.1  christos rpl_malloc (size_t n)
     33      1.1  christos {
     34      1.1  christos   if (n == 0)
     35      1.1  christos     n = 1;
     36      1.1  christos 
     37  1.1.1.2  christos   if (xalloc_oversized (n, 1))
     38  1.1.1.2  christos     {
     39  1.1.1.2  christos       errno = ENOMEM;
     40  1.1.1.2  christos       return NULL;
     41  1.1.1.2  christos     }
     42  1.1.1.2  christos 
     43  1.1.1.2  christos   void *result = malloc (n);
     44      1.1  christos 
     45      1.1  christos #if !HAVE_MALLOC_POSIX
     46      1.1  christos   if (result == NULL)
     47      1.1  christos     errno = ENOMEM;
     48      1.1  christos #endif
     49      1.1  christos 
     50      1.1  christos   return result;
     51      1.1  christos }
     52