summaryrefslogtreecommitdiff
path: root/utils.cc
blob: 4b8a48355c4c9cb9ca1f7f856b58f6ec8a695869 (plain)
1
2
3
4
5
6
7
8
9
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "utils.h"

/**********************************************************************
 *
 * Assertions
 */

#if defined(ASSERTS_ON)

namespace ledger {

void debug_assert(const string& reason,
		  const string& func,
		  const string& file,
		  unsigned long	line)
{
  std::ostringstream buf;
  buf << "Assertion failed in \"" << file << "\", line " << line
      << ": " << reason;
  throw exception(buf.str(), context());
}

} // namespace ledger

#endif

/**********************************************************************
 *
 * Verification (basically, very slow asserts)
 */

#if defined(VERIFY_ON)

namespace ledger {
#if defined(FULL_DEBUG)
  bool verify_enabled = true;
#else
  bool verify_enabled = false;
#endif

  int		new_calls = 0;
  unsigned long	new_size  = 0;
}

void * operator new(std::size_t size) throw (std::bad_alloc) {
  void * ptr = std::malloc(size);
  ledger::new_calls++;
  ledger::new_size += size;
  return ptr;
}
void * operator new[](std::size_t size) throw (std::bad_alloc) {
  void * ptr = std::malloc(size);
  ledger::new_calls++;
  ledger::new_size += size;
  return ptr;
}
void * operator new(std::size_t size, const std::nothrow_t&) throw() {
  void * ptr = std::malloc(size);
  ledger::new_calls++;
  ledger::new_size += size;
  return ptr;
}
void * operator new[](std::size_t size, const std::nothrow_t&) throw() {
  void * ptr = std::malloc(size);
  ledger::new_calls++;
  ledger::new_size += size;
  return ptr;
}
void   operator delete(void * ptr) throw() {
  std::free(ptr);
}
void   operator delete[](void * ptr) throw() {
  std::free(ptr);
}
void   operator delete(void * ptr, const std::nothrow_t&) throw() {
  std::free(ptr);
}
void   operator delete[](void * ptr, const std::nothrow_t&) throw() {
  std::free(ptr);
}

namespace ledger {

live_objects_map live_objects;
object_count_map ctor_count;
object_count_map object_count;
object_count_map live_count;

inline void add_to_count_map(object_count_map& the_map,
			     const char * name, std::size_t size)
{
  object_count_map::iterator k = the_map.find(name);
  if (k != the_map.end()) {
    (*k).second.first++;
    (*k).second.second += size;
  } else {
    std::pair<object_count_map::iterator, bool> result =
      the_map.insert(object_count_pair(name, count_size_pair(1, size)));
    assert(result.second);
  }
}

inline void report_count_map(std::ostream& out, object_count_map& the_map)
{
  for (object_count_map::iterator i = the_map.begin();
       i != the_map.end();
       i++)
    out << "  " << std::right << std::setw(12) << (*i).second.first
	<< "  " << std::right << std::setw(12) << (*i).second.second
	<< "  " << std::left  << (*i).first
	<< std::endl;
}

bool trace_ctor_func(void * ptr, const char * cls_name, const char * args,
		     std::size_t cls_size)
{
  static char name[1024];
  std::strcpy(name, cls_name);
  std::strcat(name, "(");
  std::strcat(name, args);
  std::strcat(name, ")");

  DEBUG_("ledger.trace.debug", "TRACE_CTOR " << ptr << " " << name);

  live_objects.insert(live_objects_pair(ptr, cls_name));

  add_to_count_map(ctor_count, name, cls_size);
  add_to_count_map(object_count, cls_name, cls_size);
  add_to_count_map(object_count, "__ALL__", cls_size);
  add_to_count_map(live_count, cls_name, cls_size);

  return true;
}

bool trace_dtor_func(void * ptr, const char * cls_name, std::size_t cls_size)
{
  DEBUG_("ledger.trace.debug", "TRACE_DTOR " << ptr << " " << cls_name);

  live_objects_map::iterator i = live_objects.find(ptr);
  if (i == live_objects.end()) {
    std::cerr << "Destruction of unknown object of type " << cls_name
	      << " " << ptr << std::endl;
    assert(0);
    return false;
  }

  int ptr_count = live_objects.count(ptr);
  for (int x = 0; x < ptr_count; x++, i++) {
    if ((*i).second == cls_name) {
      live_objects.erase(i);
      break;
    }
  }

  object_count_map::iterator k = live_count.find(cls_name);
  if (k == live_count.end()) {
    std::cerr << "Destruction of unregistered class " << cls_name
	      << std::endl;;
    assert(0);
    return false;
  }

  (*k).second.second -= cls_size;
  if (--(*k).second.first == 0)
    live_count.erase(k);

  return true;
}

void report_memory(std::ostream& out)
{
  if (live_count.size() > 0) {
    out << "Live object counts:" << std::endl;
    report_count_map(out, live_count);
  }

  if (live_objects.size() > 0) {
    out << "Live objects:" << std::endl;

    for (live_objects_map::iterator i = live_objects.begin();
	 i != live_objects.end();
	 i++)
      out << "  " << std::right << std::setw(7) << (*i).first
	  << "  " << std::left  << (*i).second
	  << std::endl;
  }

  if (object_count.size() > 0) {
    out << "Object counts:" << std::endl;
    report_count_map(out, object_count);
  }

  if (ctor_count.size() > 0) {
    out << "Constructor counts:" << std::endl;
    report_count_map(out, ctor_count);
  }
}

#if ! defined(USE_BOOST_PYTHON)

string::string() : std::string() {
  TRACE_CTOR(string, "");
}
string::string(const string& str) : std::string(str) {
  TRACE_CTOR(string, "const string&");
}
string::string(const std::string& str) : std::string(str) {
  TRACE_CTOR(string, "const std::string&");
}
string::string(const int len, char x) : std::string(len, x) {
  TRACE_CTOR(string, "const int, char");
}
string::string(const char * str) : std::string(str) {
  TRACE_CTOR(string, "const char *");
}
string::string(const char * str, const char * end) : std::string(str, end) {
  TRACE_CTOR(string, "const char *, const char *");
}
string::string(const string& str, int x) : std::string(str, x) {
  TRACE_CTOR(string, "const string&, int");
}
string::string(const string& str, int x, int y) : std::string(str, x, y) {
  TRACE_CTOR(string, "const string&, int, int");
}
string::string(const char * str, int x) : std::string(str, x) {
  TRACE_CTOR(string, "const char *, int");
}
string::string(const char * str, int x, int y) : std::string(str, x, y) {
  TRACE_CTOR(string, "const char *, int, int");
}
string::~string() {
  TRACE_DTOR(string);
}

#endif

} // namespace ledger

#endif // VERIFY_ON

/**********************************************************************
 *
 * Logging
 */

#if defined(LOGGING_ON) && defined(DEBUG_ON)

#include <boost/regex.hpp>

namespace ledger {

log_level_t	   _log_level;
unsigned int	   _trace_level;
std::string	   _log_category;
std::ostream *	   _log_stream = &std::cerr;
std::ostringstream _log_buffer;

bool logger_func(log_level_t level)
{
  _log_buffer.str("");
}

} // namespace ledger

#endif // LOGGING_ON && DEBUG_ON

/**********************************************************************
 *
 * Timers (allows log entries to specify cumulative time spent)
 */

#if defined(LOGGING_ON) && defined(TIMERS_ON)

namespace ledger {

void start_timer(const char * name)
{
#if 0
  begin = std::clock();
#endif
}

void stop_timer(const char * name)
{
#if 0
  cumulative += std::clock() - begin;
#endif
}

void finish_timer(const char * name)
{
#if 0
  DEBUG_(cls.c_str(), file << ":" << line << ": "
	 << category << " = "
	 << (double(cumulative) / double(CLOCKS_PER_SEC)) << "s");
#endif
}

} // namespace ledger

#endif // LOGGING_ON && TIMERS_ON

/**********************************************************************
 *
 * Exception handling
 */

namespace ledger {

std::ostringstream _exc_buffer;

} // namespace ledger

/**********************************************************************
 *
 * General utility functions
 */

namespace ledger {

string expand_path(const string& path)
{
  if (path.length() == 0 || path[0] != '~')
    return path;

  const char * pfx = NULL;
  string::size_type pos = path.find_first_of('/');

  if (path.length() == 1 || pos == 1) {
    pfx = std::getenv("HOME");
#ifdef HAVE_GETPWUID
    if (! pfx) {
      // Punt. We're trying to expand ~/, but HOME isn't set
      struct passwd * pw = getpwuid(getuid());
      if (pw)
	pfx = pw->pw_dir;
    }
#endif
  }
#ifdef HAVE_GETPWNAM
  else {
    string user(path, 1, pos == string::npos ?
		     string::npos : pos - 1);
    struct passwd * pw = getpwnam(user.c_str());
    if (pw)
      pfx = pw->pw_dir;
  }
#endif

  // if we failed to find an expansion, return the path unchanged.

  if (! pfx)
    return path;

  string result(pfx);

  if (pos == string::npos)
    return result;

  if (result.length() == 0 || result[result.length() - 1] != '/')
    result += '/';

  result += path.substr(pos + 1);

  return result;
}

string resolve_path(const string& path)
{
  if (path[0] == '~')
    return expand_path(path);
  return path;
}

} // namespace ledger