File tree Expand file tree Collapse file tree 10 files changed +79
-1
lines changed Expand file tree Collapse file tree 10 files changed +79
-1
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,7 @@ still a work in progress as its not yet complete).
90
90
- [ ArrayBuffer] ( doc/array_buffer.md )
91
91
- [ TypedArray] ( doc/typed_array.md )
92
92
- [ TypedArrayOf] ( doc/typed_array_of.md )
93
+ - [ Memory Management] ( doc/memory_management.md )
93
94
- [ Async Operations] ( doc/async_operations.md )
94
95
- [ AsyncWorker] ( doc/async_worker.md )
95
96
- [ Promises] ( doc/promises.md )
Original file line number Diff line number Diff line change
1
+ # MemoryManagement
2
+
3
+ The ` MemoryManagement ` class contains functions that give the JavaScript engine
4
+ an indication of the amount of externally allocated memory that is kept alive by
5
+ JavaScript objects.
6
+
7
+ ## Methods
8
+
9
+ ### AdjustExternalMemory
10
+
11
+ The function ` AdjustExternalMemory ` adjusts the amount of registered external
12
+ memory used to give the JavaScript engine an indication of the amount of externally
13
+ allocated memory that is kept alive by JavaScript objects.
14
+ The JavaScript engine uses this to decide when to perform global garbage collections.
15
+ Registering externally allocated memory will trigger global garbage collections
16
+ more often than it would otherwise in an attempt to garbage collect the JavaScript
17
+ objects that keep the externally allocated memory alive.
18
+
19
+ ``` cpp
20
+ static int64_t MemoryManagement::AdjustExternalMemory (Env env, int64_t change_in_bytes);
21
+ ```
22
+
23
+ - `[in] env`: The environment in which the API is invoked under.
24
+ - `[in] change_in_bytes`: The change in externally allocated memory that is kept
25
+ alive by JavaScript objects expressed in bytes.
26
+
27
+ Returns the adjusted memory value.
Original file line number Diff line number Diff line change @@ -3224,6 +3224,17 @@ inline void AsyncWorker::OnWorkComplete(
3224
3224
delete self;
3225
3225
}
3226
3226
3227
+ // //////////////////////////////////////////////////////////////////////////////
3228
+ // Memory Management class
3229
+ // //////////////////////////////////////////////////////////////////////////////
3230
+
3231
+ inline int64_t MemoryManagement::AdjustExternalMemory (Env env, int64_t change_in_bytes) {
3232
+ int64_t result;
3233
+ napi_status status = napi_adjust_external_memory (env, change_in_bytes, &result);
3234
+ NAPI_THROW_IF_FAILED (env, status, 0 );
3235
+ return result;
3236
+ }
3237
+
3227
3238
// These macros shouldn't be useful in user code.
3228
3239
#undef NAPI_THROW
3229
3240
#undef NAPI_THROW_IF_FAILED
Original file line number Diff line number Diff line change @@ -76,6 +76,8 @@ namespace Napi {
76
76
// / Defines the signature of a N-API C++ module's registration callback (init) function.
77
77
typedef Object (*ModuleRegisterCallback)(Env env, Object exports);
78
78
79
+ class MemoryManagement ;
80
+
79
81
// / Environment for N-API values and operations.
80
82
// /
81
83
// / All N-API values and operations must be associated with an environment. An environment
@@ -1549,6 +1551,12 @@ namespace Napi {
1549
1551
std::string _error;
1550
1552
};
1551
1553
1554
+ // Memory management.
1555
+ class MemoryManagement {
1556
+ public:
1557
+ static int64_t AdjustExternalMemory (Env env, int64_t change_in_bytes);
1558
+ };
1559
+
1552
1560
} // namespace Napi
1553
1561
1554
1562
// Inline implementations of all the above class methods are included here.
Original file line number Diff line number Diff line change 8
8
" Anna Henningsen (https://github.com/addaleax)" ,
9
9
" Arunesh Chandra (https://github.com/aruneshchandra)" ,
10
10
" Benjamin Byholm (https://github.com/kkoopa)" ,
11
- " Cory Mickelson (https://github.com/corymickelson)" ,
11
+ " Cory Mickelson (https://github.com/corymickelson)" ,
12
12
" David Halls (https://github.com/davedoesdev)" ,
13
13
" Eric Bickle (https://github.com/ebickle)" ,
14
14
" Gabriel Schulhof (https://github.com/gabrielschulhof)" ,
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ Object InitError(Env env);
13
13
Object InitExternal (Env env);
14
14
Object InitFunction (Env env);
15
15
Object InitHandleScope (Env env);
16
+ Object InitMemoryManagement (Env env);
16
17
Object InitName (Env env);
17
18
Object InitObject (Env env);
18
19
Object InitPromise (Env env);
@@ -33,6 +34,7 @@ Object Init(Env env, Object exports) {
33
34
exports.Set (" function" , InitFunction (env));
34
35
exports.Set (" name" , InitName (env));
35
36
exports.Set (" handlescope" , InitHandleScope (env));
37
+ exports.Set (" memory_management" , InitMemoryManagement (env));
36
38
exports.Set (" object" , InitObject (env));
37
39
exports.Set (" promise" , InitPromise (env));
38
40
exports.Set (" typedarray" , InitTypedArray (env));
Original file line number Diff line number Diff line change 13
13
'external.cc' ,
14
14
'function.cc' ,
15
15
'handlescope.cc' ,
16
+ 'memory_management.cc' ,
16
17
'name.cc' ,
17
18
'object/delete_property.cc' ,
18
19
'object/get_property.cc' ,
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ let testModules = [
19
19
'external' ,
20
20
'function' ,
21
21
'handlescope' ,
22
+ 'memory_management' ,
22
23
'name' ,
23
24
'object/delete_property' ,
24
25
'object/get_property' ,
Original file line number Diff line number Diff line change
1
+ #include " napi.h"
2
+
3
+ using namespace Napi ;
4
+
5
+ Value externalAllocatedMemory (const CallbackInfo& info) {
6
+ int64_t kSize = 1024 * 1024 ;
7
+ int64_t baseline = MemoryManagement::AdjustExternalMemory (info.Env (), 0 );
8
+ int64_t tmp = MemoryManagement::AdjustExternalMemory (info.Env (), kSize );
9
+ tmp = MemoryManagement::AdjustExternalMemory (info.Env (), -kSize );
10
+ return Boolean::New (info.Env (), tmp == baseline);
11
+ }
12
+
13
+ Object InitMemoryManagement (Env env) {
14
+ Object exports = Object::New (env);
15
+ exports[" externalAllocatedMemory" ] = Function::New (env, externalAllocatedMemory);
16
+ return exports;
17
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const buildType = process . config . target_defaults . default_configuration ;
3
+ const assert = require ( 'assert' ) ;
4
+
5
+ test ( require ( `./build/${ buildType } /binding.node` ) ) ;
6
+ test ( require ( `./build/${ buildType } /binding_noexcept.node` ) ) ;
7
+
8
+ function test ( binding ) {
9
+ assert . strictEqual ( binding . memory_management . externalAllocatedMemory ( ) , true )
10
+ }
You can’t perform that action at this time.
0 commit comments