3 # User Mike Hommey <mh+mozilla@glandium.org>
4 # Date 1463557039 -32400
5 # Node ID 68da139d0866977c0ada86319fa94388f2255446
6 # Parent a640e6fa8ab9977fb6c5bcf63dc4daca6699477b
7 Bug 1269171 - Change how mozalloc.h is hooked in STL wrappers. r=froydnj
9 Since the introduction of the STL wrappers, they have included
10 mozalloc.h, and multiple times, we've hit header reentrancy problems,
11 and worked around them as best as we could.
13 Taking a step back, all mozalloc.h does is:
14 - declare moz_* allocator functions.
15 - define inline implementations of various operator new/delete variants.
17 The first only requires the functions to be declared before they are used,
18 so mozalloc.h only needs to be included before anything that would use
21 The second doesn't actually require a specific order, as long as the
22 declaration for those functions comes before their use, and they are
23 either declared in <new> or implicitly by the C++ compiler.
25 So all in all, it doesn't matter that mozalloc.h is included before the
26 wrapped STL headers. What matters is that it's included when STL headers
27 are included. So arrange things such that mozalloc.h is included after
28 the first wrapped STL header is fully preprocessed (and all its includes
32 diff --git config/gcc-stl-wrapper.template.h config/gcc-stl-wrapper.template.h
33 --- mozilla/config/gcc-stl-wrapper.template.h
34 +++ mozilla/config/gcc-stl-wrapper.template.h
37 #if defined(__EXCEPTIONS) && __EXCEPTIONS && !(__OBJC__ && __GNUC__ && XP_IOS)
38 # error "STL code can only be used with -fno-exceptions"
41 // Silence "warning: #include_next is a GCC extension"
42 #pragma GCC system_header
44 -// Don't include mozalloc for cstdlib. See bug 1245076.
45 -#ifndef moz_dont_include_mozalloc_for_cstdlib
46 -# define moz_dont_include_mozalloc_for_cstdlib
48 -#ifndef moz_dont_include_mozalloc_for_${HEADER}
49 -// mozalloc.h wants <new>; break the cycle by always explicitly
50 -// including <new> here. NB: this is a tad sneaky. Sez the gcc docs:
52 -// `#include_next' does not distinguish between <file> and "file"
53 -// inclusion, nor does it check that the file you specify has the
54 -// same name as the current file. It simply looks for the file
55 -// named, starting with the directory in the search path after the
56 -// one where the current file was found.
59 -// See if we're in code that can use mozalloc. NB: this duplicates
60 -// code in nscore.h because nscore.h pulls in prtypes.h, and chromium
61 -// can't build with that being included before base/basictypes.h.
62 -# if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
63 -# include "mozilla/mozalloc.h"
65 -# error "STL code can only be used with infallible ::operator new()"
70 #if defined(DEBUG) && !defined(_GLIBCXX_DEBUG)
71 // Enable checked iterators and other goodies
73 // FIXME/bug 551254: gcc's debug STL implementation requires -frtti.
74 // Figure out how to resolve this with -fno-rtti. Maybe build with
75 // -frtti in DEBUG builds?
77 // # define _GLIBCXX_DEBUG 1
80 +// Don't include mozalloc for cstdlib. See bug 1245076.
81 +#ifndef moz_dont_include_mozalloc_for_cstdlib
82 +# define moz_dont_include_mozalloc_for_cstdlib
85 +// Include mozalloc after the STL header and all other headers it includes
86 +// have been preprocessed.
87 +#if !defined(MOZ_INCLUDE_MOZALLOC_H) && \
88 + !defined(moz_dont_include_mozalloc_for_${HEADER})
89 +# define MOZ_INCLUDE_MOZALLOC_H
90 +# define MOZ_INCLUDE_MOZALLOC_H_FROM_${HEADER}
93 #pragma GCC visibility push(default)
94 #include_next <${HEADER}>
95 #pragma GCC visibility pop
97 +#ifdef MOZ_INCLUDE_MOZALLOC_H_FROM_${HEADER}
98 +// See if we're in code that can use mozalloc. NB: this duplicates
99 +// code in nscore.h because nscore.h pulls in prtypes.h, and chromium
100 +// can't build with that being included before base/basictypes.h.
101 +# if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
102 +# include "mozilla/mozalloc.h"
104 +# error "STL code can only be used with infallible ::operator new()"
108 // gcc calls a __throw_*() function from bits/functexcept.h when it
109 // wants to "throw an exception". functexcept exists nominally to
110 // support -fno-exceptions, but since we'll always use the system
111 // libstdc++, and it's compiled with exceptions, then in practice
112 // these __throw_*() functions will always throw exceptions (shades of
113 // -fshort-wchar). We don't want that and so define our own inlined
115 #ifndef mozilla_throw_gcc_h
116 diff --git config/make-stl-wrappers.py config/make-stl-wrappers.py
117 --- mozilla/config/make-stl-wrappers.py
118 +++ mozilla/config/make-stl-wrappers.py
119 @@ -25,28 +25,26 @@ def header_path(header, compiler):
120 def is_comment(line):
121 return re.match(r'\s*#.*', line)
123 def main(outdir, compiler, template_file, header_list_file):
124 if not os.path.isdir(outdir):
127 template = open(template_file, 'r').read()
128 - path_to_new = header_path('new', compiler)
130 for header in open(header_list_file, 'r'):
131 header = header.rstrip()
132 if 0 == len(header) or is_comment(header):
135 path = header_path(header, compiler)
136 with FileAvoidWrite(os.path.join(outdir, header)) as f:
137 f.write(string.Template(template).substitute(HEADER=header,
139 - NEW_HEADER_PATH=path_to_new))
143 if __name__ == '__main__':
144 if 5 != len(sys.argv):
146 python {0} OUT_DIR ('msvc'|'gcc') TEMPLATE_FILE HEADER_LIST_FILE
147 """.format(sys.argv[0]), file=sys.stderr)
149 diff --git config/msvc-stl-wrapper.template.h config/msvc-stl-wrapper.template.h
150 --- mozilla/config/msvc-stl-wrapper.template.h
151 +++ mozilla/config/msvc-stl-wrapper.template.h
154 /* This Source Code Form is subject to the terms of the Mozilla Public
155 * License, v. 2.0. If a copy of the MPL was not distributed with this
156 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
158 #ifndef mozilla_${HEADER}_h
159 #define mozilla_${HEADER}_h
161 -#ifndef MOZ_HAVE_INCLUDED_ALLOC
162 -#define MOZ_HAVE_INCLUDED_ALLOC
165 # error "STL code can only be used with -fno-exceptions"
168 +// Include mozalloc after the STL header and all other headers it includes
169 +// have been preprocessed.
170 +#if !defined(MOZ_INCLUDE_MOZALLOC_H)
171 +# define MOZ_INCLUDE_MOZALLOC_H
172 +# define MOZ_INCLUDE_MOZALLOC_H_FROM_${HEADER}
175 // Code built with !_HAS_EXCEPTIONS calls std::_Throw(), but the win2k
176 // CRT doesn't export std::_Throw(). So we define it.
177 #ifndef mozilla_Throw_h
178 # include "mozilla/throw_msvc.h"
181 -// Code might include <new> before other wrapped headers, but <new>
182 -// includes <exception> and so we want to wrap it. But mozalloc.h
183 -// wants <new> also, so we break the cycle by always explicitly
184 -// including <new> here.
185 -#include <${NEW_HEADER_PATH}>
187 -// See if we're in code that can use mozalloc. NB: this duplicates
188 -// code in nscore.h because nscore.h pulls in prtypes.h, and chromium
189 -// can't build with that being included before base/basictypes.h.
190 -#if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
191 -# include "mozilla/mozalloc.h"
193 -# error "STL code can only be used with infallible ::operator new()"
195 -#endif /* MOZ_HAVE_INCLUDED_ALLOC */
199 // http://msdn.microsoft.com/en-us/library/aa985982%28VS.80%29.aspx
201 // http://msdn.microsoft.com/en-us/library/aa985965%28VS.80%29.aspx
202 // there appear to be two types of STL container checking. The
203 // former is enabled by -D_DEBUG (which is implied by -MDd or -MTd), and
204 // looks to be full generation/mutation checked iterators as done by
206 // but that's OK because we're not throwing them.
207 #pragma warning( push )
208 #pragma warning( disable : 4275 4530 )
210 #include <${HEADER_PATH}>
212 #pragma warning( pop )
214 +#ifdef MOZ_INCLUDE_MOZALLOC_H_FROM_${HEADER}
215 +// See if we're in code that can use mozalloc. NB: this duplicates
216 +// code in nscore.h because nscore.h pulls in prtypes.h, and chromium
217 +// can't build with that being included before base/basictypes.h.
218 +# if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
219 +# include "mozilla/mozalloc.h"
221 +# error "STL code can only be used with infallible ::operator new()"
225 #endif // if mozilla_${HEADER}_h
226 diff --git memory/mozalloc/mozalloc.h memory/mozalloc/mozalloc.h
227 --- mozilla/memory/mozalloc/mozalloc.h
228 +++ mozilla/memory/mozalloc/mozalloc.h
231 #ifndef mozilla_mozalloc_h
232 #define mozilla_mozalloc_h
235 * https://bugzilla.mozilla.org/show_bug.cgi?id=427099
240 #if defined(__cplusplus)
242 +// Since libstdc++ 6, including the C headers (e.g. stdlib.h) instead of the
243 +// corresponding C++ header (e.g. cstdlib) can cause confusion in C++ code
244 +// using things defined there. Specifically, with stdlib.h, the use of abs()
245 +// in gfx/graphite2/src/inc/UtfCodec.h somehow ends up picking the wrong abs()
249 +# include <stdlib.h>
250 +# include <string.h>
253 #if defined(__cplusplus)
254 #include "mozilla/fallible.h"
255 #include "mozilla/TemplateLib.h"
257 #include "mozilla/Attributes.h"
258 #include "mozilla/Types.h"