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 JSFreeOp
;
19 class JS_PUBLIC_API JSObject
;
20 struct JS_PUBLIC_API JSRuntime
;
22 extern JS_PUBLIC_API
void* JS_malloc(JSContext
* cx
, size_t nbytes
);
24 extern JS_PUBLIC_API
void* JS_realloc(JSContext
* cx
, void* p
, size_t oldBytes
,
28 * A wrapper for |js_free(p)| that may delay |js_free(p)| invocation as a
29 * performance optimization. |cx| may be nullptr.
31 extern JS_PUBLIC_API
void JS_free(JSContext
* cx
, void* p
);
34 * Same as above, but for buffers that will be used with the BYOB
35 * (Bring Your Own Buffer) JSString creation functions, such as
36 * JS_NewLatin1String and JS_NewUCString
38 extern JS_PUBLIC_API
void* JS_string_malloc(JSContext
* cx
, size_t nbytes
);
40 extern JS_PUBLIC_API
void* JS_string_realloc(JSContext
* cx
, void* p
,
41 size_t oldBytes
, size_t newBytes
);
43 extern JS_PUBLIC_API
void JS_string_free(JSContext
* cx
, void* p
);
46 * A wrapper for |js_free(p)| that may delay |js_free(p)| invocation as a
47 * performance optimization as specified by the given JSFreeOp instance.
49 extern JS_PUBLIC_API
void JS_freeop(JSFreeOp
* fop
, void* p
);
54 * The different possible memory uses to pass to Add/RemoveAssociatedMemory.
56 #define JS_FOR_EACH_PUBLIC_MEMORY_USE(_) \
60 _(CTypeFFITypeElements) \
61 _(CTypeFunctionInfo) \
73 enum class MemoryUse
: uint8_t {
74 #define DEFINE_MEMORY_USE(Name) Name,
75 JS_FOR_EACH_PUBLIC_MEMORY_USE(DEFINE_MEMORY_USE
)
76 #undef DEFINE_MEMORY_USE
80 * Advise the GC of external memory owned by a JSObject. This is used to
81 * determine when to collect zones. Calls must be matched by calls to
82 * RemoveAssociatedMemory() when the memory is deallocated or no longer owned by
85 extern JS_PUBLIC_API
void AddAssociatedMemory(JSObject
* obj
, size_t nbytes
,
89 * Advise the GC that external memory reported by JS::AddAssociatedMemory() is
90 * no longer owned by a JSObject. Calls must match those to
91 * AddAssociatedMemory().
93 extern JS_PUBLIC_API
void RemoveAssociatedMemory(JSObject
* obj
, size_t nbytes
,
98 #endif /* js_MemoryFunctions_h */