197403Sobrien// Support routines for the -*- C++ -*- dynamic memory management.
2169691Skan
3169691Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004
4169691Skan// Free Software Foundation
597403Sobrien//
6132720Skan// This file is part of GCC.
797403Sobrien//
8132720Skan// GCC is free software; you can redistribute it and/or modify
997403Sobrien// it under the terms of the GNU General Public License as published by
1097403Sobrien// the Free Software Foundation; either version 2, or (at your option)
1197403Sobrien// any later version.
1297403Sobrien//
13132720Skan// GCC is distributed in the hope that it will be useful,
1497403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1597403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1697403Sobrien// GNU General Public License for more details.
1797403Sobrien//
1897403Sobrien// You should have received a copy of the GNU General Public License
19132720Skan// along with GCC; see the file COPYING.  If not, write to
20169691Skan// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21169691Skan// Boston, MA 02110-1301, USA.
2297403Sobrien//
2397403Sobrien// As a special exception, you may use this file as part of a free software
2497403Sobrien// library without restriction.  Specifically, if other files instantiate
2597403Sobrien// templates or use macros or inline functions from this file, or you compile
2697403Sobrien// this file and link it with other files to produce an executable, this
2797403Sobrien// file does not by itself cause the resulting executable to be covered by
2897403Sobrien// the GNU General Public License.  This exception does not however
2997403Sobrien// invalidate any other reasons why the executable file might be covered by
3097403Sobrien// the GNU General Public License.
3197403Sobrien
32169691Skan#include <bits/c++config.h>
3397403Sobrien#include <cstdlib>
3497403Sobrien#include <exception_defines.h>
35169691Skan#include "new"
3697403Sobrien
3797403Sobrienusing std::new_handler;
3897403Sobrienusing std::bad_alloc;
39169691Skan#if _GLIBCXX_HOSTED
40102782Skanusing std::malloc;
41169691Skan#else
42169691Skan// A freestanding C runtime may not provide "malloc" -- but there is no
43169691Skan// other reasonable way to implement "operator new".
44169691Skanextern "C" void *malloc (std::size_t);
45169691Skan#endif
4697403Sobrien
4797403Sobrienextern new_handler __new_handler;
4897403Sobrien
49169691Skan_GLIBCXX_WEAK_DEFINITION void *
5097403Sobrienoperator new (std::size_t sz) throw (std::bad_alloc)
5197403Sobrien{
5297403Sobrien  void *p;
5397403Sobrien
5497403Sobrien  /* malloc (0) is unpredictable; avoid it.  */
5597403Sobrien  if (sz == 0)
5697403Sobrien    sz = 1;
5797403Sobrien  p = (void *) malloc (sz);
5897403Sobrien  while (p == 0)
5997403Sobrien    {
6097403Sobrien      new_handler handler = __new_handler;
6197403Sobrien      if (! handler)
6297403Sobrien#ifdef __EXCEPTIONS
6397403Sobrien	throw bad_alloc();
6497403Sobrien#else
6597403Sobrien        std::abort();
6697403Sobrien#endif
6797403Sobrien      handler ();
6897403Sobrien      p = (void *) malloc (sz);
6997403Sobrien    }
7097403Sobrien
7197403Sobrien  return p;
7297403Sobrien}
73