This is an automated email from the ASF dual-hosted git repository. cmcfarlen pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit b53d00980fd2df2646513810e5310784c1465fbf Author: Bryan Call <[email protected]> AuthorDate: Wed Sep 24 08:32:47 2025 -0700 Convert const and #define to constexpr (#12527) (cherry picked from commit c3f983f572795b32226aed7583b0839e78a6b5bf) --- src/proxy/hdrs/HdrHeap.cc | 2 +- src/proxy/hdrs/HdrToken.cc | 2 +- src/proxy/hdrs/MIME.cc | 22 +++++++++++----------- src/proxy/hdrs/URL.cc | 13 ++++++------- src/proxy/hdrs/test_urlhash.cc | 4 ++-- src/proxy/http/HttpSM.cc | 14 +++++++------- 6 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/proxy/hdrs/HdrHeap.cc b/src/proxy/hdrs/HdrHeap.cc index 03ad762728..c639337fb1 100644 --- a/src/proxy/hdrs/HdrHeap.cc +++ b/src/proxy/hdrs/HdrHeap.cc @@ -57,7 +57,7 @@ DbgCtl dbg_ctl_http{"http"}; void obj_describe(HdrHeapObjImpl *obj, bool recurse) { - static const char *obj_names[] = {"EMPTY", "RAW", "URL", "HTTP_HEADER", "MIME_HEADER", "FIELD_BLOCK"}; + static constexpr const char *obj_names[] = {"EMPTY", "RAW", "URL", "HTTP_HEADER", "MIME_HEADER", "FIELD_BLOCK"}; Dbg(dbg_ctl_http, "%s %p: [T: %d, L: %4d, OBJFLAGS: %X] ", obj_names[obj->m_type], obj, obj->m_type, obj->m_length, obj->m_obj_flags); diff --git a/src/proxy/hdrs/HdrToken.cc b/src/proxy/hdrs/HdrToken.cc index 18b05f23ea..35c7573de4 100644 --- a/src/proxy/hdrs/HdrToken.cc +++ b/src/proxy/hdrs/HdrToken.cc @@ -276,7 +276,7 @@ DFA *hdrtoken_strs_dfa = nullptr; * * ***********************************************************************/ -#define HDRTOKEN_HASH_TABLE_SIZE 65536 +static constexpr size_t HDRTOKEN_HASH_TABLE_SIZE = 65536; struct HdrTokenHashBucket { const char *wks; diff --git a/src/proxy/hdrs/MIME.cc b/src/proxy/hdrs/MIME.cc index 634e1bf3f7..35dc5bf2b4 100644 --- a/src/proxy/hdrs/MIME.cc +++ b/src/proxy/hdrs/MIME.cc @@ -54,11 +54,11 @@ using swoc::TextView; static DFA *day_names_dfa = nullptr; static DFA *month_names_dfa = nullptr; -static const char *day_names[] = { +static constexpr const char *day_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", }; -static const char *month_names[] = { +static constexpr const char *month_names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; @@ -2684,8 +2684,8 @@ mime_hdr_describe(HdrHeapObjImpl *raw, bool recurse) void mime_field_block_describe(HdrHeapObjImpl *raw, bool /* recurse ATS_UNUSED */) { - unsigned int i; - static const char *readiness_names[] = {"EMPTY", "DETACHED", "LIVE", "DELETED"}; + unsigned int i; + static constexpr const char *readiness_names[] = {"EMPTY", "DETACHED", "LIVE", "DELETED"}; MIMEFieldBlockImpl *obj = (MIMEFieldBlockImpl *)raw; @@ -2935,7 +2935,7 @@ mime_format_int64(char *buf, int64_t val, size_t buf_len) void mime_days_since_epoch_to_mdy_slowcase(time_t days_since_jan_1_1970, int *m_return, int *d_return, int *y_return) { - static const int DAYS_OFFSET = 25508; + static constexpr int DAYS_OFFSET = 25508; static const char months[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -2951,7 +2951,7 @@ mime_days_since_epoch_to_mdy_slowcase(time_t days_since_jan_1_1970, int *m_retur 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - static const int days[12] = {305, 336, -1, 30, 60, 91, 121, 152, 183, 213, 244, 274}; + static constexpr int days[12] = {305, 336, -1, 30, 60, 91, 121, 152, 183, 213, 244, 274}; time_t mday, year, month, d, dp; @@ -3023,12 +3023,12 @@ int mime_format_date(char *buffer, time_t value) { // must be 3 characters! - static const char *daystrs[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + static constexpr const char *daystrs[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // must be 3 characters! - static const char *monthstrs[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + static constexpr const char *monthstrs[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - static const char *digitstrs[] = { + static constexpr const char *digitstrs[] = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", @@ -3420,8 +3420,8 @@ mime_parse_rfc822_date_fastcase(const char *buf, int length, struct tm *tp) time_t mime_parse_date(const char *buf, const char *end) { - static const int DAYS_OFFSET = 25508; - static const int days[12] = {305, 336, -1, 30, 60, 91, 121, 152, 183, 213, 244, 274}; + static constexpr int DAYS_OFFSET = 25508; + static constexpr int days[12] = {305, 336, -1, 30, 60, 91, 121, 152, 183, 213, 244, 274}; struct tm tp; time_t t; diff --git a/src/proxy/hdrs/URL.cc b/src/proxy/hdrs/URL.cc index 0fbc39ab40..e869d71c0c 100644 --- a/src/proxy/hdrs/URL.cc +++ b/src/proxy/hdrs/URL.cc @@ -97,7 +97,10 @@ namespace { // Whether we should implement url_CryptoHash_get() using url_CryptoHash_get_fast(). Note that // url_CryptoHash_get_fast() does NOT produce the same result as url_CryptoHash_get_general(). -int url_hash_method = 0; +constexpr int url_hash_method = 0; + +// Buffer size for url_CryptoHash_get() and url_CryptoHash_get_92(). +constexpr size_t BUFSIZE = 4096; DbgCtl dbg_ctl_http{"http"}; DbgCtl dbg_ctl_url_cachekey{"url_cachekey"}; @@ -1716,8 +1719,6 @@ memcpy_tolower(char *d, const char *s, int n) } } -#define BUFSIZE 4096 - // fast path for CryptoHash, HTTP, no user/password/params/query, // no buffer overflow, no unescaping needed @@ -1864,7 +1865,7 @@ url_CryptoHash_get(const URLImpl *url, CryptoHash *hash, bool ignore_query, cach URLHashContext ctx; if ((url_hash_method != 0) && (url->m_url_type == URL_TYPE_HTTP) && ((url->m_len_user + url->m_len_password + (ignore_query ? 0 : url->m_len_query)) == 0) && - (3 + 1 + 1 + 1 + 1 + 1 + 2 + url->m_len_scheme + url->m_len_host + url->m_len_path < BUFSIZE) && + (10u + url->m_len_scheme + url->m_len_host + url->m_len_path < BUFSIZE) && (memchr(url->m_ptr_host, '%', url->m_len_host) == nullptr) && (memchr(url->m_ptr_path, '%', url->m_len_path) == nullptr)) { url_CryptoHash_get_fast(url, ctx, hash, generation); #ifdef DEBUG @@ -1981,7 +1982,7 @@ url_CryptoHash_get_92(const URLImpl *url, CryptoHash *hash, bool ignore_query, c URLHashContext ctx; if ((url_hash_method != 0) && (url->m_url_type == URL_TYPE_HTTP) && ((url->m_len_user + url->m_len_password + url->m_len_params + (ignore_query ? 0 : url->m_len_query)) == 0) && - (3 + 1 + 1 + 1 + 1 + 1 + 2 + url->m_len_scheme + url->m_len_host + url->m_len_path < BUFSIZE) && + (10u + url->m_len_scheme + url->m_len_host + url->m_len_path < BUFSIZE) && (memchr(url->m_ptr_host, '%', url->m_len_host) == nullptr) && (memchr(url->m_ptr_path, '%', url->m_len_path) == nullptr)) { url_CryptoHash_get_fast(url, ctx, hash, generation); #ifdef DEBUG @@ -1994,8 +1995,6 @@ url_CryptoHash_get_92(const URLImpl *url, CryptoHash *hash, bool ignore_query, c } } -#undef BUFSIZE - /*------------------------------------------------------------------------- -------------------------------------------------------------------------*/ diff --git a/src/proxy/hdrs/test_urlhash.cc b/src/proxy/hdrs/test_urlhash.cc index 488400bc45..c93ac7a1d0 100644 --- a/src/proxy/hdrs/test_urlhash.cc +++ b/src/proxy/hdrs/test_urlhash.cc @@ -34,8 +34,8 @@ static void test_url() { - static const char *strs[] = {"http://npdev:19080/1.6664000000/4000", "http://npdev:19080/1.8666000000/4000"}; - static int nstrs = sizeof(strs) / sizeof(strs[0]); + static constexpr const char *strs[] = {"http://npdev:19080/1.6664000000/4000", "http://npdev:19080/1.8666000000/4000"}; + static constexpr int nstrs = sizeof(strs) / sizeof(strs[0]); int err, failed; URL url; diff --git a/src/proxy/http/HttpSM.cc b/src/proxy/http/HttpSM.cc index a85928dd22..d690333460 100644 --- a/src/proxy/http/HttpSM.cc +++ b/src/proxy/http/HttpSM.cc @@ -69,9 +69,9 @@ using namespace std::literals; -#define DEFAULT_RESPONSE_BUFFER_SIZE_INDEX 6 // 8K -#define DEFAULT_REQUEST_BUFFER_SIZE_INDEX 6 // 8K -#define MIN_CONFIG_BUFFER_SIZE_INDEX 5 // 4K +static constexpr int DEFAULT_RESPONSE_BUFFER_SIZE_INDEX = 6; // 8K +static constexpr int DEFAULT_REQUEST_BUFFER_SIZE_INDEX = 6; // 8K +static constexpr int MIN_CONFIG_BUFFER_SIZE_INDEX = 5; // 4K #define hsm_release_assert(EX) \ { \ @@ -122,11 +122,11 @@ static DbgCtl dbg_ctl_ssl_early_data{"ssl_early_data"}; static DbgCtl dbg_ctl_ssl_sni{"ssl_sni"}; static DbgCtl dbg_ctl_url_rewrite{"url_rewrite"}; -static const int sub_header_size = sizeof("Content-type: ") - 1 + 2 + sizeof("Content-range: bytes ") - 1 + 4; -static const int boundary_size = 2 + sizeof("RANGE_SEPARATOR") - 1 + 2; +static constexpr int sub_header_size = sizeof("Content-type: ") - 1 + 2 + sizeof("Content-range: bytes ") - 1 + 4; +static constexpr int boundary_size = 2 + sizeof("RANGE_SEPARATOR") - 1 + 2; -static const char *str_100_continue_response = "HTTP/1.1 100 Continue\r\n\r\n"; -static const int len_100_continue_response = strlen(str_100_continue_response); +static const char *str_100_continue_response = "HTTP/1.1 100 Continue\r\n\r\n"; +static constexpr int len_100_continue_response = sizeof("HTTP/1.1 100 Continue\r\n\r\n") - 1; // Handy alias for short (single line) message generation. using lbw = swoc::LocalBufferWriter<256>;
