diff --git a/src/nsolid.h b/src/nsolid.h index 914269d7cf..77f33991ed 100644 --- a/src/nsolid.h +++ b/src/nsolid.h @@ -989,9 +989,13 @@ int ThreadMetrics::Update(Cb&& cb, Data&&... data) { } // _1 - ThreadMetrics* - UserData* user_data = new UserData( + UserData* user_data = new (std::nothrow) UserData( std::bind(std::forward(cb), _1, std::forward(data)...)); + if (user_data == nullptr) { + return UV_ENOMEM; + } + user_data_ = user_data; proxy_ = thread_metrics_proxy_; stor_.thread_id = thread_id_; @@ -1032,8 +1036,13 @@ MetricsStream* MetricsStream::CreateInstance(uint32_t flags, std::forward(cb), _1, _2, std::forward(data)...)); // _1 - MetricsStream* metrics_stream // _2 - const metrics_stream_bucket& bucket - UserData* user_data = new UserData(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, _2, std::forward(data)...)); + + if (user_data == nullptr) { + return nullptr; + } + stream->DoSetup(flags, metrics_stream_proxy_, internal::delete_proxy_, @@ -1062,8 +1071,13 @@ Tracer* Tracer::CreateInstance(uint32_t flags, Cb&& cb, Data&&... data) { std::forward(cb), _1, _2, std::forward(data)...)); // _1 - Tracer* // _2 - const SpanStor& - UserData* user_data = new UserData(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, _2, std::forward(data)...)); + + if (user_data == nullptr) { + return nullptr; + } + tracer->DoSetup(flags, trace_proxy_, internal::delete_proxy_, @@ -1092,16 +1106,23 @@ int CpuProfiler::TakeProfile(SharedEnvInst envinst, // _1 - int status // _2 - std::string json - std::unique_ptr user_data = std::make_unique(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, _2, std::forward(data)...)); + if (user_data == nullptr) { + return UV_ENOMEM; + } + + int er = get_cpu_profile_(envinst, duration, - user_data.get(), + user_data, cpu_profiler_proxy_, internal::delete_proxy_); - if (!er) - user_data.release(); + if (er) { + delete user_data; + } + return er; } @@ -1126,16 +1147,23 @@ int Snapshot::TakeSnapshot(SharedEnvInst envinst, // _1 - int status // _2 - std::string json - std::unique_ptr user_data = std::make_unique(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, _2, std::forward(data)...)); + if (user_data == nullptr) { + return UV_ENOMEM; + } + int er = get_snapshot_(envinst, redacted, - user_data.get(), + user_data, snapshot_proxy_, internal::delete_proxy_); - if (!er) - user_data.release(); + + if (er) { + delete user_data; + } + return er; } @@ -1151,14 +1179,21 @@ int QueueCallback(Cb&& cb, Data&&... data) { using UserData = decltype(std::bind( std::forward(cb), std::forward(data)...)); - std::unique_ptr user_data = std::make_unique(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), std::forward(data)...)); + if (user_data == nullptr) { + return UV_ENOMEM; + } + int er = internal::queue_callback_( - user_data.get(), + user_data, internal::queue_callback_proxy_); - if (!er) - user_data.release(); + + if (er) { + delete user_data; + } + return er; } @@ -1168,15 +1203,22 @@ int QueueCallback(uint64_t timeout, Cb&& cb, Data&&... data) { using UserData = decltype(std::bind( std::forward(cb), std::forward(data)...)); - std::unique_ptr user_data = std::make_unique(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), std::forward(data)...)); + if (user_data == nullptr) { + return UV_ENOMEM; + } + int er = internal::queue_callback_( timeout, - user_data.get(), + user_data, internal::queue_callback_proxy_); - if (!er) - user_data.release(); + + if (er) { + delete user_data; + } + return er; } @@ -1191,17 +1233,23 @@ int RunCommand(SharedEnvInst envinst, using UserData = decltype(std::bind( std::forward(cb), _1, std::forward(data)...)); // _1 - SharedEnvInst - std::unique_ptr user_data = std::make_unique(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, std::forward(data)...)); + if (user_data == nullptr) { + return UV_ENOMEM; + } + int er = internal::run_command_( envinst, type, - user_data.get(), + user_data, internal::run_command_proxy_); - if (!er) - user_data.release(); + if (er) { + delete user_data; + } + return 0; } @@ -1223,17 +1271,25 @@ int CustomCommand(SharedEnvInst envinst, // _3 - int status // _4 - std::pair error // _5 - std::pair value - std::unique_ptr user_data = std::make_unique(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, _2, _3, _4, _5, std::forward(data)...)); + + if (user_data == nullptr) { + return UV_ENOMEM; + } + int er = internal::custom_command_( envinst, req_id, command, args, - user_data.get(), + user_data, internal::custom_command_proxy_); - if (!er) - user_data.release(); + + if (er) { + delete user_data; + } + return er; } @@ -1247,15 +1303,22 @@ int AtExitHook(Cb&& cb, Data&&... data) { // _1 - bool on_signal // _2 - bool profile_stopped - std::unique_ptr user_data = std::make_unique(std::bind( + UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, _2, std::forward(data)...)); + if (user_data == nullptr) { + return UV_ENOMEM; + } + int er = internal::at_exit_hook_( - user_data.get(), + user_data, internal::at_exit_hook_proxy_, internal::delete_proxy_); - if (!er) - user_data.release(); + + if (er) { + delete user_data; + } + return er; } @@ -1271,6 +1334,7 @@ int OnBlockedLoopHook(uint64_t threshold, Cb&& cb, Data&&... data) { // _2 - std::string info UserData* user_data = new (std::nothrow) UserData(std::bind( std::forward(cb), _1, _2, std::forward(data)...)); + if (user_data == nullptr) { return UV_ENOMEM; }