Home | History | Annotate | Download | only in ustl-1.0
      1 // This file is part of the ustl library, an STL implementation.
      2 //
      3 // Copyright (C) 2005 by Mike Sharov <msharov (at) users.sourceforge.net>
      4 // This file is free software, distributed under the MIT License.
      5 //
      6 /// \file unew.h
      7 ///
      8 /// \brief Same as \<new\>, but throws ustl:: exceptions.
      9 //
     10 
     11 #ifndef UNEW_H_11D237512B324C9C05A55DAF1BF086F1
     12 #define UNEW_H_11D237512B324C9C05A55DAF1BF086F1
     13 
     14 #include "uexception.h"
     15 
     16 /// Just like malloc, but throws on failure.
     17 void* throwing_malloc (size_t n) throw (ustl::bad_alloc);
     18 /// Just like free, but doesn't crash when given a NULL.
     19 void free_nullok (void* p) throw();
     20 
     21 #ifdef WITHOUT_LIBSTDCPP
     22 
     23 //
     24 // These are replaceable signatures:
     25 //  - normal single new and delete (no arguments, throw @c bad_alloc on error)
     26 //  - normal array new and delete (same)
     27 //  - @c nothrow single new and delete (take a @c nothrow argument, return
     28 //    @c NULL on error)
     29 //  - @c nothrow array new and delete (same)
     30 //
     31 //  Placement new and delete signatures (take a memory address argument,
     32 //  does nothing) may not be replaced by a user's program.
     33 //
     34 inline void* operator new (size_t n) throw (ustl::bad_alloc)	{ return (throwing_malloc (n)); }
     35 inline void* operator new[] (size_t n) throw (ustl::bad_alloc)	{ return (throwing_malloc (n)); }
     36 inline void  operator delete (void* p) throw()			{ free_nullok (p); }
     37 inline void  operator delete[] (void* p) throw()		{ free_nullok (p); }
     38 
     39 // Default placement versions of operator new.
     40 inline void* operator new (size_t, void* p) throw() { return (p); }
     41 inline void* operator new[] (size_t, void* p) throw() { return (p); }
     42 
     43 // Default placement versions of operator delete.
     44 inline void  operator delete  (void*, void*) throw() { }
     45 inline void  operator delete[](void*, void*) throw() { }
     46 
     47 #else
     48 #include <new>
     49 #endif	// WITHOUT_LIBSTDCPP
     50 
     51 #endif
     52 
     53