/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#if defined(__INTEL_COMPILER)
#include <new>
#include <xmemory>

namespace std {
   void __throw_bad_alloc() throw(bad_alloc) {
      throw bad_alloc();
   }
}

#else // !__INTEL_COMPILER

#include <memory>

#ifdef _POLYMAKE_LACKING_REALLOCATE

namespace pm {

void* reallocate(void* p, size_t old_sz, size_t new_sz)
{
#if defined(__GNUC__) && ( __GNUC__ > 3 ) || ( __GNUC_MINOR__ >= 3)
   static const bool use_new=getenv("GLIBCPP_FORCE_NEW") || getenv("GLIBCXX_FORCE_NEW");
#else
   const bool use_new=false;
#endif
   const size_t align=8, limit=128;
   if (!use_new && ((old_sz+align-1)&~(align-1)) == ((new_sz+align-1)&~(align-1)) && new_sz < limit)
      return p;
   void* new_p=raw_alloc::allocate(new_sz);
   if (new_p) {
      memcpy(new_p,p,std::min(old_sz,new_sz));
      raw_alloc::deallocate(p,old_sz);
   }
   return new_p;
}

}

#endif // _POLYMAKE_LACKING_REALLOCATE
#endif // !__INTEL_COMPILER
