Farid Zaripov wrote:
Attached is a patch to make operator new on MSVC conforming to the C++
Standard.
I have one concern with the introduction of dynamic initialization
and the pragma into the library. First, our (undocumented) design
goal is to avoid requiring dynamic initialization in the library.
I.e., there should be no code in the library that runs at startup.
The rationale for it is efficiency and avoiding user code issues
due to initialization dependencies. Since other libraries may use
the same pragma, they will be subject to initialization dependencies
that we try to avoid.
Unless there is a way to avoid the dynamic initialization or defer
it until runtime (i.e., initialize the handler lazily) I would just
as soon add the patch to the Jira issue and close it as Won't Fix.
Martin
ChangeLog:
* _config-msvcrt.h: #undefine _RWSTD_NO_NEW_THROWS and
_RWSTD_NO_NEW_OFLOW_SAFE macros if they're #defined.
* memory.cpp (__rw_new_handler_imp): Throw std::bad_alloc() if no user
handler installed.
(set_new_handler): Don't invoke _set_new_handler().
* new_supp.cpp: New file with helper object to make operator new
conforming to the C++ Standard
Farid.
------------------------------------------------------------------------
Index: include/rw/_config-msvcrt.h
===================================================================
--- include/rw/_config-msvcrt.h (revision 570339)
+++ include/rw/_config-msvcrt.h (working copy)
@@ -148,6 +148,14 @@
# define _RWSTD_NO_DEPRECATED_C_HEADERS
#endif // _RWSTD_NO_DEPRECATED_C_HEADERS
+#ifdef _RWSTD_NO_NEW_THROWS
+# undef _RWSTD_NO_NEW_THROWS
+#endif // _RWSTD_NO_NEW_THROWS
+
+#ifdef _RWSTD_NO_NEW_OFLOW_SAFE
+# undef _RWSTD_NO_NEW_OFLOW_SAFE
+#endif // _RWSTD_NO_NEW_OFLOW_SAFE
+
// operator new and delete is not reliably replaceable across
// shared library boundaries, which includes the shared library
// version of the language support library
Index: src/memory.cpp
===================================================================
--- src/memory.cpp (revision 570339)
+++ src/memory.cpp (working copy)
@@ -178,20 +178,22 @@
#ifdef _MSC_VER
-typedef int (*__rw_new_handler_t)(_RWSTD_SIZE_T);
-
-_RWSTD_DLLIMPORT __rw_new_handler_t _set_new_handler (__rw_new_handler_t);
-
_RWSTD_NAMESPACE (__rw) {
static _STD::new_handler __rw_new_handler /* = 0 */;
-static int __rw_new_handler_imp (_RWSTD_SIZE_T)
+int __rw_new_handler_imp (_RWSTD_SIZE_T)
{
- _RWSTD_ASSERT (0 != __rw_new_handler);
+ if (!__rw_new_handler)
+#if _MSC_VER <= 1310
+ _RW::__rw_throw (_RWSTD_ERROR_FIRST + 3 /* == bad_alloc */);
+#else
+ return 0; // operator new will throw std::bad_alloc()
+#endif
__rw_new_handler ();
+ // repeat memory allocation
return 1;
}
@@ -214,8 +216,6 @@
_RW::__rw_new_handler = handler;
- _set_new_handler (_RW::__rw_new_handler_imp);
-
return save;
}
Index: src/new_supp.cpp
===================================================================
--- src/new_supp.cpp (revision 0)
+++ src/new_supp.cpp (revision 0)
@@ -0,0 +1,58 @@
+ /***************************************************************************
+ *
+ * new_supp.cpp - helper object to make operator new conforming
+ * to the C++ Standard
+ *
+ * $Id$
+ *
+ ***************************************************************************
+ *
+ * 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.
+ *
+ **************************************************************************/
+
+#define _RWSTD_LIB_SRC
+
+#ifdef _MSC_VER
+
+#include <rw/_defs.h>
+
+
+typedef int (*__rw_new_handler_t)(_RWSTD_SIZE_T);
+
+_RWSTD_DLLIMPORT __rw_new_handler_t _set_new_handler (__rw_new_handler_t);
+
+_RWSTD_NAMESPACE (__rw) {
+
+int __rw_new_handler_imp (_RWSTD_SIZE_T);
+
+} // namespace __rw
+
+
+#pragma init_seg (lib)
+
+struct __rw_new_handler_init_t
+{
+ __rw_new_handler_init_t () {
+ _set_new_handler (_RW::__rw_new_handler_imp);
+ }
+};
+
+static __rw_new_handler_init_t __rw_new_handler_init;
+
+
+#endif // _MSC_VER
Property changes on: src\new_supp.cpp
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native