src: Add util::array_size

This commit is contained in:
Tatsuhiro Tsujikawa 2014-08-28 00:45:12 +09:00
parent ec0a2e7cca
commit 8890e593e6
11 changed files with 27 additions and 20 deletions

View File

@ -969,8 +969,7 @@ int main(int argc, char **argv)
worker.stats.bytes_head += stats.bytes_head;
worker.stats.bytes_body += stats.bytes_body;
for(size_t i = 0; i < sizeof(stats.status) / sizeof(stats.status[0]);
++i) {
for(size_t i = 0; i < util::array_size(stats.status); ++i) {
worker.stats.status[i] += stats.status[i];
}
}

View File

@ -130,7 +130,7 @@ void Http2Session::on_connect()
iv[1].value = (1 << client_->worker->config->window_bits) - 1;
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv,
sizeof(iv) / sizeof(iv[0]));
util::array_size(iv));
assert(rv == 0);

View File

@ -27,6 +27,9 @@
#include <cassert>
#include "h2load.h"
#include "util.h"
using namespace nghttp2;
namespace h2load {
@ -138,7 +141,7 @@ void SpdySession::on_connect()
iv[0].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
iv[0].value = (1 << client_->worker->config->window_bits);
spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE, iv,
sizeof(iv) / sizeof(iv[0]));
util::array_size(iv));
auto config = client_->worker->config;

View File

@ -160,7 +160,7 @@ const char *DISALLOWED_HD[] = {
} // namespace
namespace {
size_t DISALLOWED_HDLEN = sizeof(DISALLOWED_HD)/sizeof(DISALLOWED_HD[0]);
auto DISALLOWED_HDLEN = util::array_size(DISALLOWED_HD);
} // namespace
namespace {
@ -173,8 +173,7 @@ const char *REQUEST_PSEUDO_HD[] = {
} // namespace
namespace {
size_t REQUEST_PSEUDO_HDLEN =
sizeof(REQUEST_PSEUDO_HD) / sizeof(REQUEST_PSEUDO_HD[0]);
auto REQUEST_PSEUDO_HDLEN = util::array_size(REQUEST_PSEUDO_HD);
} // namespace
namespace {
@ -184,8 +183,7 @@ const char *RESPONSE_PSEUDO_HD[] = {
} // namespace
namespace {
size_t RESPONSE_PSEUDO_HDLEN =
sizeof(RESPONSE_PSEUDO_HD) / sizeof(RESPONSE_PSEUDO_HD[0]);
auto RESPONSE_PSEUDO_HDLEN = util::array_size(RESPONSE_PSEUDO_HD);
} // namespace
namespace {
@ -205,7 +203,7 @@ const char *IGN_HD[] = {
} // namespace
namespace {
size_t IGN_HDLEN = sizeof(IGN_HD)/sizeof(IGN_HD[0]);
auto IGN_HDLEN = util::array_size(IGN_HD);
} // namespace
namespace {
@ -224,7 +222,7 @@ const char *HTTP1_IGN_HD[] = {
} // namespace
namespace {
size_t HTTP1_IGN_HDLEN = sizeof(HTTP1_IGN_HD)/sizeof(HTTP1_IGN_HD[0]);
auto HTTP1_IGN_HDLEN = util::array_size(HTTP1_IGN_HD);
} // namespace
bool name_less(const Headers::value_type& lhs,

View File

@ -1425,7 +1425,7 @@ int Http2Session::on_connect()
entry[2].value = (1 << get_config()->http2_downstream_window_bits) - 1;
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry,
sizeof(entry)/sizeof(nghttp2_settings_entry));
util::array_size(entry));
if(rv != 0) {
return -1;
}

View File

@ -682,8 +682,7 @@ Http2Upstream::Http2Upstream(ClientHandler *handler)
entry[1].value = (1 << get_config()->http2_upstream_window_bits) - 1;
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE,
entry,
sizeof(entry)/sizeof(nghttp2_settings_entry));
entry, util::array_size(entry));
if(rv != 0) {
ULOG(ERROR, this) << "nghttp2_submit_settings() returned error: "
<< nghttp2_strerror(rv);

View File

@ -67,7 +67,7 @@ void Log::set_severity_level(int severity)
int Log::set_severity_level_by_name(const char *name)
{
for(size_t i = 0, max = sizeof(SEVERITY_STR)/sizeof(char*); i < max; ++i) {
for(size_t i = 0, max = util::array_size(SEVERITY_STR); i < max; ++i) {
if(strcmp(SEVERITY_STR[i], name) == 0) {
severity_thres_ = i;
return 0;

View File

@ -480,8 +480,7 @@ SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler)
entry[1].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
rv = spdylay_submit_settings
(session_, SPDYLAY_FLAG_SETTINGS_NONE,
entry, sizeof(entry)/sizeof(spdylay_settings_entry));
(session_, SPDYLAY_FLAG_SETTINGS_NONE, entry, util::array_size(entry));
assert(rv == 0);
if(version >= SPDYLAY_PROTO_SPDY3_1 &&

View File

@ -196,7 +196,7 @@ int alpn_select_proto_cb(SSL *ssl,
namespace {
const char *tls_names[] = { "TLSv1.2", "TLSv1.1", "TLSv1.0", "SSLv3" };
const size_t tls_namelen = sizeof(tls_names)/sizeof(tls_names[0]);
const size_t tls_namelen = util::array_size(tls_names);
const long int tls_masks[] = { SSL_OP_NO_TLSv1_2, SSL_OP_NO_TLSv1_1,
SSL_OP_NO_TLSv1, SSL_OP_NO_SSLv3 };
} // namespace

View File

@ -27,6 +27,9 @@
#include <CUnit/CUnit.h>
#include "shrpx_ssl.h"
#include "util.h"
using namespace nghttp2;
namespace shrpx {
@ -55,7 +58,7 @@ void test_shrpx_ssl_create_lookup_tree(void)
"*.foo.bar", // oo.bar is suffix of *.foo.bar
"oo.bar"
};
int num = sizeof(ctxs)/sizeof(ctxs[0]);
int num = util::array_size(ctxs);
for(int i = 0; i < num; ++i) {
ssl::cert_lookup_tree_add_cert(tree, ctxs[i], hostnames[i],
strlen(hostnames[i]));
@ -100,7 +103,7 @@ void test_shrpx_ssl_create_lookup_tree(void)
SSL_CTX_new(SSLv23_method()),
SSL_CTX_new(SSLv23_method())};
const char *names[] = { "rab", "zab", "zzub", "ab" };
num = sizeof(ctxs2)/sizeof(ctxs2[0]);
num = util::array_size(ctxs2);
tree = ssl::cert_lookup_tree_new();
for(int i = 0; i < num; ++i) {
ssl::cert_lookup_tree_add_cert(tree, ctxs2[i], names[i], strlen(names[i]));

View File

@ -45,6 +45,12 @@ namespace nghttp2 {
namespace util {
template<typename T, size_t N>
constexpr size_t array_size(T (&)[N])
{
return N;
}
template<typename T>
class auto_delete {
private: