fs: use AliasedBuffer for fs_stats_field_array
PR-URL: https://github.com/nodejs/node/pull/18276 Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
13bc53fb26
commit
d09d87821d
@ -341,7 +341,7 @@ Stats.prototype.isSocket = function() {
|
|||||||
return this._checkModeProperty(S_IFSOCK);
|
return this._checkModeProperty(S_IFSOCK);
|
||||||
};
|
};
|
||||||
|
|
||||||
const statValues = binding.getStatValues();
|
const statValues = binding.statValues;
|
||||||
|
|
||||||
function statsFromValues() {
|
function statsFromValues() {
|
||||||
return new Stats(statValues[0], statValues[1], statValues[2], statValues[3],
|
return new Stats(statValues[0], statValues[1], statValues[2], statValues[3],
|
||||||
|
@ -322,7 +322,7 @@ inline Environment::Environment(IsolateData* isolate_data,
|
|||||||
#endif
|
#endif
|
||||||
handle_cleanup_waiting_(0),
|
handle_cleanup_waiting_(0),
|
||||||
http_parser_buffer_(nullptr),
|
http_parser_buffer_(nullptr),
|
||||||
fs_stats_field_array_(nullptr),
|
fs_stats_field_array_(isolate_, kFsStatsFieldsLength),
|
||||||
context_(context->GetIsolate(), context) {
|
context_(context->GetIsolate(), context) {
|
||||||
// We'll be creating new objects so make sure we've entered the context.
|
// We'll be creating new objects so make sure we've entered the context.
|
||||||
v8::HandleScope handle_scope(isolate());
|
v8::HandleScope handle_scope(isolate());
|
||||||
@ -547,13 +547,9 @@ inline void Environment::set_http2_state(
|
|||||||
http2_state_ = std::move(buffer);
|
http2_state_ = std::move(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline double* Environment::fs_stats_field_array() const {
|
inline AliasedBuffer<double, v8::Float64Array>*
|
||||||
return fs_stats_field_array_;
|
Environment::fs_stats_field_array() {
|
||||||
}
|
return &fs_stats_field_array_;
|
||||||
|
|
||||||
inline void Environment::set_fs_stats_field_array(double* fields) {
|
|
||||||
CHECK_EQ(fs_stats_field_array_, nullptr); // Should be set only once.
|
|
||||||
fs_stats_field_array_ = fields;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Environment::CreateImmediate(native_immediate_callback cb,
|
void Environment::CreateImmediate(native_immediate_callback cb,
|
||||||
|
@ -610,8 +610,7 @@ class Environment {
|
|||||||
inline http2::http2_state* http2_state() const;
|
inline http2::http2_state* http2_state() const;
|
||||||
inline void set_http2_state(std::unique_ptr<http2::http2_state> state);
|
inline void set_http2_state(std::unique_ptr<http2::http2_state> state);
|
||||||
|
|
||||||
inline double* fs_stats_field_array() const;
|
inline AliasedBuffer<double, v8::Float64Array>* fs_stats_field_array();
|
||||||
inline void set_fs_stats_field_array(double* fields);
|
|
||||||
|
|
||||||
inline performance::performance_state* performance_state();
|
inline performance::performance_state* performance_state();
|
||||||
inline std::map<std::string, uint64_t>* performance_marks();
|
inline std::map<std::string, uint64_t>* performance_marks();
|
||||||
@ -778,7 +777,10 @@ class Environment {
|
|||||||
char* http_parser_buffer_;
|
char* http_parser_buffer_;
|
||||||
std::unique_ptr<http2::http2_state> http2_state_;
|
std::unique_ptr<http2::http2_state> http2_state_;
|
||||||
|
|
||||||
double* fs_stats_field_array_;
|
// stat fields contains twice the number of entries because `fs.StatWatcher`
|
||||||
|
// needs room to store data for *two* `fs.Stats` instances.
|
||||||
|
static const int kFsStatsFieldsLength = 2 * 14;
|
||||||
|
AliasedBuffer<double, v8::Float64Array> fs_stats_field_array_;
|
||||||
|
|
||||||
struct BeforeExitCallback {
|
struct BeforeExitCallback {
|
||||||
void (*cb_)(void* arg);
|
void (*cb_)(void* arg);
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
#include "aliased_buffer.h"
|
||||||
#include "node_buffer.h"
|
#include "node_buffer.h"
|
||||||
#include "node_internals.h"
|
#include "node_internals.h"
|
||||||
#include "node_stat_watcher.h"
|
#include "node_stat_watcher.h"
|
||||||
@ -44,32 +45,34 @@
|
|||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
|
|
||||||
void FillStatsArray(double* fields, const uv_stat_t* s) {
|
void FillStatsArray(AliasedBuffer<double, v8::Float64Array>* fields_ptr,
|
||||||
fields[0] = s->st_dev;
|
const uv_stat_t* s, int offset) {
|
||||||
fields[1] = s->st_mode;
|
AliasedBuffer<double, v8::Float64Array>& fields = *fields_ptr;
|
||||||
fields[2] = s->st_nlink;
|
fields[offset + 0] = s->st_dev;
|
||||||
fields[3] = s->st_uid;
|
fields[offset + 1] = s->st_mode;
|
||||||
fields[4] = s->st_gid;
|
fields[offset + 2] = s->st_nlink;
|
||||||
fields[5] = s->st_rdev;
|
fields[offset + 3] = s->st_uid;
|
||||||
|
fields[offset + 4] = s->st_gid;
|
||||||
|
fields[offset + 5] = s->st_rdev;
|
||||||
#if defined(__POSIX__)
|
#if defined(__POSIX__)
|
||||||
fields[6] = s->st_blksize;
|
fields[offset + 6] = s->st_blksize;
|
||||||
#else
|
#else
|
||||||
fields[6] = -1;
|
fields[offset + 6] = -1;
|
||||||
#endif
|
#endif
|
||||||
fields[7] = s->st_ino;
|
fields[offset + 7] = s->st_ino;
|
||||||
fields[8] = s->st_size;
|
fields[offset + 8] = s->st_size;
|
||||||
#if defined(__POSIX__)
|
#if defined(__POSIX__)
|
||||||
fields[9] = s->st_blocks;
|
fields[offset + 9] = s->st_blocks;
|
||||||
#else
|
#else
|
||||||
fields[9] = -1;
|
fields[offset + 9] = -1;
|
||||||
#endif
|
#endif
|
||||||
// Dates.
|
// Dates.
|
||||||
// NO-LINT because the fields are 'long' and we just want to cast to `unsigned`
|
// NO-LINT because the fields are 'long' and we just want to cast to `unsigned`
|
||||||
#define X(idx, name) \
|
#define X(idx, name) \
|
||||||
/* NOLINTNEXTLINE(runtime/int) */ \
|
/* NOLINTNEXTLINE(runtime/int) */ \
|
||||||
fields[idx] = ((unsigned long)(s->st_##name.tv_sec) * 1e3) + \
|
fields[offset + idx] = ((unsigned long)(s->st_##name.tv_sec) * 1e3) + \
|
||||||
/* NOLINTNEXTLINE(runtime/int) */ \
|
/* NOLINTNEXTLINE(runtime/int) */ \
|
||||||
((unsigned long)(s->st_##name.tv_nsec) / 1e6); \
|
((unsigned long)(s->st_##name.tv_nsec) / 1e6); \
|
||||||
|
|
||||||
X(10, atim)
|
X(10, atim)
|
||||||
X(11, mtim)
|
X(11, mtim)
|
||||||
@ -81,7 +84,6 @@ void FillStatsArray(double* fields, const uv_stat_t* s) {
|
|||||||
namespace fs {
|
namespace fs {
|
||||||
|
|
||||||
using v8::Array;
|
using v8::Array;
|
||||||
using v8::ArrayBuffer;
|
|
||||||
using v8::Context;
|
using v8::Context;
|
||||||
using v8::Float64Array;
|
using v8::Float64Array;
|
||||||
using v8::Function;
|
using v8::Function;
|
||||||
@ -1295,22 +1297,6 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetStatValues(const FunctionCallbackInfo<Value>& args) {
|
|
||||||
Environment* env = Environment::GetCurrent(args);
|
|
||||||
double* fields = env->fs_stats_field_array();
|
|
||||||
if (fields == nullptr) {
|
|
||||||
// stat fields contains twice the number of entries because `fs.StatWatcher`
|
|
||||||
// needs room to store data for *two* `fs.Stats` instances.
|
|
||||||
fields = new double[2 * 14];
|
|
||||||
env->set_fs_stats_field_array(fields);
|
|
||||||
}
|
|
||||||
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(),
|
|
||||||
fields,
|
|
||||||
sizeof(double) * 2 * 14);
|
|
||||||
Local<Float64Array> fields_array = Float64Array::New(ab, 0, 2 * 14);
|
|
||||||
args.GetReturnValue().Set(fields_array);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitFs(Local<Object> target,
|
void InitFs(Local<Object> target,
|
||||||
Local<Value> unused,
|
Local<Value> unused,
|
||||||
Local<Context> context,
|
Local<Context> context,
|
||||||
@ -1356,7 +1342,9 @@ void InitFs(Local<Object> target,
|
|||||||
|
|
||||||
env->SetMethod(target, "mkdtemp", Mkdtemp);
|
env->SetMethod(target, "mkdtemp", Mkdtemp);
|
||||||
|
|
||||||
env->SetMethod(target, "getStatValues", GetStatValues);
|
target->Set(context,
|
||||||
|
FIXED_ONE_BYTE_STRING(env->isolate(), "statValues"),
|
||||||
|
env->fs_stats_field_array()->GetJSArray()).FromJust();
|
||||||
|
|
||||||
StatWatcher::Initialize(env, target);
|
StatWatcher::Initialize(env, target);
|
||||||
|
|
||||||
|
@ -292,7 +292,8 @@ v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
|
|||||||
const char* warning,
|
const char* warning,
|
||||||
const char* deprecation_code);
|
const char* deprecation_code);
|
||||||
|
|
||||||
void FillStatsArray(double* fields, const uv_stat_t* s);
|
void FillStatsArray(AliasedBuffer<double, v8::Float64Array>* fields_ptr,
|
||||||
|
const uv_stat_t* s, int offset = 0);
|
||||||
|
|
||||||
void SetupProcessObject(Environment* env,
|
void SetupProcessObject(Environment* env,
|
||||||
int argc,
|
int argc,
|
||||||
|
@ -89,7 +89,7 @@ void StatWatcher::Callback(uv_fs_poll_t* handle,
|
|||||||
Context::Scope context_scope(env->context());
|
Context::Scope context_scope(env->context());
|
||||||
|
|
||||||
FillStatsArray(env->fs_stats_field_array(), curr);
|
FillStatsArray(env->fs_stats_field_array(), curr);
|
||||||
FillStatsArray(env->fs_stats_field_array() + 14, prev);
|
FillStatsArray(env->fs_stats_field_array(), prev, 14);
|
||||||
Local<Value> arg = Integer::New(env->isolate(), status);
|
Local<Value> arg = Integer::New(env->isolate(), status);
|
||||||
wrap->MakeCallback(env->onchange_string(), 1, &arg);
|
wrap->MakeCallback(env->onchange_string(), 1, &arg);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user