-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add memory mangament feature For more info see the following issue: nodejs/node-addon-api#260 PR-URL: nodejs/node-addon-api#286 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
- Loading branch information
Showing
10 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# MemoryManagement | ||
|
||
The `MemoryManagement` class contains functions that give the JavaScript engine | ||
an indication of the amount of externally allocated memory that is kept alive by | ||
JavaScript objects. | ||
|
||
## Methods | ||
|
||
### AdjustExternalMemory | ||
|
||
The function `AdjustExternalMemory` adjusts the amount of registered external | ||
memory used to give the JavaScript engine an indication of the amount of externally | ||
allocated memory that is kept alive by JavaScript objects. | ||
The JavaScript engine uses this to decide when to perform global garbage collections. | ||
Registering externally allocated memory will trigger global garbage collections | ||
more often than it would otherwise in an attempt to garbage collect the JavaScript | ||
objects that keep the externally allocated memory alive. | ||
|
||
```cpp | ||
static int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in_bytes); | ||
``` | ||
- `[in] env`: The environment in which the API is invoked under. | ||
- `[in] change_in_bytes`: The change in externally allocated memory that is kept | ||
alive by JavaScript objects expressed in bytes. | ||
Returns the adjusted memory value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
Value externalAllocatedMemory(const CallbackInfo& info) { | ||
int64_t kSize = 1024 * 1024; | ||
int64_t baseline = MemoryManagement::AdjustExternalMemory(info.Env(), 0); | ||
int64_t tmp = MemoryManagement::AdjustExternalMemory(info.Env(), kSize); | ||
tmp = MemoryManagement::AdjustExternalMemory(info.Env(), -kSize); | ||
return Boolean::New(info.Env(), tmp == baseline); | ||
} | ||
|
||
Object InitMemoryManagement(Env env) { | ||
Object exports = Object::New(env); | ||
exports["externalAllocatedMemory"] = Function::New(env, externalAllocatedMemory); | ||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
assert.strictEqual(binding.memory_management.externalAllocatedMemory(), true) | ||
} |