/[jscoverage]/trunk/js/jsutil.h
ViewVC logotype

Contents of /trunk/js/jsutil.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 507 - (show annotations)
Sun Jan 10 07:23:34 2010 UTC (9 years, 9 months ago) by siliconforks
File MIME type: text/plain
File size: 9599 byte(s)
Update SpiderMonkey from Firefox 3.6rc1.

1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
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/
10 *
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.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
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.
23 *
24 * Contributor(s):
25 *
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.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 /*
41 * PR assertion checker.
42 */
43
44 #ifndef jsutil_h___
45 #define jsutil_h___
46
47 #include <stdlib.h>
48
49 JS_BEGIN_EXTERN_C
50
51 /*
52 * JS_Assert is present even in release builds, for the benefit of applications
53 * that build DEBUG and link against a non-DEBUG SpiderMonkey library.
54 */
55 extern JS_PUBLIC_API(void)
56 JS_Assert(const char *s, const char *file, JSIntn ln);
57
58 #ifdef DEBUG
59
60 #define JS_ASSERT(expr) \
61 ((expr) ? (void)0 : JS_Assert(#expr, __FILE__, __LINE__))
62
63 #define JS_ASSERT_IF(cond, expr) \
64 ((!(cond) || (expr)) ? (void)0 : JS_Assert(#expr, __FILE__, __LINE__))
65
66 #define JS_NOT_REACHED(reason) \
67 JS_Assert(reason, __FILE__, __LINE__)
68
69 #else
70
71 #define JS_ASSERT(expr) ((void) 0)
72 #define JS_ASSERT_IF(cond,expr) ((void) 0)
73 #define JS_NOT_REACHED(reason)
74
75 #endif /* defined(DEBUG) */
76
77 /*
78 * Compile-time assert. "cond" must be a constant expression.
79 * The macro can be used only in places where an "extern" declaration is
80 * allowed.
81 */
82
83 /*
84 * Sun Studio C++ compiler has a bug
85 * "sizeof expression not accepted as size of array parameter"
86 * The bug number is 6688515. It is not public yet.
87 * Turn off this assert for Sun Studio until this bug is fixed.
88 */
89 #ifdef __SUNPRO_CC
90 #define JS_STATIC_ASSERT(cond)
91 #else
92 #ifdef __COUNTER__
93 #define JS_STATIC_ASSERT_GLUE1(x,y) x##y
94 #define JS_STATIC_ASSERT_GLUE(x,y) JS_STATIC_ASSERT_GLUE1(x,y)
95 #define JS_STATIC_ASSERT(cond) \
96 typedef int JS_STATIC_ASSERT_GLUE(js_static_assert, __COUNTER__)[(cond) ? 1 : -1]
97 #else
98 #define JS_STATIC_ASSERT(cond) extern void js_static_assert(int arg[(cond) ? 1 : -1])
99 #endif
100 #endif
101
102 #define JS_STATIC_ASSERT_IF(cond, expr) JS_STATIC_ASSERT(!(cond) || (expr))
103
104 /*
105 * Abort the process in a non-graceful manner. This will cause a core file,
106 * call to the debugger or other moral equivalent as well as causing the
107 * entire process to stop.
108 */
109 extern JS_PUBLIC_API(void) JS_Abort(void);
110
111 #if 0
112 # define JS_BASIC_STATS 1
113 # define JS_SCOPE_DEPTH_METER 1
114 #endif
115
116 #if defined DEBUG && !defined JS_BASIC_STATS
117 # define JS_BASIC_STATS 1
118 #endif
119
120 #ifdef JS_BASIC_STATS
121
122 #include <stdio.h>
123
124 typedef struct JSBasicStats {
125 uint32 num;
126 uint32 max;
127 double sum;
128 double sqsum;
129 uint32 logscale; /* logarithmic scale: 0 (linear), 2, 10 */
130 uint32 hist[11];
131 } JSBasicStats;
132
133 #define JS_INIT_STATIC_BASIC_STATS {0,0,0,0,0,{0,0,0,0,0,0,0,0,0,0,0}}
134 #define JS_BASIC_STATS_INIT(bs) memset((bs), 0, sizeof(JSBasicStats))
135
136 #define JS_BASIC_STATS_ACCUM(bs,val) \
137 JS_BasicStatsAccum(bs, val)
138
139 #define JS_MeanAndStdDevBS(bs,sigma) \
140 JS_MeanAndStdDev((bs)->num, (bs)->sum, (bs)->sqsum, sigma)
141
142 extern void
143 JS_BasicStatsAccum(JSBasicStats *bs, uint32 val);
144
145 extern double
146 JS_MeanAndStdDev(uint32 num, double sum, double sqsum, double *sigma);
147
148 extern void
149 JS_DumpBasicStats(JSBasicStats *bs, const char *title, FILE *fp);
150
151 extern void
152 JS_DumpHistogram(JSBasicStats *bs, FILE *fp);
153
154 #else
155
156 #define JS_BASIC_STATS_ACCUM(bs,val) /* nothing */
157
158 #endif /* JS_BASIC_STATS */
159
160
161 #if defined(DEBUG_notme) && defined(XP_UNIX)
162
163 typedef struct JSCallsite JSCallsite;
164
165 struct JSCallsite {
166 uint32 pc;
167 char *name;
168 const char *library;
169 int offset;
170 JSCallsite *parent;
171 JSCallsite *siblings;
172 JSCallsite *kids;
173 void *handy;
174 };
175
176 extern JS_FRIEND_API(JSCallsite *)
177 JS_Backtrace(int skip);
178
179 extern JS_FRIEND_API(void)
180 JS_DumpBacktrace(JSCallsite *trace);
181 #endif
182
183 static JS_INLINE void* js_malloc(size_t bytes) {
184 if (bytes < sizeof(void*)) /* for asyncFree */
185 bytes = sizeof(void*);
186 return malloc(bytes);
187 }
188
189 static JS_INLINE void* js_calloc(size_t bytes) {
190 if (bytes < sizeof(void*)) /* for asyncFree */
191 bytes = sizeof(void*);
192 return calloc(bytes, 1);
193 }
194
195 static JS_INLINE void* js_realloc(void* p, size_t bytes) {
196 if (bytes < sizeof(void*)) /* for asyncFree */
197 bytes = sizeof(void*);
198 return realloc(p, bytes);
199 }
200
201 static JS_INLINE void js_free(void* p) {
202 free(p);
203 }
204
205 JS_END_EXTERN_C
206
207 #ifdef __cplusplus
208
209 /**
210 * The following classes are designed to cause assertions to detect
211 * inadvertent use of guard objects as temporaries. In other words,
212 * when we have a guard object whose only purpose is its constructor and
213 * destructor (and is never otherwise referenced), the intended use
214 * might be:
215 * JSAutoTempValueRooter tvr(cx, 1, &val);
216 * but is is easy to accidentally write:
217 * JSAutoTempValueRooter(cx, 1, &val);
218 * which compiles just fine, but runs the destructor well before the
219 * intended time.
220 *
221 * They work by adding (#ifdef DEBUG) an additional parameter to the
222 * guard object's constructor, with a default value, so that users of
223 * the guard object's API do not need to do anything. The default value
224 * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
225 * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
226 * guarantee that temporaries are destroyed in the reverse of their
227 * construction order, but I actually can't find a statement that that
228 * is true in the general case (beyond the two specific cases mentioned
229 * there). However, it seems to be true.
230 *
231 * These classes are intended to be used only via the macros immediately
232 * below them:
233 * JS_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
234 * variable, and should be put where a declaration of a private
235 * member variable would be placed.
236 * JS_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
237 * parameters to each constructor of the guard object; it declares
238 * (ifdef DEBUG) an additional parameter.
239 * JS_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
240 * constructor. It uses the parameter declared by
241 * JS_GUARD_OBJECT_NOTIFIER_PARAM.
242 */
243 #ifdef DEBUG
244 class JSGuardObjectNotifier
245 {
246 private:
247 bool* mStatementDone;
248 public:
249 JSGuardObjectNotifier() : mStatementDone(NULL) {}
250
251 ~JSGuardObjectNotifier() {
252 *mStatementDone = true;
253 }
254
255 void SetStatementDone(bool *aStatementDone) {
256 mStatementDone = aStatementDone;
257 }
258 };
259
260 class JSGuardObjectNotificationReceiver
261 {
262 private:
263 bool mStatementDone;
264 public:
265 JSGuardObjectNotificationReceiver() : mStatementDone(false) {}
266
267 ~JSGuardObjectNotificationReceiver() {
268 // Assert that the guard object was not used as a temporary.
269 // (Note that this assert might also fire if Init is not called
270 // because the guard object's implementation is not using the
271 // above macros correctly.)
272 JS_ASSERT(mStatementDone);
273 }
274
275 void Init(const JSGuardObjectNotifier &aNotifier) {
276 // aNotifier is passed as a const reference so that we can pass a
277 // temporary, but we really intend it as non-const
278 const_cast<JSGuardObjectNotifier&>(aNotifier).
279 SetStatementDone(&mStatementDone);
280 }
281 };
282
283 #define JS_DECL_USE_GUARD_OBJECT_NOTIFIER \
284 JSGuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
285 #define JS_GUARD_OBJECT_NOTIFIER_PARAM \
286 , const JSGuardObjectNotifier& _notifier = JSGuardObjectNotifier()
287 #define JS_GUARD_OBJECT_NOTIFIER_INIT \
288 JS_BEGIN_MACRO _mCheckNotUsedAsTemporary.Init(_notifier); JS_END_MACRO
289
290 #else /* defined(DEBUG) */
291
292 #define JS_DECL_USE_GUARD_OBJECT_NOTIFIER
293 #define JS_GUARD_OBJECT_NOTIFIER_PARAM
294 #define JS_GUARD_OBJECT_NOTIFIER_INIT JS_BEGIN_MACRO JS_END_MACRO
295
296 #endif /* !defined(DEBUG) */
297
298 #endif /* defined(__cplusplus) */
299
300 #endif /* jsutil_h___ */

  ViewVC Help
Powered by ViewVC 1.1.24