Bug 1726737 - Part 6: Add public header for out-of-memory callbacks. r=arai
authorAndré Bargull <andre.bargull@gmail.com>
Fri, 20 Aug 2021 18:10:34 +0000
changeset 589455 b2689f96728ae32f7283b55ce24fb94caed64c85
parent 589454 5bb913fbf8bd68bb89e0b306e49fd598086421d4
child 589456 8a14435cfbdca408c92f396a78269dd8e43b9d05
push id148361
push userandre.bargull@gmail.com
push dateFri, 20 Aug 2021 18:16:41 +0000
treeherderautoland@01fc3504c984 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarai
bugs1726737
milestone93.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1726737 - Part 6: Add public header for out-of-memory callbacks. r=arai Neither "js/public/MemoryFunctions.h" nor "js/public/AllocPolicy.h" seemed like the perfect place where these two functions should be placed, therefore we just create a new header file for them. Differential Revision: https://phabricator.services.mozilla.com/D123197
js/public/MemoryCallbacks.h
js/src/jsapi.cpp
js/src/jsapi.h
js/src/moz.build
js/src/shell/js.cpp
js/src/vm/JSContext.cpp
js/src/vm/Runtime.h
new file mode 100644
--- /dev/null
+++ b/js/public/MemoryCallbacks.h
@@ -0,0 +1,47 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#ifndef js_MemoryCallbacks_h
+#define js_MemoryCallbacks_h
+
+#include "jstypes.h"
+
+struct JS_PUBLIC_API JSContext;
+
+namespace JS {
+
+/**
+ * If a large allocation fails when calling pod_{calloc,realloc}CanGC, the JS
+ * engine may call the large-allocation-failure callback, if set, to allow the
+ * embedding to flush caches, possibly perform shrinking GCs, etc. to make some
+ * room. The allocation will then be retried (and may still fail.) This callback
+ * can be called on any thread and must be set at most once in a process.
+ */
+
+using LargeAllocationFailureCallback = void (*)();
+
+extern JS_PUBLIC_API void SetProcessLargeAllocationFailureCallback(
+    LargeAllocationFailureCallback afc);
+
+/**
+ * Unlike the error reporter, which is only called if the exception for an OOM
+ * bubbles up and is not caught, the OutOfMemoryCallback is called immediately
+ * at the OOM site to allow the embedding to capture the current state of heap
+ * allocation before anything is freed. If the large-allocation-failure callback
+ * is called at all (not all allocation sites call the large-allocation-failure
+ * callback on failure), it is called before the out-of-memory callback; the
+ * out-of-memory callback is only called if the allocation still fails after the
+ * large-allocation-failure callback has returned.
+ */
+
+using OutOfMemoryCallback = void (*)(JSContext*, void*);
+
+extern JS_PUBLIC_API void SetOutOfMemoryCallback(JSContext* cx,
+                                                 OutOfMemoryCallback cb,
+                                                 void* data);
+
+}  // namespace JS
+
+#endif  // js_MemoryCallbacks_h
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -46,16 +46,17 @@
 #include "js/ContextOptions.h"  // JS::ContextOptions{,Ref}
 #include "js/Conversions.h"
 #include "js/ErrorInterceptor.h"
 #include "js/friend/ErrorMessages.h"  // js::GetErrorMessage, JSMSG_*
 #include "js/friend/StackLimits.h"    // js::AutoCheckRecursionLimit
 #include "js/Initialization.h"
 #include "js/JSON.h"
 #include "js/LocaleSensitive.h"
+#include "js/MemoryCallbacks.h"
 #include "js/MemoryFunctions.h"
 #include "js/OffThreadScriptCompilation.h"  // js::UseOffThreadParseGlobal
 #include "js/PropertySpec.h"
 #include "js/Proxy.h"
 #include "js/ScriptPrivate.h"
 #include "js/StableStringChars.h"
 #include "js/Stack.h"
 #include "js/StreamConsumer.h"
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -36,16 +36,17 @@
 #include "js/ErrorInterceptor.h"
 #include "js/ErrorReport.h"
 #include "js/Exception.h"
 #include "js/GCAPI.h"
 #include "js/GCVector.h"
 #include "js/HashTable.h"
 #include "js/Id.h"
 #include "js/MapAndSet.h"
+#include "js/MemoryCallbacks.h"
 #include "js/MemoryFunctions.h"
 #include "js/OffThreadScriptCompilation.h"
 #include "js/Principals.h"
 #include "js/PropertyAndElement.h"  // JS_Enumerate
 #include "js/PropertyDescriptor.h"
 #include "js/PropertySpec.h"
 #include "js/Realm.h"
 #include "js/RealmIterators.h"
@@ -1098,46 +1099,16 @@ class MOZ_RAII AutoHideScriptedCaller {
  * Attempt to disable Wasm's usage of reserving a large virtual memory
  * allocation to avoid bounds checking overhead. This must be called before any
  * Wasm module or memory is created in this process, or else this function will
  * fail.
  */
 [[nodiscard]] extern JS_PUBLIC_API bool DisableWasmHugeMemory();
 
 /**
- * If a large allocation fails when calling pod_{calloc,realloc}CanGC, the JS
- * engine may call the large-allocation-failure callback, if set, to allow the
- * embedding to flush caches, possibly perform shrinking GCs, etc. to make some
- * room. The allocation will then be retried (and may still fail.) This callback
- * can be called on any thread and must be set at most once in a process.
- */
-
-using LargeAllocationFailureCallback = void (*)();
-
-extern JS_PUBLIC_API void SetProcessLargeAllocationFailureCallback(
-    LargeAllocationFailureCallback afc);
-
-/**
- * Unlike the error reporter, which is only called if the exception for an OOM
- * bubbles up and is not caught, the OutOfMemoryCallback is called immediately
- * at the OOM site to allow the embedding to capture the current state of heap
- * allocation before anything is freed. If the large-allocation-failure callback
- * is called at all (not all allocation sites call the large-allocation-failure
- * callback on failure), it is called before the out-of-memory callback; the
- * out-of-memory callback is only called if the allocation still fails after the
- * large-allocation-failure callback has returned.
- */
-
-using OutOfMemoryCallback = void (*)(JSContext*, void*);
-
-extern JS_PUBLIC_API void SetOutOfMemoryCallback(JSContext* cx,
-                                                 OutOfMemoryCallback cb,
-                                                 void* data);
-
-/**
  * When the JSRuntime is about to block in an Atomics.wait() JS call or in a
  * `wait` instruction in WebAssembly, it can notify the host by means of a call
  * to BeforeWaitCallback.  After the wait, it can notify the host by means of a
  * call to AfterWaitCallback.  Both callbacks must be null, or neither.
  *
  * (If you change the callbacks from null to not-null or vice versa while some
  * thread on the runtime is in a wait, you will be sorry.)
  *
--- a/js/src/moz.build
+++ b/js/src/moz.build
@@ -161,16 +161,17 @@ EXPORTS.js += [
     "../public/HashTable.h",
     "../public/HeapAPI.h",
     "../public/HelperThreadAPI.h",
     "../public/Id.h",
     "../public/Initialization.h",
     "../public/JSON.h",
     "../public/LocaleSensitive.h",
     "../public/MapAndSet.h",
+    "../public/MemoryCallbacks.h",
     "../public/MemoryFunctions.h",
     "../public/MemoryMetrics.h",
     "../public/Modules.h",
     "../public/Object.h",
     "../public/OffThreadScriptCompilation.h",
     "../public/Principals.h",
     "../public/Printf.h",
     "../public/ProfilingCategory.h",
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -126,16 +126,17 @@
 #include "js/friend/DumpFunctions.h"     // JS::FormatStackDump
 #include "js/friend/ErrorMessages.h"     // js::GetErrorMessage, JSMSG_*
 #include "js/friend/StackLimits.h"       // js::AutoCheckRecursionLimit
 #include "js/friend/WindowProxy.h"  // js::IsWindowProxy, js::SetWindowProxyClass, js::ToWindowProxyIfWindow, js::ToWindowIfWindowProxy
 #include "js/GCAPI.h"               // JS::AutoCheckCannotGC
 #include "js/GCVector.h"
 #include "js/Initialization.h"
 #include "js/JSON.h"
+#include "js/MemoryCallbacks.h"
 #include "js/MemoryFunctions.h"
 #include "js/Modules.h"  // JS::GetModulePrivate, JS::SetModule{DynamicImport,Metadata,Resolve}Hook, JS::SetModulePrivate
 #include "js/Object.h"  // JS::GetClass, JS::GetCompartment, JS::GetReservedSlot, JS::SetReservedSlot
 #include "js/OffThreadScriptCompilation.h"  // JS::SetUseOffThreadParseGlobal, js::UseOffThreadParseGlobal
 #include "js/Printf.h"
 #include "js/PropertyAndElement.h"  // JS_DefineElement, JS_DefineFunction, JS_DefineFunctions, JS_DefineProperties, JS_DefineProperty, JS_GetElement, JS_GetProperty, JS_GetPropertyById, JS_HasProperty, JS_SetElement, JS_SetProperty, JS_SetPropertyById
 #include "js/PropertySpec.h"
 #include "js/Realm.h"
--- a/js/src/vm/JSContext.cpp
+++ b/js/src/vm/JSContext.cpp
@@ -40,16 +40,17 @@
 #include "jit/PcScriptCache.h"
 #include "jit/Simulator.h"
 #include "js/CallAndConstruct.h"  // JS::Call
 #include "js/CharacterEncoding.h"
 #include "js/ContextOptions.h"        // JS::ContextOptions
 #include "js/ErrorInterceptor.h"      // JSErrorInterceptor
 #include "js/friend/ErrorMessages.h"  // js::GetErrorMessage, JSMSG_*
 #include "js/friend/StackLimits.h"    // js::ReportOverRecursed
+#include "js/MemoryCallbacks.h"
 #include "js/Printf.h"
 #include "js/PropertyAndElement.h"  // JS_GetProperty
 #include "js/Stack.h"
 #include "util/DiagnosticAssertions.h"
 #include "util/DifferentialTesting.h"
 #include "util/DoubleToString.h"
 #include "util/NativeStack.h"
 #include "util/Text.h"
--- a/js/src/vm/Runtime.h
+++ b/js/src/vm/Runtime.h
@@ -39,16 +39,17 @@
 #include "js/Debug.h"
 #include "js/experimental/CTypes.h"      // JS::CTypesActivityCallback
 #include "js/experimental/SourceHook.h"  // js::SourceHook
 #include "js/friend/StackLimits.h"       // js::ReportOverRecursed
 #include "js/friend/UsageStatistics.h"   // JSAccumulateTelemetryDataCallback
 #include "js/GCVector.h"
 #include "js/HashTable.h"
 #include "js/Initialization.h"
+#include "js/MemoryCallbacks.h"
 #include "js/Modules.h"  // JS::Module{DynamicImport,Metadata,Resolve}Hook
 #ifdef DEBUG
 #  include "js/Proxy.h"  // For AutoEnterPolicy
 #endif
 #include "js/ScriptPrivate.h"
 #include "js/Stack.h"
 #include "js/Stream.h"  // JS::AbortSignalIsAborted
 #include "js/StreamConsumer.h"
@@ -1008,17 +1009,17 @@ struct JSRuntime {
 #endif
 
   void toggleAutoWritableJitCodeActive(bool b) {
     MOZ_ASSERT(autoWritableJitCodeActive_ != b,
                "AutoWritableJitCode should not be nested.");
     autoWritableJitCodeActive_ = b;
   }
 
-  /* See comment for JS::SetOutOfMemoryCallback in jsapi.h. */
+  /* See comment for JS::SetOutOfMemoryCallback in js/MemoryCallbacks.h. */
   js::MainThreadData<JS::OutOfMemoryCallback> oomCallback;
   js::MainThreadData<void*> oomCallbackData;
 
   /*
    * Debugger.Memory functions like takeCensus use this embedding-provided
    * function to assess the size of malloc'd blocks of memory.
    */
   js::MainThreadData<mozilla::MallocSizeOf> debuggerMallocSizeOf;