new_opnt.cc revision 97403
197403Sobrien// Support routines for the -*- C++ -*- dynamic memory management. 297403Sobrien// Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation 397403Sobrien// 497403Sobrien// This file is part of GNU CC. 597403Sobrien// 697403Sobrien// GNU CC is free software; you can redistribute it and/or modify 797403Sobrien// it under the terms of the GNU General Public License as published by 897403Sobrien// the Free Software Foundation; either version 2, or (at your option) 997403Sobrien// any later version. 1097403Sobrien// 1197403Sobrien// GNU CC is distributed in the hope that it will be useful, 1297403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1397403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1497403Sobrien// GNU General Public License for more details. 1597403Sobrien// 1697403Sobrien// You should have received a copy of the GNU General Public License 1797403Sobrien// along with GNU CC; see the file COPYING. If not, write to 1897403Sobrien// the Free Software Foundation, 59 Temple Place - Suite 330, 1997403Sobrien// Boston, MA 02111-1307, USA. 2097403Sobrien// 2197403Sobrien// As a special exception, you may use this file as part of a free software 2297403Sobrien// library without restriction. Specifically, if other files instantiate 2397403Sobrien// templates or use macros or inline functions from this file, or you compile 2497403Sobrien// this file and link it with other files to produce an executable, this 2597403Sobrien// file does not by itself cause the resulting executable to be covered by 2697403Sobrien// the GNU General Public License. This exception does not however 2797403Sobrien// invalidate any other reasons why the executable file might be covered by 2897403Sobrien// the GNU General Public License. 2997403Sobrien 3097403Sobrien#include "new" 3197403Sobrien#include <exception_defines.h> 3297403Sobrien 3397403Sobrienusing std::new_handler; 3497403Sobrienusing std::bad_alloc; 3597403Sobrien 3697403Sobrienextern "C" void *malloc (std::size_t); 3797403Sobrienextern new_handler __new_handler; 3897403Sobrien 3997403Sobrienvoid * 4097403Sobrienoperator new (std::size_t sz, const std::nothrow_t&) throw() 4197403Sobrien{ 4297403Sobrien void *p; 4397403Sobrien 4497403Sobrien /* malloc (0) is unpredictable; avoid it. */ 4597403Sobrien if (sz == 0) 4697403Sobrien sz = 1; 4797403Sobrien p = (void *) malloc (sz); 4897403Sobrien while (p == 0) 4997403Sobrien { 5097403Sobrien new_handler handler = __new_handler; 5197403Sobrien if (! handler) 5297403Sobrien return 0; 5397403Sobrien try 5497403Sobrien { 5597403Sobrien handler (); 5697403Sobrien } 5797403Sobrien catch (bad_alloc &) 5897403Sobrien { 5997403Sobrien return 0; 6097403Sobrien } 6197403Sobrien 6297403Sobrien p = (void *) malloc (sz); 6397403Sobrien } 6497403Sobrien 6597403Sobrien return p; 6697403Sobrien} 67