From db3c177adc0212c6e03d3f7e2a7676d53bc02ffd Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 21 May 2015 11:01:17 -0700 Subject: [PATCH 1/7] src: add "sizeof" and "alignof" process bindings --- node.gyp | 2 ++ src/alignof.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ src/sizeof.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/alignof.cc create mode 100644 src/sizeof.cc diff --git a/node.gyp b/node.gyp index 70c9841a89a176..b43424ea7a8f69 100644 --- a/node.gyp +++ b/node.gyp @@ -120,6 +120,8 @@ 'src/node_i18n.cc', 'src/pipe_wrap.cc', 'src/signal_wrap.cc', + 'src/sizeof.cc', + 'src/alignof.cc', 'src/smalloc.cc', 'src/spawn_sync.cc', 'src/string_bytes.cc', diff --git a/src/alignof.cc b/src/alignof.cc new file mode 100644 index 00000000000000..6303daf20ed92c --- /dev/null +++ b/src/alignof.cc @@ -0,0 +1,67 @@ + +#include "env.h" +#include "env-inl.h" +#include "node.h" +#include "node_internals.h" +#include "v8-profiler.h" +#include "v8.h" + +namespace node { +namespace _alignof { + +using v8::Context; +using v8::Handle; +using v8::Object; +using v8::Persistent; +using v8::Uint32; +using v8::Value; + + +void Initialize(Handle exports, + Handle unused, + Handle context) { + Environment* env = Environment::GetCurrent(context); + +#define SET_ALIGNOF(name, type) \ + struct s_##name { type a; }; \ + exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name ), \ + Uint32::NewFromUnsigned(env->isolate(), static_cast(__alignof__(struct s_##name)))); + + // fixed alignments + SET_ALIGNOF(int8, int8_t); + SET_ALIGNOF(uint8, uint8_t); + SET_ALIGNOF(int16, int16_t); + SET_ALIGNOF(uint16, uint16_t); + SET_ALIGNOF(int32, int32_t); + SET_ALIGNOF(uint32, uint32_t); + SET_ALIGNOF(int64, int64_t); + SET_ALIGNOF(uint64, uint64_t); + SET_ALIGNOF(float, float); + SET_ALIGNOF(double, double); + + // (potentially) variable alignments + SET_ALIGNOF(bool, bool); + SET_ALIGNOF(char, char); + SET_ALIGNOF(uchar, unsigned char); + SET_ALIGNOF(short, short); + SET_ALIGNOF(ushort, unsigned short); + SET_ALIGNOF(int, int); + SET_ALIGNOF(uint, unsigned int); + SET_ALIGNOF(long, long); + SET_ALIGNOF(ulong, unsigned long); + SET_ALIGNOF(longlong, long long); + SET_ALIGNOF(ulonglong, unsigned long long); + SET_ALIGNOF(pointer, char *); + SET_ALIGNOF(size_t, size_t); + + // alignment of a Persistent handle to a JS object + SET_ALIGNOF(Object, Persistent); + +#undef SET_alignof +} + + +} // namespace _alignof +} // namespace node + +NODE_MODULE_CONTEXT_AWARE_BUILTIN(alignof, node::_alignof::Initialize) diff --git a/src/sizeof.cc b/src/sizeof.cc new file mode 100644 index 00000000000000..27dcb118da38eb --- /dev/null +++ b/src/sizeof.cc @@ -0,0 +1,68 @@ + +#include "env.h" +#include "env-inl.h" +#include "node.h" +#include "node_internals.h" +#include "v8-profiler.h" +#include "v8.h" + +namespace node { +namespace _sizeof { + +using v8::Context; +using v8::Handle; +using v8::Isolate; +using v8::Object; +using v8::Persistent; +using v8::Uint32; +using v8::Value; + + +void Initialize(Handle exports, + Handle unused, + Handle context) { + Environment* env = Environment::GetCurrent(context); + + // fixed sizes +#define SET_SIZEOF(name, type) \ + exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name ), \ + Uint32::NewFromUnsigned(env->isolate(), static_cast(sizeof( type )))); + + SET_SIZEOF(int8, int8_t); + SET_SIZEOF(uint8, uint8_t); + SET_SIZEOF(int16, int16_t); + SET_SIZEOF(uint16, uint16_t); + SET_SIZEOF(int32, int32_t); + SET_SIZEOF(uint32, uint32_t); + SET_SIZEOF(int64, int64_t); + SET_SIZEOF(uint64, uint64_t); + SET_SIZEOF(float, float); + SET_SIZEOF(double, double); + + // (potentially) variable sizes + SET_SIZEOF(bool, bool); + SET_SIZEOF(byte, unsigned char); + SET_SIZEOF(char, char); + SET_SIZEOF(uchar, unsigned char); + SET_SIZEOF(short, short); + SET_SIZEOF(ushort, unsigned short); + SET_SIZEOF(int, int); + SET_SIZEOF(uint, unsigned int); + SET_SIZEOF(long, long); + SET_SIZEOF(ulong, unsigned long); + SET_SIZEOF(longlong, long long); + SET_SIZEOF(ulonglong, unsigned long long); + SET_SIZEOF(pointer, char *); + SET_SIZEOF(size_t, size_t); + + // size of a Persistent handle to a JS object + SET_SIZEOF(Object, Persistent); + +#undef SET_SIZEOF +} + + +} // namespace _sizeof +} // namespace node + +NODE_MODULE_CONTEXT_AWARE_BUILTIN(sizeof, node::_sizeof::Initialize) From 3660bd6249303e468af0dee34821406d008e0b3a Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 21 May 2015 11:03:24 -0700 Subject: [PATCH 2/7] os: export `sizeof` and `alignof` native bindings --- lib/os.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/os.js b/lib/os.js index 4426612285e299..14e2919d4c8016 100644 --- a/lib/os.js +++ b/lib/os.js @@ -54,3 +54,6 @@ if (binding.isBigEndian) exports.endianness = function() { return 'BE'; }; else exports.endianness = function() { return 'LE'; }; + +exports.sizeof = process.binding('sizeof'); +exports.alignof = process.binding('alignof'); From 24ac6f01a4a1901fdac87c0ebf4257a33d399174 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 21 May 2015 12:25:52 -0700 Subject: [PATCH 3/7] src: correct #undef typo --- src/alignof.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/alignof.cc b/src/alignof.cc index 6303daf20ed92c..f7942cc45ac7cd 100644 --- a/src/alignof.cc +++ b/src/alignof.cc @@ -57,7 +57,7 @@ void Initialize(Handle exports, // alignment of a Persistent handle to a JS object SET_ALIGNOF(Object, Persistent); -#undef SET_alignof +#undef SET_ALIGNOF } From 34320548ff72a4aadf94a98d678f8e42defc847a Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 21 May 2015 14:40:44 -0700 Subject: [PATCH 4/7] src: add "longdouble" to signof and alignof --- src/alignof.cc | 1 + src/sizeof.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/src/alignof.cc b/src/alignof.cc index f7942cc45ac7cd..3a54fefa2b04f4 100644 --- a/src/alignof.cc +++ b/src/alignof.cc @@ -38,6 +38,7 @@ void Initialize(Handle exports, SET_ALIGNOF(uint64, uint64_t); SET_ALIGNOF(float, float); SET_ALIGNOF(double, double); + SET_ALIGNOF(longdouble, long double); // (potentially) variable alignments SET_ALIGNOF(bool, bool); diff --git a/src/sizeof.cc b/src/sizeof.cc index 27dcb118da38eb..9bf37036f4410e 100644 --- a/src/sizeof.cc +++ b/src/sizeof.cc @@ -38,6 +38,7 @@ void Initialize(Handle exports, SET_SIZEOF(uint64, uint64_t); SET_SIZEOF(float, float); SET_SIZEOF(double, double); + SET_SIZEOF(longdouble, long double); // (potentially) variable sizes SET_SIZEOF(bool, bool); From f9208712b39cf94e3180d584f372157817ee76fe Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 22 May 2015 14:29:01 -0700 Subject: [PATCH 5/7] src: remove alignof.cc Will be merged with sizeof.cc --- node.gyp | 1 - src/alignof.cc | 68 -------------------------------------------------- 2 files changed, 69 deletions(-) delete mode 100644 src/alignof.cc diff --git a/node.gyp b/node.gyp index b43424ea7a8f69..11f669d8c3dcae 100644 --- a/node.gyp +++ b/node.gyp @@ -121,7 +121,6 @@ 'src/pipe_wrap.cc', 'src/signal_wrap.cc', 'src/sizeof.cc', - 'src/alignof.cc', 'src/smalloc.cc', 'src/spawn_sync.cc', 'src/string_bytes.cc', diff --git a/src/alignof.cc b/src/alignof.cc deleted file mode 100644 index 3a54fefa2b04f4..00000000000000 --- a/src/alignof.cc +++ /dev/null @@ -1,68 +0,0 @@ - -#include "env.h" -#include "env-inl.h" -#include "node.h" -#include "node_internals.h" -#include "v8-profiler.h" -#include "v8.h" - -namespace node { -namespace _alignof { - -using v8::Context; -using v8::Handle; -using v8::Object; -using v8::Persistent; -using v8::Uint32; -using v8::Value; - - -void Initialize(Handle exports, - Handle unused, - Handle context) { - Environment* env = Environment::GetCurrent(context); - -#define SET_ALIGNOF(name, type) \ - struct s_##name { type a; }; \ - exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name ), \ - Uint32::NewFromUnsigned(env->isolate(), static_cast(__alignof__(struct s_##name)))); - - // fixed alignments - SET_ALIGNOF(int8, int8_t); - SET_ALIGNOF(uint8, uint8_t); - SET_ALIGNOF(int16, int16_t); - SET_ALIGNOF(uint16, uint16_t); - SET_ALIGNOF(int32, int32_t); - SET_ALIGNOF(uint32, uint32_t); - SET_ALIGNOF(int64, int64_t); - SET_ALIGNOF(uint64, uint64_t); - SET_ALIGNOF(float, float); - SET_ALIGNOF(double, double); - SET_ALIGNOF(longdouble, long double); - - // (potentially) variable alignments - SET_ALIGNOF(bool, bool); - SET_ALIGNOF(char, char); - SET_ALIGNOF(uchar, unsigned char); - SET_ALIGNOF(short, short); - SET_ALIGNOF(ushort, unsigned short); - SET_ALIGNOF(int, int); - SET_ALIGNOF(uint, unsigned int); - SET_ALIGNOF(long, long); - SET_ALIGNOF(ulong, unsigned long); - SET_ALIGNOF(longlong, long long); - SET_ALIGNOF(ulonglong, unsigned long long); - SET_ALIGNOF(pointer, char *); - SET_ALIGNOF(size_t, size_t); - - // alignment of a Persistent handle to a JS object - SET_ALIGNOF(Object, Persistent); - -#undef SET_ALIGNOF -} - - -} // namespace _alignof -} // namespace node - -NODE_MODULE_CONTEXT_AWARE_BUILTIN(alignof, node::_alignof::Initialize) From 76dcf72a790a04d1d95d6c00b132139a652b8502 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 22 May 2015 14:32:20 -0700 Subject: [PATCH 6/7] sizeof: move "alignof" logic to sizeof.cc This makes it easier in the future to add more types, since they're now located in one location. --- lib/os.js | 4 +-- src/sizeof.cc | 81 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/lib/os.js b/lib/os.js index 14e2919d4c8016..f412a74132e4e3 100644 --- a/lib/os.js +++ b/lib/os.js @@ -55,5 +55,5 @@ if (binding.isBigEndian) else exports.endianness = function() { return 'LE'; }; -exports.sizeof = process.binding('sizeof'); -exports.alignof = process.binding('alignof'); +exports.sizeof = process.binding('sizeof').sizeof; +exports.alignof = process.binding('sizeof').alignof; diff --git a/src/sizeof.cc b/src/sizeof.cc index 9bf37036f4410e..ba8fb59287104c 100644 --- a/src/sizeof.cc +++ b/src/sizeof.cc @@ -11,9 +11,7 @@ namespace _sizeof { using v8::Context; using v8::Handle; -using v8::Isolate; using v8::Object; -using v8::Persistent; using v8::Uint32; using v8::Value; @@ -23,43 +21,58 @@ void Initialize(Handle exports, Handle context) { Environment* env = Environment::GetCurrent(context); - // fixed sizes + Handle sizeof_obj = Object::New(env->isolate()); + Handle alignof_obj = Object::New(env->isolate()); + #define SET_SIZEOF(name, type) \ - exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name ), \ - Uint32::NewFromUnsigned(env->isolate(), static_cast(sizeof( type )))); - - SET_SIZEOF(int8, int8_t); - SET_SIZEOF(uint8, uint8_t); - SET_SIZEOF(int16, int16_t); - SET_SIZEOF(uint16, uint16_t); - SET_SIZEOF(int32, int32_t); - SET_SIZEOF(uint32, uint32_t); - SET_SIZEOF(int64, int64_t); - SET_SIZEOF(uint64, uint64_t); - SET_SIZEOF(float, float); - SET_SIZEOF(double, double); - SET_SIZEOF(longdouble, long double); + sizeof_obj->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ + Uint32::NewFromUnsigned(env->isolate(), \ + static_cast(sizeof(type)))); + +#define SET_ALIGNOF(name, type) \ + alignof_obj->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ + Uint32::NewFromUnsigned(env->isolate(), \ + static_cast(alignof(type)))); + +#define SET_TYPE(name, type) \ + SET_SIZEOF(name, type) \ + SET_ALIGNOF(name, type) + + // fixed sizes + SET_TYPE(int8, int8_t); + SET_TYPE(uint8, uint8_t); + SET_TYPE(int16, int16_t); + SET_TYPE(uint16, uint16_t); + SET_TYPE(int32, int32_t); + SET_TYPE(uint32, uint32_t); + SET_TYPE(int64, int64_t); + SET_TYPE(uint64, uint64_t); + SET_TYPE(float, float); + SET_TYPE(double, double); // (potentially) variable sizes - SET_SIZEOF(bool, bool); - SET_SIZEOF(byte, unsigned char); - SET_SIZEOF(char, char); - SET_SIZEOF(uchar, unsigned char); - SET_SIZEOF(short, short); - SET_SIZEOF(ushort, unsigned short); - SET_SIZEOF(int, int); - SET_SIZEOF(uint, unsigned int); - SET_SIZEOF(long, long); - SET_SIZEOF(ulong, unsigned long); - SET_SIZEOF(longlong, long long); - SET_SIZEOF(ulonglong, unsigned long long); - SET_SIZEOF(pointer, char *); - SET_SIZEOF(size_t, size_t); - - // size of a Persistent handle to a JS object - SET_SIZEOF(Object, Persistent); + SET_TYPE(bool, bool); + SET_TYPE(byte, unsigned char); + SET_TYPE(char, char); + SET_TYPE(uchar, unsigned char); + SET_TYPE(short, short); + SET_TYPE(ushort, unsigned short); + SET_TYPE(int, int); + SET_TYPE(uint, unsigned int); + SET_TYPE(long, long); + SET_TYPE(ulong, unsigned long); + SET_TYPE(longlong, long long); + SET_TYPE(ulonglong, unsigned long long); + SET_TYPE(longdouble, long double); + SET_TYPE(pointer, char *); + SET_TYPE(size_t, size_t); #undef SET_SIZEOF +#undef SET_ALIGNOF +#undef SET_TYPE + + exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "sizeof"), sizeof_obj); + exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "alignof"), alignof_obj); } From 35fbb999c65a5a4eb14dfb373d9a31fd38cc61e3 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 22 May 2015 14:37:02 -0700 Subject: [PATCH 7/7] sizeof: asterisk to the left --- src/sizeof.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sizeof.cc b/src/sizeof.cc index ba8fb59287104c..ebd78041c6c3ab 100644 --- a/src/sizeof.cc +++ b/src/sizeof.cc @@ -64,7 +64,7 @@ void Initialize(Handle exports, SET_TYPE(longlong, long long); SET_TYPE(ulonglong, unsigned long long); SET_TYPE(longdouble, long double); - SET_TYPE(pointer, char *); + SET_TYPE(pointer, char*); SET_TYPE(size_t, size_t); #undef SET_SIZEOF