blob: ea20b2c9afd5e2c9ca6db0df20f2d6bc54fdac6b [file] [log] [blame]
// checking for class std::bad_alloc
/***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the License); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1999-2007 Rogue Wave Software, Inc.
*
**************************************************************************/
#include "config.h"
#if !defined (_RWSTD_NO_NEW_THROWS) && !defined (_RWSTD_NO_NEW_OFLOW_SAFE)
# include <stddef.h> // for size_t
#endif // !_RWSTD_NO_NEW_THROWS && !_RWSTD_NO_NEW_OFLOW_SAFE
#ifndef _RWSTD_NO_SETRLIMIT
// test for setrlimit() presence before compiling current file
#endif
// proclimits.h must be included only after #if[n]def _RWSTD_NO_SETRLIMIT
#include "proclimits.h"
#ifndef _RWSTD_NO_HONOR_STD
# ifdef _RWSTD_NO_STD_TERMINATE
namespace std {
void terminate ()
{
static int *ip;
loop:
while ((ip [0] = ip [1])) { // force a SIGSEGV
++ip;
terminate (); // recurse infinitely
}
goto loop;
}
} // namespace std
# endif // _RWSTD_NO_STD_TERMINATE
#endif // _RWSTD_NO_HONOR_STD
// will fail if namespaces aren't supported
namespace std {
struct bad_alloc { };
}
// force a failure if namespace std isn't honored
enum bad_alloc { };
#if defined (_MSC_VER)
#include <crtdbg.h> // for _CrtSetReportMode()
void disable_msc_gui ()
{
_CrtSetReportMode (_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportMode (_CRT_ASSERT, _CRTDBG_MODE_FILE);
}
#else // if !defined (_MSC_VER)
void disable_msc_gui () { /* no-op */ }
#endif // _MSC_VER
int main ()
{
// prevent MSVC's libc from popping up a dialog box
// with an out-of-memory message
disable_msc_gui ();
try {
#ifndef _RWSTD_NO_NEW_THROWS
# ifndef _RWSTD_NO_NEW_OFLOW_SAFE
void *p = ::operator new (size_t (-1));
if (!p)
return 1;
# else // if defined (_RWSTD_NO_NEW_OFLOW_SAFE)
limit_memory (0);
for (unsigned long n = 1UL << (sizeof (long) * 8 - 1);
0 != n; n |= (n >> 1)) {
// try to allocate a huge amount of memory
void *p = ::operator new (n);
if (!p)
return 1;
}
# endif // _RWSTD_NO_NEW_OFLOW_SAFE
#else // if defined (_RWSTD_NO_NEW_THROWS)
return 4;
#endif
}
catch (std::bad_alloc&) {
return 0;
}
catch (...) {
return 2;
}
return 3;
}