~stewart/percona-playback/cassert-header

« back to all changes in this revision

Viewing changes to src/common.h

  • Committer: Oleg Tsarev
  • Date: 2011-05-04 21:38:59 UTC
  • mfrom: (108.1.18 sbt)
  • Revision ID: oleg.tsarev@percona.com-20110504213859-pw1mgjmuj9gz44vb
merge split_by_transaction

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#include <sstream>
5
5
#include <iostream>
6
6
#include <cstdio>
 
7
#include <map>
 
8
#include <set>
7
9
#include <boost/shared_ptr.hpp>
8
10
#include <pthread.h>
9
11
 
20
22
      ASSERT(value != NULL && "NULL is not string" && __FILE__ && __LINE__); \
21
23
      ASSERT(value != 0 && "0 is not string" && __FILE__ && __LINE__);  \
22
24
      ASSERT((memchr(value, 0, MAX_C_STRING_LENGTH) != NULL)            \
23
 
             && "too long C string" && __FILE__ && __LINE__);           \
 
25
             && "too long C string" && __FILE__ && __LINE__);           \
24
26
    }                                                                   \
25
27
  while(false)
26
28
#define CHECK_C_STRING_LENGTH(value,length) do                          \
28
30
      ASSERT(value != NULL && "NULL is not string" && __FILE__ && __LINE__); \
29
31
      ASSERT(value != 0 && "0 is not string" && __FILE__ && __LINE__);  \
30
32
      ASSERT(((length == 0) || (memchr(value, 0, length) == NULL))      \
31
 
             && "incorrect C string length" && __FILE__ && __LINE__);   \
 
33
             && "incorrect C string length" && __FILE__ && __LINE__);   \
32
34
      ASSERT(value[length] == 0                                         \
33
 
             && "incorrect C string length" && __FILE__ && __LINE__);   \
 
35
             && "incorrect C string length" && __FILE__ && __LINE__);   \
34
36
    }                                                                   \
35
37
  while(false)
36
38
#define CHECK_NULLABLE_C_STRING(value) do       \
37
39
    {                                           \
38
40
      if (value != NULL && value != 0)          \
39
 
        CHECK_C_STRING(value);                  \
 
41
        CHECK_C_STRING(value);                  \
40
42
    }                                           \
41
 
  while(false) 
 
43
  while(false)
42
44
#define CHECK_NULLABLE_C_STRING_LENGTH(value,length) do \
43
45
    {                                                   \
44
46
      if (value != NULL && value != 0)                  \
45
 
        CHECK_C_STRING_LENGTH(value, length);           \
 
47
        CHECK_C_STRING_LENGTH(value, length);           \
46
48
    }                                                   \
47
49
  while(false)
48
50
#ifndef TRACE
64
66
 
65
67
#endif // DEBUG
66
68
 
 
69
struct None_Copyable
 
70
{
 
71
public:
 
72
  None_Copyable() {}
 
73
  virtual ~None_Copyable() {}
 
74
private:
 
75
  None_Copyable(None_Copyable const &);
 
76
  None_Copyable& operator=(None_Copyable const &);
 
77
};
 
78
 
 
79
template<typename T>
 
80
class Memory_Pool : None_Copyable
 
81
{
 
82
public:
 
83
  typedef std::set<T*> Set;
 
84
public:
 
85
  virtual ~Memory_Pool()
 
86
  {
 
87
    for(typename Set::const_iterator i= m_set.begin(), e= m_set.end();
 
88
        e != i; ++i)
 
89
      delete *i;
 
90
  }
 
91
  void add(T *a_item)
 
92
  {
 
93
    ASSERT(!has(a_item));
 
94
    m_set.insert(a_item);
 
95
  }
 
96
  void del(T *a_item)
 
97
  {
 
98
    ASSERT(has(a_item));
 
99
    m_set.erase(m_set.find(a_item));
 
100
    delete a_item;
 
101
  }
 
102
  bool has(T *a_item) const
 
103
  {
 
104
    return (1 == m_set.count(a_item));
 
105
  }
 
106
  bool empty()
 
107
  {
 
108
    return m_set.empty();
 
109
  }
 
110
  Set& set()
 
111
  {
 
112
    return m_set;
 
113
  }
 
114
private:
 
115
  Set m_set;
 
116
};
 
117
 
67
118
class Error : public std::exception
68
119
{
69
120
public:
78
129
std::ostream& operator<< (std::ostream&, Error const&);
79
130
 
80
131
class Condition_Variable;
81
 
class Mutex
 
132
class Mutex : None_Copyable
82
133
{
83
 
private:
84
 
  Mutex(Mutex const&);
85
 
  Mutex& operator=(Mutex const&);
86
134
public:
87
135
  Mutex();
88
136
  ~Mutex();
89
137
public:
90
 
  class Lock
 
138
  class UnLock;
 
139
  class Lock : None_Copyable
91
140
  {
92
 
  private:
93
 
    Lock(Lock const&);
94
 
    Lock& operator=(Lock const&);
95
141
  public:
96
142
    Lock(Mutex&);
97
143
    ~Lock();
98
144
  private:
99
145
    Mutex &m_mutex;
100
 
    friend class Condition_Variable;    
 
146
    friend class Condition_Variable;
 
147
    friend class UnLock;
 
148
  };
 
149
  class UnLock : None_Copyable
 
150
  {
 
151
public:
 
152
    UnLock(Lock&);
 
153
    ~UnLock();
 
154
private:
 
155
    Mutex &m_mutex;
101
156
  };
102
157
private:
103
158
  void lock();
182
237
  void               join();
183
238
  std::string const& name() const;
184
239
  bool               run() const;
 
240
  bool               run(bool) const;
185
241
private:
186
242
  Impl_Pointer   m_impl;
187
243
  pthread_t      m_thread;