Skip to content

Commit 26962d7

Browse files
committed
Move literals into image state
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
1 parent a9e67cd commit 26962d7

5 files changed

Lines changed: 87 additions & 49 deletions

File tree

runtime-light/state/image-state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "runtime-light/allocator/allocator-state.h"
1919
#include "runtime-light/core/reference-counter/reference-counter-functions.h"
2020
#include "runtime-light/k2-platform/k2-api.h"
21+
#include "runtime-light/stdlib/curl/curl-state.h"
2122
#include "runtime-light/stdlib/diagnostics/logs.h"
2223
#include "runtime-light/stdlib/file/file-system-state.h"
2324
#include "runtime-light/stdlib/math/math-state.h"
@@ -46,6 +47,7 @@ struct ImageState final : private vk::not_copyable {
4647
TimeImageState time_image_state;
4748
MathImageState math_image_state;
4849
RpcImageState rpc_image_state;
50+
CurlImageState curl_image_stage;
4951

5052
ImageState() noexcept {
5153
if (const int64_t sysconf_max_buffer_size{k2::sysconf(_SC_GETPW_R_SIZE_MAX)}; sysconf_max_buffer_size != -1) {

runtime-light/stdlib/curl/curl-multi-functions.h

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ inline auto f$curl_multi_add_handle(kphp::web::curl::multi_type multi_id, kphp::
4646

4747
auto res{co_await kphp::forks::id_managed(kphp::web::composite_transfer_add(kphp::web::composite_transfer{multi_id}, kphp::web::simple_transfer{easy_id}))};
4848
if (!res.has_value()) [[unlikely]] {
49-
multi_ctx.set_errno(res.error().code, res.error().description);
49+
multi_ctx.set_errno(res.error().code);
5050
kphp::web::curl::print_error("could not add a curl easy handler into multi handle", std::move(res.error()));
5151
co_return multi_ctx.error_code;
5252
}
@@ -67,7 +67,7 @@ inline auto f$curl_multi_remove_handle(kphp::web::curl::multi_type multi_id,
6767
auto res{
6868
co_await kphp::forks::id_managed(kphp::web::composite_transfer_remove(kphp::web::composite_transfer{multi_id}, kphp::web::simple_transfer{easy_id}))};
6969
if (!res.has_value()) [[unlikely]] {
70-
multi_ctx.set_errno(res.error().code, res.error().description);
70+
multi_ctx.set_errno(res.error().code);
7171
kphp::web::curl::print_error("could not remove a curl easy handler from multi handle", std::move(res.error()));
7272
co_return multi_ctx.error_code;
7373
}
@@ -100,7 +100,7 @@ inline auto f$curl_multi_setopt(kphp::web::curl::multi_type multi_id, int64_t op
100100
return true;
101101
}
102102
default:
103-
multi_ctx.set_errno(kphp::web::curl::CURLME::UNKNOWN_OPTION, "a libcurl function was given an incorrect PROXYTYPE kind");
103+
multi_ctx.set_errno(kphp::web::curl::CURLME::UNKNOWN_OPTION);
104104
return false;
105105
}
106106
}
@@ -130,7 +130,7 @@ inline auto f$curl_multi_exec(kphp::web::curl::multi_type multi_id, int64_t& sti
130130
auto res{co_await kphp::forks::id_managed(kphp::web::composite_transfer_perform(kphp::web::composite_transfer{multi_id}))};
131131
auto& multi_ctx{curl_state.multi_ctx.get_or_init(multi_id)};
132132
if (!res.has_value()) [[unlikely]] {
133-
multi_ctx.set_errno(res.error().code, res.error().description);
133+
multi_ctx.set_errno(res.error().code);
134134
kphp::web::curl::print_error("could not execute curl multi handle", std::move(res.error()));
135135
co_return multi_ctx.error_code;
136136
}
@@ -148,7 +148,7 @@ inline auto f$curl_multi_getcontent(kphp::web::curl::easy_type easy_id) noexcept
148148
if (easy_ctx.return_transfer) {
149149
auto res{co_await kphp::forks::id_managed(kphp::web::simple_transfer_get_response(kphp::web::simple_transfer{easy_id}))};
150150
if (!res.has_value()) [[unlikely]] {
151-
easy_ctx.set_errno(res.error().code, res.error().description);
151+
easy_ctx.set_errno(res.error().code);
152152
kphp::web::curl::print_error("could not get response of curl easy handle", std::move(res.error()));
153153
co_return false;
154154
}
@@ -165,7 +165,7 @@ inline auto f$curl_multi_close(kphp::web::curl::multi_type multi_id) noexcept ->
165165
auto& multi_ctx{curl_state.multi_ctx.get_or_init(multi_id)};
166166
auto res{co_await kphp::forks::id_managed(kphp::web::composite_transfer_close(kphp::web::composite_transfer{multi_id}))};
167167
if (!res.has_value()) [[unlikely]] {
168-
multi_ctx.set_errno(res.error().code, res.error().description);
168+
multi_ctx.set_errno(res.error().code);
169169
kphp::web::curl::print_error("could not close curl multi handle", std::move(res.error()));
170170
co_return;
171171
}
@@ -175,48 +175,34 @@ inline auto f$curl_multi_close(kphp::web::curl::multi_type multi_id) noexcept ->
175175

176176
inline auto f$curl_multi_strerror(int64_t error_num) noexcept -> Optional<string> {
177177
switch (static_cast<kphp::web::curl::CURLME>(error_num)) {
178-
case kphp::web::curl::CURLME::CALL_MULTI_PERFORM: {
179-
return string{"Please call curl_multi_perform() soon"};
180-
}
181-
case kphp::web::curl::CURLME::OK: {
182-
return string{"No error"};
183-
}
184-
case kphp::web::curl::CURLME::BAD_HANDLE: {
185-
return string{"Invalid multi handle"};
186-
}
187-
case kphp::web::curl::CURLME::BAD_EASY_HANDLE: {
188-
return string{"Invalid easy handle"};
189-
}
190-
case kphp::web::curl::CURLME::OUT_OF_MEMORY: {
191-
return string{"Out of memory"};
192-
}
193-
case kphp::web::curl::CURLME::INTERNAL_ERROR: {
194-
return string{"Internal error"};
195-
}
196-
case kphp::web::curl::CURLME::BAD_SOCKET: {
197-
return string{"Invalid socket argument"};
198-
}
199-
case kphp::web::curl::CURLME::UNKNOWN_OPTION: {
200-
return string{"Unknown option"};
201-
}
202-
case kphp::web::curl::CURLME::ADDED_ALREADY: {
203-
return string{"The easy handle is already added to a multi handle"};
204-
}
205-
case kphp::web::curl::CURLME::RECURSIVE_API_CALL: {
206-
return string{"API function called from within callback"};
207-
}
208-
case kphp::web::curl::CURLME::WAKEUP_FAILURE: {
209-
return string{"Wakeup is unavailable or failed"};
210-
}
211-
case kphp::web::curl::CURLME::BAD_FUNCTION_ARGUMENT: {
212-
return string{"A libcurl function was given a bad argument"};
213-
}
214-
case kphp::web::curl::CURLME::ABORTED_BY_CALLBACK: {
215-
return string{"Operation was aborted by an application callback"};
216-
}
217-
case kphp::web::curl::CURLME::UNRECOVERABLE_POLL: {
218-
return string{"Unrecoverable error in select/poll"};
219-
}
178+
case kphp::web::curl::CURLME::CALL_MULTI_PERFORM:
179+
return CurlImageState::get().CURLME_CALL_MULTI_PERFORM;
180+
case kphp::web::curl::CURLME::OK:
181+
return CurlImageState::get().CURLME_OK;
182+
case kphp::web::curl::CURLME::BAD_HANDLE:
183+
return CurlImageState::get().CURLME_BAD_HANDLE;
184+
case kphp::web::curl::CURLME::BAD_EASY_HANDLE:
185+
return CurlImageState::get().CURLME_BAD_EASY_HANDLE;
186+
case kphp::web::curl::CURLME::OUT_OF_MEMORY:
187+
return CurlImageState::get().CURLME_OUT_OF_MEMORY;
188+
case kphp::web::curl::CURLME::INTERNAL_ERROR:
189+
return CurlImageState::get().CURLME_INTERNAL_ERROR;
190+
case kphp::web::curl::CURLME::BAD_SOCKET:
191+
return CurlImageState::get().CURLME_BAD_SOCKET;
192+
case kphp::web::curl::CURLME::UNKNOWN_OPTION:
193+
return CurlImageState::get().CURLME_UNKNOWN_OPTION;
194+
case kphp::web::curl::CURLME::ADDED_ALREADY:
195+
return CurlImageState::get().CURLME_ADDED_ALREADY;
196+
case kphp::web::curl::CURLME::RECURSIVE_API_CALL:
197+
return CurlImageState::get().CURLME_RECURSIVE_API_CALL;
198+
case kphp::web::curl::CURLME::WAKEUP_FAILURE:
199+
return CurlImageState::get().CURLME_WAKEUP_FAILURE;
200+
case kphp::web::curl::CURLME::BAD_FUNCTION_ARGUMENT:
201+
return CurlImageState::get().CURLME_BAD_FUNCTION_ARGUMENT;
202+
case kphp::web::curl::CURLME::ABORTED_BY_CALLBACK:
203+
return CurlImageState::get().CURLME_ABORTED_BY_CALLBACK;
204+
case kphp::web::curl::CURLME::UNRECOVERABLE_POLL:
205+
return CurlImageState::get().CURLME_UNRECOVERABLE_POLL;
220206
default:
221207
return {};
222208
}
@@ -240,7 +226,7 @@ inline auto f$curl_multi_select(kphp::web::curl::multi_type multi_id, double tim
240226
auto res{co_await kphp::forks::id_managed(kphp::web::composite_transfer_wait_updates(
241227
kphp::web::composite_transfer{multi_id}, std::chrono::duration_cast<std::chrono::seconds>(std::chrono::duration<double>{timeout})))};
242228
if (!res.has_value()) [[unlikely]] {
243-
multi_ctx.set_errno(res.error().code, res.error().description);
229+
multi_ctx.set_errno(res.error().code);
244230
kphp::web::curl::print_error("could not select curl multi handle", std::move(res.error()));
245231
co_return multi_ctx.error_code;
246232
}

runtime-light/stdlib/curl/curl-state.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
#include "runtime-light/stdlib/curl/curl-state.h"
66

7+
#include "runtime-light/state/image-state.h"
78
#include "runtime-light/state/instance-state.h"
89

910
CurlInstanceState& CurlInstanceState::get() noexcept {
1011
return InstanceState::get().curl_instance_state;
1112
}
13+
14+
const CurlImageState& CurlImageState::get() noexcept {
15+
return ImageState::get().curl_image_stage;
16+
}

runtime-light/stdlib/curl/curl-state.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "common/mixin/not_copyable.h"
88
#include "runtime-common/core/allocator/script-allocator.h"
99
#include "runtime-common/core/std/containers.h"
10+
#include "runtime-light/core/reference-counter/reference-counter-functions.h"
1011
#include "runtime-light/stdlib/curl/curl-context.h"
1112
#include "runtime-light/stdlib/curl/defs.h"
1213

@@ -58,3 +59,28 @@ inline auto CurlInstanceState::multi_ctx::get_or_init(kphp::web::curl::multi_typ
5859
inline auto CurlInstanceState::multi_ctx::has(kphp::web::curl::multi_type multi_id) noexcept -> bool {
5960
return ctx.find(multi_id) != ctx.end();
6061
}
62+
63+
struct CurlImageState final : private vk::not_copyable {
64+
string CURLME_CALL_MULTI_PERFORM{kphp::web::curl::details::CURLME_CALL_MULTI_PERFORM.data(), kphp::web::curl::details::CURLME_CALL_MULTI_PERFORM.size()};
65+
string CURLME_OK{kphp::web::curl::details::CURLME_OK.data(), kphp::web::curl::details::CURLME_OK.size()};
66+
string CURLME_BAD_HANDLE{kphp::web::curl::details::CURLME_BAD_HANDLE.data(), kphp::web::curl::details::CURLME_BAD_HANDLE.size()};
67+
string CURLME_BAD_EASY_HANDLE{kphp::web::curl::details::CURLME_BAD_EASY_HANDLE.data(), kphp::web::curl::details::CURLME_BAD_EASY_HANDLE.size()};
68+
string CURLME_OUT_OF_MEMORY{kphp::web::curl::details::CURLME_OUT_OF_MEMORY.data(), kphp::web::curl::details::CURLME_OUT_OF_MEMORY.size()};
69+
string CURLME_INTERNAL_ERROR{kphp::web::curl::details::CURLME_INTERNAL_ERROR.data(), kphp::web::curl::details::CURLME_INTERNAL_ERROR.size()};
70+
string CURLME_BAD_SOCKET{kphp::web::curl::details::CURLME_BAD_SOCKET.data(), kphp::web::curl::details::CURLME_BAD_SOCKET.size()};
71+
string CURLME_UNKNOWN_OPTION{kphp::web::curl::details::CURLME_UNKNOWN_OPTION.data(), kphp::web::curl::details::CURLME_UNKNOWN_OPTION.size()};
72+
string CURLME_ADDED_ALREADY{kphp::web::curl::details::CURLME_ADDED_ALREADY.data(), kphp::web::curl::details::CURLME_ADDED_ALREADY.size()};
73+
string CURLME_RECURSIVE_API_CALL{kphp::web::curl::details::CURLME_RECURSIVE_API_CALL.data(), kphp::web::curl::details::CURLME_RECURSIVE_API_CALL.size()};
74+
string CURLME_WAKEUP_FAILURE{kphp::web::curl::details::CURLME_WAKEUP_FAILURE.data(), kphp::web::curl::details::CURLME_WAKEUP_FAILURE.size()};
75+
string CURLME_BAD_FUNCTION_ARGUMENT{kphp::web::curl::details::CURLME_BAD_FUNCTION_ARGUMENT.data(),
76+
kphp::web::curl::details::CURLME_BAD_FUNCTION_ARGUMENT.size()};
77+
string CURLME_ABORTED_BY_CALLBACK{kphp::web::curl::details::CURLME_ABORTED_BY_CALLBACK.data(), kphp::web::curl::details::CURLME_ABORTED_BY_CALLBACK.size()};
78+
string CURLME_UNRECOVERABLE_POLL{kphp::web::curl::details::CURLME_UNRECOVERABLE_POLL.data(), kphp::web::curl::details::CURLME_UNRECOVERABLE_POLL.size()};
79+
80+
CurlImageState() noexcept {
81+
php_assert((kphp::core::set_reference_counter_recursive(CURLME_CALL_MULTI_PERFORM, ExtraRefCnt::for_global_const),
82+
kphp::core::is_reference_counter_recursive(CURLME_CALL_MULTI_PERFORM, ExtraRefCnt::for_global_const)));
83+
}
84+
85+
static const CurlImageState& get() noexcept;
86+
};

runtime-light/stdlib/curl/defs.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,22 @@ enum class CURLPIPE : uint8_t {
283283
};
284284

285285
} // namespace kphp::web::curl
286+
287+
namespace kphp::web::curl::details {
288+
289+
inline constexpr std::string_view CURLME_CALL_MULTI_PERFORM{"Please call curl_multi_perform() soon"};
290+
inline constexpr std::string_view CURLME_OK{"No error"};
291+
inline constexpr std::string_view CURLME_BAD_HANDLE{"Invalid multi handle"};
292+
inline constexpr std::string_view CURLME_BAD_EASY_HANDLE{"Invalid easy handle"};
293+
inline constexpr std::string_view CURLME_OUT_OF_MEMORY{"Out of memory"};
294+
inline constexpr std::string_view CURLME_INTERNAL_ERROR{"Internal error"};
295+
inline constexpr std::string_view CURLME_BAD_SOCKET{"Invalid socket argument"};
296+
inline constexpr std::string_view CURLME_UNKNOWN_OPTION{"Unknown option"};
297+
inline constexpr std::string_view CURLME_ADDED_ALREADY{"The easy handle is already added to a multi handle"};
298+
inline constexpr std::string_view CURLME_RECURSIVE_API_CALL{"API function called from within callback"};
299+
inline constexpr std::string_view CURLME_WAKEUP_FAILURE{"Wakeup is unavailable or failed"};
300+
inline constexpr std::string_view CURLME_BAD_FUNCTION_ARGUMENT{"A libcurl function was given a bad argument"};
301+
inline constexpr std::string_view CURLME_ABORTED_BY_CALLBACK{"Operation was aborted by an application callback"};
302+
inline constexpr std::string_view CURLME_UNRECOVERABLE_POLL{"Unrecoverable error in select/poll"};
303+
304+
} // namespace kphp::web::curl::details

0 commit comments

Comments
 (0)