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
,
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
);
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
);
47 * The different possible memory uses to pass to Add/RemoveAssociatedMemory.
49 #define JS_FOR_EACH_PUBLIC_MEMORY_USE(_) \
53 _(CTypeFFITypeElements) \
54 _(CTypeFunctionInfo) \
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
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
78 extern JS_PUBLIC_API
void AddAssociatedMemory(JSObject
* obj
, size_t nbytes
,
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
,
91 #endif /* js_MemoryFunctions_h */