1cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* malloc() function that is glibc compatible.
205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Copyright (C) 1997-1998, 2006-2007, 2009-2012 Free Software Foundation, Inc.
4cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
5cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   This program is free software; you can redistribute it and/or modify
6cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   it under the terms of the GNU General Public License as published by
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   the Free Software Foundation; either version 3, or (at your option)
8cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   any later version.
9cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
10cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   This program is distributed in the hope that it will be useful,
11cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   but WITHOUT ANY WARRANTY; without even the implied warranty of
12cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   GNU General Public License for more details.
14cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
15cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   You should have received a copy of the GNU General Public License
1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
1705436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* written by Jim Meyering and Bruno Haible */
1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define _GL_USE_STDLIB_ALLOC 1
2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <config.h>
2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Only the AC_FUNC_MALLOC macro defines 'malloc' already in config.h.  */
2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifdef malloc
2405436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define NEED_MALLOC_GNU 1
2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# undef malloc
2605436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Whereas the gnulib module 'malloc-gnu' defines HAVE_MALLOC_GNU.  */
2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang#elif GNULIB_MALLOC_GNU && !HAVE_MALLOC_GNU
2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define NEED_MALLOC_GNU 1
29cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
30cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
31cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include <stdlib.h>
32cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
3305436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <errno.h>
3405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
35cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Allocate an N-byte block of memory from the heap.
36cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   If N is zero, allocate a 1-byte block.  */
37cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
38cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid *
39cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectrpl_malloc (size_t n)
40cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
4105436638acc7c010349a69c3395f1a57c642dc62Ying Wang  void *result;
4205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
4305436638acc7c010349a69c3395f1a57c642dc62Ying Wang#if NEED_MALLOC_GNU
44cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if (n == 0)
45cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    n = 1;
4605436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif
4705436638acc7c010349a69c3395f1a57c642dc62Ying Wang
4805436638acc7c010349a69c3395f1a57c642dc62Ying Wang  result = malloc (n);
4905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#if !HAVE_MALLOC_POSIX
5105436638acc7c010349a69c3395f1a57c642dc62Ying Wang  if (result == NULL)
5205436638acc7c010349a69c3395f1a57c642dc62Ying Wang    errno = ENOMEM;
5305436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif
5405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5505436638acc7c010349a69c3395f1a57c642dc62Ying Wang  return result;
56cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
57