Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / public / MemoryFunctions.h
blobf0417f5eb34fc0613d43f66409a259aaa2aad02d
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/. */
6 /* Low-level memory-allocation functions. */
8 #ifndef js_MemoryFunctions_h
9 #define js_MemoryFunctions_h
11 #include "mozilla/Assertions.h" // MOZ_ASSERT
13 #include <stddef.h> // size_t
15 #include "jstypes.h" // JS_PUBLIC_API
17 struct JS_PUBLIC_API JSContext;
18 class JS_PUBLIC_API JSObject;
19 struct JS_PUBLIC_API JSRuntime;
21 extern JS_PUBLIC_API void* JS_malloc(JSContext* cx, size_t nbytes);
23 extern JS_PUBLIC_API void* JS_realloc(JSContext* cx, void* p, size_t oldBytes,
24 size_t newBytes);
26 /**
27 * A wrapper for |js_free(p)| that may delay |js_free(p)| invocation as a
28 * performance optimization. |cx| may be nullptr.
30 extern JS_PUBLIC_API void JS_free(JSContext* cx, void* p);
32 /**
33 * Same as above, but for buffers that will be used with the BYOB
34 * (Bring Your Own Buffer) JSString creation functions, such as
35 * JS_NewLatin1String and JS_NewUCString
37 extern JS_PUBLIC_API void* JS_string_malloc(JSContext* cx, size_t nbytes);
39 extern JS_PUBLIC_API void* JS_string_realloc(JSContext* cx, void* p,
40 size_t oldBytes, size_t newBytes);
42 extern JS_PUBLIC_API void JS_string_free(JSContext* cx, void* p);
44 namespace JS {
46 /**
47 * The different possible memory uses to pass to Add/RemoveAssociatedMemory.
49 #define JS_FOR_EACH_PUBLIC_MEMORY_USE(_) \
50 _(XPCWrappedNative) \
51 _(DOMBinding) \
52 _(CTypeFFIType) \
53 _(CTypeFFITypeElements) \
54 _(CTypeFunctionInfo) \
55 _(CTypeFieldInfo) \
56 _(CDataBufferPtr) \
57 _(CDataBuffer) \
58 _(CClosureInfo) \
59 _(CTypesInt64) \
60 _(Embedding1) \
61 _(Embedding2) \
62 _(Embedding3) \
63 _(Embedding4) \
64 _(Embedding5)
66 enum class MemoryUse : uint8_t {
67 #define DEFINE_MEMORY_USE(Name) Name,
68 JS_FOR_EACH_PUBLIC_MEMORY_USE(DEFINE_MEMORY_USE)
69 #undef DEFINE_MEMORY_USE
72 /**
73 * Advise the GC of external memory owned by a JSObject. This is used to
74 * determine when to collect zones. Calls must be matched by calls to
75 * RemoveAssociatedMemory() when the memory is deallocated or no longer owned by
76 * the object.
78 extern JS_PUBLIC_API void AddAssociatedMemory(JSObject* obj, size_t nbytes,
79 MemoryUse use);
81 /**
82 * Advise the GC that external memory reported by JS::AddAssociatedMemory() is
83 * no longer owned by a JSObject. Calls must match those to
84 * AddAssociatedMemory().
86 extern JS_PUBLIC_API void RemoveAssociatedMemory(JSObject* obj, size_t nbytes,
87 MemoryUse use);
89 } // namespace JS
91 #endif /* js_MemoryFunctions_h */