1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef js_MemoryCallbacks_h 7 #define js_MemoryCallbacks_h 8 9 #include "jstypes.h" 10 11 struct JS_PUBLIC_API JSContext; 12 13 namespace JS { 14 15 /** 16 * If a large allocation fails when calling pod_{calloc,realloc}CanGC, the JS 17 * engine may call the large-allocation-failure callback, if set, to allow the 18 * embedding to flush caches, possibly perform shrinking GCs, etc. to make some 19 * room. The allocation will then be retried (and may still fail.) This callback 20 * can be called on any thread and must be set at most once in a process. 21 */ 22 23 using LargeAllocationFailureCallback = void (*)(); 24 25 extern JS_PUBLIC_API void SetProcessLargeAllocationFailureCallback( 26 LargeAllocationFailureCallback afc); 27 28 /** 29 * Unlike the error reporter, which is only called if the exception for an OOM 30 * bubbles up and is not caught, the OutOfMemoryCallback is called immediately 31 * at the OOM site to allow the embedding to capture the current state of heap 32 * allocation before anything is freed. If the large-allocation-failure callback 33 * is called at all (not all allocation sites call the large-allocation-failure 34 * callback on failure), it is called before the out-of-memory callback; the 35 * out-of-memory callback is only called if the allocation still fails after the 36 * large-allocation-failure callback has returned. 37 */ 38 39 using OutOfMemoryCallback = void (*)(JSContext*, void*); 40 41 extern JS_PUBLIC_API void SetOutOfMemoryCallback(JSContext* cx, 42 OutOfMemoryCallback cb, 43 void* data); 44 45 } // namespace JS 46 47 #endif // js_MemoryCallbacks_h 48