From 94e011240eff45eeba00456e086f87459b4eda35 Mon Sep 17 00:00:00 2001 From: "Peter D. Barnes, Jr." Date: Tue, 22 May 2018 15:10:10 -0700 Subject: [PATCH] core: Use std::size_t for buffer size/len arguments --- src/core/model/command-line.cc | 2 +- src/core/model/hash-fnv.cc | 4 +-- src/core/model/hash-function.cc | 4 +-- src/core/model/hash-function.h | 14 +++++----- src/core/model/hash-murmur3.cc | 47 +++++++++++++++++--------------- src/core/model/hash-murmur3.h | 8 +++--- src/core/model/hash.h | 16 +++++------ src/core/model/system-path.cc | 2 +- src/core/test/hash-test-suite.cc | 6 ++-- 9 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/core/model/command-line.cc b/src/core/model/command-line.cc index 217462e23..7cc8de493 100644 --- a/src/core/model/command-line.cc +++ b/src/core/model/command-line.cc @@ -555,7 +555,7 @@ CommandLine::AddValue (const std::string &name, { NS_LOG_FUNCTION (this << name << attributePath); // Attribute name is last token - size_t colon = attributePath.rfind ("::"); + std::size_t colon = attributePath.rfind ("::"); const std::string typeName = attributePath.substr (0, colon); NS_LOG_DEBUG ("typeName: '" << typeName << "', colon: " << colon); diff --git a/src/core/model/hash-fnv.cc b/src/core/model/hash-fnv.cc index 357ca585a..77d15e64e 100644 --- a/src/core/model/hash-fnv.cc +++ b/src/core/model/hash-fnv.cc @@ -752,7 +752,7 @@ Fnv1a::Fnv1a () } uint32_t -Fnv1a::GetHash32 (const char * buffer, const size_t size) +Fnv1a::GetHash32 (const char * buffer, const std::size_t size) { m_hash32 = Fnv1aImplementation::fnv_32a_buf ((void *)buffer, size, m_hash32); @@ -760,7 +760,7 @@ Fnv1a::GetHash32 (const char * buffer, const size_t size) } uint64_t -Fnv1a::GetHash64 (const char * buffer, const size_t size) +Fnv1a::GetHash64 (const char * buffer, const std::size_t size) { m_hash64 = Fnv1aImplementation::fnv_64a_buf ((void *)buffer, size, m_hash64); diff --git a/src/core/model/hash-function.cc b/src/core/model/hash-function.cc index b97d331d1..ea17ae26a 100644 --- a/src/core/model/hash-function.cc +++ b/src/core/model/hash-function.cc @@ -24,7 +24,7 @@ /** * \file * \ingroup hash - * \brief ns3::Hash::Implementation::GetHash64 defaul implementation. + * \brief ns3::Hash::Implementation::GetHash64 default implementation. */ @@ -35,7 +35,7 @@ NS_LOG_COMPONENT_DEFINE ("HashFunction"); namespace Hash { uint64_t -Implementation::GetHash64 (const char * buffer, const size_t size) +Implementation::GetHash64 (const char * buffer, const std::size_t size) { NS_LOG_WARN ("64-bit hash requested, only 32-bit implementation available"); return GetHash32 (buffer, size); diff --git a/src/core/model/hash-function.h b/src/core/model/hash-function.h index 8cd254621..07efe8d98 100644 --- a/src/core/model/hash-function.h +++ b/src/core/model/hash-function.h @@ -61,7 +61,7 @@ public: * \param [in] size Length of the buffer, in bytes. * \return 32-bit hash of the buffer. */ - virtual uint32_t GetHash32 (const char * buffer, const size_t size) = 0; + virtual uint32_t GetHash32 (const char * buffer, const std::size_t size) = 0; /** * Compute 64-bit hash of a byte buffer. * @@ -78,7 +78,7 @@ public: * \param [in] size Length of the buffer, in bytes. * \return 64-bit hash of the buffer. */ - virtual uint64_t GetHash64 (const char * buffer, const size_t size); + virtual uint64_t GetHash64 (const char * buffer, const std::size_t size); /** * Restore initial state. */ @@ -108,8 +108,8 @@ public: * See Hash::Function::Hash32 or Hash::Function::Hash64 * @{ */ -typedef uint32_t (*Hash32Function_ptr) (const char *, const size_t); -typedef uint64_t (*Hash64Function_ptr) (const char *, const size_t); +typedef uint32_t (*Hash32Function_ptr) (const char *, const std::size_t); +typedef uint64_t (*Hash64Function_ptr) (const char *, const std::size_t); /**@}*/ /** @@ -133,7 +133,7 @@ public: * \param [in] hp Function pointer to a 32-bit hash function. */ Hash32 (Hash32Function_ptr hp) : m_fp (hp) { }; - uint32_t GetHash32 (const char * buffer, const size_t size) + uint32_t GetHash32 (const char * buffer, const std::size_t size) { return (*m_fp) (buffer, size); } @@ -157,11 +157,11 @@ public: * \param [in] hp Function pointer to a 64-bit hash function. */ Hash64 (Hash64Function_ptr hp) : m_fp (hp) { }; - uint64_t GetHash64 (const char * buffer, const size_t size) + uint64_t GetHash64 (const char * buffer, const std::size_t size) { return (*m_fp) (buffer, size); } - uint32_t GetHash32 (const char * buffer, const size_t size) + uint32_t GetHash32 (const char * buffer, const std::size_t size) { uint32_t hash32; uint64_t hash64 = GetHash64 (buffer, size); diff --git a/src/core/model/hash-murmur3.cc b/src/core/model/hash-murmur3.cc index e190c15ac..74a677044 100644 --- a/src/core/model/hash-murmur3.cc +++ b/src/core/model/hash-murmur3.cc @@ -28,6 +28,9 @@ * * Changes from the murmur3 distribution are marked with `//PDB' * In addition comment blocks have been converted to Doxygen format. + * Function arguments for buffer length which were originally + * "int len" or "int i" have been changed to "std::size_t". + * Other conversions to std::size_t are marked. */ #include "log.h" @@ -119,12 +122,12 @@ inline uint64_t rotl64 ( uint64_t x, int8_t r ) * \param [in] i Index into the block. * \returns The \c i'th word from the block. */ -inline uint32_t getblock ( const uint32_t * p, int i ) +inline uint32_t getblock ( const uint32_t * p, std::size_t i ) { return p[i]; } /** \copydoc getblock(const uint32_t*,int) */ -inline uint64_t getblock ( const uint64_t * p, int i ) +inline uint64_t getblock ( const uint64_t * p, std::size_t i ) { return p[i]; } @@ -171,7 +174,7 @@ inline uint64_t fmix ( uint64_t h ) * \param [in] seed Initial or current hash state. * \param [out] out Output hash value. */ -void MurmurHash3_x86_32_incr ( const void * key, int len, +void MurmurHash3_x86_32_incr ( const void * key, std::size_t len, uint32_t seed, void * out ); /** * Finalize a hash. @@ -180,12 +183,12 @@ void MurmurHash3_x86_32_incr ( const void * key, int len, * \param [in] seed Initial or current hash state. * \param [out] out Output hash value. */ -void MurmurHash3_x86_32_fin ( int len, +void MurmurHash3_x86_32_fin ( std::size_t len, uint32_t seed, void * out ); //PDB - incremental hashing /** \copydoc MurmurHash3_x86_32_incr() */ -void MurmurHash3_x86_32 ( const void * key, int len, +void MurmurHash3_x86_32 ( const void * key, std::size_t len, uint32_t seed, void * out ) { uint32_t h1; @@ -193,11 +196,11 @@ void MurmurHash3_x86_32 ( const void * key, int len, MurmurHash3_x86_32_fin (len, h1, out); } -void MurmurHash3_x86_32_incr ( const void * key, int len, +void MurmurHash3_x86_32_incr ( const void * key, std::size_t len, uint32_t seed, void * out ) { const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 4; + const std::size_t nblocks = len / 4; //PDB: was const int nblocks uint32_t h1 = seed; @@ -209,7 +212,7 @@ void MurmurHash3_x86_32_incr ( const void * key, int len, const uint32_t * blocks = (const uint32_t *)(data + nblocks*4); - for(int i = -nblocks; i; i++) + for(std::size_t i = -nblocks; i; i++) //PDB: was int i { uint32_t k1 = getblock(blocks,i); @@ -241,7 +244,7 @@ void MurmurHash3_x86_32_incr ( const void * key, int len, } //PDB - incremental hashing - finalization -void MurmurHash3_x86_32_fin ( int len, +void MurmurHash3_x86_32_fin ( std::size_t len, uint32_t seed, void * out ) { uint32_t h1 = seed; @@ -267,7 +270,7 @@ void MurmurHash3_x86_32_fin ( int len, * \param [in] seeds Initial or current hash state. * \param [out] out Output hash value. */ -void MurmurHash3_x86_128_incr ( const void * key, const int len, +void MurmurHash3_x86_128_incr ( const void * key, const std::size_t len, uint32_t * seeds, void * out ); /** * Finalize a hash. @@ -276,7 +279,7 @@ void MurmurHash3_x86_128_incr ( const void * key, const int len, * \param [in] seeds Initial or current hash state. * \param [out] out Output hash value. */ -void MurmurHash3_x86_128_fin ( const int len, +void MurmurHash3_x86_128_fin ( const std::size_t len, uint32_t * seeds, void * out ); //PDB - incremental hashing @@ -288,7 +291,7 @@ void MurmurHash3_x86_128_fin ( const int len, * \param [in] seed Initial or current hash state. * \param [out] out Output hash value. */ -void MurmurHash3_x86_128 ( const void * key, const int len, +void MurmurHash3_x86_128 ( const void * key, const std::size_t len, uint32_t seed, void * out ) { uint32_t seeds[4]; @@ -298,11 +301,11 @@ void MurmurHash3_x86_128 ( const void * key, const int len, MurmurHash3_x86_128_fin (len, h, out); } -void MurmurHash3_x86_128_incr ( const void * key, const int len, +void MurmurHash3_x86_128_incr ( const void * key, const std::size_t len, uint32_t * seeds, void * out ) { const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 16; + const std::size_t nblocks = len / 16; //PDB: was const int nblocks uint32_t h1 = seeds[0]; uint32_t h2 = seeds[1]; @@ -319,7 +322,7 @@ void MurmurHash3_x86_128_incr ( const void * key, const int len, const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); - for(int i = -nblocks; i; i++) + for(std::size_t i = -nblocks; i; i++) //PDB: was int i { uint32_t k1 = getblock(blocks,i*4+0); uint32_t k2 = getblock(blocks,i*4+1); @@ -386,7 +389,7 @@ void MurmurHash3_x86_128_incr ( const void * key, const int len, } //PDB - incremental hashing - finalization -void MurmurHash3_x86_128_fin ( const int len, +void MurmurHash3_x86_128_fin ( const std::size_t len, uint32_t * seeds, void * out ) { //---------- @@ -418,11 +421,11 @@ void MurmurHash3_x86_128_fin ( const int len, //----------------------------------------------------------------------------- /** \copydoc MurmurHash3_x86_32() */ -void MurmurHash3_x64_128 ( const void * key, const int len, +void MurmurHash3_x64_128 ( const void * key, const std::size_t len, const uint32_t seed, void * out ) { const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 16; + const std::size_t nblocks = len / 16; //PDB: was const int nblocks uint64_t h1 = seed; uint64_t h2 = seed; @@ -435,7 +438,7 @@ void MurmurHash3_x64_128 ( const void * key, const int len, const uint64_t * blocks = (const uint64_t *)(data); - for(int i = 0; i < nblocks; i++) + for(std::size_t i = 0; i < nblocks; i++) //PDB: was int i { uint64_t k1 = getblock(blocks,i*2+0); uint64_t k2 = getblock(blocks,i*2+1); @@ -517,11 +520,11 @@ Murmur3::Murmur3 () } uint32_t -Murmur3::GetHash32 (const char * buffer, const size_t size) +Murmur3::GetHash32 (const char * buffer, const std::size_t size) { using namespace Murmur3Implementation; - MurmurHash3_x86_32_incr (buffer, static_cast (size), + MurmurHash3_x86_32_incr (buffer, size, m_hash32, (void *)& m_hash32); m_size32 += static_cast (size); uint32_t hash; @@ -531,7 +534,7 @@ Murmur3::GetHash32 (const char * buffer, const size_t size) } uint64_t -Murmur3::GetHash64 (const char * buffer, const size_t size) +Murmur3::GetHash64 (const char * buffer, const std::size_t size) { using namespace Murmur3Implementation; diff --git a/src/core/model/hash-murmur3.h b/src/core/model/hash-murmur3.h index 060b343bd..67aaf28c5 100644 --- a/src/core/model/hash-murmur3.h +++ b/src/core/model/hash-murmur3.h @@ -71,7 +71,7 @@ public: * \param [in] size length of the buffer, in bytes * \return 32-bit hash of the buffer */ - uint32_t GetHash32 (const char * buffer, const size_t size); + uint32_t GetHash32 (const char * buffer, const std::size_t size); /** * Compute 64-bit hash of a byte buffer. * @@ -86,7 +86,7 @@ public: * \param [in] size length of the buffer, in bytes * \return 64-bit hash of the buffer */ - uint64_t GetHash64 (const char * buffer, const size_t size); + uint64_t GetHash64 (const char * buffer, const std::size_t size); /** * Restore initial state */ @@ -109,13 +109,13 @@ private: */ /**@{*/ uint32_t m_hash32; - uint32_t m_size32; + std::size_t m_size32; /**@}*/ /** murmur3 produces 128-bit hash and state; we use just the first 64-bits. */ /**@{*/ uint64_t m_hash64[2]; - uint64_t m_size64; + std::size_t m_size64; /**@}*/ }; // class Murmur3 diff --git a/src/core/model/hash.h b/src/core/model/hash.h index 3572ddb84..f5917373d 100644 --- a/src/core/model/hash.h +++ b/src/core/model/hash.h @@ -111,7 +111,7 @@ public: * \param [in] size Length of the buffer, in bytes. * \return 32-bit hash of the buffer.. */ - uint32_t GetHash32 (const char * buffer, const size_t size); + uint32_t GetHash32 (const char * buffer, const std::size_t size); /** * Compute 64-bit hash of a byte buffer. * @@ -126,7 +126,7 @@ public: * \param [in] size Length of the buffer, in bytes. * \return 64-bit hash of the buffer. */ - uint64_t GetHash64 (const char * buffer, const size_t size); + uint64_t GetHash64 (const char * buffer, const std::size_t size); /** * Compute 32-bit hash of a string. @@ -190,7 +190,7 @@ private: * \param [in] size Length of the buffer, in bytes. * \return 32-bit hash of the buffer. */ -uint32_t Hash32 (const char * buffer, const size_t size); +uint32_t Hash32 (const char * buffer, const std::size_t size); /** * \ingroup hash * @@ -200,7 +200,7 @@ uint32_t Hash32 (const char * buffer, const size_t size); * \param [in] size Length of the buffer, in bytes. * \return 64-bit hash of the buffer. */ -uint64_t Hash64 (const char * buffer, const size_t size); +uint64_t Hash64 (const char * buffer, const std::size_t size); /** * \ingroup hash @@ -236,7 +236,7 @@ namespace ns3 { inline uint32_t -Hasher::GetHash32 (const char * buffer, const size_t size) +Hasher::GetHash32 (const char * buffer, const std::size_t size) { NS_ASSERT (m_impl != 0); return m_impl->GetHash32 (buffer, size); @@ -244,7 +244,7 @@ Hasher::GetHash32 (const char * buffer, const size_t size) inline uint64_t -Hasher::GetHash64 (const char * buffer, const size_t size) +Hasher::GetHash64 (const char * buffer, const std::size_t size) { NS_ASSERT (m_impl != 0); return m_impl->GetHash64 (buffer, size); @@ -273,14 +273,14 @@ Hasher::GetHash64 (const std::string s) inline uint32_t -Hash32 (const char * buffer, const size_t size) +Hash32 (const char * buffer, const std::size_t size) { return Hasher ().GetHash32 (buffer, size); } inline uint64_t -Hash64 (const char * buffer, const size_t size) +Hash64 (const char * buffer, const std::size_t size) { return Hasher ().GetHash64 (buffer, size); } diff --git a/src/core/model/system-path.cc b/src/core/model/system-path.cc index 00965dee5..1a4cbbac3 100644 --- a/src/core/model/system-path.cc +++ b/src/core/model/system-path.cc @@ -170,7 +170,7 @@ std::string FindSelfDirectory (void) #elif defined (__FreeBSD__) { int mib[4]; - size_t bufSize = 1024; + std::size_t bufSize = 1024; char *buf = (char *) malloc(bufSize); mib[0] = CTL_KERN; diff --git a/src/core/test/hash-test-suite.cc b/src/core/test/hash-test-suite.cc index ef76ea410..e4360e2c5 100644 --- a/src/core/test/hash-test-suite.cc +++ b/src/core/test/hash-test-suite.cc @@ -272,7 +272,7 @@ Murmur3TestCase::DoRun (void) * \returns The checksum of the buffer contents. */ uint16_t -gnu_sum (const char * buffer, const size_t size) +gnu_sum (const char * buffer, const std::size_t size) { const char * p = buffer; const char * const pend = p + size; @@ -293,7 +293,7 @@ gnu_sum (const char * buffer, const size_t size) * \copydetails gnu_sum() */ uint32_t -gnu_sum32 (const char * buffer, const size_t size) +gnu_sum32 (const char * buffer, const std::size_t size) { uint32_t h = gnu_sum (buffer, size); return (uint32_t)( (h << 16) + h); @@ -305,7 +305,7 @@ gnu_sum32 (const char * buffer, const size_t size) * \copydetails gnu_sum() */ uint64_t -gnu_sum64 (const char * buffer, const size_t size) +gnu_sum64 (const char * buffer, const std::size_t size) { uint64_t h = gnu_sum32 (buffer, size); return (uint64_t)( (h << 32) + h);