Skip to content

Commit

Permalink
darwin: implement node:os.freemem() (#12870)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Aug 7, 2024
1 parent ff0f9d5 commit d449697
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/bun.js/bindings/OsBinding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "root.h"

#if OS(DARWIN)
#include <mach/vm_types.h>
#include <mach/mach_host.h>
#include <mach/mach_init.h>
#include <mach/message.h>
#include <mach/vm_statistics.h>
#include <unistd.h>

// Adapted from libuv darwin uv_get_free_memory, MIT
extern "C" uint64_t Bun__Os__getFreeMemory(void)
{
vm_statistics_data_t info;
mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);

if (host_statistics(mach_host_self(), HOST_VM_INFO,
(host_info_t)&info, &count)
!= KERN_SUCCESS) {
return 0;
}

return (uint64_t)info.free_count * sysconf(_SC_PAGESIZE);
}
#endif
3 changes: 1 addition & 2 deletions src/darwin_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ pub const kFSEventStreamEventFlagUnmount: c_int = 128;
pub const kFSEventStreamEventFlagUserDropped: c_int = 2;

pub fn getFreeMemory() u64 {
// NOT IMPLEMENTED YET
return 1024 * 1024;
return @extern(*const fn () callconv(.C) u64, .{ .name = "Bun__Os__getFreeMemory" })();
}

pub fn getTotalMemory() u64 {
Expand Down
4 changes: 2 additions & 2 deletions test/js/node/os/os.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ it("endianness", () => {
});

it("freemem", () => {
expect(os.freemem() > 0).toBe(true);
expect(os.freemem()).toBeGreaterThan(1024 * 1024);
});

it("totalmem", () => {
expect(os.totalmem() > 0).toBe(true);
expect(os.totalmem()).toBeGreaterThan(1024 * 1024);
});

it("getPriority", () => {
Expand Down

0 comments on commit d449697

Please sign in to comment.