1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99 ft=cpp:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla SpiderMonkey JavaScript code.
19 * The Initial Developer of the Original Code is
20 * the Mozilla Foundation.
21 * Portions created by the Initial Developer are Copyright (C) 2011
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * 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 ***** */
40 #ifndef js_utility_h__
41 #define js_utility_h__
46 #include "mozilla/Util.h"
50 /* The public JS engine namespace. */
53 /* The mozilla-shared reusable template/utility namespace. */
56 /* The private JS engine namespace. */
59 /* The private namespace is a superset of the public/shared namespaces. */
61 using namespace mozilla
;
64 #endif /* __cplusplus */
69 * Pattern used to overwrite freed memory. If you are accessing an object with
70 * this pattern, you probably have a dangling pointer.
72 #define JS_FREE_PATTERN 0xDA
76 # define JS_ASSERT(expr) \
77 ((expr) ? (void)0 : JS_Assert(#expr, __FILE__, __LINE__))
78 # define JS_ASSERT_IF(cond, expr) \
79 ((!(cond) || (expr)) ? (void)0 : JS_Assert(#expr, __FILE__, __LINE__))
80 # define JS_NOT_REACHED(reason) \
81 JS_Assert(reason, __FILE__, __LINE__)
82 # define JS_ALWAYS_TRUE(expr) JS_ASSERT(expr)
83 # define JS_ALWAYS_FALSE(expr) JS_ASSERT(!(expr))
85 # define JS_THREADSAFE_ASSERT(expr) JS_ASSERT(expr)
87 # define JS_THREADSAFE_ASSERT(expr) ((void) 0)
90 # define JS_ASSERT(expr) ((void) 0)
91 # define JS_ASSERT_IF(cond,expr) ((void) 0)
92 # define JS_NOT_REACHED(reason)
93 # define JS_ALWAYS_TRUE(expr) ((void) (expr))
94 # define JS_ALWAYS_FALSE(expr) ((void) (expr))
95 # define JS_THREADSAFE_ASSERT(expr) ((void) 0)
101 * A compile-time assert. "cond" must be a constant expression. The macro can
102 * be used only in places where an "extern" declaration is allowed.
106 * Sun Studio C++ compiler has a bug
107 * "sizeof expression not accepted as size of array parameter"
108 * It happens when js_static_assert() function is declared inside functions.
109 * The bug number is 6688515. It is not public yet.
110 * Therefore, for Sun Studio, declare js_static_assert as an array instead.
112 # define JS_STATIC_ASSERT(cond) extern char js_static_assert[(cond) ? 1 : -1]
115 # define JS_STATIC_ASSERT_GLUE1(x,y) x##y
116 # define JS_STATIC_ASSERT_GLUE(x,y) JS_STATIC_ASSERT_GLUE1(x,y)
117 # define JS_STATIC_ASSERT(cond) \
118 typedef int JS_STATIC_ASSERT_GLUE(js_static_assert, __COUNTER__)[(cond) ? 1 : -1]
120 # define JS_STATIC_ASSERT(cond) extern void js_static_assert(int arg[(cond) ? 1 : -1])
124 #define JS_STATIC_ASSERT_IF(cond, expr) JS_STATIC_ASSERT(!(cond) || (expr))
127 * Abort the process in a non-graceful manner. This will cause a core file,
128 * call to the debugger or other moral equivalent as well as causing the
129 * entire process to stop.
131 extern JS_PUBLIC_API(void) JS_Abort(void);
134 * Custom allocator support for SpiderMonkey
136 #if defined JS_USE_CUSTOM_ALLOCATOR
137 # include "jscustomallocator.h"
141 * In order to test OOM conditions, when the shell command-line option
142 * |-A NUM| is passed, we fail continuously after the NUM'th allocation.
144 extern JS_PUBLIC_DATA(JSUint32
) OOM_maxAllocations
; /* set from shell/js.cpp */
145 extern JS_PUBLIC_DATA(JSUint32
) OOM_counter
; /* data race, who cares. */
146 # define JS_OOM_POSSIBLY_FAIL() \
149 if (OOM_counter++ >= OOM_maxAllocations) { \
155 # define JS_OOM_POSSIBLY_FAIL() do {} while(0)
159 * SpiderMonkey code should not be calling these allocation functions directly.
160 * Instead, all calls should go through JSRuntime, JSContext or OffTheBooks.
161 * However, js_free() can be called directly.
163 static JS_INLINE
void* js_malloc(size_t bytes
)
165 JS_OOM_POSSIBLY_FAIL();
166 return malloc(bytes
);
169 static JS_INLINE
void* js_calloc(size_t bytes
)
171 JS_OOM_POSSIBLY_FAIL();
172 return calloc(bytes
, 1);
175 static JS_INLINE
void* js_realloc(void* p
, size_t bytes
)
177 JS_OOM_POSSIBLY_FAIL();
178 return realloc(p
, bytes
);
181 static JS_INLINE
void js_free(void* p
)
185 #endif/* JS_USE_CUSTOM_ALLOCATOR */
188 * Replace bit-scanning code sequences with CPU-specific instructions to
189 * speedup calculations of ceiling/floor log2.
191 * With GCC 3.4 or later we can use __builtin_clz for that, see bug 327129.
193 * SWS: Added MSVC intrinsic bitscan support. See bugs 349364 and 356856.
195 #if defined(_WIN32) && (_MSC_VER >= 1300) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
197 unsigned char _BitScanForward(unsigned long * Index
, unsigned long Mask
);
198 unsigned char _BitScanReverse(unsigned long * Index
, unsigned long Mask
);
199 # pragma intrinsic(_BitScanForward,_BitScanReverse)
201 __forceinline
static int
202 __BitScanForward32(unsigned int val
)
206 _BitScanForward(&idx
, (unsigned long)val
);
209 __forceinline
static int
210 __BitScanReverse32(unsigned int val
)
214 _BitScanReverse(&idx
, (unsigned long)val
);
215 return (int)(31-idx
);
217 # define js_bitscan_ctz32(val) __BitScanForward32(val)
218 # define js_bitscan_clz32(val) __BitScanReverse32(val)
219 # define JS_HAS_BUILTIN_BITSCAN32
221 #if defined(_M_AMD64) || defined(_M_X64)
222 unsigned char _BitScanForward64(unsigned long * Index
, unsigned __int64 Mask
);
223 unsigned char _BitScanReverse64(unsigned long * Index
, unsigned __int64 Mask
);
224 # pragma intrinsic(_BitScanForward64,_BitScanReverse64)
226 __forceinline
static int
227 __BitScanForward64(unsigned __int64 val
)
231 _BitScanForward64(&idx
, val
);
234 __forceinline
static int
235 __BitScanReverse64(unsigned __int64 val
)
239 _BitScanReverse64(&idx
, val
);
240 return (int)(63-idx
);
242 # define js_bitscan_ctz64(val) __BitScanForward64(val)
243 # define js_bitscan_clz64(val) __BitScanReverse64(val)
244 # define JS_HAS_BUILTIN_BITSCAN64
246 #elif (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
248 # define js_bitscan_ctz32(val) __builtin_ctz(val)
249 # define js_bitscan_clz32(val) __builtin_clz(val)
250 # define JS_HAS_BUILTIN_BITSCAN32
251 # if (JS_BYTES_PER_WORD == 8)
252 # define js_bitscan_ctz64(val) __builtin_ctzll(val)
253 # define js_bitscan_clz64(val) __builtin_clzll(val)
254 # define JS_HAS_BUILTIN_BITSCAN64
260 ** Macro version of JS_CeilingLog2: Compute the log of the least power of
261 ** 2 greater than or equal to _n. The result is returned in _log2.
263 #ifdef JS_HAS_BUILTIN_BITSCAN32
265 * Use intrinsic function or count-leading-zeros to calculate ceil(log2(_n)).
266 * The macro checks for "n <= 1" and not "n != 0" as js_bitscan_clz32(0) is
269 # define JS_CEILING_LOG2(_log2,_n) \
271 unsigned int j_ = (unsigned int)(_n); \
272 (_log2) = (j_ <= 1 ? 0 : 32 - js_bitscan_clz32(j_ - 1)); \
275 # define JS_CEILING_LOG2(_log2,_n) \
277 JSUint32 j_ = (JSUint32)(_n); \
279 if ((j_) & ((j_)-1)) \
282 (_log2) += 16, (j_) >>= 16; \
284 (_log2) += 8, (j_) >>= 8; \
286 (_log2) += 4, (j_) >>= 4; \
288 (_log2) += 2, (j_) >>= 2; \
295 ** Macro version of JS_FloorLog2: Compute the log of the greatest power of
296 ** 2 less than or equal to _n. The result is returned in _log2.
298 ** This is equivalent to finding the highest set bit in the word.
300 #ifdef JS_HAS_BUILTIN_BITSCAN32
302 * Use js_bitscan_clz32 or count-leading-zeros to calculate floor(log2(_n)).
303 * Since js_bitscan_clz32(0) is undefined, the macro set the loweset bit to 1
304 * to ensure 0 result when _n == 0.
306 # define JS_FLOOR_LOG2(_log2,_n) \
308 (_log2) = 31 - js_bitscan_clz32(((unsigned int)(_n)) | 1); \
311 # define JS_FLOOR_LOG2(_log2,_n) \
313 JSUint32 j_ = (JSUint32)(_n); \
316 (_log2) += 16, (j_) >>= 16; \
318 (_log2) += 8, (j_) >>= 8; \
320 (_log2) += 4, (j_) >>= 4; \
322 (_log2) += 2, (j_) >>= 2; \
330 * Compute the log of the least power of 2 greater than or equal to n. This is
331 * a version of JS_CeilingLog2 that operates on unsigned integers with
332 * CPU-dependant size.
334 #define JS_CEILING_LOG2W(n) ((n) <= 1 ? 0 : 1 + JS_FLOOR_LOG2W((n) - 1))
338 * Compute the log of the greatest power of 2 less than or equal to n.
339 * This is a version of JS_FloorLog2 that operates on unsigned integers with
340 * CPU-dependant size and requires that n != 0.
342 #define JS_FLOOR_LOG2W(n) (JS_ASSERT((n) != 0), js_FloorLog2wImpl(n))
344 #if JS_BYTES_PER_WORD == 4
345 # ifdef JS_HAS_BUILTIN_BITSCAN32
346 # define js_FloorLog2wImpl(n) \
347 ((size_t)(JS_BITS_PER_WORD - 1 - js_bitscan_clz32(n)))
349 extern size_t js_FloorLog2wImpl(size_t n
);
351 #elif JS_BYTES_PER_WORD == 8
352 # ifdef JS_HAS_BUILTIN_BITSCAN64
353 # define js_FloorLog2wImpl(n) \
354 ((size_t)(JS_BITS_PER_WORD - 1 - js_bitscan_clz64(n)))
356 extern size_t js_FloorLog2wImpl(size_t n
);
359 # error "NOT SUPPORTED"
368 * User guide to memory management within SpiderMonkey:
373 * - Prefer to allocate using JSContext:
374 * cx->{malloc_,realloc_,calloc_,new_,array_new}
376 * - If no JSContext is available, use a JSRuntime:
377 * rt->{malloc_,realloc_,calloc_,new_,array_new}
379 * - As a last resort, use unaccounted allocation ("OffTheBooks"):
380 * js::OffTheBooks::{malloc_,realloc_,calloc_,new_,array_new}
383 * - When the deallocation occurs on a slow path, use:
384 * Foreground::{free_,delete_,array_delete}
386 * - Otherwise deallocate on a background thread using a JSContext:
387 * cx->{free_,delete_,array_delete}
389 * - If no JSContext is available, use a JSRuntime:
390 * rt->{free_,delete_,array_delete}
392 * - As a last resort, use UnwantedForeground deallocation:
393 * js::UnwantedForeground::{free_,delete_,array_delete}
397 * - Mixing and matching these allocators is allowed (you may free memory
398 * allocated by any allocator, with any deallocator).
400 * - Never, ever use normal C/C++ memory management:
401 * malloc, free, new, new[], delete, operator new, etc.
403 * - Never, ever use low-level SpiderMonkey allocators:
404 * js_malloc(), js_free(), js_calloc(), js_realloc()
405 * Their use is reserved for the other memory managers.
407 * - Classes which have private constructors or destructors should have
408 * JS_DECLARE_ALLOCATION_FRIENDS_FOR_PRIVATE_CONSTRUCTOR added to their
413 * Using vanilla new/new[] is unsafe in SpiderMonkey because they throw on
414 * failure instead of returning NULL, which is what SpiderMonkey expects.
415 * (Even overriding them is unsafe, as the system's C++ runtime library may
416 * throw, which we do not support. We also can't just use the 'nothrow'
417 * variant of new/new[], because we want to mediate *all* allocations
418 * within SpiderMonkey, to satisfy any embedders using
419 * JS_USE_CUSTOM_ALLOCATOR.)
421 * JSContexts and JSRuntimes keep track of memory allocated, and use this
422 * accounting to schedule GC. OffTheBooks does not. We'd like to remove
423 * OffTheBooks allocations as much as possible (bug 636558).
425 * On allocation failure, a JSContext correctly reports an error, which a
426 * JSRuntime and OffTheBooks does not.
428 * A JSContext deallocates in a background thread. A JSRuntime might
429 * deallocate in the background in the future, but does not now. Foreground
430 * deallocation is preferable on slow paths. UnwantedForeground deallocations
431 * occur where we have no JSContext or JSRuntime, and the deallocation is not
432 * on a slow path. We want to remove UnwantedForeground deallocations (bug
435 * JS_DECLARE_ALLOCATION_FRIENDS_FOR_PRIVATE_CONSTRUCTOR makes the allocation
436 * classes friends with your class, giving them access to private
437 * constructors and destructors.
439 * |make check| does a source level check on the number of uses OffTheBooks,
440 * UnwantedForeground, js_malloc, js_free etc, to prevent regressions. If you
441 * really must add one, update Makefile.in, and run |make check|.
443 * |make check| also statically prevents the use of vanilla new/new[].
446 #define JS_NEW_BODY(allocator, t, parms) \
447 void *memory = allocator(sizeof(t)); \
448 return memory ? new(memory) t parms : NULL;
451 * Given a class which should provide new_() methods, add
452 * JS_DECLARE_NEW_METHODS (see JSContext for a usage example). This
453 * adds new_()s with up to 12 parameters. Add more versions of new_ below if
454 * you need more than 12 parameters.
456 * Note: Do not add a ; at the end of a use of JS_DECLARE_NEW_METHODS,
457 * or the build will break.
459 #define JS_DECLARE_NEW_METHODS(ALLOCATOR, QUALIFIERS)\
461 QUALIFIERS T *new_() {\
462 JS_NEW_BODY(ALLOCATOR, T, ())\
465 template <class T, class P1>\
466 QUALIFIERS T *new_(P1 p1) {\
467 JS_NEW_BODY(ALLOCATOR, T, (p1))\
470 template <class T, class P1, class P2>\
471 QUALIFIERS T *new_(P1 p1, P2 p2) {\
472 JS_NEW_BODY(ALLOCATOR, T, (p1, p2))\
475 template <class T, class P1, class P2, class P3>\
476 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3) {\
477 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3))\
480 template <class T, class P1, class P2, class P3, class P4>\
481 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4) {\
482 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4))\
485 template <class T, class P1, class P2, class P3, class P4, class P5>\
486 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {\
487 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5))\
490 template <class T, class P1, class P2, class P3, class P4, class P5, class P6>\
491 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {\
492 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5, p6))\
495 template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7>\
496 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {\
497 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5, p6, p7))\
500 template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>\
501 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) {\
502 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5, p6, p7, p8))\
505 template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9>\
506 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9) {\
507 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5, p6, p7, p8, p9))\
510 template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10>\
511 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10) {\
512 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10))\
515 template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11>\
516 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11) {\
517 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11))\
520 template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12>\
521 QUALIFIERS T *new_(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11, P12 p12) {\
522 JS_NEW_BODY(ALLOCATOR, T, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12))\
524 static const int JSMinAlignment = 8;\
526 QUALIFIERS T *array_new(size_t n) {\
527 /* The length is stored just before the vector memory. */\
528 uint64 numBytes64 = uint64(JSMinAlignment) + uint64(sizeof(T)) * uint64(n);\
529 size_t numBytes = size_t(numBytes64);\
530 if (numBytes64 != numBytes) {\
531 JS_ASSERT(0); /* we want to know if this happens in debug builds */\
534 void *memory = ALLOCATOR(numBytes);\
537 *(size_t *)memory = n;\
538 memory = (void*)(uintptr_t(memory) + JSMinAlignment);\
539 return new(memory) T[n];\
543 #define JS_DECLARE_DELETE_METHODS(DEALLOCATOR, QUALIFIERS)\
545 QUALIFIERS void delete_(T *p) {\
553 QUALIFIERS void array_delete(T *p) {\
555 void* p0 = (void *)(uintptr_t(p) - js::OffTheBooks::JSMinAlignment);\
556 size_t n = *(size_t *)p0;\
557 for (size_t i = 0; i < n; i++)\
565 * In general, all allocations should go through a JSContext or JSRuntime, so
566 * that the garbage collector knows how much memory has been allocated. In
567 * cases where it is difficult to use a JSContext or JSRuntime, OffTheBooks can
568 * be used, though this is undesirable.
574 JS_DECLARE_NEW_METHODS(::js_malloc
, JS_ALWAYS_INLINE
static)
576 static JS_INLINE
void* malloc_(size_t bytes
) {
577 return ::js_malloc(bytes
);
580 static JS_INLINE
void* calloc_(size_t bytes
) {
581 return ::js_calloc(bytes
);
584 static JS_INLINE
void* realloc_(void* p
, size_t bytes
) {
585 return ::js_realloc(p
, bytes
);
590 * We generally prefer deallocating using JSContext because it can happen in
591 * the background. On slow paths, we may prefer foreground allocation.
595 /* See parentheses comment above. */
596 static JS_ALWAYS_INLINE
void free_(void* p
) {
600 JS_DECLARE_DELETE_METHODS(::js_free
, JS_ALWAYS_INLINE
static)
603 class UnwantedForeground
: public Foreground
{
609 * Note lack of ; in JSRuntime below. This is intentional so "calling" this
612 #define JS_DECLARE_ALLOCATION_FRIENDS_FOR_PRIVATE_CONSTRUCTOR \
613 friend class js::OffTheBooks;\
614 friend class js::Foreground;\
615 friend class js::UnwantedForeground;\
616 friend struct ::JSContext;\
617 friend struct ::JSRuntime
620 * The following classes are designed to cause assertions to detect
621 * inadvertent use of guard objects as temporaries. In other words,
622 * when we have a guard object whose only purpose is its constructor and
623 * destructor (and is never otherwise referenced), the intended use
625 * JSAutoTempValueRooter tvr(cx, 1, &val);
626 * but is is easy to accidentally write:
627 * JSAutoTempValueRooter(cx, 1, &val);
628 * which compiles just fine, but runs the destructor well before the
631 * They work by adding (#ifdef DEBUG) an additional parameter to the
632 * guard object's constructor, with a default value, so that users of
633 * the guard object's API do not need to do anything. The default value
634 * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
635 * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
636 * guarantee that temporaries are destroyed in the reverse of their
637 * construction order, but I actually can't find a statement that that
638 * is true in the general case (beyond the two specific cases mentioned
639 * there). However, it seems to be true.
641 * These classes are intended to be used only via the macros immediately
643 * JS_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
644 * variable, and should be put where a declaration of a private
645 * member variable would be placed.
646 * JS_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
647 * parameters to each constructor of the guard object; it declares
648 * (ifdef DEBUG) an additional parameter.
649 * JS_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
650 * constructor. It uses the parameter declared by
651 * JS_GUARD_OBJECT_NOTIFIER_PARAM.
654 class JS_FRIEND_API(JSGuardObjectNotifier
)
657 bool* mStatementDone
;
659 JSGuardObjectNotifier() : mStatementDone(NULL
) {}
661 ~JSGuardObjectNotifier() {
662 *mStatementDone
= true;
665 void setStatementDone(bool *aStatementDone
) {
666 mStatementDone
= aStatementDone
;
670 class JS_FRIEND_API(JSGuardObjectNotificationReceiver
)
675 JSGuardObjectNotificationReceiver() : mStatementDone(false) {}
677 ~JSGuardObjectNotificationReceiver() {
679 * Assert that the guard object was not used as a temporary.
680 * (Note that this assert might also fire if Init is not called
681 * because the guard object's implementation is not using the
682 * above macros correctly.)
684 JS_ASSERT(mStatementDone
);
687 void Init(const JSGuardObjectNotifier
&aNotifier
) {
689 * aNotifier is passed as a const reference so that we can pass a
690 * temporary, but we really intend it as non-const
692 const_cast<JSGuardObjectNotifier
&>(aNotifier
).
693 setStatementDone(&mStatementDone
);
697 #define JS_DECL_USE_GUARD_OBJECT_NOTIFIER \
698 JSGuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
699 #define JS_GUARD_OBJECT_NOTIFIER_PARAM \
700 , const JSGuardObjectNotifier& _notifier = JSGuardObjectNotifier()
701 #define JS_GUARD_OBJECT_NOTIFIER_PARAM_NO_INIT \
702 , const JSGuardObjectNotifier& _notifier
703 #define JS_GUARD_OBJECT_NOTIFIER_PARAM0 \
704 const JSGuardObjectNotifier& _notifier = JSGuardObjectNotifier()
705 #define JS_GUARD_OBJECT_NOTIFIER_INIT \
706 JS_BEGIN_MACRO _mCheckNotUsedAsTemporary.Init(_notifier); JS_END_MACRO
708 #else /* defined(DEBUG) */
710 #define JS_DECL_USE_GUARD_OBJECT_NOTIFIER
711 #define JS_GUARD_OBJECT_NOTIFIER_PARAM
712 #define JS_GUARD_OBJECT_NOTIFIER_PARAM_NO_INIT
713 #define JS_GUARD_OBJECT_NOTIFIER_PARAM0
714 #define JS_GUARD_OBJECT_NOTIFIER_INIT JS_BEGIN_MACRO JS_END_MACRO
716 #endif /* !defined(DEBUG) */
723 * Some types can be copied much more efficiently if we know the original's
724 * value need not be preserved --- that is, if we are doing a "move", not a
725 * "copy". For example, if we have:
730 * the constructor for v must apply a copy constructor to each element of u ---
731 * taking time linear in the length of u. However, if we know we will not need u
732 * any more once v has been initialized, then we could initialize v very
733 * efficiently simply by stealing u's dynamically allocated buffer and giving it
734 * to v --- a constant-time operation, regardless of the size of u.
736 * Moves often appear in container implementations. For example, when we append
737 * to a vector, we may need to resize its buffer. This entails moving each of
738 * its extant elements from the old, smaller buffer to the new, larger buffer.
739 * But once the elements have been migrated, we're just going to throw away the
740 * old buffer; we don't care if they still have their values. So if the vector's
741 * element type can implement "move" more efficiently than "copy", the vector
742 * resizing should by all means use a "move" operation. Hash tables also need to
745 * The details of the optimization, and whether it's worth applying, vary from
746 * one type to the next. And while some constructor calls are moves, many really
747 * are copies, and can't be optimized this way. So we need:
749 * 1) a way for a particular invocation of a copy constructor to say that it's
750 * really a move, and that the value of the original isn't important
751 * afterwards (althought it must still be safe to destroy); and
753 * 2) a way for a type (like Vector) to announce that it can be moved more
754 * efficiently than it can be copied, and provide an implementation of that
757 * The Move(T &) function takes a reference to a T, and returns an MoveRef<T>
758 * referring to the same value; that's 1). An MoveRef<T> is simply a reference
759 * to a T, annotated to say that a copy constructor applied to it may move that
760 * T, instead of copying it. Finally, a constructor that accepts an MoveRef<T>
761 * should perform a more efficient move, instead of a copy, providing 2).
763 * So, where we might define a copy constructor for a class C like this:
765 * C(const C &rhs) { ... copy rhs to this ... }
767 * we would declare a move constructor like this:
769 * C(MoveRef<C> rhs) { ... move rhs to this ... }
771 * And where we might perform a copy like this:
775 * we would perform a move like this:
779 * Note that MoveRef<T> implicitly converts to T &, so you can pass an
780 * MoveRef<T> to an ordinary copy constructor for a type that doesn't support a
781 * special move constructor, and you'll just get a copy. This means that
782 * templates can use Move whenever they know they won't use the original value
783 * any more, even if they're not sure whether the type at hand has a specialized
784 * move constructor. If it doesn't, the MoveRef<T> will just convert to a T &,
785 * and the ordinary copy constructor will apply.
787 * A class with a move constructor can also provide a move assignment operator,
788 * which runs this's destructor, and then applies the move constructor to
789 * *this's memory. A typical definition:
791 * C &operator=(MoveRef<C> rhs) {
797 * With that in place, one can write move assignments like this:
801 * This destroys c1, moves c1's value to c2, and leaves c1 in an undefined but
802 * destructible state.
804 * This header file defines MoveRef and Move in the js namespace. It's up to
805 * individual containers to annotate moves as such, by calling Move; and it's up
806 * to individual types to define move constructors.
808 * One hint: if you're writing a move constructor where the type has members
809 * that should be moved themselves, it's much nicer to write this:
811 * C(MoveRef<C> c) : x(c->x), y(c->y) { }
813 * than the equivalent:
815 * C(MoveRef<C> c) { new(&x) X(c->x); new(&y) Y(c->y); }
817 * especially since GNU C++ fails to notice that this does indeed initialize x
818 * and y, which may matter if they're const.
824 explicit MoveRef(T
&t
) : pointer(&t
) { }
825 T
&operator*() const { return *pointer
; }
826 T
*operator->() const { return pointer
; }
827 #ifdef __GXX_EXPERIMENTAL_CXX0X__
829 * If MoveRef is used in a rvalue position (which is expected), we can
830 * end up in a situation where, without this ifdef, we would try to pass
831 * a T& to a move constructor, which fails. It is not clear if the compiler
832 * should instead use the copy constructor, but for now this lets us build
833 * with clang. See bug 689066 and llvm.org/pr11003 for the details.
834 * Note: We can probably remove MoveRef completely once we are comfortable
837 operator T
&& () const { return static_cast<T
&&>(*pointer
); }
839 operator T
& () const { return *pointer
; }
846 MoveRef
<T
> Move(T
&t
) { return MoveRef
<T
>(t
); }
849 MoveRef
<T
> Move(const T
&t
) { return MoveRef
<T
>(const_cast<T
&>(t
)); }
851 /* Useful for implementing containers that assert non-reentrancy */
852 class ReentrancyGuard
854 /* ReentrancyGuard is not copyable. */
855 ReentrancyGuard(const ReentrancyGuard
&);
856 void operator=(const ReentrancyGuard
&);
864 ReentrancyGuard(T
&obj
)
865 : entered(obj
.entered
)
867 ReentrancyGuard(T
&/*obj*/)
884 * Round x up to the nearest power of 2. This function assumes that the most
885 * significant bit of x is not set, which would lead to overflow.
887 JS_ALWAYS_INLINE
size_t
888 RoundUpPow2(size_t x
)
890 return size_t(1) << JS_CEILING_LOG2W(x
);
895 #endif /* defined(__cplusplus) */
898 * This signature is for malloc_usable_size-like functions used to measure
899 * memory usage. A return value of zero indicates that the size is unknown,
900 * and so a fall-back computation should be done for the size.
902 typedef size_t(*JSUsableSizeFun
)(void *p
);
904 /* sixgill annotation defines */
905 #ifndef HAVE_STATIC_ANNOTATIONS
906 # define HAVE_STATIC_ANNOTATIONS
908 # define STATIC_PRECONDITION(COND) __attribute__((precondition(#COND)))
909 # define STATIC_PRECONDITION_ASSUME(COND) __attribute__((precondition_assume(#COND)))
910 # define STATIC_POSTCONDITION(COND) __attribute__((postcondition(#COND)))
911 # define STATIC_POSTCONDITION_ASSUME(COND) __attribute__((postcondition_assume(#COND)))
912 # define STATIC_INVARIANT(COND) __attribute__((invariant(#COND)))
913 # define STATIC_INVARIANT_ASSUME(COND) __attribute__((invariant_assume(#COND)))
914 # define STATIC_PASTE2(X,Y) X ## Y
915 # define STATIC_PASTE1(X,Y) STATIC_PASTE2(X,Y)
916 # define STATIC_ASSERT(COND) \
918 __attribute__((assert_static(#COND), unused)) \
919 int STATIC_PASTE1(assert_static_, __COUNTER__); \
921 # define STATIC_ASSUME(COND) \
923 __attribute__((assume_static(#COND), unused)) \
924 int STATIC_PASTE1(assume_static_, __COUNTER__); \
926 # define STATIC_ASSERT_RUNTIME(COND) \
928 __attribute__((assert_static_runtime(#COND), unused)) \
929 int STATIC_PASTE1(assert_static_runtime_, __COUNTER__); \
931 # else /* XGILL_PLUGIN */
932 # define STATIC_PRECONDITION(COND) /* nothing */
933 # define STATIC_PRECONDITION_ASSUME(COND) /* nothing */
934 # define STATIC_POSTCONDITION(COND) /* nothing */
935 # define STATIC_POSTCONDITION_ASSUME(COND) /* nothing */
936 # define STATIC_INVARIANT(COND) /* nothing */
937 # define STATIC_INVARIANT_ASSUME(COND) /* nothing */
938 # define STATIC_ASSERT(COND) JS_BEGIN_MACRO /* nothing */ JS_END_MACRO
939 # define STATIC_ASSUME(COND) JS_BEGIN_MACRO /* nothing */ JS_END_MACRO
940 # define STATIC_ASSERT_RUNTIME(COND) JS_BEGIN_MACRO /* nothing */ JS_END_MACRO
941 # endif /* XGILL_PLUGIN */
942 # define STATIC_SKIP_INFERENCE STATIC_INVARIANT(skip_inference())
943 #endif /* HAVE_STATIC_ANNOTATIONS */
945 #endif /* js_utility_h__ */