This is an automated email from the ASF dual-hosted git repository.
bcall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new c3f983f572 Convert const and #define to constexpr (#12527)
c3f983f572 is described below
commit c3f983f572795b32226aed7583b0839e78a6b5bf
Author: Bryan Call <[email protected]>
AuthorDate: Wed Sep 24 08:32:47 2025 -0700
Convert const and #define to constexpr (#12527)
---
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 8279881d6b..1aab23b93f 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 3fde664e1c..c57089ced5 100644
--- a/src/proxy/hdrs/HdrToken.cc
+++ b/src/proxy/hdrs/HdrToken.cc
@@ -289,7 +289,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 f46fcdbd31..7e237331ca 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",
};
@@ -2495,8 +2495,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;
@@ -2754,7 +2754,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,
@@ -2770,7 +2770,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;
@@ -2842,12 +2842,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",
@@ -3239,8 +3239,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 042bce8964..1c0b0c1ab8 100644
--- a/src/proxy/hdrs/URL.cc
+++ b/src/proxy/hdrs/URL.cc
@@ -78,7 +78,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"};
@@ -1691,8 +1694,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
@@ -1839,7 +1840,7 @@ url_CryptoHash_get(const URLImpl *url, CryptoHash *hash,
bool ignore_query, cach
URLHashContext ctx;
if ((url_hash_method != 0) && (url->m_url_type == URLType::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
@@ -1956,7 +1957,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 == URLType::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
@@ -1969,8 +1970,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 434044bd75..33da05895b 100644
--- a/src/proxy/http/HttpSM.cc
+++ b/src/proxy/http/HttpSM.cc
@@ -70,9 +70,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) \
{ \
@@ -123,11 +123,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>;