From ae3fad9217833a871af2440f92f816ccfad3189b Mon Sep 17 00:00:00 2001 From: songbingyu Date: Wed, 2 Nov 2016 17:22:10 +0800 Subject: [PATCH 1/4] prof SlidingWindow constructor --- include/sliding_window.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/sliding_window.h b/include/sliding_window.h index f5dc8a4..31d5c98 100644 --- a/include/sliding_window.h +++ b/include/sliding_window.h @@ -22,13 +22,10 @@ class SlidingWindow { public: typedef boost::function SlidingCallback; SlidingWindow(int32_t size, SlidingCallback callback) - : bitmap_(NULL), items_(NULL), item_count_(0), - callback_(callback), size_(size), + : bitmap_(new char[size]), items_(new Item[size]), + item_count_(0), callback_(callback), size_(size), base_offset_(0), max_offset_(-1), ready_(0), notifying_(false) { - bitmap_ = new char[size]; memset(bitmap_, 0, size); - items_ = new Item[size]; - size_ = size; } ~SlidingWindow() { delete[] bitmap_; From e2c1c143b12feada8835e2682d8c4bc04048d6ce Mon Sep 17 00:00:00 2001 From: songbingyu Date: Wed, 2 Nov 2016 17:57:39 +0800 Subject: [PATCH 2/4] add non copyable for reference sematics class --- include/mutex.h | 7 ++++--- include/sliding_window.h | 3 ++- include/thread.h | 3 ++- include/thread_pool.h | 3 ++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/mutex.h b/include/mutex.h index 2418974..6d45aed 100644 --- a/include/mutex.h +++ b/include/mutex.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "timer.h" namespace baidu { @@ -27,7 +28,7 @@ static void PthreadCall(const char* label, int result) { } // A Mutex represents an exclusive lock. -class Mutex { +class Mutex :boost::noncopyable { public: Mutex() : owner_(0), msg_(NULL), msg_threshold_(0), lock_time_(0) { @@ -104,7 +105,7 @@ class Mutex { }; // Mutex lock guard -class MutexLock { +class MutexLock : boost::noncopyable { public: explicit MutexLock(Mutex *mu, const char* msg = NULL, int64_t msg_threshold = 5000) : mu_(mu) { @@ -120,7 +121,7 @@ class MutexLock { }; // Conditional variable -class CondVar { +class CondVar : boost::noncopyable { public: explicit CondVar(Mutex* mu) : mu_(mu) { PthreadCall("init condvar", pthread_cond_init(&cond_, NULL)); diff --git a/include/sliding_window.h b/include/sliding_window.h index 31d5c98..2c1ddd2 100644 --- a/include/sliding_window.h +++ b/include/sliding_window.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "mutex.h" @@ -18,7 +19,7 @@ namespace baidu { namespace common { template -class SlidingWindow { +class SlidingWindow : boost::noncopyable { public: typedef boost::function SlidingCallback; SlidingWindow(int32_t size, SlidingCallback callback) diff --git a/include/thread.h b/include/thread.h index 17ecb5c..248d2fd 100644 --- a/include/thread.h +++ b/include/thread.h @@ -11,11 +11,12 @@ #include #include +#include namespace baidu { namespace common { -class Thread { +class Thread : boost::noncopyable { public: Thread() { memset(&tid_, 0, sizeof(tid_)); diff --git a/include/thread_pool.h b/include/thread_pool.h index d060d3b..1cbb019 100644 --- a/include/thread_pool.h +++ b/include/thread_pool.h @@ -13,6 +13,7 @@ #include #include #include +#include #include "mutex.h" #include "timer.h" @@ -22,7 +23,7 @@ namespace common { static const int kDebugCheckTime = 5000; // An unscalable thread pool implimention. -class ThreadPool { +class ThreadPool :boost::noncopyable { public: ThreadPool(int thread_num = 10) : threads_num_(thread_num), From 9c3b6827580ddf783a1454bc7673f632c5d140e7 Mon Sep 17 00:00:00 2001 From: songbingyu Date: Thu, 3 Nov 2016 11:19:43 +0800 Subject: [PATCH 3/4] remove old noncopyable --- include/mutex.h | 8 +------- include/thread.h | 2 -- include/thread_pool.h | 3 --- 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/include/mutex.h b/include/mutex.h index 6d45aed..cedfd85 100644 --- a/include/mutex.h +++ b/include/mutex.h @@ -28,7 +28,7 @@ static void PthreadCall(const char* label, int result) { } // A Mutex represents an exclusive lock. -class Mutex :boost::noncopyable { +class Mutex : boost::noncopyable { public: Mutex() : owner_(0), msg_(NULL), msg_threshold_(0), lock_time_(0) { @@ -95,8 +95,6 @@ class Mutex :boost::noncopyable { } private: friend class CondVar; - Mutex(const Mutex&); - void operator=(const Mutex&); pthread_mutex_t mu_; pthread_t owner_; const char* msg_; @@ -116,8 +114,6 @@ class MutexLock : boost::noncopyable { } private: Mutex *const mu_; - MutexLock(const MutexLock&); - void operator=(const MutexLock&); }; // Conditional variable @@ -156,8 +152,6 @@ class CondVar : boost::noncopyable { PthreadCall("broadcast", pthread_cond_broadcast(&cond_)); } private: - CondVar(const CondVar&); - void operator=(const CondVar&); Mutex* mu_; pthread_cond_t cond_; }; diff --git a/include/thread.h b/include/thread.h index 248d2fd..bfeaea4 100644 --- a/include/thread.h +++ b/include/thread.h @@ -36,8 +36,6 @@ class Thread : boost::noncopyable { return (ret == 0); } private: - Thread(const Thread&); - void operator=(const Thread&); static void* ProcWrapper(void* arg) { reinterpret_cast(arg)->user_proc_(); return NULL; diff --git a/include/thread_pool.h b/include/thread_pool.h index 1cbb019..8be7145 100644 --- a/include/thread_pool.h +++ b/include/thread_pool.h @@ -174,9 +174,6 @@ class ThreadPool :boost::noncopyable { } private: - ThreadPool(const ThreadPool&); - void operator=(const ThreadPool&); - static void* ThreadWrapper(void* arg) { reinterpret_cast(arg)->ThreadProc(); return NULL; From 47fa90b54577cacbfbfb48b32bdc80785c12c53a Mon Sep 17 00:00:00 2001 From: songbingyu Date: Thu, 3 Nov 2016 13:46:28 +0800 Subject: [PATCH 4/4] prof --- include/util.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/util.h b/include/util.h index a5ca282..add2648 100644 --- a/include/util.h +++ b/include/util.h @@ -23,8 +23,7 @@ static inline std::string GetLocalHostName() { if (0 != gethostname(str, kMaxHostNameSize + 1)) { return ""; } - std::string hostname(str); - return hostname; + return std::string(str); } static const uint32_t MAX_PATH_LENGHT = 10240;