@encharm/cws 4.3.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -7
- package/dist/bindings/cws_darwin_arm64_node108.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node83.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node93.node +0 -0
- package/dist/bindings/cws_darwin_x64_node108.node +0 -0
- package/dist/bindings/cws_darwin_x64_node115.node +0 -0
- package/dist/bindings/cws_darwin_x64_node83.node +0 -0
- package/dist/bindings/cws_darwin_x64_node93.node +0 -0
- package/dist/bindings/cws_linux_arm64_node108.node +0 -0
- package/dist/bindings/cws_linux_arm64_node115.node +0 -0
- package/dist/bindings/cws_linux_arm64_node83.node +0 -0
- package/dist/bindings/cws_linux_arm64_node93.node +0 -0
- package/dist/bindings/cws_linux_x64_node108.node +0 -0
- package/dist/bindings/cws_linux_x64_node115.node +0 -0
- package/dist/bindings/cws_linux_x64_node83.node +0 -0
- package/dist/bindings/cws_linux_x64_node93.node +0 -0
- package/dist/bindings/cws_win32_x64_node108.node +0 -0
- package/dist/bindings/cws_win32_x64_node115.node +0 -0
- package/dist/bindings/cws_win32_x64_node83.node +0 -0
- package/dist/bindings/cws_win32_x64_node93.node +0 -0
- package/dist/shared.js +1 -1
- package/package.json +5 -5
- package/src/Addon.cpp +4 -12
- package/src/Addon.h +55 -90
- package/src/Socket.h +1 -0
- package/src/cSNode.cpp +3 -8
- package/src/headers/20/acorn_version.h +6 -0
- package/src/headers/20/aliased_buffer-inl.h +243 -0
- package/src/headers/20/aliased_buffer.h +206 -0
- package/src/headers/20/aliased_struct-inl.h +54 -0
- package/src/headers/20/aliased_struct.h +63 -0
- package/src/headers/20/async_wrap-inl.h +93 -0
- package/src/headers/20/async_wrap.h +252 -0
- package/src/headers/20/base64-inl.h +193 -0
- package/src/headers/20/base64.h +53 -0
- package/src/headers/20/base64_version.h +6 -0
- package/src/headers/20/base_object-inl.h +316 -0
- package/src/headers/20/base_object.h +320 -0
- package/src/headers/20/base_object_types.h +73 -0
- package/src/headers/20/blob_serializer_deserializer-inl.h +361 -0
- package/src/headers/20/blob_serializer_deserializer.h +132 -0
- package/src/headers/20/callback_queue-inl.h +97 -0
- package/src/headers/20/callback_queue.h +79 -0
- package/src/headers/20/cares_wrap.h +524 -0
- package/src/headers/20/cjs_module_lexer_version.h +6 -0
- package/src/headers/20/cleanup_queue-inl.h +62 -0
- package/src/headers/20/cleanup_queue.h +85 -0
- package/src/headers/20/connect_wrap.h +26 -0
- package/src/headers/20/connection_wrap.h +29 -0
- package/src/headers/20/cppgc/DEPS +8 -0
- package/src/headers/20/cppgc/OWNERS +2 -0
- package/src/headers/20/cppgc/README.md +135 -0
- package/src/headers/20/cppgc/allocation.h +310 -0
- package/src/headers/20/cppgc/common.h +28 -0
- package/src/headers/20/cppgc/cross-thread-persistent.h +466 -0
- package/src/headers/20/cppgc/custom-space.h +97 -0
- package/src/headers/20/cppgc/default-platform.h +67 -0
- package/src/headers/20/cppgc/ephemeron-pair.h +30 -0
- package/src/headers/20/cppgc/explicit-management.h +100 -0
- package/src/headers/20/cppgc/garbage-collected.h +106 -0
- package/src/headers/20/cppgc/heap-consistency.h +309 -0
- package/src/headers/20/cppgc/heap-handle.h +48 -0
- package/src/headers/20/cppgc/heap-state.h +82 -0
- package/src/headers/20/cppgc/heap-statistics.h +120 -0
- package/src/headers/20/cppgc/heap.h +202 -0
- package/src/headers/20/cppgc/internal/api-constants.h +68 -0
- package/src/headers/20/cppgc/internal/atomic-entry-flag.h +48 -0
- package/src/headers/20/cppgc/internal/base-page-handle.h +45 -0
- package/src/headers/20/cppgc/internal/caged-heap-local-data.h +111 -0
- package/src/headers/20/cppgc/internal/caged-heap.h +61 -0
- package/src/headers/20/cppgc/internal/compiler-specific.h +38 -0
- package/src/headers/20/cppgc/internal/finalizer-trait.h +93 -0
- package/src/headers/20/cppgc/internal/gc-info.h +157 -0
- package/src/headers/20/cppgc/internal/logging.h +50 -0
- package/src/headers/20/cppgc/internal/member-storage.h +248 -0
- package/src/headers/20/cppgc/internal/name-trait.h +137 -0
- package/src/headers/20/cppgc/internal/persistent-node.h +214 -0
- package/src/headers/20/cppgc/internal/pointer-policies.h +243 -0
- package/src/headers/20/cppgc/internal/write-barrier.h +487 -0
- package/src/headers/20/cppgc/liveness-broker.h +78 -0
- package/src/headers/20/cppgc/macros.h +35 -0
- package/src/headers/20/cppgc/member.h +604 -0
- package/src/headers/20/cppgc/name-provider.h +65 -0
- package/src/headers/20/cppgc/object-size-trait.h +58 -0
- package/src/headers/20/cppgc/persistent.h +373 -0
- package/src/headers/20/cppgc/platform.h +158 -0
- package/src/headers/20/cppgc/prefinalizer.h +75 -0
- package/src/headers/20/cppgc/process-heap-statistics.h +36 -0
- package/src/headers/20/cppgc/sentinel-pointer.h +32 -0
- package/src/headers/20/cppgc/source-location.h +92 -0
- package/src/headers/20/cppgc/testing.h +106 -0
- package/src/headers/20/cppgc/trace-trait.h +120 -0
- package/src/headers/20/cppgc/type-traits.h +250 -0
- package/src/headers/20/cppgc/visitor.h +426 -0
- package/src/headers/20/crypto/crypto_clienthello.h +131 -0
- package/src/headers/20/crypto/crypto_context.h +160 -0
- package/src/headers/20/crypto/crypto_keys.h +416 -0
- package/src/headers/20/crypto/crypto_tls.h +295 -0
- package/src/headers/20/crypto/crypto_util.h +805 -0
- package/src/headers/20/crypto_clienthello.h +131 -0
- package/src/headers/20/debug_utils-inl.h +226 -0
- package/src/headers/20/debug_utils.h +188 -0
- package/src/headers/20/diagnosticfilename-inl.h +33 -0
- package/src/headers/20/encoding_binding.h +59 -0
- package/src/headers/20/env-inl.h +923 -0
- package/src/headers/20/env.h +1193 -0
- package/src/headers/20/env_properties.h +444 -0
- package/src/headers/20/handle_wrap.h +123 -0
- package/src/headers/20/histogram-inl.h +107 -0
- package/src/headers/20/histogram.h +240 -0
- package/src/headers/20/inspector_agent.h +144 -0
- package/src/headers/20/inspector_io.h +78 -0
- package/src/headers/20/inspector_profiler.h +149 -0
- package/src/headers/20/inspector_socket.h +60 -0
- package/src/headers/20/inspector_socket_server.h +110 -0
- package/src/headers/20/js_native_api.h +591 -0
- package/src/headers/20/js_native_api_types.h +168 -0
- package/src/headers/20/js_native_api_v8.h +426 -0
- package/src/headers/20/js_native_api_v8_internals.h +39 -0
- package/src/headers/20/js_stream.h +52 -0
- package/src/headers/20/json_parser.h +38 -0
- package/src/headers/20/json_utils.h +172 -0
- package/src/headers/20/memory_tracker-inl.h +356 -0
- package/src/headers/20/memory_tracker.h +289 -0
- package/src/headers/20/module_wrap.h +117 -0
- package/src/headers/20/node.h +1514 -0
- package/src/headers/20/node_api.h +260 -0
- package/src/headers/20/node_api_internals.h +42 -0
- package/src/headers/20/node_api_types.h +52 -0
- package/src/headers/20/node_binding.h +142 -0
- package/src/headers/20/node_blob.h +159 -0
- package/src/headers/20/node_bob-inl.h +36 -0
- package/src/headers/20/node_bob.h +107 -0
- package/src/headers/20/node_buffer.h +92 -0
- package/src/headers/20/node_builtins.h +197 -0
- package/src/headers/20/node_constants.h +79 -0
- package/src/headers/20/node_context_data.h +98 -0
- package/src/headers/20/node_contextify.h +182 -0
- package/src/headers/20/node_crypto.h +56 -0
- package/src/headers/20/node_dir.h +52 -0
- package/src/headers/20/node_dotenv.h +37 -0
- package/src/headers/20/node_errors.h +313 -0
- package/src/headers/20/node_exit_code.h +54 -0
- package/src/headers/20/node_external_reference.h +172 -0
- package/src/headers/20/node_file-inl.h +357 -0
- package/src/headers/20/node_file.h +524 -0
- package/src/headers/20/node_http2.h +1146 -0
- package/src/headers/20/node_http2_state.h +147 -0
- package/src/headers/20/node_http_common-inl.h +201 -0
- package/src/headers/20/node_http_common.h +532 -0
- package/src/headers/20/node_i18n.h +145 -0
- package/src/headers/20/node_internals.h +443 -0
- package/src/headers/20/node_main_instance.h +67 -0
- package/src/headers/20/node_mem-inl.h +112 -0
- package/src/headers/20/node_mem.h +45 -0
- package/src/headers/20/node_messaging.h +377 -0
- package/src/headers/20/node_metadata.h +138 -0
- package/src/headers/20/node_mutex.h +323 -0
- package/src/headers/20/node_object_wrap.h +132 -0
- package/src/headers/20/node_options-inl.h +478 -0
- package/src/headers/20/node_options.h +538 -0
- package/src/headers/20/node_perf.h +166 -0
- package/src/headers/20/node_perf_common.h +98 -0
- package/src/headers/20/node_platform.h +195 -0
- package/src/headers/20/node_process-inl.h +26 -0
- package/src/headers/20/node_process.h +99 -0
- package/src/headers/20/node_realm-inl.h +139 -0
- package/src/headers/20/node_realm.h +184 -0
- package/src/headers/20/node_report.h +42 -0
- package/src/headers/20/node_revert.h +82 -0
- package/src/headers/20/node_root_certs.h +3443 -0
- package/src/headers/20/node_sea.h +56 -0
- package/src/headers/20/node_shadow_realm.h +46 -0
- package/src/headers/20/node_snapshot_builder.h +54 -0
- package/src/headers/20/node_snapshotable.h +164 -0
- package/src/headers/20/node_sockaddr-inl.h +266 -0
- package/src/headers/20/node_sockaddr.h +405 -0
- package/src/headers/20/node_stat_watcher.h +73 -0
- package/src/headers/20/node_threadsafe_cow-inl.h +54 -0
- package/src/headers/20/node_threadsafe_cow.h +105 -0
- package/src/headers/20/node_union_bytes.h +81 -0
- package/src/headers/20/node_url.h +92 -0
- package/src/headers/20/node_v8.h +77 -0
- package/src/headers/20/node_v8_platform-inl.h +205 -0
- package/src/headers/20/node_version.h +109 -0
- package/src/headers/20/node_wasi.h +184 -0
- package/src/headers/20/node_wasm_web_api.h +55 -0
- package/src/headers/20/node_watchdog.h +154 -0
- package/src/headers/20/node_worker.h +151 -0
- package/src/headers/20/permission/child_process_permission.h +28 -0
- package/src/headers/20/permission/fs_permission.h +152 -0
- package/src/headers/20/permission/permission.h +71 -0
- package/src/headers/20/permission/permission_base.h +49 -0
- package/src/headers/20/permission/worker_permission.h +28 -0
- package/src/headers/20/pipe_wrap.h +80 -0
- package/src/headers/20/req_wrap-inl.h +171 -0
- package/src/headers/20/req_wrap.h +77 -0
- package/src/headers/20/spawn_sync.h +242 -0
- package/src/headers/20/stream_base-inl.h +173 -0
- package/src/headers/20/stream_base.h +469 -0
- package/src/headers/20/stream_pipe.h +76 -0
- package/src/headers/20/stream_wrap.h +131 -0
- package/src/headers/20/string_bytes.h +120 -0
- package/src/headers/20/string_decoder-inl.h +37 -0
- package/src/headers/20/string_decoder.h +50 -0
- package/src/headers/20/string_search.h +638 -0
- package/src/headers/20/tcp_wrap.h +105 -0
- package/src/headers/20/threadpoolwork-inl.h +70 -0
- package/src/headers/20/timer_wrap-inl.h +32 -0
- package/src/headers/20/timer_wrap.h +85 -0
- package/src/headers/20/timers.h +70 -0
- package/src/headers/20/tracing/agent.h +195 -0
- package/src/headers/20/tracing/trace_event.h +720 -0
- package/src/headers/20/tracing/trace_event_common.h +1109 -0
- package/src/headers/20/tty_wrap.h +65 -0
- package/src/headers/20/udp_wrap.h +227 -0
- package/src/headers/20/undici_version.h +6 -0
- package/src/headers/20/util-inl.h +623 -0
- package/src/headers/20/util.h +988 -0
- package/src/headers/20/v8-cppgc.h +245 -0
- package/src/headers/20/v8-fast-api-calls.h +957 -0
- package/dist/bindings/cws_darwin_arm64_node64.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node72.node +0 -0
- package/dist/bindings/cws_darwin_arm64_node79.node +0 -0
- package/dist/bindings/cws_darwin_x64_node64.node +0 -0
- package/dist/bindings/cws_darwin_x64_node72.node +0 -0
- package/dist/bindings/cws_darwin_x64_node79.node +0 -0
- package/dist/bindings/cws_linux_x64_node64.node +0 -0
- package/dist/bindings/cws_linux_x64_node72.node +0 -0
- package/dist/bindings/cws_linux_x64_node79.node +0 -0
- package/dist/bindings/cws_win32_x64_node64.node +0 -0
- package/dist/bindings/cws_win32_x64_node72.node +0 -0
- package/dist/bindings/cws_win32_x64_node79.node +0 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
4
|
+
// copy of this software and associated documentation files (the
|
|
5
|
+
// "Software"), to deal in the Software without restriction, including
|
|
6
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
8
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
// following conditions:
|
|
10
|
+
//
|
|
11
|
+
// The above copyright notice and this permission notice shall be included
|
|
12
|
+
// in all copies or substantial portions of the Software.
|
|
13
|
+
//
|
|
14
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
15
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
17
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
18
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
19
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
20
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
#ifndef SRC_NODE_INTERNALS_H_
|
|
23
|
+
#define SRC_NODE_INTERNALS_H_
|
|
24
|
+
|
|
25
|
+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
26
|
+
|
|
27
|
+
#include "env.h"
|
|
28
|
+
#include "node.h"
|
|
29
|
+
#include "node_binding.h"
|
|
30
|
+
#include "node_mutex.h"
|
|
31
|
+
#include "tracing/trace_event.h"
|
|
32
|
+
#include "util.h"
|
|
33
|
+
#include "uv.h"
|
|
34
|
+
#include "v8.h"
|
|
35
|
+
|
|
36
|
+
#include <cstdint>
|
|
37
|
+
#include <cstdlib>
|
|
38
|
+
|
|
39
|
+
#include <string>
|
|
40
|
+
#include <vector>
|
|
41
|
+
|
|
42
|
+
struct sockaddr;
|
|
43
|
+
|
|
44
|
+
namespace node {
|
|
45
|
+
|
|
46
|
+
namespace builtins {
|
|
47
|
+
class BuiltinLoader;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
namespace per_process {
|
|
51
|
+
extern Mutex env_var_mutex;
|
|
52
|
+
extern uint64_t node_start_time;
|
|
53
|
+
} // namespace per_process
|
|
54
|
+
|
|
55
|
+
// Forward declaration
|
|
56
|
+
class Environment;
|
|
57
|
+
|
|
58
|
+
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
|
|
59
|
+
// Sets address and port properties on the info object and returns it.
|
|
60
|
+
// If |info| is omitted, a new object is returned.
|
|
61
|
+
v8::MaybeLocal<v8::Object> AddressToJS(
|
|
62
|
+
Environment* env,
|
|
63
|
+
const sockaddr* addr,
|
|
64
|
+
v8::Local<v8::Object> info = v8::Local<v8::Object>());
|
|
65
|
+
|
|
66
|
+
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
|
|
67
|
+
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
68
|
+
T* wrap;
|
|
69
|
+
ASSIGN_OR_RETURN_UNWRAP(&wrap,
|
|
70
|
+
args.Holder(),
|
|
71
|
+
args.GetReturnValue().Set(UV_EBADF));
|
|
72
|
+
CHECK(args[0]->IsObject());
|
|
73
|
+
sockaddr_storage storage;
|
|
74
|
+
int addrlen = sizeof(storage);
|
|
75
|
+
sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
|
|
76
|
+
const int err = F(&wrap->handle_, addr, &addrlen);
|
|
77
|
+
if (err == 0)
|
|
78
|
+
AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
|
|
79
|
+
args.GetReturnValue().Set(err);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
void PrintStackTrace(v8::Isolate* isolate, v8::Local<v8::StackTrace> stack);
|
|
83
|
+
void PrintCaughtException(v8::Isolate* isolate,
|
|
84
|
+
v8::Local<v8::Context> context,
|
|
85
|
+
const v8::TryCatch& try_catch);
|
|
86
|
+
std::string FormatCaughtException(v8::Isolate* isolate,
|
|
87
|
+
v8::Local<v8::Context> context,
|
|
88
|
+
const v8::TryCatch& try_catch);
|
|
89
|
+
|
|
90
|
+
void ResetStdio(); // Safe to call more than once and from signal handlers.
|
|
91
|
+
#ifdef __POSIX__
|
|
92
|
+
void SignalExit(int signal, siginfo_t* info, void* ucontext);
|
|
93
|
+
#endif
|
|
94
|
+
|
|
95
|
+
std::string GetProcessTitle(const char* default_title);
|
|
96
|
+
std::string GetHumanReadableProcessName();
|
|
97
|
+
|
|
98
|
+
v8::Maybe<bool> InitializeBaseContextForSnapshot(
|
|
99
|
+
v8::Local<v8::Context> context);
|
|
100
|
+
v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
|
|
101
|
+
v8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);
|
|
102
|
+
|
|
103
|
+
class NodeArrayBufferAllocator : public ArrayBufferAllocator {
|
|
104
|
+
public:
|
|
105
|
+
inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
|
|
106
|
+
|
|
107
|
+
void* Allocate(size_t size) override; // Defined in src/node.cc
|
|
108
|
+
void* AllocateUninitialized(size_t size) override;
|
|
109
|
+
void Free(void* data, size_t size) override;
|
|
110
|
+
void* Reallocate(void* data, size_t old_size, size_t size) override;
|
|
111
|
+
virtual void RegisterPointer(void* data, size_t size) {
|
|
112
|
+
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
|
|
113
|
+
}
|
|
114
|
+
virtual void UnregisterPointer(void* data, size_t size) {
|
|
115
|
+
total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
NodeArrayBufferAllocator* GetImpl() final { return this; }
|
|
119
|
+
inline uint64_t total_mem_usage() const {
|
|
120
|
+
return total_mem_usage_.load(std::memory_order_relaxed);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private:
|
|
124
|
+
uint32_t zero_fill_field_ = 1; // Boolean but exposed as uint32 to JS land.
|
|
125
|
+
std::atomic<size_t> total_mem_usage_ {0};
|
|
126
|
+
|
|
127
|
+
// Delegate to V8's allocator for compatibility with the V8 memory cage.
|
|
128
|
+
std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_{
|
|
129
|
+
v8::ArrayBuffer::Allocator::NewDefaultAllocator()};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {
|
|
133
|
+
public:
|
|
134
|
+
~DebuggingArrayBufferAllocator() override;
|
|
135
|
+
void* Allocate(size_t size) override;
|
|
136
|
+
void* AllocateUninitialized(size_t size) override;
|
|
137
|
+
void Free(void* data, size_t size) override;
|
|
138
|
+
void* Reallocate(void* data, size_t old_size, size_t size) override;
|
|
139
|
+
void RegisterPointer(void* data, size_t size) override;
|
|
140
|
+
void UnregisterPointer(void* data, size_t size) override;
|
|
141
|
+
|
|
142
|
+
private:
|
|
143
|
+
void RegisterPointerInternal(void* data, size_t size);
|
|
144
|
+
void UnregisterPointerInternal(void* data, size_t size);
|
|
145
|
+
Mutex mutex_;
|
|
146
|
+
std::unordered_map<void*, size_t> allocations_;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
namespace Buffer {
|
|
150
|
+
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
|
|
151
|
+
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
|
|
152
|
+
// Takes ownership of |data|.
|
|
153
|
+
v8::MaybeLocal<v8::Object> New(Environment* env,
|
|
154
|
+
char* data,
|
|
155
|
+
size_t length,
|
|
156
|
+
void (*callback)(char* data, void* hint),
|
|
157
|
+
void* hint);
|
|
158
|
+
// Takes ownership of |data|. Must allocate |data| with the current Isolate's
|
|
159
|
+
// ArrayBuffer::Allocator().
|
|
160
|
+
v8::MaybeLocal<v8::Object> New(Environment* env,
|
|
161
|
+
char* data,
|
|
162
|
+
size_t length);
|
|
163
|
+
// Creates a Buffer instance over an existing ArrayBuffer.
|
|
164
|
+
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
|
|
165
|
+
v8::Local<v8::ArrayBuffer> ab,
|
|
166
|
+
size_t byte_offset,
|
|
167
|
+
size_t length);
|
|
168
|
+
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
|
|
169
|
+
// Utf8Value and TwoByteValue).
|
|
170
|
+
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
|
|
171
|
+
// changed.
|
|
172
|
+
// If |buf| contains actual data, this method takes ownership of |buf|'s
|
|
173
|
+
// underlying buffer. However, |buf| itself can be reused even after this call,
|
|
174
|
+
// but its capacity, if increased through AllocateSufficientStorage, is not
|
|
175
|
+
// guaranteed to stay the same.
|
|
176
|
+
template <typename T>
|
|
177
|
+
static v8::MaybeLocal<v8::Object> New(Environment* env,
|
|
178
|
+
MaybeStackBuffer<T>* buf) {
|
|
179
|
+
v8::MaybeLocal<v8::Object> ret;
|
|
180
|
+
char* src = reinterpret_cast<char*>(buf->out());
|
|
181
|
+
const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
|
|
182
|
+
|
|
183
|
+
if (buf->IsAllocated())
|
|
184
|
+
ret = New(env, src, len_in_bytes);
|
|
185
|
+
else if (!buf->IsInvalidated())
|
|
186
|
+
ret = Copy(env, src, len_in_bytes);
|
|
187
|
+
|
|
188
|
+
if (ret.IsEmpty())
|
|
189
|
+
return ret;
|
|
190
|
+
|
|
191
|
+
if (buf->IsAllocated())
|
|
192
|
+
buf->Release();
|
|
193
|
+
|
|
194
|
+
return ret;
|
|
195
|
+
}
|
|
196
|
+
} // namespace Buffer
|
|
197
|
+
|
|
198
|
+
v8::MaybeLocal<v8::Value> InternalMakeCallback(
|
|
199
|
+
Environment* env,
|
|
200
|
+
v8::Local<v8::Object> resource,
|
|
201
|
+
v8::Local<v8::Object> recv,
|
|
202
|
+
const v8::Local<v8::Function> callback,
|
|
203
|
+
int argc,
|
|
204
|
+
v8::Local<v8::Value> argv[],
|
|
205
|
+
async_context asyncContext);
|
|
206
|
+
|
|
207
|
+
v8::MaybeLocal<v8::Value> MakeSyncCallback(v8::Isolate* isolate,
|
|
208
|
+
v8::Local<v8::Object> recv,
|
|
209
|
+
v8::Local<v8::Function> callback,
|
|
210
|
+
int argc,
|
|
211
|
+
v8::Local<v8::Value> argv[]);
|
|
212
|
+
|
|
213
|
+
class InternalCallbackScope {
|
|
214
|
+
public:
|
|
215
|
+
enum Flags {
|
|
216
|
+
kNoFlags = 0,
|
|
217
|
+
// Indicates whether 'before' and 'after' hooks should be skipped.
|
|
218
|
+
kSkipAsyncHooks = 1,
|
|
219
|
+
// Indicates whether nextTick and microtask queues should be skipped.
|
|
220
|
+
// This should only be used when there is no call into JS in this scope.
|
|
221
|
+
// (The HTTP parser also uses it for some weird backwards
|
|
222
|
+
// compatibility issues, but it shouldn't.)
|
|
223
|
+
kSkipTaskQueues = 2
|
|
224
|
+
};
|
|
225
|
+
InternalCallbackScope(Environment* env,
|
|
226
|
+
v8::Local<v8::Object> object,
|
|
227
|
+
const async_context& asyncContext,
|
|
228
|
+
int flags = kNoFlags);
|
|
229
|
+
// Utility that can be used by AsyncWrap classes.
|
|
230
|
+
explicit InternalCallbackScope(AsyncWrap* async_wrap, int flags = 0);
|
|
231
|
+
~InternalCallbackScope();
|
|
232
|
+
void Close();
|
|
233
|
+
|
|
234
|
+
inline bool Failed() const { return failed_; }
|
|
235
|
+
inline void MarkAsFailed() { failed_ = true; }
|
|
236
|
+
|
|
237
|
+
private:
|
|
238
|
+
Environment* env_;
|
|
239
|
+
async_context async_context_;
|
|
240
|
+
v8::Local<v8::Object> object_;
|
|
241
|
+
bool skip_hooks_;
|
|
242
|
+
bool skip_task_queues_;
|
|
243
|
+
bool failed_ = false;
|
|
244
|
+
bool pushed_ids_ = false;
|
|
245
|
+
bool closed_ = false;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
class DebugSealHandleScope {
|
|
249
|
+
public:
|
|
250
|
+
explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr)
|
|
251
|
+
#ifdef DEBUG
|
|
252
|
+
: actual_scope_(isolate != nullptr ? isolate : v8::Isolate::GetCurrent())
|
|
253
|
+
#endif
|
|
254
|
+
{}
|
|
255
|
+
|
|
256
|
+
private:
|
|
257
|
+
#ifdef DEBUG
|
|
258
|
+
v8::SealHandleScope actual_scope_;
|
|
259
|
+
#endif
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
class ThreadPoolWork {
|
|
263
|
+
public:
|
|
264
|
+
explicit inline ThreadPoolWork(Environment* env, const char* type)
|
|
265
|
+
: env_(env), type_(type) {
|
|
266
|
+
CHECK_NOT_NULL(env);
|
|
267
|
+
}
|
|
268
|
+
inline virtual ~ThreadPoolWork() = default;
|
|
269
|
+
|
|
270
|
+
inline void ScheduleWork();
|
|
271
|
+
inline int CancelWork();
|
|
272
|
+
|
|
273
|
+
virtual void DoThreadPoolWork() = 0;
|
|
274
|
+
virtual void AfterThreadPoolWork(int status) = 0;
|
|
275
|
+
|
|
276
|
+
Environment* env() const { return env_; }
|
|
277
|
+
|
|
278
|
+
private:
|
|
279
|
+
Environment* env_;
|
|
280
|
+
uv_work_t work_req_;
|
|
281
|
+
const char* type_;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
#define TRACING_CATEGORY_NODE "node"
|
|
285
|
+
#define TRACING_CATEGORY_NODE1(one) \
|
|
286
|
+
TRACING_CATEGORY_NODE "," \
|
|
287
|
+
TRACING_CATEGORY_NODE "." #one
|
|
288
|
+
#define TRACING_CATEGORY_NODE2(one, two) \
|
|
289
|
+
TRACING_CATEGORY_NODE "," \
|
|
290
|
+
TRACING_CATEGORY_NODE "." #one "," \
|
|
291
|
+
TRACING_CATEGORY_NODE "." #one "." #two
|
|
292
|
+
|
|
293
|
+
// Functions defined in node.cc that are exposed via the bootstrapper object
|
|
294
|
+
|
|
295
|
+
#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
|
|
296
|
+
#define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
|
|
297
|
+
#endif // defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
|
|
298
|
+
|
|
299
|
+
namespace credentials {
|
|
300
|
+
bool SafeGetenv(const char* key,
|
|
301
|
+
std::string* text,
|
|
302
|
+
std::shared_ptr<KVStore> env_vars = nullptr,
|
|
303
|
+
v8::Isolate* isolate = nullptr);
|
|
304
|
+
} // namespace credentials
|
|
305
|
+
|
|
306
|
+
void DefineZlibConstants(v8::Local<v8::Object> target);
|
|
307
|
+
v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
|
|
308
|
+
uv_loop_t* event_loop,
|
|
309
|
+
MultiIsolatePlatform* platform,
|
|
310
|
+
const SnapshotData* snapshot_data = nullptr,
|
|
311
|
+
const IsolateSettings& settings = {});
|
|
312
|
+
// This overload automatically picks the right 'main_script_id' if no callback
|
|
313
|
+
// was provided by the embedder.
|
|
314
|
+
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
|
|
315
|
+
StartExecutionCallback cb = nullptr);
|
|
316
|
+
v8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
|
|
317
|
+
void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
318
|
+
|
|
319
|
+
class InitializationResultImpl final : public InitializationResult {
|
|
320
|
+
public:
|
|
321
|
+
~InitializationResultImpl() = default;
|
|
322
|
+
int exit_code() const { return static_cast<int>(exit_code_enum()); }
|
|
323
|
+
ExitCode exit_code_enum() const { return exit_code_; }
|
|
324
|
+
bool early_return() const { return early_return_; }
|
|
325
|
+
const std::vector<std::string>& args() const { return args_; }
|
|
326
|
+
const std::vector<std::string>& exec_args() const { return exec_args_; }
|
|
327
|
+
const std::vector<std::string>& errors() const { return errors_; }
|
|
328
|
+
MultiIsolatePlatform* platform() const { return platform_; }
|
|
329
|
+
|
|
330
|
+
ExitCode exit_code_ = ExitCode::kNoFailure;
|
|
331
|
+
std::vector<std::string> args_;
|
|
332
|
+
std::vector<std::string> exec_args_;
|
|
333
|
+
std::vector<std::string> errors_;
|
|
334
|
+
bool early_return_ = false;
|
|
335
|
+
MultiIsolatePlatform* platform_ = nullptr;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
|
|
339
|
+
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
|
|
340
|
+
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
|
|
341
|
+
|
|
342
|
+
#if HAVE_INSPECTOR
|
|
343
|
+
namespace profiler {
|
|
344
|
+
void StartProfilers(Environment* env);
|
|
345
|
+
}
|
|
346
|
+
#endif // HAVE_INSPECTOR
|
|
347
|
+
|
|
348
|
+
#ifdef __POSIX__
|
|
349
|
+
static constexpr unsigned kMaxSignal = 32;
|
|
350
|
+
#endif
|
|
351
|
+
|
|
352
|
+
bool HasSignalJSHandler(int signum);
|
|
353
|
+
|
|
354
|
+
#ifdef _WIN32
|
|
355
|
+
typedef SYSTEMTIME TIME_TYPE;
|
|
356
|
+
#else // UNIX, OSX
|
|
357
|
+
typedef struct tm TIME_TYPE;
|
|
358
|
+
#endif
|
|
359
|
+
|
|
360
|
+
double GetCurrentTimeInMicroseconds();
|
|
361
|
+
int WriteFileSync(const char* path, uv_buf_t buf);
|
|
362
|
+
int WriteFileSync(v8::Isolate* isolate,
|
|
363
|
+
const char* path,
|
|
364
|
+
v8::Local<v8::String> string);
|
|
365
|
+
|
|
366
|
+
class DiagnosticFilename {
|
|
367
|
+
public:
|
|
368
|
+
static void LocalTime(TIME_TYPE* tm_struct);
|
|
369
|
+
|
|
370
|
+
inline DiagnosticFilename(Environment* env,
|
|
371
|
+
const char* prefix,
|
|
372
|
+
const char* ext);
|
|
373
|
+
|
|
374
|
+
inline DiagnosticFilename(uint64_t thread_id,
|
|
375
|
+
const char* prefix,
|
|
376
|
+
const char* ext);
|
|
377
|
+
|
|
378
|
+
inline const char* operator*() const;
|
|
379
|
+
|
|
380
|
+
private:
|
|
381
|
+
static std::string MakeFilename(
|
|
382
|
+
uint64_t thread_id,
|
|
383
|
+
const char* prefix,
|
|
384
|
+
const char* ext);
|
|
385
|
+
|
|
386
|
+
std::string filename_;
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
namespace heap {
|
|
390
|
+
v8::Maybe<void> WriteSnapshot(Environment* env,
|
|
391
|
+
const char* filename,
|
|
392
|
+
v8::HeapProfiler::HeapSnapshotOptions options);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
namespace heap {
|
|
396
|
+
|
|
397
|
+
void DeleteHeapSnapshot(const v8::HeapSnapshot* snapshot);
|
|
398
|
+
using HeapSnapshotPointer =
|
|
399
|
+
DeleteFnPtr<const v8::HeapSnapshot, DeleteHeapSnapshot>;
|
|
400
|
+
|
|
401
|
+
BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
|
|
402
|
+
Environment* env, HeapSnapshotPointer&& snapshot);
|
|
403
|
+
} // namespace heap
|
|
404
|
+
|
|
405
|
+
namespace fs {
|
|
406
|
+
std::string Basename(const std::string& str, const std::string& extension);
|
|
407
|
+
} // namespace fs
|
|
408
|
+
|
|
409
|
+
node_module napi_module_to_node_module(const napi_module* mod);
|
|
410
|
+
|
|
411
|
+
std::ostream& operator<<(std::ostream& output,
|
|
412
|
+
const std::vector<SnapshotIndex>& v);
|
|
413
|
+
std::ostream& operator<<(std::ostream& output,
|
|
414
|
+
const std::vector<std::string>& vec);
|
|
415
|
+
std::ostream& operator<<(std::ostream& output,
|
|
416
|
+
const std::vector<PropInfo>& vec);
|
|
417
|
+
std::ostream& operator<<(std::ostream& output, const PropInfo& d);
|
|
418
|
+
std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& d);
|
|
419
|
+
std::ostream& operator<<(std::ostream& output,
|
|
420
|
+
const ImmediateInfo::SerializeInfo& d);
|
|
421
|
+
std::ostream& operator<<(std::ostream& output,
|
|
422
|
+
const TickInfo::SerializeInfo& d);
|
|
423
|
+
std::ostream& operator<<(std::ostream& output,
|
|
424
|
+
const AsyncHooks::SerializeInfo& d);
|
|
425
|
+
std::ostream& operator<<(std::ostream& output, const SnapshotMetadata& d);
|
|
426
|
+
|
|
427
|
+
namespace performance {
|
|
428
|
+
std::ostream& operator<<(std::ostream& output,
|
|
429
|
+
const PerformanceState::SerializeInfo& d);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
bool linux_at_secure();
|
|
433
|
+
|
|
434
|
+
namespace heap {
|
|
435
|
+
v8::HeapProfiler::HeapSnapshotOptions GetHeapSnapshotOptions(
|
|
436
|
+
v8::Local<v8::Value> options);
|
|
437
|
+
} // namespace heap
|
|
438
|
+
|
|
439
|
+
} // namespace node
|
|
440
|
+
|
|
441
|
+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
442
|
+
|
|
443
|
+
#endif // SRC_NODE_INTERNALS_H_
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#ifndef SRC_NODE_MAIN_INSTANCE_H_
|
|
2
|
+
#define SRC_NODE_MAIN_INSTANCE_H_
|
|
3
|
+
|
|
4
|
+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
5
|
+
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <memory>
|
|
8
|
+
|
|
9
|
+
#include "node.h"
|
|
10
|
+
#include "node_exit_code.h"
|
|
11
|
+
#include "util.h"
|
|
12
|
+
#include "uv.h"
|
|
13
|
+
#include "v8.h"
|
|
14
|
+
|
|
15
|
+
namespace node {
|
|
16
|
+
|
|
17
|
+
class ExternalReferenceRegistry;
|
|
18
|
+
struct EnvSerializeInfo;
|
|
19
|
+
struct SnapshotData;
|
|
20
|
+
|
|
21
|
+
// TODO(joyeecheung): align this with the Worker/WorkerThreadData class.
|
|
22
|
+
// We may be able to create an abstract class to reuse some of the routines.
|
|
23
|
+
class NodeMainInstance {
|
|
24
|
+
public:
|
|
25
|
+
// Create a main instance that owns the isolate
|
|
26
|
+
NodeMainInstance(const SnapshotData* snapshot_data,
|
|
27
|
+
uv_loop_t* event_loop,
|
|
28
|
+
MultiIsolatePlatform* platform,
|
|
29
|
+
const std::vector<std::string>& args,
|
|
30
|
+
const std::vector<std::string>& exec_args);
|
|
31
|
+
~NodeMainInstance();
|
|
32
|
+
|
|
33
|
+
// Start running the Node.js instances, return the exit code when finished.
|
|
34
|
+
ExitCode Run();
|
|
35
|
+
void Run(ExitCode* exit_code, Environment* env);
|
|
36
|
+
|
|
37
|
+
IsolateData* isolate_data() { return isolate_data_.get(); }
|
|
38
|
+
|
|
39
|
+
DeleteFnPtr<Environment, FreeEnvironment> CreateMainEnvironment(
|
|
40
|
+
ExitCode* exit_code);
|
|
41
|
+
|
|
42
|
+
NodeMainInstance(const NodeMainInstance&) = delete;
|
|
43
|
+
NodeMainInstance& operator=(const NodeMainInstance&) = delete;
|
|
44
|
+
NodeMainInstance(NodeMainInstance&&) = delete;
|
|
45
|
+
NodeMainInstance& operator=(NodeMainInstance&&) = delete;
|
|
46
|
+
|
|
47
|
+
private:
|
|
48
|
+
NodeMainInstance(v8::Isolate* isolate,
|
|
49
|
+
uv_loop_t* event_loop,
|
|
50
|
+
MultiIsolatePlatform* platform,
|
|
51
|
+
const std::vector<std::string>& args,
|
|
52
|
+
const std::vector<std::string>& exec_args);
|
|
53
|
+
|
|
54
|
+
std::vector<std::string> args_;
|
|
55
|
+
std::vector<std::string> exec_args_;
|
|
56
|
+
std::unique_ptr<ArrayBufferAllocator> array_buffer_allocator_;
|
|
57
|
+
v8::Isolate* isolate_;
|
|
58
|
+
MultiIsolatePlatform* platform_;
|
|
59
|
+
std::unique_ptr<IsolateData> isolate_data_;
|
|
60
|
+
std::unique_ptr<v8::Isolate::CreateParams> isolate_params_;
|
|
61
|
+
const SnapshotData* snapshot_data_ = nullptr;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
} // namespace node
|
|
65
|
+
|
|
66
|
+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
67
|
+
#endif // SRC_NODE_MAIN_INSTANCE_H_
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#ifndef SRC_NODE_MEM_INL_H_
|
|
2
|
+
#define SRC_NODE_MEM_INL_H_
|
|
3
|
+
|
|
4
|
+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
5
|
+
|
|
6
|
+
#include "node_mem.h"
|
|
7
|
+
#include "node_internals.h"
|
|
8
|
+
|
|
9
|
+
namespace node {
|
|
10
|
+
namespace mem {
|
|
11
|
+
|
|
12
|
+
template <typename Class, typename AllocatorStruct>
|
|
13
|
+
AllocatorStruct NgLibMemoryManager<Class, AllocatorStruct>::MakeAllocator() {
|
|
14
|
+
return AllocatorStruct {
|
|
15
|
+
static_cast<void*>(static_cast<Class*>(this)),
|
|
16
|
+
MallocImpl,
|
|
17
|
+
FreeImpl,
|
|
18
|
+
CallocImpl,
|
|
19
|
+
ReallocImpl
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
template <typename Class, typename T>
|
|
24
|
+
void* NgLibMemoryManager<Class, T>::ReallocImpl(void* ptr,
|
|
25
|
+
size_t size,
|
|
26
|
+
void* user_data) {
|
|
27
|
+
Class* manager = static_cast<Class*>(user_data);
|
|
28
|
+
|
|
29
|
+
size_t previous_size = 0;
|
|
30
|
+
char* original_ptr = nullptr;
|
|
31
|
+
|
|
32
|
+
// We prepend each allocated buffer with a size_t containing the full
|
|
33
|
+
// size of the allocation.
|
|
34
|
+
if (size > 0) size += sizeof(size_t);
|
|
35
|
+
|
|
36
|
+
if (ptr != nullptr) {
|
|
37
|
+
// We are free()ing or re-allocating.
|
|
38
|
+
original_ptr = static_cast<char*>(ptr) - sizeof(size_t);
|
|
39
|
+
previous_size = *reinterpret_cast<size_t*>(original_ptr);
|
|
40
|
+
// This means we called StopTracking() on this pointer before.
|
|
41
|
+
if (previous_size == 0) {
|
|
42
|
+
// Fall back to the standard Realloc() function.
|
|
43
|
+
char* ret = UncheckedRealloc(original_ptr, size);
|
|
44
|
+
if (ret != nullptr)
|
|
45
|
+
ret += sizeof(size_t);
|
|
46
|
+
return ret;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
manager->CheckAllocatedSize(previous_size);
|
|
51
|
+
|
|
52
|
+
char* mem = UncheckedRealloc(original_ptr, size);
|
|
53
|
+
|
|
54
|
+
if (mem != nullptr) {
|
|
55
|
+
// Adjust the memory info counter.
|
|
56
|
+
// TODO(addaleax): Avoid the double bookkeeping we do with
|
|
57
|
+
// current_nghttp2_memory_ + AdjustAmountOfExternalAllocatedMemory
|
|
58
|
+
// and provide versions of our memory allocation utilities that take an
|
|
59
|
+
// Environment*/Isolate* parameter and call the V8 method transparently.
|
|
60
|
+
const int64_t new_size = size - previous_size;
|
|
61
|
+
manager->IncreaseAllocatedSize(new_size);
|
|
62
|
+
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
|
|
63
|
+
new_size);
|
|
64
|
+
*reinterpret_cast<size_t*>(mem) = size;
|
|
65
|
+
mem += sizeof(size_t);
|
|
66
|
+
} else if (size == 0) {
|
|
67
|
+
manager->DecreaseAllocatedSize(previous_size);
|
|
68
|
+
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
|
|
69
|
+
-static_cast<int64_t>(previous_size));
|
|
70
|
+
}
|
|
71
|
+
return mem;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
template <typename Class, typename T>
|
|
75
|
+
void* NgLibMemoryManager<Class, T>::MallocImpl(size_t size, void* user_data) {
|
|
76
|
+
return ReallocImpl(nullptr, size, user_data);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
template <typename Class, typename T>
|
|
80
|
+
void NgLibMemoryManager<Class, T>::FreeImpl(void* ptr, void* user_data) {
|
|
81
|
+
if (ptr == nullptr) return;
|
|
82
|
+
CHECK_NULL(ReallocImpl(ptr, 0, user_data));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
template <typename Class, typename T>
|
|
86
|
+
void* NgLibMemoryManager<Class, T>::CallocImpl(size_t nmemb,
|
|
87
|
+
size_t size,
|
|
88
|
+
void* user_data) {
|
|
89
|
+
size_t real_size = MultiplyWithOverflowCheck(nmemb, size);
|
|
90
|
+
void* mem = MallocImpl(real_size, user_data);
|
|
91
|
+
if (mem != nullptr)
|
|
92
|
+
memset(mem, 0, real_size);
|
|
93
|
+
return mem;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
template <typename Class, typename T>
|
|
97
|
+
void NgLibMemoryManager<Class, T>::StopTrackingMemory(void* ptr) {
|
|
98
|
+
size_t* original_ptr = reinterpret_cast<size_t*>(
|
|
99
|
+
static_cast<char*>(ptr) - sizeof(size_t));
|
|
100
|
+
Class* manager = static_cast<Class*>(this);
|
|
101
|
+
manager->DecreaseAllocatedSize(*original_ptr);
|
|
102
|
+
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
|
|
103
|
+
-static_cast<int64_t>(*original_ptr));
|
|
104
|
+
*original_ptr = 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
} // namespace mem
|
|
108
|
+
} // namespace node
|
|
109
|
+
|
|
110
|
+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
111
|
+
|
|
112
|
+
#endif // SRC_NODE_MEM_INL_H_
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#ifndef SRC_NODE_MEM_H_
|
|
2
|
+
#define SRC_NODE_MEM_H_
|
|
3
|
+
|
|
4
|
+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
5
|
+
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
|
|
8
|
+
namespace node {
|
|
9
|
+
namespace mem {
|
|
10
|
+
|
|
11
|
+
// nghttp2 allows custom allocators that
|
|
12
|
+
// follow exactly the same structure and behavior, but
|
|
13
|
+
// use different struct names. To allow for code re-use,
|
|
14
|
+
// the NgLibMemoryManager template class can be used for both.
|
|
15
|
+
|
|
16
|
+
struct NgLibMemoryManagerBase {
|
|
17
|
+
virtual void StopTrackingMemory(void* ptr) = 0;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
template <typename Class, typename AllocatorStructName>
|
|
21
|
+
class NgLibMemoryManager : public NgLibMemoryManagerBase {
|
|
22
|
+
public:
|
|
23
|
+
// Class needs to provide these methods:
|
|
24
|
+
// void CheckAllocatedSize(size_t previous_size) const;
|
|
25
|
+
// void IncreaseAllocatedSize(size_t size);
|
|
26
|
+
// void DecreaseAllocatedSize(size_t size);
|
|
27
|
+
// Environment* env() const;
|
|
28
|
+
|
|
29
|
+
AllocatorStructName MakeAllocator();
|
|
30
|
+
|
|
31
|
+
void StopTrackingMemory(void* ptr) override;
|
|
32
|
+
|
|
33
|
+
private:
|
|
34
|
+
static void* ReallocImpl(void* ptr, size_t size, void* user_data);
|
|
35
|
+
static void* MallocImpl(size_t size, void* user_data);
|
|
36
|
+
static void FreeImpl(void* ptr, void* user_data);
|
|
37
|
+
static void* CallocImpl(size_t nmemb, size_t size, void* user_data);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
} // namespace mem
|
|
41
|
+
} // namespace node
|
|
42
|
+
|
|
43
|
+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
44
|
+
|
|
45
|
+
#endif // SRC_NODE_MEM_H_
|