From 2770cd941e498e5221d9155a08c620e9f950ce3c Mon Sep 17 00:00:00 2001 From: Yash Ladha Date: Wed, 9 Sep 2020 21:08:20 +0530 Subject: [PATCH] src: remove duplicate logic for getting buffer We were fetching the buffer from the float array to send out the response in js land, however that logic is being duplicated in node_process.h. Now they will be using an inline to fetch the array buffers and making it more generic. PR-URL: https://github.com/nodejs/node/pull/34553 Reviewed-By: Anna Henningsen Reviewed-By: Joyee Cheung --- src/node_process_methods.cc | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 8c8a209b3ec151..4614fdd0711968 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -87,6 +87,16 @@ static void Chdir(const FunctionCallbackInfo& args) { } } +inline Local get_fields_array_buffer( + const FunctionCallbackInfo& args, + size_t index, + size_t array_length) { + CHECK(args[index]->IsFloat64Array()); + Local arr = args[index].As(); + CHECK_EQ(arr->Length(), array_length); + return arr->Buffer(); +} + // CPUUsage use libuv's uv_getrusage() this-process resource usage accessor, // to access ru_utime (user CPU time used) and ru_stime (system CPU time used), // which are uv_timeval_t structs (long tv_sec, long tv_usec). @@ -104,10 +114,7 @@ static void CPUUsage(const FunctionCallbackInfo& args) { } // Get the double array pointer from the Float64Array argument. - CHECK(args[0]->IsFloat64Array()); - Local array = args[0].As(); - CHECK_EQ(array->Length(), 2); - Local ab = array->Buffer(); + Local ab = get_fields_array_buffer(args, 0, 2); double* fields = static_cast(ab->GetBackingStore()->Data()); // Set the Float64Array elements to be user / system values in microseconds. @@ -203,10 +210,7 @@ static void MemoryUsage(const FunctionCallbackInfo& args) { env->isolate_data()->node_allocator(); // Get the double array pointer from the Float64Array argument. - CHECK(args[0]->IsFloat64Array()); - Local array = args[0].As(); - CHECK_EQ(array->Length(), 5); - Local ab = array->Buffer(); + Local ab = get_fields_array_buffer(args, 0, 5); double* fields = static_cast(ab->GetBackingStore()->Data()); fields[0] = rss; @@ -292,10 +296,7 @@ static void ResourceUsage(const FunctionCallbackInfo& args) { if (err) return env->ThrowUVException(err, "uv_getrusage"); - CHECK(args[0]->IsFloat64Array()); - Local array = args[0].As(); - CHECK_EQ(array->Length(), 16); - Local ab = array->Buffer(); + Local ab = get_fields_array_buffer(args, 0, 16); double* fields = static_cast(ab->GetBackingStore()->Data()); fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;