Skip to content

Commit ac5b904

Browse files
Chris YoungMylesBorins
Chris Young
authored andcommittedApr 16, 2018
n-api: adds function to adjust external memory
Added a wrapper around v8::Isolate::AdjustAmountOfExternalAllocatedMemory Backport-PR-URL: #19447 PR-URL: #14310 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Fixes: #13928
1 parent 278a2d0 commit ac5b904

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed
 

‎doc/api/n-api.md

+25
Original file line numberDiff line numberDiff line change
@@ -3394,6 +3394,31 @@ support it:
33943394
* If the function is not available, provide an alternate implementation
33953395
that does not use the function.
33963396

3397+
## Memory Management
3398+
3399+
### napi_adjust_external_memory
3400+
<!-- YAML
3401+
added: REPLACEME
3402+
-->
3403+
```C
3404+
NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
3405+
int64_t change_in_bytes,
3406+
int64_t* result);
3407+
```
3408+
3409+
- `[in] env`: The environment that the API is invoked under.
3410+
- `[in] change_in_bytes`: The change in externally allocated memory that is
3411+
kept alive by JavaScript objects.
3412+
- `[out] result`: The adjusted value
3413+
3414+
Returns `napi_ok` if the API succeeded.
3415+
3416+
This function gives V8 an indication of the amount of externally allocated
3417+
memory that is kept alive by JavaScript objects (i.e. a JavaScript object
3418+
that points to its own memory allocated by a native module). Registering
3419+
externally allocated memory will trigger global garbage collections more
3420+
often than it would otherwise.
3421+
33973422
<!-- it's very convenient to have all the anchors indexed -->
33983423
<!--lint disable no-unused-definitions remark-lint-->
33993424
## Promises

‎src/node_api.cc

+13
Original file line numberDiff line numberDiff line change
@@ -3216,6 +3216,19 @@ napi_status napi_get_node_version(napi_env env,
32163216
return napi_clear_last_error(env);
32173217
}
32183218

3219+
napi_status napi_adjust_external_memory(napi_env env,
3220+
int64_t change_in_bytes,
3221+
int64_t* adjusted_value) {
3222+
CHECK_ENV(env);
3223+
CHECK_ARG(env, &change_in_bytes);
3224+
CHECK_ARG(env, adjusted_value);
3225+
3226+
*adjusted_value = env->isolate->AdjustAmountOfExternalAllocatedMemory(
3227+
change_in_bytes);
3228+
3229+
return napi_clear_last_error(env);
3230+
}
3231+
32193232
namespace uvimpl {
32203233

32213234
static napi_status ConvertUVErrorCode(int code) {

‎src/node_api.h

+5
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,11 @@ NAPI_EXTERN napi_status napi_is_promise(napi_env env,
557557
napi_value promise,
558558
bool* is_promise);
559559

560+
// Memory management
561+
NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
562+
int64_t change_in_bytes,
563+
int64_t* adjusted_value);
564+
560565
EXTERN_C_END
561566

562567
#endif // SRC_NODE_API_H_

‎test/addons-napi/test_general/test.js

+5
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ z = null;
9191
global.gc();
9292
assert.strictEqual(test_general.finalizeWasCalled(), false,
9393
'finalize callback was not called upon garbage collection');
94+
95+
// test napi_adjust_external_memory
96+
const adjustedValue = test_general.testAdjustExternalMemory();
97+
assert.strictEqual(typeof adjustedValue, 'number');
98+
assert(adjustedValue > 0);

‎test/addons-napi/test_general/test_general.c

+11
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ napi_value finalize_was_called(napi_env env, napi_callback_info info) {
205205
return it_was_called;
206206
}
207207

208+
napi_value testAdjustExternalMemory(napi_env env, napi_callback_info info) {
209+
napi_value result;
210+
int64_t adjustedValue;
211+
212+
NAPI_CALL(env, napi_adjust_external_memory(env, 1, &adjustedValue));
213+
NAPI_CALL(env, napi_create_double(env, adjustedValue, &result));
214+
215+
return result;
216+
}
217+
208218
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
209219
napi_property_descriptor descriptors[] = {
210220
DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals),
@@ -222,6 +232,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
222232
DECLARE_NAPI_PROPERTY("testFinalizeWrap", test_finalize_wrap),
223233
DECLARE_NAPI_PROPERTY("finalizeWasCalled", finalize_was_called),
224234
DECLARE_NAPI_PROPERTY("derefItemWasCalled", deref_item_was_called),
235+
DECLARE_NAPI_PROPERTY("testAdjustExternalMemory", testAdjustExternalMemory)
225236
};
226237

227238
NAPI_CALL_RETURN_VOID(env, napi_define_properties(

0 commit comments

Comments
 (0)
Please sign in to comment.