Bug 588735 - Mirror glass caption buttons for rtl windows. r=roc, a=blocking-betaN.
[mozilla-central.git] / js / src / jsutil.h
blob377a460b03e7c81365a8588b17e5d25c6ab766b1
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * PR assertion checker.
44 #ifndef jsutil_h___
45 #define jsutil_h___
47 #include "jstypes.h"
48 #include <stdlib.h>
50 JS_BEGIN_EXTERN_C
53 * JS_Assert is present even in release builds, for the benefit of applications
54 * that build DEBUG and link against a non-DEBUG SpiderMonkey library.
56 extern JS_PUBLIC_API(void)
57 JS_Assert(const char *s, const char *file, JSIntn ln);
59 #ifdef DEBUG
61 #define JS_ASSERT(expr) \
62 ((expr) ? (void)0 : JS_Assert(#expr, __FILE__, __LINE__))
64 #define JS_ASSERT_IF(cond, expr) \
65 ((!(cond) || (expr)) ? (void)0 : JS_Assert(#expr, __FILE__, __LINE__))
67 #define JS_NOT_REACHED(reason) \
68 JS_Assert(reason, __FILE__, __LINE__)
70 #define JS_ALWAYS_TRUE(expr) JS_ASSERT(expr)
72 #else
74 #define JS_ASSERT(expr) ((void) 0)
75 #define JS_ASSERT_IF(cond,expr) ((void) 0)
76 #define JS_NOT_REACHED(reason)
77 #define JS_ALWAYS_TRUE(expr) ((void) (expr))
79 #endif /* defined(DEBUG) */
82 * Compile-time assert. "cond" must be a constant expression.
83 * The macro can be used only in places where an "extern" declaration is
84 * allowed.
88 * Sun Studio C++ compiler has a bug
89 * "sizeof expression not accepted as size of array parameter"
90 * The bug number is 6688515. It is not public yet.
91 * Turn off this assert for Sun Studio until this bug is fixed.
93 #ifdef __SUNPRO_CC
94 #define JS_STATIC_ASSERT(cond)
95 #else
96 #ifdef __COUNTER__
97 #define JS_STATIC_ASSERT_GLUE1(x,y) x##y
98 #define JS_STATIC_ASSERT_GLUE(x,y) JS_STATIC_ASSERT_GLUE1(x,y)
99 #define JS_STATIC_ASSERT(cond) \
100 typedef int JS_STATIC_ASSERT_GLUE(js_static_assert, __COUNTER__)[(cond) ? 1 : -1]
101 #else
102 #define JS_STATIC_ASSERT(cond) extern void js_static_assert(int arg[(cond) ? 1 : -1])
103 #endif
104 #endif
106 #define JS_STATIC_ASSERT_IF(cond, expr) JS_STATIC_ASSERT(!(cond) || (expr))
109 * Abort the process in a non-graceful manner. This will cause a core file,
110 * call to the debugger or other moral equivalent as well as causing the
111 * entire process to stop.
113 extern JS_PUBLIC_API(void) JS_Abort(void);
115 #ifdef DEBUG
116 # define JS_BASIC_STATS 1
117 #endif
119 #ifdef DEBUG_brendan
120 # define JS_SCOPE_DEPTH_METER 1
121 #endif
123 #ifdef JS_BASIC_STATS
125 #include <stdio.h>
127 typedef struct JSBasicStats {
128 uint32 num;
129 uint32 max;
130 double sum;
131 double sqsum;
132 uint32 logscale; /* logarithmic scale: 0 (linear), 2, 10 */
133 uint32 hist[11];
134 } JSBasicStats;
136 #define JS_INIT_STATIC_BASIC_STATS {0,0,0,0,0,{0,0,0,0,0,0,0,0,0,0,0}}
137 #define JS_BASIC_STATS_INIT(bs) memset((bs), 0, sizeof(JSBasicStats))
139 #define JS_BASIC_STATS_ACCUM(bs,val) \
140 JS_BasicStatsAccum(bs, val)
142 #define JS_MeanAndStdDevBS(bs,sigma) \
143 JS_MeanAndStdDev((bs)->num, (bs)->sum, (bs)->sqsum, sigma)
145 extern void
146 JS_BasicStatsAccum(JSBasicStats *bs, uint32 val);
148 extern double
149 JS_MeanAndStdDev(uint32 num, double sum, double sqsum, double *sigma);
151 extern void
152 JS_DumpBasicStats(JSBasicStats *bs, const char *title, FILE *fp);
154 extern void
155 JS_DumpHistogram(JSBasicStats *bs, FILE *fp);
157 #else
159 #define JS_BASIC_STATS_ACCUM(bs,val) /* nothing */
161 #endif /* JS_BASIC_STATS */
164 #if defined(DEBUG_notme) && defined(XP_UNIX)
166 typedef struct JSCallsite JSCallsite;
168 struct JSCallsite {
169 uint32 pc;
170 char *name;
171 const char *library;
172 int offset;
173 JSCallsite *parent;
174 JSCallsite *siblings;
175 JSCallsite *kids;
176 void *handy;
179 extern JS_FRIEND_API(JSCallsite *)
180 JS_Backtrace(int skip);
182 extern JS_FRIEND_API(void)
183 JS_DumpBacktrace(JSCallsite *trace);
184 #endif
186 #if defined JS_USE_CUSTOM_ALLOCATOR
188 #include "jscustomallocator.h"
190 #else
192 static JS_INLINE void* js_malloc(size_t bytes) {
193 return malloc(bytes);
196 static JS_INLINE void* js_calloc(size_t bytes) {
197 return calloc(bytes, 1);
200 static JS_INLINE void* js_realloc(void* p, size_t bytes) {
201 return realloc(p, bytes);
204 static JS_INLINE void js_free(void* p) {
205 free(p);
207 #endif/* JS_USE_CUSTOM_ALLOCATOR */
209 JS_END_EXTERN_C
211 #ifdef __cplusplus
214 * The following classes are designed to cause assertions to detect
215 * inadvertent use of guard objects as temporaries. In other words,
216 * when we have a guard object whose only purpose is its constructor and
217 * destructor (and is never otherwise referenced), the intended use
218 * might be:
219 * JSAutoTempValueRooter tvr(cx, 1, &val);
220 * but is is easy to accidentally write:
221 * JSAutoTempValueRooter(cx, 1, &val);
222 * which compiles just fine, but runs the destructor well before the
223 * intended time.
225 * They work by adding (#ifdef DEBUG) an additional parameter to the
226 * guard object's constructor, with a default value, so that users of
227 * the guard object's API do not need to do anything. The default value
228 * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
229 * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
230 * guarantee that temporaries are destroyed in the reverse of their
231 * construction order, but I actually can't find a statement that that
232 * is true in the general case (beyond the two specific cases mentioned
233 * there). However, it seems to be true.
235 * These classes are intended to be used only via the macros immediately
236 * below them:
237 * JS_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
238 * variable, and should be put where a declaration of a private
239 * member variable would be placed.
240 * JS_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
241 * parameters to each constructor of the guard object; it declares
242 * (ifdef DEBUG) an additional parameter.
243 * JS_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
244 * constructor. It uses the parameter declared by
245 * JS_GUARD_OBJECT_NOTIFIER_PARAM.
247 #ifdef DEBUG
248 class JSGuardObjectNotifier
250 private:
251 bool* mStatementDone;
252 public:
253 JSGuardObjectNotifier() : mStatementDone(NULL) {}
255 ~JSGuardObjectNotifier() {
256 *mStatementDone = true;
259 void SetStatementDone(bool *aStatementDone) {
260 mStatementDone = aStatementDone;
264 class JSGuardObjectNotificationReceiver
266 private:
267 bool mStatementDone;
268 public:
269 JSGuardObjectNotificationReceiver() : mStatementDone(false) {}
271 ~JSGuardObjectNotificationReceiver() {
273 * Assert that the guard object was not used as a temporary.
274 * (Note that this assert might also fire if Init is not called
275 * because the guard object's implementation is not using the
276 * above macros correctly.)
278 JS_ASSERT(mStatementDone);
281 void Init(const JSGuardObjectNotifier &aNotifier) {
283 * aNotifier is passed as a const reference so that we can pass a
284 * temporary, but we really intend it as non-const
286 const_cast<JSGuardObjectNotifier&>(aNotifier).
287 SetStatementDone(&mStatementDone);
291 #define JS_DECL_USE_GUARD_OBJECT_NOTIFIER \
292 JSGuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
293 #define JS_GUARD_OBJECT_NOTIFIER_PARAM \
294 , const JSGuardObjectNotifier& _notifier = JSGuardObjectNotifier()
295 #define JS_GUARD_OBJECT_NOTIFIER_INIT \
296 JS_BEGIN_MACRO _mCheckNotUsedAsTemporary.Init(_notifier); JS_END_MACRO
298 #else /* defined(DEBUG) */
300 #define JS_DECL_USE_GUARD_OBJECT_NOTIFIER
301 #define JS_GUARD_OBJECT_NOTIFIER_PARAM
302 #define JS_GUARD_OBJECT_NOTIFIER_INIT JS_BEGIN_MACRO JS_END_MACRO
304 #endif /* !defined(DEBUG) */
306 namespace js {
308 template <class T>
309 JS_ALWAYS_INLINE static void
310 PodZero(T *t)
312 memset(t, 0, sizeof(T));
315 template <class T>
316 JS_ALWAYS_INLINE static void
317 PodZero(T *t, size_t nelem)
319 memset(t, 0, nelem * sizeof(T));
323 * Arrays implicitly convert to pointers to their first element, which is
324 * dangerous when combined with the above PodZero definitions. Adding an
325 * overload for arrays is ambiguous, so we need another identifier. The
326 * ambiguous overload is left to catch mistaken uses of PodZero; if you get a
327 * compile error involving PodZero and array types, use PodArrayZero instead.
329 template <class T, size_t N> static void PodZero(T (&)[N]); /* undefined */
330 template <class T, size_t N> static void PodZero(T (&)[N], size_t); /* undefined */
332 template <class T, size_t N>
333 JS_ALWAYS_INLINE static void
334 PodArrayZero(T (&t)[N])
336 memset(t, 0, N * sizeof(T));
339 } /* namespace js */
341 #endif /* defined(__cplusplus) */
343 #endif /* jsutil_h___ */