Skip to content

Commit

Permalink
lib, src: add maxmem prop in os module
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJithil committed Dec 9, 2023
1 parent 1ba508d commit b92f5f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/api/os.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,16 @@ added: v0.3.3

Returns the total amount of system memory in bytes as an integer.

## `os.maxmem()`

<!-- YAML
added: v0.3.3
-->

* Returns: {integer}

Returns the total 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, 0 is returned.

## `os.type()`

<!-- YAML
Expand Down
3 changes: 3 additions & 0 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {
getPriority: _getPriority,
getOSInformation: _getOSInformation,
getTotalMem,
getConstrainedMem,
getUserInfo,
getUptime: _getUptime,
isBigEndian,
Expand Down Expand Up @@ -109,6 +110,7 @@ getOSRelease[SymbolToPrimitive] = () => getOSRelease();
getMachine[SymbolToPrimitive] = () => getMachine();
getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory();
getTotalMem[SymbolToPrimitive] = () => getTotalMem();
getConstrainedMem[SymbolToPrimitive] = () => getConstrainedMem();
getUptime[SymbolToPrimitive] = () => getUptime();

const kEndianness = isBigEndian ? 'BE' : 'LE';
Expand Down Expand Up @@ -388,6 +390,7 @@ module.exports = {
setPriority,
tmpdir,
totalmem: getTotalMem,
maxmem: getConstrainedMem,
type: getOSType,
userInfo,
uptime: getUptime,
Expand Down
6 changes: 6 additions & 0 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(amount);
}

static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
double amount = static_cast<double>(uv_get_constrained_memory());
args.GetReturnValue().Set(amount);
}

static void GetUptime(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -394,6 +398,7 @@ void Initialize(Local<Object> target,
SetMethod(context, target, "getLoadAvg", GetLoadAvg);
SetMethod(context, target, "getUptime", GetUptime);
SetMethod(context, target, "getTotalMem", GetTotalMemory);
SetMethod(context, target, "getConstrainedMem", GetConstrainedMemory);
SetMethod(context, target, "getFreeMem", GetFreeMemory);
SetMethod(context, target, "getCPUs", GetCPUInfo);
SetMethod(context, target, "getInterfaceAddresses", GetInterfaceAddresses);
Expand All @@ -416,6 +421,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetLoadAvg);
registry->Register(GetUptime);
registry->Register(GetTotalMemory);
registry->Register(GetConstrainedMemory);
registry->Register(GetFreeMemory);
registry->Register(GetCPUInfo);
registry->Register(GetInterfaceAddresses);
Expand Down
1 change: 1 addition & 0 deletions typings/internalBinding/os.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface OSBinding {
getLoadAvg(array: Float64Array): void;
getUptime(): number;
getTotalMem(): number;
getConstrainedMem(): number;
getFreeMem(): number;
getCPUs(): Array<string | number>;
getInterfaceAddresses(ctx: InternalOSBinding.OSContext): Array<string | number | boolean> | undefined;
Expand Down

0 comments on commit b92f5f5

Please sign in to comment.