Skip to content

Commit

Permalink
src: return a number from process.constrainedMemory() constantly
Browse files Browse the repository at this point in the history
`0` is already a special value returned from
`uv_get_constrained_memory` representing unknown or no constraint.
Make `process.constrainedMemory()` constantly return a number instead
to avoid polymorphic return type.

PR-URL: nodejs#52039
Reviewed-By: theanarkh <theratliter@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
  • Loading branch information
legendecas authored and rdw-msft committed Mar 26, 2024
1 parent 0ed2abb commit 908f64a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
8 changes: 6 additions & 2 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1114,15 +1114,19 @@ over the IPC channel using `process.send()`.
added:
- v19.6.0
- v18.15.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/52039
description: Aligned return value with `uv_get_constrained_memory`.
-->

> Stability: 1 - Experimental
* {number|undefined}
* {number}

Gets the amount of memory available to the process (in bytes) based on
limits imposed by the OS. If there is no such constraint, or the constraint
is unknown, `undefined` is returned.
is unknown, `0` is returned.

See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more
information.
Expand Down
4 changes: 1 addition & 3 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {

static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
uint64_t value = uv_get_constrained_memory();
if (value != 0) {
args.GetReturnValue().Set(static_cast<double>(value));
}
args.GetReturnValue().Set(static_cast<double>(value));
}

static void GetAvailableMemory(const FunctionCallbackInfo<Value>& args) {
Expand Down
10 changes: 2 additions & 8 deletions test/parallel/test-process-constrained-memory.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
'use strict';
require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');

const constrainedMemory = process.constrainedMemory();
if (constrainedMemory !== undefined) {
assert(constrainedMemory > 0);
}
if (!process.env.isWorker) {
process.env.isWorker = true;
new Worker(__filename);
}
assert.strictEqual(typeof constrainedMemory, 'number');

0 comments on commit 908f64a

Please sign in to comment.