2013-04-16 13:47:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-10-04 15:33:00 -07:00
|
|
|
|
2013-06-19 17:59:09 -07:00
|
|
|
#ifndef js_Utility_h
|
|
|
|
#define js_Utility_h
|
2011-10-04 15:33:00 -07:00
|
|
|
|
2011-12-19 12:54:12 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-06-07 14:41:11 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-05-01 12:45:05 -07:00
|
|
|
#include "mozilla/Compiler.h"
|
2013-07-02 17:25:13 -07:00
|
|
|
#include "mozilla/Move.h"
|
2013-09-19 12:24:53 -07:00
|
|
|
#include "mozilla/NullPtr.h"
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/Scoped.h"
|
2013-07-08 12:42:13 -07:00
|
|
|
#include "mozilla/TemplateLib.h"
|
2011-12-19 12:54:12 -08:00
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-03-10 12:53:03 -08:00
|
|
|
#ifdef JS_OOM_DO_BACKTRACES
|
|
|
|
#include <execinfo.h>
|
2013-08-13 15:34:12 -07:00
|
|
|
#include <stdio.h>
|
2012-03-10 12:53:03 -08:00
|
|
|
#endif
|
|
|
|
|
2011-12-13 11:26:58 -08:00
|
|
|
#include "jstypes.h"
|
2011-10-04 15:33:00 -07:00
|
|
|
|
|
|
|
/* The public JS engine namespace. */
|
|
|
|
namespace JS {}
|
|
|
|
|
|
|
|
/* The mozilla-shared reusable template/utility namespace. */
|
|
|
|
namespace mozilla {}
|
|
|
|
|
|
|
|
/* The private JS engine namespace. */
|
2013-03-19 03:35:41 -07:00
|
|
|
namespace js {}
|
2011-10-04 15:33:00 -07:00
|
|
|
|
|
|
|
/*
|
2014-03-20 14:38:50 -07:00
|
|
|
* Patterns used by SpiderMonkey to overwrite unused memory. If you are
|
|
|
|
* accessing an object with one of these pattern, you probably have a dangling
|
|
|
|
* pointer.
|
2011-10-04 15:33:00 -07:00
|
|
|
*/
|
2014-03-20 14:38:50 -07:00
|
|
|
#define JS_FRESH_NURSERY_PATTERN 0x2F
|
|
|
|
#define JS_SWEPT_NURSERY_PATTERN 0x2B
|
|
|
|
#define JS_ALLOCATED_NURSERY_PATTERN 0x2D
|
|
|
|
#define JS_FRESH_TENURED_PATTERN 0x4F
|
|
|
|
#define JS_SWEPT_TENURED_PATTERN 0x4B
|
|
|
|
#define JS_ALLOCATED_TENURED_PATTERN 0x4D
|
|
|
|
#define JS_SWEPT_CODE_PATTERN 0x3b
|
|
|
|
#define JS_SWEPT_FRAME_PATTERN 0x5b
|
2014-06-09 22:04:14 -07:00
|
|
|
#define JS_POISONED_FORKJOIN_CHUNK 0xBD
|
2011-10-04 15:33:00 -07:00
|
|
|
|
2011-12-19 12:54:12 -08:00
|
|
|
#define JS_ASSERT(expr) MOZ_ASSERT(expr)
|
2012-01-02 12:55:34 -08:00
|
|
|
#define JS_ASSERT_IF(cond, expr) MOZ_ASSERT_IF(cond, expr)
|
2013-02-12 11:07:26 -08:00
|
|
|
|
2013-07-18 10:59:53 -07:00
|
|
|
#define JS_STATIC_ASSERT(cond) static_assert(cond, "JS_STATIC_ASSERT")
|
2011-12-19 13:58:30 -08:00
|
|
|
#define JS_STATIC_ASSERT_IF(cond, expr) MOZ_STATIC_ASSERT_IF(cond, expr, "JS_STATIC_ASSERT_IF")
|
2011-10-04 15:33:00 -07:00
|
|
|
|
2012-06-07 14:41:11 -07:00
|
|
|
extern MOZ_NORETURN JS_PUBLIC_API(void)
|
|
|
|
JS_Assert(const char *s, const char *file, int ln);
|
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
/*
|
|
|
|
* Custom allocator support for SpiderMonkey
|
|
|
|
*/
|
|
|
|
#if defined JS_USE_CUSTOM_ALLOCATOR
|
|
|
|
# include "jscustomallocator.h"
|
|
|
|
#else
|
2014-03-27 11:15:22 -07:00
|
|
|
# if defined(DEBUG) || defined(JS_OOM_BREAKPOINT)
|
2011-10-04 15:33:00 -07:00
|
|
|
/*
|
2013-05-20 12:59:55 -07:00
|
|
|
* In order to test OOM conditions, when the testing function
|
|
|
|
* oomAfterAllocations COUNT is passed, we fail continuously after the NUM'th
|
|
|
|
* allocation from now.
|
2011-10-04 15:33:00 -07:00
|
|
|
*/
|
2014-04-08 10:52:24 -07:00
|
|
|
extern JS_PUBLIC_DATA(uint32_t) OOM_maxAllocations; /* set in builtin/TestingFunctions.cpp */
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
extern JS_PUBLIC_DATA(uint32_t) OOM_counter; /* data race, who cares. */
|
2012-03-10 12:53:03 -08:00
|
|
|
|
2014-03-25 16:31:54 -07:00
|
|
|
#ifdef JS_OOM_BREAKPOINT
|
|
|
|
static MOZ_NEVER_INLINE void js_failedAllocBreakpoint() { asm(""); }
|
|
|
|
#define JS_OOM_CALL_BP_FUNC() js_failedAllocBreakpoint()
|
|
|
|
#else
|
|
|
|
#define JS_OOM_CALL_BP_FUNC() do {} while(0)
|
|
|
|
#endif
|
2012-03-10 12:53:03 -08:00
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
# define JS_OOM_POSSIBLY_FAIL() \
|
|
|
|
do \
|
|
|
|
{ \
|
2012-03-01 07:01:45 -08:00
|
|
|
if (++OOM_counter > OOM_maxAllocations) { \
|
2014-03-25 16:31:54 -07:00
|
|
|
JS_OOM_CALL_BP_FUNC();\
|
2013-09-19 12:24:53 -07:00
|
|
|
return nullptr; \
|
2011-10-04 15:33:00 -07:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
# else
|
|
|
|
# define JS_OOM_POSSIBLY_FAIL() do {} while(0)
|
2014-03-27 11:15:22 -07:00
|
|
|
# endif /* DEBUG || JS_OOM_BREAKPOINT */
|
2011-10-04 15:33:00 -07:00
|
|
|
|
2013-10-21 14:34:24 -07:00
|
|
|
static inline void* js_malloc(size_t bytes)
|
2011-10-04 15:33:00 -07:00
|
|
|
{
|
|
|
|
JS_OOM_POSSIBLY_FAIL();
|
|
|
|
return malloc(bytes);
|
|
|
|
}
|
|
|
|
|
2013-10-21 14:34:24 -07:00
|
|
|
static inline void* js_calloc(size_t bytes)
|
2011-10-04 15:33:00 -07:00
|
|
|
{
|
|
|
|
JS_OOM_POSSIBLY_FAIL();
|
|
|
|
return calloc(bytes, 1);
|
|
|
|
}
|
|
|
|
|
2013-10-21 14:34:24 -07:00
|
|
|
static inline void* js_calloc(size_t nmemb, size_t size)
|
2013-09-25 12:18:43 -07:00
|
|
|
{
|
|
|
|
JS_OOM_POSSIBLY_FAIL();
|
|
|
|
return calloc(nmemb, size);
|
|
|
|
}
|
|
|
|
|
2013-10-21 14:34:24 -07:00
|
|
|
static inline void* js_realloc(void* p, size_t bytes)
|
2011-10-04 15:33:00 -07:00
|
|
|
{
|
|
|
|
JS_OOM_POSSIBLY_FAIL();
|
|
|
|
return realloc(p, bytes);
|
|
|
|
}
|
|
|
|
|
2013-10-21 14:34:24 -07:00
|
|
|
static inline void js_free(void* p)
|
2011-10-04 15:33:00 -07:00
|
|
|
{
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
#endif/* JS_USE_CUSTOM_ALLOCATOR */
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
/*
|
2012-08-31 15:01:33 -07:00
|
|
|
* Low-level memory management in SpiderMonkey:
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* ** Do not use the standard malloc/free/realloc: SpiderMonkey allows these
|
|
|
|
* to be redefined (via JS_USE_CUSTOM_ALLOCATOR) and Gecko even #define's
|
|
|
|
* these symbols.
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* ** Do not use the builtin C++ operator new and delete: these throw on
|
|
|
|
* error and we cannot override them not to.
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* Allocation:
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* - If the lifetime of the allocation is tied to the lifetime of a GC-thing
|
|
|
|
* (that is, finalizing the GC-thing will free the allocation), call one of
|
|
|
|
* the following functions:
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* JSContext::{malloc_,realloc_,calloc_,new_}
|
|
|
|
* JSRuntime::{malloc_,realloc_,calloc_,new_}
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* These functions accumulate the number of bytes allocated which is used as
|
|
|
|
* part of the GC-triggering heuristic.
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* The difference between the JSContext and JSRuntime versions is that the
|
|
|
|
* cx version reports an out-of-memory error on OOM. (This follows from the
|
|
|
|
* general SpiderMonkey idiom that a JSContext-taking function reports its
|
|
|
|
* own errors.)
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* - Otherwise, use js_malloc/js_realloc/js_calloc/js_free/js_new
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* Deallocation:
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* - Ordinarily, use js_free/js_delete.
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* - For deallocations during GC finalization, use one of the following
|
|
|
|
* operations on the FreeOp provided to the finalizer:
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* FreeOp::{free_,delete_}
|
2011-10-04 15:33:00 -07:00
|
|
|
*
|
2012-08-31 15:01:33 -07:00
|
|
|
* The advantage of these operations is that the memory is batched and freed
|
|
|
|
* on another thread.
|
2011-10-04 15:33:00 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define JS_NEW_BODY(allocator, t, parms) \
|
|
|
|
void *memory = allocator(sizeof(t)); \
|
2013-09-19 12:24:53 -07:00
|
|
|
return memory ? new(memory) t parms : nullptr;
|
2011-10-04 15:33:00 -07:00
|
|
|
|
|
|
|
/*
|
2012-08-31 15:01:33 -07:00
|
|
|
* Given a class which should provide 'new' methods, add
|
2011-10-04 15:33:00 -07:00
|
|
|
* JS_DECLARE_NEW_METHODS (see JSContext for a usage example). This
|
2012-08-31 15:01:33 -07:00
|
|
|
* adds news with up to 12 parameters. Add more versions of new below if
|
2011-10-04 15:33:00 -07:00
|
|
|
* you need more than 12 parameters.
|
|
|
|
*
|
|
|
|
* Note: Do not add a ; at the end of a use of JS_DECLARE_NEW_METHODS,
|
|
|
|
* or the build will break.
|
|
|
|
*/
|
2012-08-31 15:01:33 -07:00
|
|
|
#define JS_DECLARE_NEW_METHODS(NEWNAME, ALLOCATOR, QUALIFIERS)\
|
2011-10-04 15:33:00 -07:00
|
|
|
template <class T>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME() MOZ_HEAP_ALLOCATOR {\
|
2011-10-04 15:33:00 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T, ())\
|
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3, class P4>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3, class P4, class P5>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3, class P4, class P5, class P6>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5),\
|
|
|
|
mozilla::Forward<P6>(p6)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5),\
|
|
|
|
mozilla::Forward<P6>(p6),\
|
|
|
|
mozilla::Forward<P7>(p7)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5),\
|
|
|
|
mozilla::Forward<P6>(p6),\
|
|
|
|
mozilla::Forward<P7>(p7),\
|
|
|
|
mozilla::Forward<P8>(p8)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8, P9 &&p9) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5),\
|
|
|
|
mozilla::Forward<P6>(p6),\
|
|
|
|
mozilla::Forward<P7>(p7),\
|
|
|
|
mozilla::Forward<P8>(p8),\
|
|
|
|
mozilla::Forward<P9>(p9)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
template <class T, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8, P9 &&p9, P10 &&p10) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5),\
|
|
|
|
mozilla::Forward<P6>(p6),\
|
|
|
|
mozilla::Forward<P7>(p7),\
|
|
|
|
mozilla::Forward<P8>(p8),\
|
|
|
|
mozilla::Forward<P9>(p9),\
|
|
|
|
mozilla::Forward<P10>(p10)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
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>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8, P9 &&p9, P10 &&p10, P11 &&p11) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5),\
|
|
|
|
mozilla::Forward<P6>(p6),\
|
|
|
|
mozilla::Forward<P7>(p7),\
|
|
|
|
mozilla::Forward<P8>(p8),\
|
|
|
|
mozilla::Forward<P9>(p9),\
|
|
|
|
mozilla::Forward<P10>(p10),\
|
|
|
|
mozilla::Forward<P11>(p11)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
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>\
|
2013-12-06 19:21:54 -08:00
|
|
|
QUALIFIERS T *NEWNAME(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8, P9 &&p9, P10 &&p10, P11 &&p11, P12 &&p12) MOZ_HEAP_ALLOCATOR {\
|
2013-09-27 11:19:43 -07:00
|
|
|
JS_NEW_BODY(ALLOCATOR, T,\
|
|
|
|
(mozilla::Forward<P1>(p1),\
|
|
|
|
mozilla::Forward<P2>(p2),\
|
|
|
|
mozilla::Forward<P3>(p3),\
|
|
|
|
mozilla::Forward<P4>(p4),\
|
|
|
|
mozilla::Forward<P5>(p5),\
|
|
|
|
mozilla::Forward<P6>(p6),\
|
|
|
|
mozilla::Forward<P7>(p7),\
|
|
|
|
mozilla::Forward<P8>(p8),\
|
|
|
|
mozilla::Forward<P9>(p9),\
|
|
|
|
mozilla::Forward<P10>(p10),\
|
|
|
|
mozilla::Forward<P11>(p11),\
|
|
|
|
mozilla::Forward<P12>(p12)))\
|
2011-10-04 15:33:00 -07:00
|
|
|
}\
|
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
JS_DECLARE_NEW_METHODS(js_new, js_malloc, static MOZ_ALWAYS_INLINE)
|
2011-10-04 15:33:00 -07:00
|
|
|
|
2012-08-31 15:01:33 -07:00
|
|
|
template <class T>
|
2014-01-24 20:14:56 -08:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2012-08-31 15:01:33 -07:00
|
|
|
js_delete(T *p)
|
|
|
|
{
|
|
|
|
if (p) {
|
|
|
|
p->~T();
|
|
|
|
js_free(p);
|
2011-10-04 15:33:00 -07:00
|
|
|
}
|
2012-08-31 15:01:33 -07:00
|
|
|
}
|
2011-10-04 15:33:00 -07:00
|
|
|
|
2013-04-11 04:48:09 -07:00
|
|
|
template<class T>
|
2014-01-24 20:14:56 -08:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2013-04-11 04:48:09 -07:00
|
|
|
js_delete_poison(T *p)
|
|
|
|
{
|
|
|
|
if (p) {
|
|
|
|
p->~T();
|
|
|
|
memset(p, 0x3B, sizeof(T));
|
|
|
|
js_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 15:10:10 -07:00
|
|
|
template <class T>
|
2014-01-24 20:14:56 -08:00
|
|
|
static MOZ_ALWAYS_INLINE T *
|
2012-08-31 15:10:10 -07:00
|
|
|
js_pod_malloc()
|
|
|
|
{
|
|
|
|
return (T *)js_malloc(sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2014-01-24 20:14:56 -08:00
|
|
|
static MOZ_ALWAYS_INLINE T *
|
2012-08-31 15:10:10 -07:00
|
|
|
js_pod_calloc()
|
|
|
|
{
|
|
|
|
return (T *)js_calloc(sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2014-01-24 20:14:56 -08:00
|
|
|
static MOZ_ALWAYS_INLINE T *
|
2012-08-31 15:10:10 -07:00
|
|
|
js_pod_malloc(size_t numElems)
|
|
|
|
{
|
2013-07-08 12:42:13 -07:00
|
|
|
if (numElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value)
|
2013-09-19 12:24:53 -07:00
|
|
|
return nullptr;
|
2012-08-31 15:10:10 -07:00
|
|
|
return (T *)js_malloc(numElems * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2014-01-24 20:14:56 -08:00
|
|
|
static MOZ_ALWAYS_INLINE T *
|
2012-08-31 15:10:10 -07:00
|
|
|
js_pod_calloc(size_t numElems)
|
|
|
|
{
|
2013-07-08 12:42:13 -07:00
|
|
|
if (numElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value)
|
2013-09-19 12:24:53 -07:00
|
|
|
return nullptr;
|
2012-08-31 15:10:10 -07:00
|
|
|
return (T *)js_calloc(numElems * sizeof(T));
|
|
|
|
}
|
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
namespace js {
|
|
|
|
|
2012-07-05 17:29:56 -07:00
|
|
|
template<typename T>
|
|
|
|
struct ScopedFreePtrTraits
|
|
|
|
{
|
|
|
|
typedef T* type;
|
2013-09-19 12:24:53 -07:00
|
|
|
static T* empty() { return nullptr; }
|
2012-08-31 15:01:33 -07:00
|
|
|
static void release(T* ptr) { js_free(ptr); }
|
2012-07-05 17:29:56 -07:00
|
|
|
};
|
2012-11-01 21:35:21 -07:00
|
|
|
SCOPED_TEMPLATE(ScopedJSFreePtr, ScopedFreePtrTraits)
|
2012-07-05 17:29:56 -07:00
|
|
|
|
2012-06-11 17:08:34 -07:00
|
|
|
template <typename T>
|
2012-07-05 17:29:56 -07:00
|
|
|
struct ScopedDeletePtrTraits : public ScopedFreePtrTraits<T>
|
2012-06-11 17:08:34 -07:00
|
|
|
{
|
2012-08-31 15:01:33 -07:00
|
|
|
static void release(T *ptr) { js_delete(ptr); }
|
2012-06-11 17:08:34 -07:00
|
|
|
};
|
2012-11-01 21:35:21 -07:00
|
|
|
SCOPED_TEMPLATE(ScopedJSDeletePtr, ScopedDeletePtrTraits)
|
2012-06-11 17:08:34 -07:00
|
|
|
|
2013-03-15 02:29:02 -07:00
|
|
|
template <typename T>
|
|
|
|
struct ScopedReleasePtrTraits : public ScopedFreePtrTraits<T>
|
|
|
|
{
|
|
|
|
static void release(T *ptr) { if (ptr) ptr->release(); }
|
|
|
|
};
|
|
|
|
SCOPED_TEMPLATE(ScopedReleasePtr, ScopedReleasePtrTraits)
|
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2014-07-09 16:53:42 -07:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct DeletePolicy
|
|
|
|
{
|
|
|
|
void operator()(T* ptr) {
|
|
|
|
js_delete(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FreePolicy
|
|
|
|
{
|
|
|
|
void operator()(void* ptr) {
|
|
|
|
js_free(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
namespace js {
|
2013-01-08 02:04:19 -08:00
|
|
|
|
2012-07-03 14:33:38 -07:00
|
|
|
/* Integral types for all hash functions. */
|
|
|
|
typedef uint32_t HashNumber;
|
2012-07-25 10:43:29 -07:00
|
|
|
const unsigned HashNumberSizeBits = 32;
|
2012-07-03 14:33:38 -07:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a raw hash code, h, return a number that can be used to select a hash
|
|
|
|
* bucket.
|
|
|
|
*
|
|
|
|
* This function aims to produce as uniform an output distribution as possible,
|
|
|
|
* especially in the most significant (leftmost) bits, even though the input
|
|
|
|
* distribution may be highly nonrandom, given the constraints that this must
|
|
|
|
* be deterministic and quick to compute.
|
|
|
|
*
|
|
|
|
* Since the leftmost bits of the result are best, the hash bucket index is
|
|
|
|
* computed by doing ScrambleHashCode(h) / (2^32/N) or the equivalent
|
|
|
|
* right-shift, not ScrambleHashCode(h) % N or the equivalent bit-mask.
|
|
|
|
*
|
|
|
|
* FIXME: OrderedHashTable uses a bit-mask; see bug 775896.
|
|
|
|
*/
|
|
|
|
inline HashNumber
|
|
|
|
ScrambleHashCode(HashNumber h)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Simply returning h would not cause any hash tables to produce wrong
|
|
|
|
* answers. But it can produce pathologically bad performance: The caller
|
|
|
|
* right-shifts the result, keeping only the highest bits. The high bits of
|
|
|
|
* hash codes are very often completely entropy-free. (So are the lowest
|
|
|
|
* bits.)
|
|
|
|
*
|
|
|
|
* So we use Fibonacci hashing, as described in Knuth, The Art of Computer
|
|
|
|
* Programming, 6.4. This mixes all the bits of the input hash code h.
|
|
|
|
*
|
|
|
|
* The value of goldenRatio is taken from the hex
|
|
|
|
* expansion of the golden ratio, which starts 1.9E3779B9....
|
|
|
|
* This value is especially good if values with consecutive hash codes
|
|
|
|
* are stored in a hash table; see Knuth for details.
|
|
|
|
*/
|
|
|
|
static const HashNumber goldenRatio = 0x9E3779B9U;
|
|
|
|
return h * goldenRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2012-04-12 18:15:26 -07:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Methods for poisoning GC heap pointer words and checking for poisoned words.
|
|
|
|
* These are in this file for use in Value methods and so forth.
|
|
|
|
*
|
|
|
|
* If the moving GC hazard analysis is in use and detects a non-rooted stack
|
|
|
|
* pointer to a GC thing, one byte of that pointer is poisoned to refer to an
|
|
|
|
* invalid location. For both 32 bit and 64 bit systems, the fourth byte of the
|
|
|
|
* pointer is overwritten, to reduce the likelihood of accidentally changing
|
|
|
|
* a live integer value.
|
|
|
|
*/
|
|
|
|
|
2012-07-20 16:22:00 -07:00
|
|
|
inline void PoisonPtr(void *v)
|
2012-04-12 18:15:26 -07:00
|
|
|
{
|
2013-12-06 15:03:08 -08:00
|
|
|
#if defined(JSGC_ROOT_ANALYSIS) && defined(JS_DEBUG)
|
2012-04-12 18:15:26 -07:00
|
|
|
uint8_t *ptr = (uint8_t *) v + 3;
|
|
|
|
*ptr = JS_FREE_PATTERN;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline bool IsPoisonedPtr(T *v)
|
|
|
|
{
|
2013-12-06 15:03:08 -08:00
|
|
|
#if defined(JSGC_ROOT_ANALYSIS) && defined(JS_DEBUG)
|
2012-04-12 18:15:26 -07:00
|
|
|
uint32_t mask = uintptr_t(v) & 0xff000000;
|
|
|
|
return mask == uint32_t(JS_FREE_PATTERN << 24);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
/* sixgill annotation defines */
|
|
|
|
#ifndef HAVE_STATIC_ANNOTATIONS
|
|
|
|
# define HAVE_STATIC_ANNOTATIONS
|
|
|
|
# ifdef XGILL_PLUGIN
|
|
|
|
# define STATIC_PRECONDITION(COND) __attribute__((precondition(#COND)))
|
|
|
|
# define STATIC_PRECONDITION_ASSUME(COND) __attribute__((precondition_assume(#COND)))
|
|
|
|
# define STATIC_POSTCONDITION(COND) __attribute__((postcondition(#COND)))
|
|
|
|
# define STATIC_POSTCONDITION_ASSUME(COND) __attribute__((postcondition_assume(#COND)))
|
|
|
|
# define STATIC_INVARIANT(COND) __attribute__((invariant(#COND)))
|
|
|
|
# define STATIC_INVARIANT_ASSUME(COND) __attribute__((invariant_assume(#COND)))
|
|
|
|
# define STATIC_PASTE2(X,Y) X ## Y
|
|
|
|
# define STATIC_PASTE1(X,Y) STATIC_PASTE2(X,Y)
|
|
|
|
# define STATIC_ASSERT(COND) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
__attribute__((assert_static(#COND), unused)) \
|
|
|
|
int STATIC_PASTE1(assert_static_, __COUNTER__); \
|
|
|
|
JS_END_MACRO
|
|
|
|
# define STATIC_ASSUME(COND) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
__attribute__((assume_static(#COND), unused)) \
|
|
|
|
int STATIC_PASTE1(assume_static_, __COUNTER__); \
|
|
|
|
JS_END_MACRO
|
|
|
|
# define STATIC_ASSERT_RUNTIME(COND) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
__attribute__((assert_static_runtime(#COND), unused)) \
|
|
|
|
int STATIC_PASTE1(assert_static_runtime_, __COUNTER__); \
|
|
|
|
JS_END_MACRO
|
|
|
|
# else /* XGILL_PLUGIN */
|
|
|
|
# define STATIC_PRECONDITION(COND) /* nothing */
|
|
|
|
# define STATIC_PRECONDITION_ASSUME(COND) /* nothing */
|
|
|
|
# define STATIC_POSTCONDITION(COND) /* nothing */
|
|
|
|
# define STATIC_POSTCONDITION_ASSUME(COND) /* nothing */
|
|
|
|
# define STATIC_INVARIANT(COND) /* nothing */
|
|
|
|
# define STATIC_INVARIANT_ASSUME(COND) /* nothing */
|
|
|
|
# define STATIC_ASSERT(COND) JS_BEGIN_MACRO /* nothing */ JS_END_MACRO
|
|
|
|
# define STATIC_ASSUME(COND) JS_BEGIN_MACRO /* nothing */ JS_END_MACRO
|
|
|
|
# define STATIC_ASSERT_RUNTIME(COND) JS_BEGIN_MACRO /* nothing */ JS_END_MACRO
|
|
|
|
# endif /* XGILL_PLUGIN */
|
|
|
|
# define STATIC_SKIP_INFERENCE STATIC_INVARIANT(skip_inference())
|
|
|
|
#endif /* HAVE_STATIC_ANNOTATIONS */
|
|
|
|
|
2013-06-19 17:59:09 -07:00
|
|
|
#endif /* js_Utility_h */
|